-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
174 lines (139 loc) · 5.24 KB
/
main.c
File metadata and controls
174 lines (139 loc) · 5.24 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#define debug_mode false
#include "c_script/struct.h"
#include <time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "c_script/opcodes.h"
#include "c_script/vm.h"
#include "c_script/threads.h"
#include "bytecode_assembler/assembler.h"
#include "file_handler.h"
/**
#define get_left *(_thread->stack-1)
#define get_right *(_thread->stack)
*(_thread->stack-1) = (int64_t)get_left+(int64_t)get_right;
_thread->stack--;
_thread->code_pointer+=1; // 0 argument
*/
#define get_left *(((thread_t*)instance->next_thread->value)->stack-1)
#define get_right *(((thread_t*)instance->next_thread->value)->stack)
#define get_stack_offset(x) *(((thread_t*)instance->next_thread->value)->stack+x)
#define pop_stack(x) ((thread_t*)instance->next_thread->value)->stack-=x
#define push_stack(x) *(++((thread_t*)instance->next_thread->value)->stack) = x
assembler_t assember;
rnu_int test(instance_t* instance){
get_left = get_left+get_right;
printf("helloooo from program %d\n",get_left);
pop_stack(1);
return 0;
}
rnu_int rnu_print(instance_t* instance){
printf((char*)get_right);
pop_stack(1);
return 0;
}
rnu_int rnu_println(instance_t* instance){
printf("%s\n",(char*)get_right);
pop_stack(1);
return 0;
}
rnu_int rnu_println_num(instance_t* instance){
printf("%d\n",get_right);
pop_stack(1);
return 0;
}
rnu_int rnu_malloc(instance_t* instance){
#if debug_mode == true
printf("Malloc : %d bytes\n",get_right);
#endif
get_right = (rnu_uint)malloc(get_right);
return 0;
}
rnu_int rnu_free(instance_t* instance){
#if debug_mode == true
printf("free : %d bytes\n",get_right);
#endif
free((void*)get_right);
pop_stack(1);
return 0;
}
rnu_int rnu_memcpy(instance_t* instance){
#if debug_mode == true
printf("memcpy %d %d %d\n",get_stack_offset(-2),get_stack_offset(-1),get_stack_offset(0));
#endif
memcpy((char*)get_stack_offset(-2),(char*)get_stack_offset(-1),get_stack_offset(0));
pop_stack(3);
return 0;
}
rnu_int rnu_compile(instance_t* instance){
string_t* str = c_string((char*)get_right);
get_right = (rnu_uint)assemble_bytecode(&assember,str);
free_string(str);
return 0;
}
// typedef int (*external_func)(instance* instance);
int main(int argc, char *argv[]){
if( argc == 2 ) {
if (file_exist(argv[1])==false){
printf("ERROR FILE %s DOES NOT EXIST",argv[1]);
exit(2);
}
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument [file] expected.\n");
}
clock_t start_time;
double elapsed_time;
instance_t instance;
instance.heap = calloc(1,sizeof(rnu_uint)*256); // 2 kb - 256 64bit ints
instance.enternal_functions = calloc(1,sizeof(rnu_uint)*256); // 2 kb - 256 64bit ints
instance.thread_count=0;
instance.threads=NULL;
instance.void_thread = create_new_node(0);
instance.available_threads=NULL;
string_t* string = c_string(read_file(argv[1]));
//assembler_t assember;
assembler_init(&assember);
add_external_function(&assember,"ex_test",(external_func)test);
add_external_function(&assember,"print",(external_func)rnu_print);
add_external_function(&assember,"println",(external_func)rnu_println);
add_external_function(&assember,"println_num",(external_func)rnu_println_num);
add_external_function(&assember,"malloc",(external_func)rnu_malloc);
add_external_function(&assember,"free",(external_func)rnu_free);
add_external_function(&assember,"memcpy",(external_func)rnu_memcpy);
add_external_function(&assember,"compile",(external_func)rnu_compile);
printf("starting compiling\n");
start_time = clock();
rnu_uint* compiled_code = assemble_bytecode(&assember,string);
elapsed_time = (double)((double)clock() - start_time) / (double)CLOCKS_PER_SEC;
printf("time to compile : %.10f\n",elapsed_time);
add_thread(&instance,create_thread(&instance,(rnu_uint*)compiled_code,1)); // create thread with code
printf("########### PROGRAM ############\n\n");
start_time = clock();
while (instance.thread_count!=0){
instance.next_thread = instance.threads;
/*Go through list*/
while(instance.next_thread!=NULL){
for (int i = 0; opcode_run(&instance,(thread_t*)instance.next_thread->value,instance.next_thread)
&& (i<((thread_t*)instance.next_thread->value)->priority
|| ((thread_t*)instance.next_thread->value)->lock==true)
; i++){
}
instance.next_thread = instance.next_thread->next;
}
}
elapsed_time = (double)((double)clock() - start_time) / (double)CLOCKS_PER_SEC;
printf("\n################################\n");
printf("time to execute : %.10f\n",elapsed_time);
// for (int i = 0; i<=10; i++)
// printf("stack %d: %d \n",i,*(instance.stack+i));
// printf("stack pointer : %d\n",instance.stack_pointer);
printf("Heap : \n");
for (int i = 0; i<=10; i++)
printf("[%d] = %d ",i,*(instance.heap+i)); printf("\n");
return 0;
}