New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No pass fn addr #6

Closed
wants to merge 2 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+57 −19
Diff settings

Always

Just for now

Copy path View file
35 jit.c
@@ -11,10 +11,6 @@
// return 0 is success
#define GUARD(expr) assert(!(expr))
typedef void* fn_memset (void*, int, size_t);
typedef int fn_putchar (int);
typedef int fn_getchar ();
void jit (const char* const file_contents) {
struct vector instruction_stream;
struct stack relocation_table = { .size = 0, .items = { 0 } };
@@ -28,16 +24,23 @@ void jit (const char* const file_contents) {
0x41, 0x54, // pushq %r12
0x41, 0x55, // pushq %r13
0x41, 0x56, // pushq %r14
// %rdi = memset
// %rsi = putchar
// %rdx = getgetchar
// backup args to callee saved registers
0x49, 0x89, 0xFC, // movq %rdi, %r12
0x49, 0x89, 0xF5, // movq %rsi, %r13
0x49, 0x89, 0xD6, // movq %rdx, %r14
// %r12 = memset
// %r13 = putchar
// %r14 = getchar
0x49, 0xBC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // movq $x, %r12
0x49, 0xBD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // movq $x, %r13
0x49, 0xBE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // movq $x, %r14
};
GUARD(vector_push(&instruction_stream, prologue, sizeof(prologue)));
GUARD(vector_write64LE(&instruction_stream, instruction_stream.size - 28,
(long long int) &memset));
GUARD(vector_write64LE(&instruction_stream, instruction_stream.size - 18,
(long long int) &putchar));
GUARD(vector_write64LE(&instruction_stream, instruction_stream.size - 8,
(long long int) &getchar));
// %r12 = memset
// %r13 = putchar
// %r14 = getchar
char prologue2 [] = {
// allocate 30,008 B on stack
0x48, 0x81, 0xEC, 0x38, 0x75, 0x00, 0x00, // subq $30000, %rsp
// address of beginning of tape
@@ -51,7 +54,7 @@ void jit (const char* const file_contents) {
0x49, 0x89, 0xE4 // movq %rsp, %r12
// %r12 = &tape[0];
};
GUARD(vector_push(&instruction_stream, prologue, sizeof(prologue)));
GUARD(vector_push(&instruction_stream, prologue2, sizeof(prologue2)));
for (unsigned long i = 0; file_contents[i] != '\0'; ++i) {
switch (file_contents[i]) {
@@ -150,8 +153,8 @@ void jit (const char* const file_contents) {
void* mem = mmap(NULL, instruction_stream.size, PROT_WRITE | PROT_EXEC,
MAP_ANON | MAP_PRIVATE, -1, 0);
memcpy(mem, instruction_stream.data, instruction_stream.size);
void (*jitted_func) (fn_memset, fn_putchar, fn_getchar) = mem;
jitted_func(memset, putchar, getchar);
void (*jitted_func) () = mem;
jitted_func();
munmap(mem, instruction_stream.size);
vector_destroy(&instruction_stream);
}
Copy path View file
@@ -66,5 +66,23 @@ int main () {
assert(vec.data[7] == (char) 0xFF);
GUARD(vector_destroy(&vec));
puts("testing writing 64 b little endian 2's compliment");
GUARD(vector_create(&vec, 10));
GUARD(vector_push(&vec, (char[]) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 10));
// 5 billion > 2^23 - 1
GUARD(vector_write64LE(&vec, 1, 5000000000));
assert(vec.data[0] == 0x00);
assert(vec.data[1] == 0x00);
assert(vec.data[2] == (char) 0xF2);
assert(vec.data[3] == 0x05);
assert(vec.data[4] == 0x2A);
assert(vec.data[5] == 0x01);
assert(vec.data[6] == 0x00);
assert(vec.data[7] == 0x00);
assert(vec.data[8] == 0x00);
assert(vec.data[9] == 0x09);
GUARD(vector_destroy(&vec));
puts("tests pass");
}
Copy path View file
@@ -1,6 +1,5 @@
#include <stdlib.h> // malloc, free, realloc
#include <string.h> // memcpy
#include <stdint.h> // int32_t
#include "vector.h"
int vector_create (struct vector* const vec, int capacity) {
@@ -35,7 +34,7 @@ int vector_push (struct vector* const vec, char* bytes, int len) {
return 0;
}
int vector_write32LE (struct vector* const vec, int offset, int32_t value) {
int vector_write32LE (struct vector* const vec, int offset, long int value) {
if (offset >= vec->size) {
return -1;
}
@@ -47,3 +46,20 @@ int vector_write32LE (struct vector* const vec, int offset, int32_t value) {
return 0;
}
int vector_write64LE (struct vector* const vec, int offset, long long int value) {
if (offset >= vec->size) {
return -1;
}
// offset opposite of usual since we explicitly want LE
vec->data[offset + 7] = (value & 0xFF00000000000000) >> 56;
vec->data[offset + 6] = (value & 0x00FF000000000000) >> 48;
vec->data[offset + 5] = (value & 0x0000FF0000000000) >> 40;
vec->data[offset + 4] = (value & 0x000000FF00000000) >> 32;
vec->data[offset + 3] = (value & 0x00000000FF000000) >> 24;
vec->data[offset + 2] = (value & 0x0000000000FF0000) >> 16;
vec->data[offset + 1] = (value & 0x000000000000FF00) >> 8;
vec->data[offset + 0] = (value & 0x00000000000000FF);
return 0;
}
Copy path View file
@@ -7,5 +7,6 @@ struct vector {
int vector_create (struct vector* const vec, int capacity);
int vector_destroy (struct vector* vec);
int vector_push (struct vector* const vec, char* bytes, int len);
int vector_write32LE (struct vector* const vec, int offset, int value);
int vector_write32LE (struct vector* const vec, int offset, long int value);
int vector_write64LE (struct vector* const vec, int offset, long long int value);
ProTip! Use n and p to navigate between commits in a pull request.