-
Notifications
You must be signed in to change notification settings - Fork 0
/
from_ir.c
109 lines (103 loc) · 1.74 KB
/
from_ir.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
#include "types.h"
#include "user.h"
#include "from_ir.h"
char heap[4096];
int heap_pos = 0;
int
get_int(
char* prog,
uint prog_len,
uint* place,
int* rv)
{
if (*place + 4 >= prog_len)
return -1;
if (rv != 0) {
char* start = prog + *place;
memmove(rv, start, sizeof(int));
}
*place += 4;
return 0;
}
int
get_float(
char* prog,
uint prog_len,
uint* place,
float* rv)
{
if (*place + 4 >= prog_len)
return -1;
if (rv != 0) {
char* start = prog + *place;
memmove(rv, start, sizeof(float));
}
*place += 4;
return 0;
}
int
get_type_list(
char* prog,
uint prog_len,
uint* place,
char* type_l,
uint* len)
{
*len = 0;
int pos = 0;
char* start = prog + *place;
while (1) {
int ind = pos/4;
int off = pos%4;
if (ind + *place >= prog_len)
return -1;
int val = (start[ind]>>(off*2)) & 3;
if (val == 0)
break;
type_l[pos] = (char) val;
pos += 1;
}
*place += (pos)/4 + 1;
*len = pos;
return 0;
}
int
add_fn_params(
fn_info* fn,
char* param_t,
int param_n)
{
if (fn->param_t != 0) {
if (param_n != fn->param_n)
return -1;
if (memcmp(fn->param_t, param_t, param_n) != 0)
return -1;
}
else {
fn->param_t = heap+heap_pos;
fn->param_n = param_n;
memmove(fn->param_t, param_t, param_n);
heap_pos += param_n;
}
return 0;
}
int
add_fn_locals(
fn_info* fn,
char* local_t,
int local_n)
{
if (fn->local_t != 0) {
if (local_n != fn->local_n)
return -1;
if (memcmp(fn->local_t, local_t, local_n) != 0)
return -1;
}
else {
fn->local_t = heap+heap_pos;
fn->local_n = local_n;
memmove(fn->local_t, local_t, local_n);
heap_pos += local_n;
}
return 0;
}