Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Machine-code/Wren/machine-code-1.wren
Normal file
20
Task/Machine-code/Wren/machine-code-1.wren
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* machine_code.wren */
|
||||
|
||||
class C {
|
||||
// pass the machine code in string form to the host
|
||||
foreign static runMachineCode(s, a, b)
|
||||
}
|
||||
|
||||
var a = 7
|
||||
var b = 12
|
||||
|
||||
// x64 opcodes for this task
|
||||
var m = [
|
||||
0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d,
|
||||
0xfc, 0x89, 0x75, 0xf8, 0x8b, 0x75,
|
||||
0xfc, 0x03, 0x75, 0xf8, 0x89, 0x75,
|
||||
0xf4, 0x8b, 0x45, 0xf4, 0x5d, 0xc3
|
||||
]
|
||||
|
||||
var s = m.map { |byte| String.fromByte(byte) }.join()
|
||||
System.print("%(a) + %(b) = %(C.runMachineCode(s, a, b))")
|
||||
109
Task/Machine-code/Wren/machine-code-2.wren
Normal file
109
Task/Machine-code/Wren/machine-code-2.wren
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include "wren.h"
|
||||
|
||||
unsigned char rmc_helper(const char *code, unsigned char a, unsigned char b, int l) {
|
||||
void *buf;
|
||||
unsigned char c;
|
||||
|
||||
/* copy code to executable buffer */
|
||||
buf = mmap (0, l, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
|
||||
memcpy(buf, code, l);
|
||||
|
||||
/* run code */
|
||||
c = ((unsigned char (*) (unsigned char, unsigned char))buf)(a, b);
|
||||
|
||||
/* free buffer */
|
||||
munmap(buf, l);
|
||||
|
||||
/* return result to caller */
|
||||
return c;
|
||||
}
|
||||
|
||||
void C_runMachineCode(WrenVM* vm) {
|
||||
/* unpack arguments passed from Wren */
|
||||
int *len;
|
||||
const char *code = wrenGetSlotBytes(vm, 1, len);
|
||||
unsigned char a = (unsigned char)wrenGetSlotDouble(vm, 2);
|
||||
unsigned char b = (unsigned char)wrenGetSlotDouble(vm, 3);
|
||||
|
||||
/* obtain result */
|
||||
unsigned char c = rmc_helper(code, a, b, *len);
|
||||
|
||||
/* return result to Wren */
|
||||
wrenSetSlotDouble(vm, 0, (double)c);
|
||||
}
|
||||
|
||||
WrenForeignMethodFn bindForeignMethod(
|
||||
WrenVM* vm,
|
||||
const char* module,
|
||||
const char* className,
|
||||
bool isStatic,
|
||||
const char* signature) {
|
||||
if (strcmp(module, "main") == 0) {
|
||||
if (strcmp(className, "C") == 0) {
|
||||
if (isStatic && strcmp(signature, "runMachineCode(_,_,_)") == 0) {
|
||||
return C_runMachineCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void writeFn(WrenVM* vm, const char* text) {
|
||||
printf("%s", text);
|
||||
}
|
||||
|
||||
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
|
||||
switch (errorType) {
|
||||
case WREN_ERROR_COMPILE:
|
||||
printf("[%s line %d] [Error] %s\n", module, line, msg);
|
||||
break;
|
||||
case WREN_ERROR_STACK_TRACE:
|
||||
printf("[%s line %d] in %s\n", module, line, msg);
|
||||
break;
|
||||
case WREN_ERROR_RUNTIME:
|
||||
printf("[Runtime Error] %s\n", msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char *readFile(const char *fileName) {
|
||||
FILE *f = fopen(fileName, "r");
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
rewind(f);
|
||||
char *script = malloc(fsize + 1);
|
||||
fread(script, 1, fsize, f);
|
||||
fclose(f);
|
||||
script[fsize] = 0;
|
||||
return script;
|
||||
}
|
||||
|
||||
int main() {
|
||||
WrenConfiguration config;
|
||||
wrenInitConfiguration(&config);
|
||||
config.writeFn = &writeFn;
|
||||
config.errorFn = &errorFn;
|
||||
config.bindForeignMethodFn = &bindForeignMethod;
|
||||
WrenVM* vm = wrenNewVM(&config);
|
||||
const char* module = "main";
|
||||
const char* fileName = "machine_code.wren";
|
||||
char *script = readFile(fileName);
|
||||
WrenInterpretResult result = wrenInterpret(vm, module, script);
|
||||
switch (result) {
|
||||
case WREN_RESULT_COMPILE_ERROR:
|
||||
printf("Compile Error!\n");
|
||||
break;
|
||||
case WREN_RESULT_RUNTIME_ERROR:
|
||||
printf("Runtime Error!\n");
|
||||
break;
|
||||
case WREN_RESULT_SUCCESS:
|
||||
break;
|
||||
}
|
||||
wrenFreeVM(vm);
|
||||
free(script);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue