-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathml1.c
304 lines (259 loc) · 5.23 KB
/
ml1.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "lowl.h"
#define ML1_VERSION "LOWL-to-LLVM version " LOWL_VERSION " (AJB)"
#define SVARS_NO 23
lowlint_t ml1_svars[SVARS_NO + 1];
#define SVAR(_x) ml1_svars[SVARS_NO - (_x)]
extern lowlint_t LOWLVAR(OPSW);
extern lowlint_t LOWLVAR(OP1);
extern lowlint_t LOWLVAR(MEVAL);
extern lowlint_t LOWLVAR(IDPT);
extern lowlint_t LOWLVAR(IDLEN);
extern lowlint_t LOWLVAR(HASHPT);
extern lowlint_t LOWLVAR(HTABPT);
extern lowlint_t LOWLVAR(SVARPT);
void
ml1_init(void)
{
/* Initial S10 value: 1 */
SVAR(10) = 1;
/* Initial S21 value: 1 */
SVAR(21) = 1;
/* Initial S23 value: 1 */
SVAR(23) = 1;
/* ML/I MD Spec: Last element must
* contains the numbers of SVARS. */
ml1_svars[SVARS_NO] = SVARS_NO;
/* ML/I MD Spec: Save last element address to SVARPT */
LOWLVAR(SVARPT) = (lowlint_t)(uintptr_t)(ml1_svars + SVARS_NO);
}
void
ml1_fini(void)
{
}
/*
* Main function and argument parsing.
*/
#define MAX_OUF 4
#define MAX_INF 5
int oufs = 0;
FILE *output[MAX_OUF];
int infs = 0;
FILE *input[MAX_INF];
FILE *debug = NULL;
size_t wspace = 0;
int opt_v = 0;
void
version(void)
{
fprintf(stderr, "ML/I: %s\n", ML1_VERSION);
}
void
usage(char *name)
{
version();
fprintf(stderr, "\nUsage:\n");
fprintf(stderr, "\t%s [-v] [-w workspace] [-o outpufile]+ "
"[-d debugfile] [file ...]\n\n", name);
exit(-1);
}
static FILE *
get_file(char *file, char *mode)
{
FILE *f;
f = fopen(file, mode);
if ( f == NULL ) {
perror(file);
exit(-1);
}
return f;
}
static void
add_ofile(char *file)
{
if ( oufs > MAX_OUF - 1 ) {
fprintf(stderr, "Too many output files.\n");
exit(-1);
}
if ( !strcmp(file, "-") ) {
output[oufs++] = stdout;
return;
}
output[oufs++] = get_file(file, "w");
}
static void
add_ifile(char *file)
{
if ( infs > MAX_INF - 1 ) {
fprintf(stderr, "Too many input files.\n");
exit(-1);
}
if ( !strcmp(file, "-") ) {
input[infs++] = stdin;
return;
}
input[infs++] = get_file(file, "r");
}
void
arg_parse(int argc, char *argv[])
{
int argno = 0;
#define next_arg() (++argno < argc )
while ( next_arg() ) {
if ( !strcmp(argv[argno], "-") )
add_ifile(argv[argno]);
else if ( *argv[argno] == '-' ) {
if ( !strcmp(argv[argno], "-v") )
opt_v = 1;
else if ( !strcmp(argv[argno], "-w")
&& wspace == 0
&& next_arg() )
wspace = strtoul(argv[argno], NULL, 0);
else if ( !strcmp(argv[argno], "-d")
&& debug == stderr
&& next_arg() )
debug = get_file(argv[argno], "w");
else if ( !strcmp(argv[argno], "-o")
&& next_arg() )
add_ofile(argv[argno]);
else usage(argv[0]);
} else add_ifile(argv[argno]);
}
}
int
main(int argc, char *argv[])
{
/* Initialize standard I/O files. */
debug = stderr;
output[0] = stdout;
/* Parse arguments. */
arg_parse(argc, argv);
/* If no input has been specified, set infs to 1, as we're
* going to use stdin as input. */
if ( infs == 0 ) {
infs++;
input[0] = stdin;
}
if ( opt_v )
version();
/* Initialize ML/I LOWL. */
ml1_init();
/* Initialize LOWL runtime. */
lowl_runtime_init(wspace, debug);
/* Run ML/I LOWL code. */
lowl_run();
/* Exit now. */
lowl_runtime_fini();
ml1_fini();
return 0;
}
/*
* ML/I MD runtime support functions.
*/
void
mderch(uint8_t c)
{
putc(c, debug);
}
void
mdouch(uint8_t c)
{
lowlint_t ouflags = SVAR(21);
if ( ouflags & 1 )
putc(c, output[0]);
if ( (ouflags & 2) || (SVAR(22) != 0) )
if ( output[1] != NULL )
putc(c, output[1]);
if ( ouflags & 4)
if ( output[2] != NULL )
putc(c, output[2]);
if ( ouflags & 8)
if ( output[3] != NULL )
putc(c, output[3]);
}
uint8_t
mdread(uint8_t *c)
{
int r;
int inno;
retry:
if ( (inno = SVAR(10)) == 0 )
return 1;
if ( inno > infs + 100 ||
(inno <= 100 && inno > infs) ||
inno < 0 ) {
fprintf(debug, "S10 has illegal value, viz %d\n", inno);
exit(-2);
}
/* Reset input position */
if ( inno > 100 ) {
inno -= 100;
SVAR(10) = inno;
rewind(input[inno - 1]);
}
r = getc(input[inno - 1]);
if ( r == EOF ) {
int revert = SVAR(23);
if ( inno == revert )
return 1;
else {
SVAR(10) = revert;
goto retry;
}
}
*c = r;
return 2;
}
void
mdconv(void)
{
static char buf[LOWLINT_ITOA_LEN];
LOWLVAR(IDLEN) = snprintf(buf, LOWLINT_ITOA_LEN, "%"PRIdLWI,
LOWLVAR(MEVAL));
LOWLVAR(IDPT) = (uintptr_t)buf;
}
void
mdfind(void)
{
lowlint_t n;
n = ml1_hash((char *)LOWLVAR(IDPT), LOWLVAR(IDLEN));
LOWLVAR(HTABPT) = LOWLVAR(HASHPT) + n * (LLVM_PTRSIZE/8);
}
uint8_t
mdop()
{
lowlint_t op1 = LOWLVAR(OP1);
lowlint_t meval = LOWLVAR(MEVAL);
switch ( LOWLVAR(OPSW) ) {
case 1: /* Multiplication. */
meval = op1 * meval;
LOWLVAR(MEVAL) = meval;
return 1;
break;
default: /* Division. */
{
/* Divide rounding to the lowest number.
Use ANSI C's integer division (that rounds
to zero) and fix the result if needed. */
ldiv_t res;
int sign = 0; /* Zero is positive. */
if ( meval == 0 )
return 0; /* Overflow. */
/* Calculate the sign of the result. */
if ( meval < 0 ) sign = !sign;
if ( op1 < 0 ) sign = !sign;
res = ldiv(op1, meval);
if ( sign && (res.rem != 0) ) {
/* Fix it. */
res.quot -= 1;
}
LOWLVAR(MEVAL) = res.quot;
return 1;
}
break;
}
}