aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/expand.c
blob: fa8f588ce077caca80eb3547484550c5ba1532a6 (plain)
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
 * sparse/expand.c
 *
 * Copyright (C) 2003 Transmeta Corp.
 *
 *  Licensed under the Open Software License version 1.1
 *
 * expand constant expressions.
 */
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>

#include "lib.h"
#include "parse.h"
#include "token.h"
#include "symbol.h"
#include "target.h"
#include "expression.h"

static void expand_expression(struct expression *);
static void expand_statement(struct statement *);

static void expand_symbol_expression(struct expression *expr)
{
	struct symbol *sym = expr->symbol;

	/*
	 * The preprocessor can cause unknown symbols to be generated
	 */
	if (!sym) {
		warn(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
		expr->type = EXPR_VALUE;
		expr->value = 0;
		return;
	}
}

void cast_value(struct expression *expr, struct symbol *newtype,
		struct expression *old, struct symbol *oldtype)
{
	int old_size = oldtype->bit_size;
	int new_size = newtype->bit_size;
	long long value, mask, ormask, andmask;
	int is_signed;

	// FIXME! We don't handle FP casts of constant values yet
	if (newtype->ctype.base_type == &fp_type)
		return;
	if (oldtype->ctype.base_type == &fp_type)
		return;

	// For pointers and integers, we can just move the value around
	expr->type = EXPR_VALUE;
	if (old_size == new_size) {
		expr->value = old->value;
		return;
	}

	// expand it to the full "long long" value
	is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
	mask = 1ULL << (old_size-1);
	value = old->value;
	if (!(value & mask))
		is_signed = 0;
	andmask = mask | (mask-1);
	ormask = ~andmask;
	if (!is_signed)
		ormask = 0;
	value = (value & andmask) | ormask;

	// Truncate it to the new size
	mask = 1ULL << (new_size-1);
	mask = mask | (mask-1);
	expr->value = value & mask;
}

static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
{
	if (count >= ctype->bit_size) {
		warn(expr->pos, "shift too big for type");
		count &= ctype->bit_size-1;
	}
	return count;
}

/*
 * CAREFUL! We need to get the size and sign of the
 * result right!
 */
static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
{
	struct expression *left = expr->left, *right = expr->right;
	unsigned long long v, l, r, mask;
	signed long long s, sl, sr;
	int is_signed, shift;

	if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
		return;
	l = left->value; r = right->value;
	is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
	mask = 1ULL << (ctype->bit_size-1);
	sl = l; sr = r;
	if (is_signed && (sl & mask))
		sl |= ~(mask-1);
	if (is_signed && (sr & mask))
		sr |= ~(mask-1);
	
	switch (expr->op) {
	case '+':		v = l + r; s = v; break;
	case '-':		v = l - r; s = v; break;
	case '&':		v = l & r; s = v; break;
	case '|':		v = l | r; s = v; break;
	case '^':		v = l ^ r; s = v; break;
	case '*':		v = l * r; s = sl * sr; break;
	case '/':		if (!r) return; v = l / r; s = sl / sr; break;
	case '%':		if (!r) return; v = l % r; s = sl % sr; break;
	case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break; 
	case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
	case '<':		v = l < r; s = sl < sr; break;
	case '>':		v = l > r; s = sl > sr; break;
	case SPECIAL_LTE:	v = l <= r; s = sl <= sr; break;
	case SPECIAL_GTE:	v = l >= r; s = sl >= sr; break;
	case SPECIAL_EQUAL:	v = l == r; s = v; break;
	case SPECIAL_NOTEQUAL:	v = l != r; s = v; break;
	default: return;
	}
	if (is_signed)
		v = s;
	mask = mask | (mask-1);
	expr->value = v & mask;
	expr->type = EXPR_VALUE;
}

static void expand_int_binop(struct expression *expr)
{
	simplify_int_binop(expr, expr->ctype);
}

static void expand_logical(struct expression *expr)
{
	struct expression *left = expr->left;
	struct expression *right;

	/* Do immediate short-circuiting ... */
	expand_expression(left);
	if (left->type == EXPR_VALUE) {
		if (expr->op == SPECIAL_LOGICAL_AND) {
			if (!left->value) {
				expr->type = EXPR_VALUE;
				expr->value = 0;
				return;
			}
		} else {
			if (left->value) {
				expr->type = EXPR_VALUE;
				expr->value = 1;
				return;
			}
		}
	}

	right = expr->right;
	expand_expression(right);
	if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
		/*
		 * We know the left value doesn't matter, since
		 * otherwise we would have short-circuited it..
		 */
		expr->type = EXPR_VALUE;
		expr->value = right->value;
	}
}

static void expand_binop(struct expression *expr)
{
	expand_int_binop(expr);
}

