~vdupras/duskos

duskos/fs/tests/comp/c/test.c -rw-r--r-- 10.8 KiB
c515797bVirgil Dupras comp/c/vm/i386: fix integer promotion bug in logical ops 3 hours ago
                                                                                
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* test a few simple C constructs */

#define FOUR 4
// The tokenizer used to crash on a // comment before a #define
#define MYCONST 40+FOUR-2

// just return a constant
short retconst() {
    return MYCONST;
}
short variables() {
    short foo = 40, _bar = 2;
    _bar = foo + _bar;
    return foo + _bar;
}
// test unary op and that we don't require whitespace around symbols
int neg() {int a=$2a; return -a;}
int bwnot() {
    int a='*';
    return ~a;
}
// test binop precedence
int exprbinops() {
    unsigned int a=1, b=2;
    return a + b * 3 + NULL;
}
int binopand() {
    int a=$ff;
    return (unsigned int)a & 42;
}
int binopor() {
    int a=2;
    return 40 | a;
}
int binopxor() {
    int a=43;
    return a ^ 1;
}
int binopshl() {
    int a=42;
    return a << 3;
}
int binopshr() {
    int a=42;
    return a >> 2;
}
int binopdiv() {
    int a=42;
    return a / 3;
}
int binopmod() {
    int a=43;
    return a % 3;
}
int binopcondeval(int x) {
	return x ? 42 : 12 ;
}
int assignops() {
    int a=42, b=2, c=3;
    a += b; // 44
    a -= c; // 41
    a *= b; // 82
    a |= c; // 83
    return a;
}
int boolops() {
    int a=66, b=2;
    return a < 54 && 2 == b;
}
int funcall() {
    return retconst();
}
void pushpop() {
    pspush(pspop());
}

// let's try function prototyping support
static int adder(int a, int b);
// are arguments, both constants and lvalues, properly passed?
// do we support expressions as arguments?
int plusone(int x) {
    return adder(1, x+x-x);
}
// implements the above prototype
static int adder(int a, int b) {
    return a + b;
}
int subber(int a, int b) {
    return a - b;
}
int ptrget() {
    int a = 42;
    int *b = &a;
    return *b;
}
int ptrset() {
    int a = 42;
    int *b = &a;
    *b = 54;
    return a;
}
int condif(int x) {
    if (x == 42) {
      x = x+100;
    } else {
      x = x+1;
    }
    return x;
}
// test that ++ and -- modify the lvalue directly
int incdec(int x) {
    ++x;
    --x;
    return ++x;
}
// test that the final "--" doesn't affect the result
int incdecp(int x) {
    x++;
    x--;
    return x--;
}
// test that parens override precedence
int exprparens() {
    return (1 + 2) * 3;
}
// test that a void function doesn't add anything to PS
void cnoop() {}
// test that pointer arithmetics properly multiply operands by 2 or 4.
int* ptrari(int *x) {
    cnoop(); // this doesn't mess up PS
    x++; ++x; x--;
    return x + 1;
}
// subtracting two pointers yield a number divided by the type size.
int ptrari2(int *lo, int *hi) {
    return hi-lo;
}
int array() {
    int a[3] = {42, 12, 2};
    return *a + a[1] - *(a+2);
}

#define GLOB2SZ 3
static int global1 = 1234, global2[GLOB2SZ+1-1] = {4, 5, 6};

int global() {
    return global1;
}
int globalinc() {
    global1++;
    return ++global1;
}
// allow static init of non-int globals
static short shortarray[3] = {1, 2, 3};
short globalshort(int idx) {
	return shortarray[idx];
}

typedef unsigned int (*AdderSig)(unsigned int, unsigned int);
int funcsig(int a, int b) {
    AdderSig fn = adder;
    return fn(a, b);
}

void stype(char *str);
static char *msgs[1] = {"Hello World!"};
void helloworld() {
    stype(msgs[0]);
}
void nullstr() {
	return "Null terminated"0;
}
int forsum(int n) {
    int i;
    int r = 0;
    for (i=0; i<n; i++) {
        r = r+i;
    }
    return r;
}
unsigned int multret(unsigned int x) {
    if (x < 10) {
        return x;
    } else {
        return x-10;
    }
}

