-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime.c
151 lines (125 loc) · 2.39 KB
/
runtime.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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "lowl.h"
void lowl_main();
/* Default stack size. */
#define LOWL_STACKSZ (0x10000*sizeof(lowlint_t))
char *lowl_stack;
size_t lowl_stacksz;
FILE *errorstream = NULL;
/*
* LOWL runtime init/fini.
*/
void
lowl_runtime_init(size_t ws, FILE *errstream)
{
lowl_stacksz = (ws == 0) ? LOWL_STACKSZ : ws*sizeof(lowlint_t);
lowl_stack = malloc(lowl_stacksz);
errorstream = errstream;
}
void
lowl_runtime_fini(void)
{
free(lowl_stack);
}
/*
* Execute the LOWL program.
*/
void
lowl_run(void)
{
lowl_main(lowl_stack, lowl_stack+lowl_stacksz);
}
/*
* Error handling support functions.
*/
void
lowl_goadd_jmperror(void)
{
fprintf(errorstream, "GOADD fail: variable too big.\n"
"Please increase the switch instruction table.\n");
exit(-1);
}
void
lowl_exit_jmperror(void)
{
fprintf(errorstream, "EXIT fail: this is a serious BUG in callgraph.\n"
"Please report.\n");
exit(-1);
}
/*
* LOWL instructions support functions.
*/
extern lowlint_t LOWLVAR(SRCPT);
extern lowlint_t LOWLVAR(DSTPT);
void
lowl_puts(char *str)
{
char *ptr = str;
while ( *ptr != '\0' ) {
if ( *ptr == '$' )
putc('\n', errorstream);
else
putc(*ptr, errorstream);
ptr++;
}
}
uint8_t
lowl_digit(uint8_t c)
{
return ( c >= '0' && c <= '9' ) ? 1 : 0;
}
uint8_t
lowl_punctuation(uint8_t c)
{
return ( (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9') ) ? 0 : 1;
}
void
lowl_bmove(lowlint_t len)
{
int i;
char *src = (char *)(uintptr_t)LOWLVAR(SRCPT);
char *dst = (char *)(uintptr_t)LOWLVAR(DSTPT);
for ( i = (int)len - 1; i >= 0; i-- )
*(dst + i) = *(src + i);
}
void
lowl_fmove(lowlint_t len)
{
char *src = (char *)(uintptr_t)LOWLVAR(SRCPT);
char *dst = (char *)(uintptr_t)LOWLVAR(DSTPT);
memcpy(dst, src, len);
}
/*
* Subroutine Stack.
*/
#define STACKSZ 24 /* Lowl manual says 'a dozen' are sufficient. */
lowlint_t stack[STACKSZ];
int stackp = -1;
void
lowl_pushlink(lowlint_t addr)
{
if ( stackp == STACKSZ - 1 ) {
fprintf(errorstream, "Subrouting stack exhausted! Increase STACKSZ in ml1-llvm sources.\n");
exit(-1);
}
stack[++stackp] = addr;
}
lowlint_t
lowl_poplink(void)
{
if ( stackp < 0 ) {
fprintf(errorstream, "Subroutine stack underflow! This is a serious BUG in ml1-llvm. Please report.\n");
exit(-1);
}
return stack[stackp--];
}
void
lowl_clearlink(void)
{
stackp = -1;
}