[be] return fixes: move return jump target, emit return value properly
[smatch.git] / compile-i386.c
blob834962c2ff4edd6667a20210e0939ebd76285ab2
1 /*
2 * sparse/compile-i386.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
6 * Copyright 2003 Jeff Garzik
8 * Licensed under the Open Software License version 1.1
10 * x86 backend
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <assert.h>
22 #include "lib.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "scope.h"
27 #include "expression.h"
28 #include "target.h"
31 struct textbuf;
32 struct textbuf {
33 unsigned int len; /* does NOT include terminating null */
34 char *text;
35 struct textbuf *next;
36 struct textbuf *prev;
39 struct function {
40 int pseudo_nr;
41 struct ptr_list *pseudo_list;
42 struct ptr_list *atom_list;
43 struct ptr_list *str_list;
44 struct symbol **argv;
45 unsigned int argc;
46 int ret_target;
49 enum storage_type {
50 STOR_PSEUDO, /* variable stored on the stack */
51 STOR_ARG, /* function argument */
52 STOR_SYM, /* a symbol we can directly ref in the asm */
53 STOR_REG, /* scratch register */
54 STOR_VALUE, /* integer constant */
55 STOR_LABEL, /* label / jump target */
58 struct reg_info {
59 const char *name;
62 struct storage {
63 enum storage_type type;
65 /* STOR_REG */
66 struct reg_info *reg;
68 union {
69 /* STOR_PSEUDO */
70 struct {
71 int pseudo;
73 /* STOR_ARG */
74 struct {
75 int idx;
77 /* STOR_SYM */
78 struct {
79 struct symbol *sym;
81 /* STOR_VALUE */
82 struct {
83 long long value;
85 /* STOR_LABEL */
86 struct {
87 int label;
88 unsigned long flags;
93 enum {
94 STOR_LABEL_VAL = (1 << 0),
97 struct symbol_private {
98 struct storage *addr;
101 enum atom_type {
102 ATOM_TEXT,
103 ATOM_INSN,
104 ATOM_CSTR,
107 enum {
108 ATOM_FREE_OP1 = (1 << 0),
109 ATOM_FREE_OP2 = (1 << 1),
112 struct atom {
113 enum atom_type type;
114 union {
115 /* stuff for text */
116 struct {
117 char *text;
118 unsigned int text_len; /* w/o terminating null */
121 /* stuff for insns */
122 struct {
123 char insn[32];
124 char comment[40];
125 struct storage *op1;
126 struct storage *op2;
127 unsigned long flags;
130 /* stuff for C strings */
131 struct {
132 struct string *string;
133 int label;
139 struct function *current_func = NULL;
140 struct textbuf *unit_post_text = NULL;
141 static const char *current_section;
143 static struct reg_info reg_info_table[] = {
144 { "%eax" },
145 { "%ecx" },
146 { "%edx" },
147 { "%esp" },
148 { "%dl" },
151 static struct storage hardreg_storage_table[] = {
152 { /* eax */
153 .type = STOR_REG,
154 .reg = &reg_info_table[0],
157 { /* ecx */
158 .type = STOR_REG,
159 .reg = &reg_info_table[1],
162 { /* edx */
163 .type = STOR_REG,
164 .reg = &reg_info_table[2],
167 { /* esp */
168 .type = STOR_REG,
169 .reg = &reg_info_table[3],
172 { /* dl */
173 .type = STOR_REG,
174 .reg = &reg_info_table[4],
178 #define REG_EAX (&hardreg_storage_table[0])
179 #define REG_ECX (&hardreg_storage_table[1])
180 #define REG_EDX (&hardreg_storage_table[2])
181 #define REG_ESP (&hardreg_storage_table[3])
182 #define REG_DL (&hardreg_storage_table[4])
185 static void emit_move(struct storage *src, struct storage *dest,
186 struct symbol *ctype, const char *comment,
187 unsigned long flags);
188 static int type_is_signed(struct symbol *sym);
189 static struct storage *x86_address_gen(struct expression *expr);
190 static struct storage *x86_symbol_expr(struct symbol *sym);
191 static void x86_symbol(struct symbol *sym);
192 static struct storage *x86_statement(struct statement *stmt);
193 static struct storage *x86_expression(struct expression *expr);
196 static inline unsigned int pseudo_offset(struct storage *s)
198 if (s->type != STOR_PSEUDO)
199 return 123456; /* intentionally bogus value */
201 return ((s->pseudo - 1) * 4);
204 static inline unsigned int arg_offset(struct storage *s)
206 if (s->type != STOR_ARG)
207 return 123456; /* intentionally bogus value */
209 /* FIXME: this is wrong wrong wrong */
210 return (current_func->pseudo_nr + 1 + s->idx) * 4;
213 static const char *pretty_offset(int ofs)
215 static char esp_buf[64];
217 if (ofs)
218 sprintf(esp_buf, "%d(%%esp)", ofs);
219 else
220 strcpy(esp_buf, "(%esp)");
222 return esp_buf;
225 static void stor_sym_init(struct symbol *sym)
227 struct storage *stor;
228 struct symbol_private *priv;
230 priv = calloc(1, sizeof(*priv) + sizeof(*stor));
231 if (!priv)
232 die("OOM in stor_sym_init");
234 stor = (struct storage *) (priv + 1);
236 priv->addr = stor;
237 stor->type = STOR_SYM;
238 stor->sym = sym;
241 static const char *stor_op_name(struct storage *s, unsigned long flags)
243 static char name[32];
245 switch (s->type) {
246 case STOR_PSEUDO:
247 strcpy(name, pretty_offset((int) pseudo_offset(s)));
248 break;
249 case STOR_ARG:
250 strcpy(name, pretty_offset((int) arg_offset(s)));
251 break;
252 case STOR_SYM:
253 strcpy(name, show_ident(s->sym->ident));
254 break;
255 case STOR_REG:
256 strcpy(name, s->reg->name);
257 break;
258 case STOR_VALUE:
259 sprintf(name, "$%Ld", s->value);
260 break;
261 case STOR_LABEL:
262 sprintf(name, "%s.L%d", flags & STOR_LABEL_VAL ? "$" : "",
263 s->label);
264 break;
267 return name;
270 static struct atom *new_atom(enum atom_type type)
272 struct atom *atom;
274 atom = calloc(1, sizeof(*atom)); /* TODO: chunked alloc */
275 if (!atom)
276 die("nuclear OOM");
278 atom->type = type;
280 return atom;
283 static inline void push_cstring(struct function *f, struct string *str,
284 int label)
286 struct atom *atom;
288 atom = new_atom(ATOM_CSTR);
289 atom->string = str;
290 atom->label = label;
292 add_ptr_list(&f->str_list, atom); /* note: _not_ atom_list */
295 static inline void push_atom(struct function *f, struct atom *atom)
297 add_ptr_list(&f->atom_list, atom);
300 static void push_text_atom(struct function *f, const char *text)
302 struct atom *atom = new_atom(ATOM_TEXT);
304 atom->text = strdup(text);
305 atom->text_len = strlen(text);
307 push_atom(f, atom);
310 static struct storage *new_storage(enum storage_type type)
312 struct storage *stor;
314 stor = calloc(1, sizeof(*stor));
315 if (!stor)
316 die("OOM in new_storage");
318 stor->type = type;
320 return stor;
323 static struct storage *new_pseudo(void)
325 struct function *f = current_func;
326 struct storage *stor;
328 assert(f != NULL);
330 stor = new_storage(STOR_PSEUDO);
331 stor->type = STOR_PSEUDO;
332 stor->pseudo = ++f->pseudo_nr;
334 add_ptr_list(&f->pseudo_list, stor);
336 return stor;
339 static int new_label(void)
341 static int label = 0;
342 return ++label;
345 static void textbuf_push(struct textbuf **buf_p, const char *text)
347 struct textbuf *tmp, *list = *buf_p;
348 unsigned int text_len = strlen(text);
349 unsigned int alloc_len = text_len + 1 + sizeof(*list);
351 tmp = calloc(1, alloc_len);
352 if (!tmp)
353 die("OOM on textbuf alloc");
355 tmp->text = ((void *) tmp) + sizeof(*tmp);
356 memcpy(tmp->text, text, text_len + 1);
357 tmp->len = text_len;
359 /* add to end of list */
360 if (!list) {
361 list = tmp;
362 tmp->prev = tmp;
363 } else {
364 tmp->prev = list->prev;
365 tmp->prev->next = tmp;
366 list->prev = tmp;
368 tmp->next = list;
370 *buf_p = list;
373 static void textbuf_emit(struct textbuf **buf_p)
375 struct textbuf *tmp, *list = *buf_p;
377 while (list) {
378 tmp = list;
379 if (tmp->next == tmp)
380 list = NULL;
381 else {
382 tmp->prev->next = tmp->next;
383 tmp->next->prev = tmp->prev;
384 list = tmp->next;
387 fputs(tmp->text, stdout);
389 free(tmp);
392 *buf_p = list;
395 static void insn(const char *insn, struct storage *op1, struct storage *op2,
396 const char *comment_in, unsigned long flags)
398 struct function *f = current_func;
399 struct atom *atom = new_atom(ATOM_INSN);
401 assert(insn != NULL);
403 strcpy(atom->insn, insn);
404 if (comment_in && (*comment_in))
405 strncpy(atom->comment, comment_in,
406 sizeof(atom->comment) - 1);
408 atom->op1 = op1;
409 atom->op2 = op2;
410 atom->flags = flags;
412 push_atom(f, atom);
415 static void emit_unit_pre(const char *basename)
417 printf("\t.file\t\"%s\"\n", basename);
420 static void emit_unit_post(void)
422 textbuf_emit(&unit_post_text);
423 printf("\t.ident\t\"sparse silly x86 backend (built %s)\"\n", __DATE__);
426 /* conditionally switch sections */
427 static void emit_section(const char *s)
429 if (s == current_section)
430 return;
431 if (current_section && (!strcmp(s, current_section)))
432 return;
434 printf("\t%s\n", s);
435 current_section = s;
438 static void emit_insn_atom(struct function *f, struct atom *atom)
440 char s[128];
441 char comment[64];
442 struct storage *op1 = atom->op1;
443 struct storage *op2 = atom->op2;
445 if (atom->comment[0])
446 sprintf(comment, "\t\t# %s", atom->comment);
447 else
448 comment[0] = 0;
450 if (atom->op2) {
451 char tmp[16];
452 strcpy(tmp, stor_op_name(op1, op1->flags));
453 sprintf(s, "\t%s\t%s, %s%s\n",
454 atom->insn, tmp, stor_op_name(op2, op2->flags), comment);
455 } else if (atom->op1)
456 sprintf(s, "\t%s\t%s%s%s\n",
457 atom->insn, stor_op_name(op1, op1->flags),
458 comment[0] ? "\t" : "", comment);
459 else
460 sprintf(s, "\t%s\t%s%s\n",
461 atom->insn,
462 comment[0] ? "\t\t" : "", comment);
464 write(STDOUT_FILENO, s, strlen(s));
467 static void emit_atom_list(struct function *f)
469 struct atom *atom;
471 FOR_EACH_PTR(f->atom_list, atom) {
472 switch (atom->type) {
473 case ATOM_TEXT: {
474 ssize_t rc = write(STDOUT_FILENO, atom->text,
475 atom->text_len);
476 (void) rc; /* FIXME */
477 break;
479 case ATOM_INSN:
480 emit_insn_atom(f, atom);
481 break;
482 case ATOM_CSTR:
483 assert(0);
484 break;
486 } END_FOR_EACH_PTR;
489 static void emit_string_list(struct function *f)
491 struct atom *atom;
493 emit_section(".section\t.rodata");
495 FOR_EACH_PTR(f->str_list, atom) {
496 /* FIXME: escape " in string */
497 printf(".L%d:\n", atom->label);
498 printf("\t.string\t%s\n", show_string(atom->string));
500 free(atom);
501 } END_FOR_EACH_PTR;
504 static void func_cleanup(struct function *f)
506 struct storage *stor;
507 struct atom *atom;
509 FOR_EACH_PTR(f->pseudo_list, stor) {
510 free(stor);
511 } END_FOR_EACH_PTR;
513 FOR_EACH_PTR(f->atom_list, atom) {
514 if ((atom->type == ATOM_TEXT) && (atom->text))
515 free(atom->text);
516 if (atom->flags & ATOM_FREE_OP1)
517 free(atom->op1);
518 if (atom->flags & ATOM_FREE_OP2)
519 free(atom->op2);
520 free(atom);
521 } END_FOR_EACH_PTR;
523 free_ptr_list(&f->pseudo_list);
524 free(f);
527 /* function prologue */
528 static void emit_func_pre(struct symbol *sym)
530 struct function *f;
531 struct symbol *arg;
532 unsigned int i, argc = 0, alloc_len;
533 unsigned char *mem;
534 struct symbol_private *privbase;
535 struct storage *storage_base;
536 struct symbol *base_type = sym->ctype.base_type;
538 FOR_EACH_PTR(base_type->arguments, arg) {
539 argc++;
540 } END_FOR_EACH_PTR;
542 alloc_len =
543 sizeof(*f) +
544 (argc * sizeof(struct symbol *)) +
545 (argc * sizeof(struct symbol_private)) +
546 (argc * sizeof(struct storage));
547 mem = calloc(1, alloc_len);
548 if (!mem)
549 die("OOM on func info");
551 f = (struct function *) mem;
552 mem += sizeof(*f);
553 f->argv = (struct symbol **) mem;
554 mem += (argc * sizeof(struct symbol *));
555 privbase = (struct symbol_private *) mem;
556 mem += (argc * sizeof(struct symbol_private));
557 storage_base = (struct storage *) mem;
559 f->argc = argc;
560 f->ret_target = new_label();
562 i = 0;
563 FOR_EACH_PTR(base_type->arguments, arg) {
564 f->argv[i] = arg;
565 arg->aux = &privbase[i];
566 storage_base[i].type = STOR_ARG;
567 storage_base[i].idx = i;
568 privbase[i].addr = &storage_base[i];
569 i++;
570 } END_FOR_EACH_PTR;
572 assert(current_func == NULL);
573 current_func = f;
576 /* function epilogue */
577 static void emit_func_post(struct symbol *sym, struct storage *ret_val)
579 const char *name = show_ident(sym->ident);
580 struct function *f = current_func;
581 struct storage *val;
582 int pseudo_nr = f->pseudo_nr;
583 char pseudo_const[16], jump_target[16];
585 if (f->str_list)
586 emit_string_list(f);
588 /* function prologue */
589 emit_section(".text");
590 if ((sym->ctype.modifiers & MOD_STATIC) == 0)
591 printf(".globl %s\n", name);
592 printf("\t.type\t%s, @function\n", name);
593 printf("%s:\n", name);
594 sprintf(pseudo_const, "$%d", pseudo_nr * 4);
595 printf("\tsubl\t%s, %%esp\n", pseudo_const);
597 /* function epilogue */
599 if (ret_val) {
600 /* FIXME: mem operand hardcoded at 32 bits */
601 emit_move(ret_val, REG_EAX, NULL, NULL, 0);
604 /* jump target for 'return' statements */
605 sprintf(jump_target, ".L%d:\n", f->ret_target);
606 push_text_atom(f, jump_target);
608 val = new_storage(STOR_VALUE);
609 val->value = (long long) (pseudo_nr * 4);
611 insn("addl", val, REG_ESP, NULL, ATOM_FREE_OP1);
612 insn("ret", NULL, NULL, NULL, 0);
614 /* output everything to stdout */
615 fflush(stdout); /* paranoia; needed? */
616 emit_atom_list(f);
618 /* function footer */
619 printf("\t.size\t%s, .-%s\n", name, name);
621 func_cleanup(f);
622 current_func = NULL;
625 /* emit object (a.k.a. variable, a.k.a. data) prologue */
626 static void emit_object_pre(const char *name, unsigned long modifiers,
627 unsigned long alignment, unsigned int byte_size)
629 if ((modifiers & MOD_STATIC) == 0)
630 printf(".globl %s\n", name);
631 emit_section(".data");
632 if (alignment)
633 printf("\t.align %lu\n", alignment);
634 printf("\t.type\t%s, @object\n", name);
635 printf("\t.size\t%s, %d\n", name, byte_size);
636 printf("%s:\n", name);
639 /* emit value (only) for an initializer scalar */
640 static void emit_scalar(struct expression *expr, unsigned int bit_size)
642 const char *type;
643 long long ll;
645 assert(expr->type == EXPR_VALUE);
647 if (expr->value == 0ULL) {
648 printf("\t.zero\t%d\n", bit_size / 8);
649 return;
652 ll = (long long) expr->value;
654 switch (bit_size) {
655 case 8: type = "byte"; ll = (char) ll; break;
656 case 16: type = "value"; ll = (short) ll; break;
657 case 32: type = "long"; ll = (int) ll; break;
658 case 64: type = "quad"; break;
659 default: type = NULL; break;
662 assert(type != NULL);
664 printf("\t.%s\t%Ld\n", type, ll);
667 static void emit_global_noinit(const char *name, unsigned long modifiers,
668 unsigned long alignment, unsigned int byte_size)
670 char s[64];
672 if (modifiers & MOD_STATIC) {
673 sprintf(s, "\t.local\t%s\n", name);
674 textbuf_push(&unit_post_text, s);
676 if (alignment)
677 sprintf(s, "\t.comm\t%s,%d,%lu\n", name, byte_size, alignment);
678 else
679 sprintf(s, "\t.comm\t%s,%d\n", name, byte_size);
680 textbuf_push(&unit_post_text, s);
683 static int ea_current, ea_last;
685 static void emit_initializer(struct symbol *sym,
686 struct expression *expr)
688 int distance = ea_current - ea_last - 1;
690 if (distance > 0)
691 printf("\t.zero\t%d\n", (sym->bit_size / 8) * distance);
693 if (expr->type == EXPR_VALUE) {
694 struct symbol *base_type = sym->ctype.base_type;
695 assert(base_type != NULL);
697 emit_scalar(expr, sym->bit_size / base_type->array_size);
698 return;
700 if (expr->type != EXPR_INITIALIZER)
701 return;
703 assert(0); /* FIXME */
706 static int sort_array_cmp(const struct expression *a,
707 const struct expression *b)
709 int a_ofs = 0, b_ofs = 0;
711 if (a->type == EXPR_POS)
712 a_ofs = (int) a->init_offset;
713 if (b->type == EXPR_POS)
714 b_ofs = (int) b->init_offset;
716 return a_ofs - b_ofs;
719 /* move to front-end? */
720 static void sort_array(struct expression *expr)
722 struct expression *entry, **list;
723 unsigned int elem, sorted, i;
725 elem = 0;
726 FOR_EACH_PTR(expr->expr_list, entry) {
727 elem++;
728 } END_FOR_EACH_PTR;
730 if (!elem)
731 return;
733 list = malloc(sizeof(entry) * elem);
734 if (!list)
735 die("OOM in sort_array");
737 /* this code is no doubt evil and ignores EXPR_INDEX possibly
738 * to its detriment and other nasty things. improvements
739 * welcome.
741 i = 0;
742 sorted = 0;
743 FOR_EACH_PTR(expr->expr_list, entry) {
744 if ((entry->type == EXPR_POS) || (entry->type == EXPR_VALUE)) {
745 /* add entry to list[], in sorted order */
746 if (sorted == 0) {
747 list[0] = entry;
748 sorted = 1;
749 } else {
750 unsigned int i;
752 for (i = 0; i < sorted; i++)
753 if (sort_array_cmp(entry, list[i]) <= 0)
754 break;
756 /* If inserting into the middle of list[]
757 * instead of appending, we memmove.
758 * This is ugly, but thankfully
759 * uncommon. Input data with tons of
760 * entries very rarely have explicit
761 * offsets. convert to qsort eventually...
763 if (i != sorted)
764 memmove(&list[i + 1], &list[i],
765 (sorted - i) * sizeof(entry));
766 list[i] = entry;
767 sorted++;
770 } END_FOR_EACH_PTR;
772 i = 0;
773 FOR_EACH_PTR(expr->expr_list, entry) {
774 if ((entry->type == EXPR_POS) || (entry->type == EXPR_VALUE))
775 __list->list[__i] = list[i++];
776 } END_FOR_EACH_PTR;
780 static void emit_array(struct symbol *sym)
782 struct symbol *base_type = sym->ctype.base_type;
783 struct expression *expr = sym->initializer;
784 struct expression *entry;
786 assert(base_type != NULL);
788 stor_sym_init(sym);
790 ea_last = -1;
792 emit_object_pre(show_ident(sym->ident), sym->ctype.modifiers,
793 sym->ctype.alignment,
794 sym->bit_size / 8);
796 sort_array(expr);
798 FOR_EACH_PTR(expr->expr_list, entry) {
799 if (entry->type == EXPR_VALUE) {
800 ea_current = 0;
801 emit_initializer(sym, entry);
802 ea_last = ea_current;
803 } else if (entry->type == EXPR_POS) {
804 ea_current =
805 entry->init_offset / (base_type->bit_size / 8);
806 emit_initializer(sym, entry->init_expr);
807 ea_last = ea_current;
809 } END_FOR_EACH_PTR;
812 static void emit_one_symbol(struct symbol *sym, void *dummy, int flags)
814 x86_symbol(sym);
817 void emit_unit(const char *basename, struct symbol_list *list)
819 emit_unit_pre(basename);
820 symbol_iterate(list, emit_one_symbol, NULL);
821 emit_unit_post();
824 static void emit_copy(struct storage *src, struct symbol *src_ctype,
825 struct storage *dest, struct symbol *dest_ctype)
827 /* FIXME: Bitfield move! */
829 emit_move(src, REG_EAX, src_ctype, "begin copy ..", 0);
830 emit_move(REG_EAX, dest, dest_ctype, ".... end copy", 0);
833 static void emit_store(struct expression *dest_expr, struct storage *dest,
834 struct storage *src, int bits)
836 /* FIXME: Bitfield store! */
837 printf("\tst.%d\t\tv%d,[v%d]\n", bits, src->pseudo, dest->pseudo);
840 static void emit_scalar_noinit(struct symbol *sym)
842 emit_global_noinit(show_ident(sym->ident),
843 sym->ctype.modifiers, sym->ctype.alignment,
844 sym->bit_size / 8);
845 stor_sym_init(sym);
848 static void emit_array_noinit(struct symbol *sym)
850 emit_global_noinit(show_ident(sym->ident),
851 sym->ctype.modifiers, sym->ctype.alignment,
852 sym->array_size * (sym->bit_size / 8));
853 stor_sym_init(sym);
856 static const char *opbits(const char *insn, unsigned int bits)
858 static char opbits_str[32];
859 char c;
861 switch (bits) {
862 case 8: c = 'b'; break;
863 case 16: c = 'w'; break;
864 case 32: c = 'l'; break;
865 case 64: c = 'q'; break;
866 default: assert(0); break;
869 sprintf(opbits_str, "%s%c", insn, bits);
871 return opbits_str;
874 static void emit_move(struct storage *src, struct storage *dest,
875 struct symbol *ctype, const char *comment,
876 unsigned long flags)
878 unsigned int bits;
879 unsigned int is_signed;
880 unsigned int is_dest = (src->type == STOR_REG);
881 const char *opname;
883 if (ctype) {
884 bits = ctype->bit_size;
885 is_signed = type_is_signed(ctype);
886 } else {
887 bits = 32;
888 is_signed = 0;
891 if ((dest->type == STOR_REG) && (src->type == STOR_REG)) {
892 insn("mov", src, dest, NULL, 0);
893 return;
896 switch (bits) {
897 case 8:
898 if (is_dest)
899 opname = "movb";
900 else {
901 if (is_signed) opname = "movsxb";
902 else opname = "movzxb";
904 break;
905 case 16:
906 if (is_dest)
907 opname = "movw";
908 else {
909 if (is_signed) opname = "movsxw";
910 else opname = "movzxw";
912 break;
914 case 32: opname = "movl"; break;
915 case 64: opname = "movq"; break;
917 default: assert(0); break;
920 insn(opname, src, dest, comment, flags);
923 static struct storage *emit_compare(struct expression *expr)
925 struct storage *left = x86_expression(expr->left);
926 struct storage *right = x86_expression(expr->right);
927 struct storage *new;
928 const char *opname = NULL;
929 unsigned int is_signed = type_is_signed(expr->left->ctype); /* FIXME */
930 unsigned int right_bits = expr->right->ctype->bit_size;
932 switch(expr->op) {
933 case '<':
934 if (is_signed) opname = "setl";
935 else opname = "setb";
936 break;
937 case '>':
938 if (is_signed) opname = "setg";
939 else opname = "seta";
940 break;
941 case SPECIAL_LTE:
942 if (is_signed) opname = "setle";
943 else opname = "setbe";
944 break;
945 case SPECIAL_GTE:
946 if (is_signed) opname = "setge";
947 else opname = "setae";
948 break;
950 case SPECIAL_EQUAL: opname = "sete"; break;
951 case SPECIAL_NOTEQUAL: opname = "setne"; break;
953 default:
954 assert(0);
955 break;
958 /* init EDX to 0 */
959 insn("xor", REG_EDX, REG_EDX, "begin EXPR_COMPARE", 0);
961 /* move op1 into EAX */
962 emit_move(left, REG_EAX, expr->left->ctype, NULL, 0);
964 /* perform comparison, RHS (op1, right) and LHS (op2, EAX) */
965 insn(opbits("cmp", right_bits), right, REG_EAX, NULL, 0);
967 /* store result of operation, 0 or 1, in DL using SETcc */
968 insn(opname, REG_DL, NULL, NULL, 0);
970 /* finally, store the result (DL) in a new pseudo / stack slot */
971 new = new_pseudo();
972 emit_move(REG_EDX, new, NULL, "end EXPR_COMPARE", 0);
974 return new;
977 static struct storage *emit_value(struct expression *expr)
979 #if 0 /* old and slow way */
980 struct storage *new = new_pseudo();
981 struct storage *val;
983 val = new_storage(STOR_VALUE);
984 val->value = (long long) expr->value;
985 insn("movl", val, new, NULL, ATOM_FREE_OP1);
987 return new;
988 #else
989 struct storage *val;
991 val = new_storage(STOR_VALUE);
992 val->value = (long long) expr->value;
994 return val; /* FIXME: memory leak */
995 #endif
998 static struct storage *emit_binop(struct expression *expr)
1000 struct storage *left = x86_expression(expr->left);
1001 struct storage *right = x86_expression(expr->right);
1002 struct storage *new;
1003 const char *opname;
1004 static const char *name[] = {
1005 ['+'] = "addl", ['-'] = "subl",
1006 ['*'] = "mull", ['/'] = "divl",
1007 ['%'] = "modl", ['&'] = "andl",
1008 ['|'] = "orl", ['^'] = "xorl"
1010 unsigned int op = expr->op;
1013 * FIXME FIXME this routine is so wrong it's not even funny.
1014 * On x86 both mod/div are handled with the same instruction.
1015 * We don't pay attention to signed/unsigned issues,
1016 * and like elsewhere we hardcode the operand size at 32 bits.
1019 opname = show_special(op);
1020 if (op < sizeof(name)/sizeof(*name)) {
1021 opname = name[op];
1022 assert(opname != NULL);
1023 } else
1024 assert(0); /* FIXME: no operations other than name[], ATM */
1026 /* load op2 into EAX */
1027 insn("movl", right, REG_EAX, "EXPR_BINOP/COMMA/LOGICAL", 0);
1029 /* perform binop */
1030 insn(opname, left, REG_EAX, NULL, 0);
1032 /* store result (EAX) in new pseudo / stack slot */
1033 new = new_pseudo();
1034 insn("movl", REG_EAX, new, "end EXPR_BINOP", 0);
1036 return new;
1039 static void emit_if_conditional(struct statement *stmt)
1041 struct function *f = current_func;
1042 struct storage *val, *target_val;
1043 int target;
1044 struct expression *cond = stmt->if_conditional;
1045 char s[16];
1047 /* This is only valid if nobody can jump into the "dead" statement */
1048 #if 0
1049 if (cond->type == EXPR_VALUE) {
1050 struct statement *s = stmt->if_true;
1051 if (!cond->value)
1052 s = stmt->if_false;
1053 x86_statement(s);
1054 break;
1056 #endif
1057 val = x86_expression(cond);
1059 /* load 'if' test result into EAX */
1060 insn("movl", val, REG_EAX, "begin if conditional", 0);
1062 /* compare 'if' test result */
1063 insn("test", REG_EAX, REG_EAX, NULL, 0);
1065 /* create end-of-if label / if-failed labelto jump to,
1066 * and jump to it if the expression returned zero.
1068 target = new_label();
1069 target_val = new_storage(STOR_LABEL);
1070 target_val->label = target;
1071 insn("jz", target_val, NULL, NULL, ATOM_FREE_OP1);
1073 x86_statement(stmt->if_true);
1074 if (stmt->if_false) {
1075 struct storage *last_val;
1076 int last;
1078 /* finished generating code for if-true statement.
1079 * add a jump-to-end jump to avoid falling through
1080 * to the if-false statement code.
1082 last = new_label();
1083 last_val = new_storage(STOR_LABEL);
1084 last_val->label = last;
1085 insn("jmp", last_val, NULL, NULL, ATOM_FREE_OP1);
1087 /* if we have both if-true and if-false statements,
1088 * the failed-conditional case will fall through to here
1090 sprintf(s, ".L%d:\n", target);
1091 push_text_atom(f, s);
1093 target = last;
1094 x86_statement(stmt->if_false);
1097 sprintf(s, ".L%d:\t\t\t\t\t# end if\n", target);
1098 push_text_atom(f, s);
1101 static struct storage *emit_inc_dec(struct expression *expr, int postop)
1103 struct storage *addr = x86_address_gen(expr->unop);
1104 struct storage *retval;
1105 const char *opname = expr->op == SPECIAL_INCREMENT ? "incl" : "decl";
1106 #if 0
1107 /* FIXME: don't hardware operand size */
1108 int bits = expr->ctype->bit_size;
1109 #endif
1111 if (postop) {
1112 struct storage *new = new_pseudo();
1114 emit_copy(addr, expr->unop->ctype, new, NULL);
1116 retval = new;
1117 } else
1118 retval = addr;
1120 insn(opname, addr, NULL, NULL, 0);
1122 return retval;
1125 static struct storage *emit_postop(struct expression *expr)
1127 return emit_inc_dec(expr, 1);
1130 static struct storage *emit_return_stmt(struct statement *stmt)
1132 struct function *f = current_func;
1133 struct expression *expr = stmt->ret_value;
1134 struct storage *val = NULL;
1135 char s[32];
1137 if (expr && expr->ctype) {
1138 val = x86_expression(expr);
1139 assert(val != NULL);
1140 emit_move(val, REG_EAX, expr->ctype, "return", 0);
1143 sprintf(s, "\tjmp\t.L%d\n", f->ret_target);
1144 push_text_atom(f, s);
1146 return val;
1149 static struct storage *emit_conditional_expr(struct expression *expr)
1151 struct storage *cond = x86_expression(expr->conditional);
1152 struct storage *true = x86_expression(expr->cond_true);
1153 struct storage *false = x86_expression(expr->cond_false);
1154 struct storage *new = new_pseudo();
1156 if (!true)
1157 true = cond;
1159 emit_move(cond, REG_EAX, expr->conditional->ctype,
1160 "begin EXPR_CONDITIONAL", 0);
1161 emit_move(true, REG_ECX, expr->cond_true->ctype, NULL, 0);
1162 emit_move(false, REG_EDX, expr->cond_false->ctype, NULL, 0);
1164 /* test EAX (for zero/non-zero) */
1165 insn("test", REG_EAX, REG_EAX, NULL, 0);
1167 /* if false, move EDX to ECX */
1168 insn("cmovz", REG_EDX, REG_ECX, NULL, 0);
1170 /* finally, store the result (ECX) in a new pseudo / stack slot */
1171 new = new_pseudo();
1172 emit_move(REG_ECX, new, expr->ctype, "end EXPR_CONDITIONAL", 0);
1173 /* FIXME: we lose type knowledge of expression result at this point */
1175 return new;
1178 static struct storage *emit_symbol_expr_init(struct symbol *sym)
1180 struct expression *expr = sym->initializer;
1181 struct symbol_private *priv = sym->aux;
1183 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC))
1184 expr = NULL;
1186 if (priv == NULL) {
1187 priv = calloc(1, sizeof(*priv));
1188 sym->aux = priv;
1190 if (expr == NULL) {
1191 struct storage *new = new_pseudo();
1192 fprintf(stderr, "FIXME! no value for symbol. creating pseudo %d (stack offset %d)\n",
1193 new->pseudo, new->pseudo * 4);
1194 priv->addr = new;
1195 } else {
1196 priv->addr = x86_expression(expr);
1200 return priv->addr;
1203 static struct storage *emit_string_expr(struct expression *expr)
1205 struct function *f = current_func;
1206 int label = new_label();
1207 struct storage *new = new_pseudo();
1209 push_cstring(f, expr->string, label);
1211 new = new_storage(STOR_LABEL);
1212 new->label = label;
1213 new->flags = STOR_LABEL_VAL;
1214 return new;
1217 static struct storage *emit_cast_expr(struct expression *expr)
1219 struct symbol *old_type, *new_type;
1220 struct storage *op = x86_expression(expr->cast_expression);
1221 int oldbits, newbits;
1222 struct storage *new;
1224 old_type = expr->cast_expression->ctype;
1225 new_type = expr->cast_type;
1227 oldbits = old_type->bit_size;
1228 newbits = new_type->bit_size;
1229 if (oldbits >= newbits)
1230 return op;
1232 emit_move(op, REG_EAX, old_type, "begin cast ..", 0);
1234 new = new_pseudo();
1235 emit_move(REG_EAX, new, new_type, ".... end cast", 0);
1237 return new;
1240 static void x86_struct_member(struct symbol *sym, void *data, int flags)
1242 if (flags & ITERATE_FIRST)
1243 printf(" {\n\t");
1244 printf("%s:%d:%ld at offset %ld", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset);
1245 if (sym->fieldwidth)
1246 printf("[%d..%d]", sym->bit_offset, sym->bit_offset+sym->fieldwidth-1);
1247 if (flags & ITERATE_LAST)
1248 printf("\n} ");
1249 else
1250 printf(", ");
1253 static void x86_symbol(struct symbol *sym)
1255 struct symbol *type;
1257 if (!sym)
1258 return;
1260 type = sym->ctype.base_type;
1261 if (!type)
1262 return;
1265 * Show actual implementation information
1267 switch (type->type) {
1269 case SYM_ARRAY:
1270 if (sym->initializer)
1271 emit_array(sym);
1272 else
1273 emit_array_noinit(sym);
1274 break;
1276 case SYM_BASETYPE:
1277 if (sym->initializer) {
1278 emit_object_pre(show_ident(sym->ident),
1279 sym->ctype.modifiers,
1280 sym->ctype.alignment,
1281 sym->bit_size / 8);
1282 emit_scalar(sym->initializer, sym->bit_size);
1283 stor_sym_init(sym);
1284 } else
1285 emit_scalar_noinit(sym);
1286 break;
1288 case SYM_STRUCT:
1289 symbol_iterate(type->symbol_list, x86_struct_member, NULL);
1290 break;
1292 case SYM_UNION:
1293 symbol_iterate(type->symbol_list, x86_struct_member, NULL);
1294 break;
1296 case SYM_FN: {
1297 struct statement *stmt = type->stmt;
1298 if (stmt) {
1299 struct storage *val;
1301 emit_func_pre(sym);
1302 val = x86_statement(stmt);
1303 emit_func_post(sym, val);
1305 break;
1308 default:
1309 break;
1312 if (sym->initializer && (type->type != SYM_BASETYPE) &&
1313 (type->type != SYM_ARRAY)) {
1314 printf(" = \n");
1315 x86_expression(sym->initializer);
1319 static void x86_symbol_init(struct symbol *sym);
1321 static void x86_switch_statement(struct statement *stmt)
1323 struct storage *val = x86_expression(stmt->switch_expression);
1324 struct symbol *sym;
1325 printf("\tswitch v%d\n", val->pseudo);
1328 * Debugging only: Check that the case list is correct
1329 * by printing it out.
1331 * This is where a _real_ back-end would go through the
1332 * cases to decide whether to use a lookup table or a
1333 * series of comparisons etc
1335 printf("# case table:\n");
1336 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
1337 struct statement *case_stmt = sym->stmt;
1338 struct expression *expr = case_stmt->case_expression;
1339 struct expression *to = case_stmt->case_to;
1341 if (!expr) {
1342 printf(" default");
1343 } else {
1344 if (expr->type == EXPR_VALUE) {
1345 printf(" case %lld", expr->value);
1346 if (to) {
1347 if (to->type == EXPR_VALUE) {
1348 printf(" .. %lld", to->value);
1349 } else {
1350 printf(" .. what?");
1353 } else
1354 printf(" what?");
1356 printf(": .L%p\n", sym);
1357 } END_FOR_EACH_PTR;
1358 printf("# end case table\n");
1360 x86_statement(stmt->switch_statement);
1362 if (stmt->switch_break->used)
1363 printf(".L%p:\n", stmt->switch_break);
1366 static void x86_symbol_decl(struct symbol_list *syms)
1368 struct symbol *sym;
1369 FOR_EACH_PTR(syms, sym) {
1370 x86_symbol_init(sym);
1371 } END_FOR_EACH_PTR;
1375 * Print out a statement
1377 static struct storage *x86_statement(struct statement *stmt)
1379 if (!stmt)
1380 return 0;
1381 switch (stmt->type) {
1382 case STMT_RETURN:
1383 return emit_return_stmt(stmt);
1384 case STMT_COMPOUND: {
1385 struct statement *s;
1386 struct storage *last = NULL;
1388 x86_symbol_decl(stmt->syms);
1389 FOR_EACH_PTR(stmt->stmts, s) {
1390 last = x86_statement(s);
1391 } END_FOR_EACH_PTR;
1393 return last;
1396 case STMT_EXPRESSION:
1397 return x86_expression(stmt->expression);
1398 case STMT_IF:
1399 emit_if_conditional(stmt);
1400 return NULL;
1401 case STMT_SWITCH:
1402 x86_switch_statement(stmt);
1403 break;
1405 case STMT_CASE:
1406 printf(".L%p:\n", stmt->case_label);
1407 x86_statement(stmt->case_statement);
1408 break;
1410 case STMT_ITERATOR: {
1411 struct statement *pre_statement = stmt->iterator_pre_statement;
1412 struct expression *pre_condition = stmt->iterator_pre_condition;
1413 struct statement *statement = stmt->iterator_statement;
1414 struct statement *post_statement = stmt->iterator_post_statement;
1415 struct expression *post_condition = stmt->iterator_post_condition;
1416 int loop_top = 0, loop_bottom = 0;
1417 struct storage *val;
1419 x86_symbol_decl(stmt->iterator_syms);
1420 x86_statement(pre_statement);
1421 if (pre_condition) {
1422 if (pre_condition->type == EXPR_VALUE) {
1423 if (!pre_condition->value) {
1424 loop_bottom = new_label();
1425 printf("\tjmp\t\t.L%d\n", loop_bottom);
1427 } else {
1428 loop_bottom = new_label();
1429 val = x86_expression(pre_condition);
1430 printf("\tje\t\tv%d, .L%d\n", val->pseudo, loop_bottom);
1433 if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
1434 loop_top = new_label();
1435 printf(".L%d:\n", loop_top);
1437 x86_statement(statement);
1438 if (stmt->iterator_continue->used)
1439 printf(".L%p:\n", stmt->iterator_continue);
1440 x86_statement(post_statement);
1441 if (!post_condition) {
1442 printf("\tjmp\t\t.L%d\n", loop_top);
1443 } else if (post_condition->type == EXPR_VALUE) {
1444 if (post_condition->value)
1445 printf("\tjmp\t\t.L%d\n", loop_top);
1446 } else {
1447 val = x86_expression(post_condition);
1448 printf("\tjne\t\tv%d, .L%d\n", val->pseudo, loop_top);
1450 if (stmt->iterator_break->used)
1451 printf(".L%p:\n", stmt->iterator_break);
1452 if (loop_bottom)
1453 printf(".L%d:\n", loop_bottom);
1454 break;
1456 case STMT_NONE:
1457 break;
1459 case STMT_LABEL:
1460 printf(".L%p:\n", stmt->label_identifier);
1461 x86_statement(stmt->label_statement);
1462 break;
1464 case STMT_GOTO:
1465 if (stmt->goto_expression) {
1466 struct storage *val = x86_expression(stmt->goto_expression);
1467 printf("\tgoto *v%d\n", val->pseudo);
1468 } else {
1469 printf("\tgoto .L%p\n", stmt->goto_label);
1471 break;
1472 case STMT_ASM:
1473 printf("\tasm( .... )\n");
1474 break;
1477 return 0;
1480 static struct storage *x86_call_expression(struct expression *expr)
1482 struct function *f = current_func;
1483 struct symbol *direct;
1484 struct expression *arg, *fn;
1485 struct storage *retval, *fncall;
1486 int framesize;
1487 char s[64];
1489 if (!expr->ctype) {
1490 warn(expr->pos, "\tcall with no type!");
1491 return NULL;
1494 framesize = 0;
1495 FOR_EACH_PTR_REVERSE(expr->args, arg) {
1496 struct storage *new = x86_expression(arg);
1497 int size = arg->ctype->bit_size;
1499 /* FIXME: pay attention to 'size' */
1500 insn("pushl", new, NULL,
1501 !framesize ? "begin function call" : NULL, 0);
1503 framesize += size >> 3;
1504 } END_FOR_EACH_PTR_REVERSE;
1506 fn = expr->fn;
1508 /* Remove dereference, if any */
1509 direct = NULL;
1510 if (fn->type == EXPR_PREOP) {
1511 if (fn->unop->type == EXPR_SYMBOL) {
1512 struct symbol *sym = fn->unop->symbol;
1513 if (sym->ctype.base_type->type == SYM_FN)
1514 direct = sym;
1517 if (direct) {
1518 sprintf(s, "\tcall\t%s\n", show_ident(direct->ident));
1519 push_text_atom(f, s);
1520 } else {
1521 fncall = x86_expression(fn);
1522 emit_move(fncall, REG_EAX, fn->ctype, NULL, 0);
1524 strcpy(s, "\tcall\t*%%eax\n");
1525 push_text_atom(f, s);
1528 /* FIXME: pay attention to BITS_IN_POINTER */
1529 if (framesize) {
1530 struct storage *val = new_storage(STOR_VALUE);
1531 val->value = (long long) framesize;
1532 insn("addl", val, REG_ESP, NULL, ATOM_FREE_OP1);
1535 retval = new_pseudo();
1536 emit_move(REG_EAX, retval, expr->ctype, "end function call", 0);
1538 return retval;
1541 static struct storage *x86_regular_preop(struct expression *expr)
1543 struct storage *target = x86_expression(expr->unop);
1544 struct storage *new = new_pseudo();
1545 static const char *name[] = {
1546 ['!'] = "nonzero", ['-'] = "neg",
1547 ['~'] = "not",
1549 unsigned int op = expr->op;
1550 const char *opname;
1552 opname = show_special(op);
1553 if (op < sizeof(name)/sizeof(*name))
1554 opname = name[op];
1555 printf("\t%s.%d\t\tv%d,v%d\n", opname, expr->ctype->bit_size, new->pseudo, target->pseudo);
1556 return new;
1560 * FIXME! Not all accesses are memory loads. We should
1561 * check what kind of symbol is behind the dereference.
1563 static struct storage *x86_address_gen(struct expression *expr)
1565 if (expr->type == EXPR_PREOP)
1566 return x86_expression(expr->unop);
1567 return x86_expression(expr->address);
1570 static struct storage *x86_assignment(struct expression *expr)
1572 struct expression *target = expr->left;
1573 struct storage *val, *addr;
1574 int bits;
1576 if (!expr->ctype)
1577 return NULL;
1579 bits = expr->ctype->bit_size;
1580 val = x86_expression(expr->right);
1581 addr = x86_address_gen(target);
1582 emit_copy(val, expr->right->ctype, addr, expr->left->ctype);
1583 return val;
1586 static int x86_initialization(struct symbol *sym, struct expression *expr)
1588 struct storage *val, *addr;
1589 int bits;
1591 if (!expr->ctype)
1592 return 0;
1594 bits = expr->ctype->bit_size;
1595 val = x86_expression(expr);
1596 addr = x86_symbol_expr(sym);
1597 // FIXME! The "target" expression is for bitfield store information.
1598 // Leave it NULL, which works fine.
1599 emit_store(NULL, addr, val, bits);
1600 return 0;
1603 static struct storage *x86_access(struct expression *expr)
1605 return x86_address_gen(expr);
1608 static struct storage *x86_preop(struct expression *expr)
1611 * '*' is an lvalue access, and is fundamentally different
1612 * from an arithmetic operation. Maybe it should have an
1613 * expression type of its own..
1615 if (expr->op == '*')
1616 return x86_access(expr);
1617 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
1618 return emit_inc_dec(expr, 0);
1619 return x86_regular_preop(expr);
1622 static struct storage *x86_symbol_expr(struct symbol *sym)
1624 struct storage *new = new_pseudo();
1626 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
1627 printf("\tmovi.%d\t\tv%d,$%s\n", BITS_IN_POINTER, new->pseudo, show_ident(sym->ident));
1628 return new;
1630 if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
1631 printf("\taddi.%d\t\tv%d,vFP,$%lld\n", BITS_IN_POINTER, new->pseudo, sym->value);
1632 return new;
1634 printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", BITS_IN_POINTER, new->pseudo, show_ident(sym->ident), sym);
1635 return new;
1638 static void x86_symbol_init(struct symbol *sym)
1640 struct symbol_private *priv = sym->aux;
1641 struct expression *expr = sym->initializer;
1642 struct storage *new;
1644 if (expr)
1645 new = x86_expression(expr);
1646 else
1647 new = new_pseudo();
1649 if (!priv) {
1650 priv = calloc(1, sizeof(*priv));
1651 sym->aux = priv;
1652 /* FIXME: leak! we don't free... */
1653 /* (well, we don't free symbols either) */
1656 priv->addr = new;
1659 static int type_is_signed(struct symbol *sym)
1661 if (sym->type == SYM_NODE)
1662 sym = sym->ctype.base_type;
1663 if (sym->type == SYM_PTR)
1664 return 0;
1665 return !(sym->ctype.modifiers & MOD_UNSIGNED);
1668 static struct storage *x86_bitfield_expr(struct expression *expr)
1670 return x86_access(expr);
1673 static struct storage *x86_label_expr(struct expression *expr)
1675 struct storage *new = new_pseudo();
1676 printf("\tmovi.%d\t\tv%d,.L%p\n",BITS_IN_POINTER, new->pseudo, expr->label_symbol);
1677 return new;
1680 static struct storage *x86_statement_expr(struct expression *expr)
1682 return x86_statement(expr->statement);
1685 static int x86_position_expr(struct expression *expr, struct symbol *base)
1687 struct storage *new = x86_expression(expr->init_expr);
1688 struct symbol *ctype = expr->init_sym;
1690 printf("\tinsert v%d at [%d:%d] of %s\n", new->pseudo,
1691 expr->init_offset, ctype->bit_offset,
1692 show_ident(base->ident));
1693 return 0;
1696 static void x86_initializer_expr(struct expression *expr, struct symbol *ctype)
1698 struct expression *entry;
1700 FOR_EACH_PTR(expr->expr_list, entry) {
1701 // Nested initializers have their positions already
1702 // recursively calculated - just output them too
1703 if (entry->type == EXPR_INITIALIZER) {
1704 x86_initializer_expr(entry, ctype);
1705 continue;
1708 // Ignore initializer indexes and identifiers - the
1709 // evaluator has taken them into account
1710 if (entry->type == EXPR_IDENTIFIER || entry->type == EXPR_INDEX)
1711 continue;
1712 if (entry->type == EXPR_POS) {
1713 x86_position_expr(entry, ctype);
1714 continue;
1716 x86_initialization(ctype, entry);
1717 } END_FOR_EACH_PTR;
1721 * Print out an expression. Return the pseudo that contains the
1722 * variable.
1724 static struct storage *x86_expression(struct expression *expr)
1726 if (!expr)
1727 return 0;
1729 if (!expr->ctype) {
1730 struct position *pos = &expr->pos;
1731 printf("\tno type at %s:%d:%d\n",
1732 input_streams[pos->stream].name,
1733 pos->line, pos->pos);
1734 return 0;
1737 switch (expr->type) {
1738 case EXPR_CALL:
1739 return x86_call_expression(expr);
1741 case EXPR_ASSIGNMENT:
1742 return x86_assignment(expr);
1744 case EXPR_COMPARE:
1745 return emit_compare(expr);
1746 case EXPR_BINOP:
1747 case EXPR_COMMA:
1748 case EXPR_LOGICAL:
1749 return emit_binop(expr);
1750 case EXPR_PREOP:
1751 return x86_preop(expr);
1752 case EXPR_POSTOP:
1753 return emit_postop(expr);
1754 case EXPR_SYMBOL:
1755 return emit_symbol_expr_init(expr->symbol);
1756 case EXPR_DEREF:
1757 case EXPR_SIZEOF:
1758 warn(expr->pos, "invalid expression after evaluation");
1759 return 0;
1760 case EXPR_CAST:
1761 return emit_cast_expr(expr);
1762 case EXPR_VALUE:
1763 return emit_value(expr);
1764 case EXPR_STRING:
1765 return emit_string_expr(expr);
1766 case EXPR_BITFIELD:
1767 return x86_bitfield_expr(expr);
1768 case EXPR_INITIALIZER:
1769 x86_initializer_expr(expr, expr->ctype);
1770 return NULL;
1771 case EXPR_CONDITIONAL:
1772 return emit_conditional_expr(expr);
1773 case EXPR_STATEMENT:
1774 return x86_statement_expr(expr);
1775 case EXPR_LABEL:
1776 return x86_label_expr(expr);
1778 // None of these should exist as direct expressions: they are only
1779 // valid as sub-expressions of initializers.
1780 case EXPR_POS:
1781 warn(expr->pos, "unable to show plain initializer position expression");
1782 return 0;
1783 case EXPR_IDENTIFIER:
1784 warn(expr->pos, "unable to show identifier expression");
1785 return 0;
1786 case EXPR_INDEX:
1787 warn(expr->pos, "unable to show index expression");
1788 return 0;
1790 return 0;