void multretvoid(unsigned int x) {
    if (x == 42) {
        stype("Answer to the universe");
        return;
    }
    stype("Nope");
}
int forbreak() {
    int i, j;
    for (i=0; i<100; i++) {
        if (i==10) break;
        // the presence of a for() after the break doesn't break "break".
        for (j=0; j<1; j++) {}
    }
    return i;
}
int forcontinue() {
    int i, j=0;
    for (i=0; i<10; i++) {
        if (i==5) continue;
		++j;
    }
    return j;
}
// the first and last element of for() can be empty
void forempty() {
	for (;1;) return;
}
int lts(int a, int b) {
    return a < b;
}
int ltu(unsigned int a, unsigned int b) {
    return a < b;
}
int boolnot(int x) {
    return !x;
}
char get8b(char *s, int i) {
    return s[i]; // 0th is length
}
void set8b(char *s, int i, char c) {
    s[i] = c;
}
int typecast() {
	char x = $ff;
	int y = 1;
	return x == (char)(y-2);
}
void whilesum(int n) {
    int res = 0;
    while (n) {
        res = res + n--;
    }
    return res;
}
void dowhilesum(int n) {
    int res = 0;
    do {
        res = res + n--;
    } while (n);
    return res;
}

typedef char MyType;
typedef MyType** MyTypePtr;

struct MyStruct {
    int foo;
    short bar;
    MyType baz[2];
    int array[2];
};

MyType structget(MyStruct *s) {
    int x = 1;
    return s->baz[x-1] + 1;
}
void structset(MyStruct *s, char val) {
    s->baz[1] = val;
}

MyStruct globdata;

short globstructget() {
    return globdata.bar;
}
void globstructset(short val) {
    globdata.bar = val;
}

// support funcsig ident in global arrays and don't mess up thing when the
// function's return type isn't 4b
typedef short (*ShortRet)();
static ShortRet globfuncs[3] = {retconst, NULL, variables};

int callfuncidx(int idx) {
    if (idx != 1) {
        return globfuncs[idx]();
    } else {
        return 0;
    }
}

int switchstmt(int x) {
	int y = 1;
	switch (x) {
		case 42: return 12;
		case 1234: ++y;
		case 'B'-1:
		case 5678: ++y; break;
		default: --y;
	}
	return y;
}

// Below this comment are simple construct that were buggy before
int binop1(int a, int b) {
    int c;
    c = a ^ b;
    return c;
}
int binop2(int n) {
    int x = 42;
    x = x + n - '0';
    return x;
}
/* There used to be a bug where this type of comment with "'" char in it would
   cause a tokenization error. */
int binop3() {
    return global2[2] << 8 | global2[1];
}
// assigning a pointer to another pointer doesn't trigger pointer arithmetics
void binop4() {
    globfuncs[1] = neg;
}
// properly "boolify" arguments of logical operators
int binop5() {
    int x = 2;
    return x && 1;
}
// the i386 VM performed this add in 8b mode, not carrying the $100.
unsigned int binop6() {
    unsigned int x = $ff;
    unsigned char y = $ff;
    return x + y;
}
// properly "boolify" arguments of logical operators, again...
int binop7() {
    int x = 2;
    int y = 0;
    return x && y;
}
// multiple function calls in an expression can do funky things
int binop8() {
    return bwnot() + neg();
}
int binop9() {
    char array[2] = {$12, $34};
    return array[0]<<8|array[1];
}
// i386 VM used to crash on this
int binop10(int a, int b) {
	return a == 42 || b == 12;
}
// i386 VM used to have a register allocation problem with this very form of op.
char binop11(char a, char b) {
	MyStruct *s = &globdata;
	s->baz[0] = a >> b;
	return s->baz[0];
}
// forth VM used to leak to PS when having a funcall in a ?: operator
int binop12(int a, int b) {
	return a < 42 ? adder(a, b) : -1 ;
}
short structop1() {
    globdata.bar += 2;
    return globdata.bar;
}
// postop on a struct field failed under the Forth VM
int structop2() {
    globdata.bar++;
    return globdata.bar;
}
// In the Forth VM, indexing a struct array with a struct field resulted in TOS
// mixup.
unsigned char structop3() {
    globdata.baz[1] = 42;
    globdata.bar = 1;
    return globdata.baz[globdata.bar];
}
int structop4() {
    return &globdata.array[1];
}
// the combination of struct pointer, struct field subscripting, assignment and
// postop all at once caused the i386 VM to misallocate registers.
int structop5() {
    MyStruct *s = &globdata;
    s->foo = 1;
    s->baz[s->foo++] = 42;
    return globdata.baz[1];
}
// in the forth VM, PS would get mixed up in assignops that weren't "=", when
// being assigned a complex expression.
int structop6() {
    int n = 12;
    globdata.baz[1] = 42;
    globdata.bar = 1;
    n += globdata.baz[globdata.bar];
    return n;
}