static void expand_comma(struct expression *expr)
{
	if (expr->left->type == EXPR_VALUE)
		*expr = *expr->right;
}

static void expand_compare(struct expression *expr)
{
	simplify_int_binop(expr, expr->ctype);
}

static void expand_conditional(struct expression *expr)
{
	struct expression *cond = expr->conditional;

	if (cond->type == EXPR_VALUE) {
		struct expression *true, *false;

		true = expr->cond_true ? : cond;
		false = expr->cond_false;

		if (!cond->value)
			true = false;
		*expr = *true;
	}
}
		
static void expand_assignment(struct expression *expr)
{
}

static void expand_addressof(struct expression *expr)
{
}

static void expand_dereference(struct expression *expr)
{
	struct expression *unop = expr->unop;
	if (unop->type == EXPR_SYMBOL) {
		struct symbol *sym = unop->symbol;

		if (sym->ctype.modifiers & MOD_NODEREF)
			warn(expr->pos, "dereference of '%s'", show_ident(sym->ident));

		/* Const symbol with a constant initializer? */
		if (!(sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))) {
			struct expression *value = sym->initializer;
			if (value) {
				if (value->type == EXPR_VALUE) {
					expr->type = EXPR_VALUE;
					expr->value = value->value;
				}
			}
		}
	}
}

static void simplify_preop(struct expression *expr)
{
	struct expression *op = expr->unop;
	unsigned long long v, mask;

	if (op->type != EXPR_VALUE)
		return;
	v = op->value;
	switch (expr->op) {
	case '+': break;
	case '-': v = -v; break;
	case '!': v = !v; break;
	case '~': v = ~v; break;
	default: return;
	}
	mask = 1ULL << (expr->ctype->bit_size-1);
	mask = mask | (mask-1);
	expr->value = v & mask;
	expr->type = EXPR_VALUE;
}

/*
 * Unary post-ops: x++ and x--
 */
static void expand_postop(struct expression *expr)
{
}

static void expand_preop(struct expression *expr)
{
	switch (expr->op) {
	case '*':
		expand_dereference(expr);
		return;

	case '&':
		expand_addressof(expr);
		return;

	case SPECIAL_INCREMENT:
	case SPECIAL_DECREMENT:
		/*
		 * From a type evaluation standpoint the pre-ops are
		 * the same as the postops
		 */
		expand_postop(expr);
		return;

	default:
		break;
	}
	simplify_preop(expr);
}

static void expand_arguments(struct expression_list *head)
{
	struct expression *expr;

	FOR_EACH_PTR (head, expr) {
		expand_expression(expr);
	} END_FOR_EACH_PTR;
}

static void expand_cast(struct expression *expr)
{
	struct expression *target = expr->cast_expression;

	expand_expression(target);

	/* Simplify normal integer casts.. */
	if (target->type == EXPR_VALUE)
		cast_value(expr, expr->ctype, target, target->ctype);
}

/*
 * expand a call expression with a symbol. This
 * should expand inline functions, and expand
 * builtins.
 */
static void expand_symbol_call(struct expression *expr)
{
	struct expression *fn = expr->fn;
	struct symbol *ctype = fn->ctype;

	if (fn->type != EXPR_PREOP)
		return;

	if (ctype->op && ctype->op->expand) {
		ctype->op->expand(expr);
		return;
	}
}

static void expand_call(struct expression *expr)
{
	struct symbol *sym;
	struct expression *fn = expr->fn;

	sym = fn->ctype;
	expand_arguments(expr->args);
	if (sym->type == SYM_NODE)
		expand_symbol_call(expr);
}

static void expand_expression_list(struct expression_list *list)
{
	struct expression *expr;

	FOR_EACH_PTR(list, expr) {
		expand_expression(expr);
	} END_FOR_EACH_PTR;
}

