-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.c
156 lines (133 loc) · 2.97 KB
/
Driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "DemoBasic.h"
#include "MachineCode.h"
#include "CodeGen.h"
#include "Types.h"
#include "Api.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#if !defined(_WIN32)
#include <sys/mman.h>
#else
#include <windows.h>
#endif
static int readFile(const char* filename, char** bufptr, size_t* size)
{
FILE *f = fopen(filename, "rb");
if (!f) {
return errno;
}
fseek(f, 0, SEEK_END);
*size = ftell(f);
fseek(f, 0, SEEK_SET);
*bufptr = (char*)malloc(*size + 1);
if (!*bufptr) {
fclose(f);
return ENOMEM;
}
fread(*bufptr, 1, *size, f);
(*bufptr)[*size++] = '\0';
fclose(f);
return 0;
}
static u8*
allocCodeMemory(size_t size)
{
#if defined(_WIN32)
u8* result = (unsigned char *)VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
#else
u8* result = mmap(0, size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
#endif
assert(result != (u8*)~0ul);
return result;
}
static void
freeCodeMemory(u8* mem, size_t size)
{
if (!mem)
return;
#if defined(_WIN32)
VirtualFree(mem, size, MEM_DECOMMIT);
#else
munmap(mem, size);
#endif
}
typedef long (DemoBasicFunc)(const void** api);
static int
execute(mc_MachineCode* mc, const void** api)
{
size_t Capacity = 1024*1024;
int result = 0;
u8* execMem = allocCodeMemory(Capacity);
DemoBasicFunc* f = (DemoBasicFunc*)(execMem);
memcpy(execMem, (u8*)mc + mc->codeOffset, mc->codeSize);
result = f(api);
freeCodeMemory(execMem, Capacity);
return result;
}
static int
runCode(mem_Allocator* allocator, const char* code, size_t size)
{
int result;
api_Functions api = api_getFunctions();
mc_MachineCode* machineCode = demobasic_compile(code, size, api.descs, api.count, allocator);
result = execute(machineCode, api.functions);
free(machineCode);
return result;
}
static int
runTests(mem_Allocator* allocator, char* code, size_t size)
{
int testsRun = 0;
int testsOk = 0;
char* p = code;
const char* pend = code + size;
if(size == 0 || code[0] != '@')
return 0;
while (p != pend) {
const char* idStart = p + 1;
const char* codeStart;
int expectedResult = 1;
int result = 0;
while(p != pend && *p != '\n')
++p;
if (p != pend)
*p++ = 0;
codeStart = p;
while(p != pend && *p != '@')
++p;
result = runCode(allocator, codeStart, p - codeStart);
++testsRun;
if (result == expectedResult)
++testsOk;
printf("%-40s: %s\n", idStart, (result == expectedResult) ? "Ok" : "Fail");
}
if (testsRun == testsOk)
printf("\nOK\n");
else
printf("\nFAILED: %d out of %d\n", (testsRun - testsOk), testsRun);
return 1;
}
int main(int argc, char** argv)
{
int ret = 0;
char* code = 0;
size_t size;
mem_Allocator allocator;
if (0 != readFile(argv[1], &code, &size))
{
printf ("Could not open input file\n");
ret = -1;
goto done;
}
allocator.allocMem = malloc;
allocator.freeMem = free;
if (!runTests(&allocator, code, size))
runCode(&allocator, code, size);
done:
api_shutdown();
free(code);
return ret;
}