MyStruct *globdataptr;
short structop7() {
    globdata.bar = 42;
    globdataptr = &globdata;
    return globdataptr->bar;
}

// in the forth VM, the address of the call would get lost on PS in certain
// situations, such as this one: a function living in a struct and accessed
// through a pointer.
struct StructWithFunc { int (*func)(int, int); };
int structop8(int a, int b) {
    StructWithFunc s;
    StructWithFunc *ptr = &s;
    s.func = adder;
    return ptr->func(a, b);
}

// we used to leak VM ops in condition blocks without {}
void cond1() {
    int x = 42;
    if (x==0) x++; else x--;
}

// Having a return statement in a conditional, if nothing came after it, would
// prevent the parent from having an implicit return.
void cond2() { if (0) return; }
// The forth VM used to assign to the SF in the wrong width
short opwidth1() {
    short x = 42;
    short y = $12345678;
    return x;
}
short opwidth2() {
    short x = 42;
    short y = 12;
    y += $12345678;
    return x;
}
// The i386 VM didn't carry the $100
int opwidth3() {
    int x = 42;
    unsigned char y = $ff;
    x += y;
    return x;
}
// The Forth VM lost track of opwidth through expressions
// Forth VM and i386 VM mis-initialized the char array.
char opwidth4() {
    char x[2] = {1, 2};
    int y = 0;
    x[y] = 12;
    return x[0] + x[1];
}
// The Forth and i386 VMs didn't properly apply size in inc/dec ops
unsigned char opwidth5() {
    unsigned char x = 42;
    unsigned char y = $ff;
    y++; y--;
    ++y;
    return x;
}
// The i386 VM always performed "test" on a 4b width
short opwidth6() {
    short x = 42;
    short y = 0;
    if (y) return 0; else return 1;
}
// The i386 VM didn't properly promote "a" to int
int opwidth7(char a, int b) {
	return a << b;
}
// Under i386, integer promotion of a non-reg operand would result in a buggy
// operation because we would read too much information from memory.
int opwidth8() {
	int x = 54;
	globdata.baz[0] = 42;
	globdata.baz[1] = 1;
	return x < globdata.baz[0];
}
// when subtracting 2 pointers, the result is considered a scalar for the
// remainder of the expression.
int* ptrari3(int *lo, int *hi) {
    return lo+((hi-lo)/2);
}
int ptrari4(int *a) {
    return *(a++) == 42;
}
struct SixBytes { int foo; short bar; };
static SixBytes globstructarray[2];
// Previously, pointer arithmetics adjustments only worked with power of two.
SixBytes* ptrari5() {
    globstructarray[1].foo = 42;
    return globstructarray;
}
// unary op, apart from ++ and --, *don't* modify their target.
int unaryop1(int n) {
    !n;
    return n;
}
// ... even if it's a pointer!
int* unaryop2(int *n) {
    !*n;
    return n;
}
// Function calls with other function calls in arguments used to mess things up
int funcall1() {
    return adder(54, retconst() + retconst());
}
int funcall2(int x) {
    return adder(++x, 42);
}
// There used to be a bug in the forth VM where an expression (something that
// isn't a simple reference, but the result of a computation) with an arg on PS
// would mess PS up.
int switch1(char *x) {
	switch (x+1) {
		case 43: return 43;
	}
	return x;
}