static void expand_expression(struct expression *expr)
{
	if (!expr)
		return;
	if (!expr->ctype)
		return;

	switch (expr->type) {
	case EXPR_VALUE:
		return;
	case EXPR_STRING:
		return;
	case EXPR_SYMBOL:
		expand_symbol_expression(expr);
		return;
	case EXPR_BINOP:
		expand_expression(expr->left);
		expand_expression(expr->right);
		expand_binop(expr);
		return;

	case EXPR_LOGICAL:
		expand_logical(expr);
		return;

	case EXPR_COMMA:
		expand_expression(expr->left);
		expand_expression(expr->right);
		expand_comma(expr);
		return;

	case EXPR_COMPARE:
		expand_expression(expr->left);
		expand_expression(expr->right);
		expand_compare(expr);
		return;

	case EXPR_ASSIGNMENT:
		expand_expression(expr->left);
		expand_expression(expr->right);
		expand_assignment(expr);
		return;

	case EXPR_PREOP:
		expand_expression(expr->unop);
		expand_preop(expr);
		return;

	case EXPR_POSTOP:
		expand_expression(expr->unop);
		expand_postop(expr);
		return;

	case EXPR_CAST:
		expand_cast(expr);
		return;

	case EXPR_CALL:
		expand_call(expr);
		return;

	case EXPR_DEREF:
		return;

	case EXPR_BITFIELD:
		expand_expression(expr->address);
		return;

	case EXPR_CONDITIONAL:
		expand_expression(expr->conditional);
		expand_expression(expr->cond_false);
		expand_expression(expr->cond_true);
		expand_conditional(expr);
		return;

	case EXPR_STATEMENT:
		expand_statement(expr->statement);
		return;

	case EXPR_LABEL:
		return;

	case EXPR_INITIALIZER:
		expand_expression_list(expr->expr_list);
		return;

	case EXPR_IDENTIFIER:
		return;

	case EXPR_INDEX:
		return;

	case EXPR_POS:
		expand_expression(expr->init_expr);
		return;

	case EXPR_SIZEOF:
		warn(expr->pos, "internal front-end error: sizeof in expansion?");
		return;
	}
	return;
}


void expand_symbol(struct symbol *sym)
{
	struct symbol *base_type;

	if (!sym)
		return;
	base_type = sym->ctype.base_type;
	if (!base_type)
		return;

	expand_expression(sym->initializer);
	/* expand the body of the symbol */
	if (base_type->type == SYM_FN) {
		if (base_type->stmt)
			expand_statement(base_type->stmt);
	}
}

static void expand_return_expression(struct statement *stmt)
{
	expand_expression(stmt->expression);
}

static void expand_if_statement(struct statement *stmt)
{
	struct expression *expr = stmt->if_conditional;

	if (!expr || !expr->ctype)
		return;

	expand_expression(expr);

	/* Simplify constant conditionals without even evaluating the false side */
	if (expr->type == EXPR_VALUE) {
		struct statement *simple;
		simple = expr->value ? stmt->if_true : stmt->if_false;

		/* Nothing? */
		if (!simple) {
			stmt->type = STMT_NONE;
			return;
		}
		expand_statement(simple);
		*stmt = *simple;
		return;
	}
	expand_statement(stmt->if_true);
	expand_statement(stmt->if_false);
}

static void expand_statement(struct statement *stmt)
{
	if (!stmt)
		return;

	switch (stmt->type) {
	case STMT_RETURN:
		expand_return_expression(stmt);
		return;

	case STMT_EXPRESSION:
		expand_expression(stmt->expression);
		return;

	case STMT_COMPOUND: {
		struct symbol *sym;
		struct statement *s;

		FOR_EACH_PTR(stmt->syms, sym) {
			expand_symbol(sym);
		} END_FOR_EACH_PTR;
		expand_symbol(stmt->ret);

		FOR_EACH_PTR(stmt->stmts, s) {
			expand_statement(s);
		} END_FOR_EACH_PTR;
		return;
	}

	case STMT_IF:
		expand_if_statement(stmt);
		return;

	case STMT_ITERATOR:
		expand_expression(stmt->iterator_pre_condition);
		expand_expression(stmt->iterator_post_condition);
		expand_statement(stmt->iterator_pre_statement);
		expand_statement(stmt->iterator_statement);
		expand_statement(stmt->iterator_post_statement);
		return;

	case STMT_SWITCH:
		expand_expression(stmt->switch_expression);
		expand_statement(stmt->switch_statement);
		return;

	case STMT_CASE:
		expand_expression(stmt->case_expression);
		expand_expression(stmt->case_to);
		expand_statement(stmt->case_statement);
		return;

	case STMT_LABEL:
		expand_statement(stmt->label_statement);
		return;

	case STMT_GOTO:
		expand_expression(stmt->goto_expression);
		return;

	case STMT_NONE:
		break;
	case STMT_ASM:
		/* FIXME! Do the asm parameter evaluation! */
		break;
	}
}

long long get_expression_value(struct expression *expr)
{
	long long value, mask;
	struct symbol *ctype;

	ctype = evaluate_expression(expr);
	if (!ctype) {
		warn(expr->pos, "bad constant expression type");
		return 0;
	}
	expand_expression(expr);
	if (expr->type != EXPR_VALUE) {
		warn(expr->pos, "bad constant expression");
		return 0;
	}

	value = expr->value;
	mask = 1ULL << (ctype->bit_size-1);

	if (value & mask) {
		while (ctype->type != SYM_BASETYPE)
			ctype = ctype->ctype.base_type;
		if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
			value = value | mask | ~(mask-1);
	}
	return value;
}