| 1 | /* $Id: code.c,v 1.3 2009/02/08 16:41:35 ragge Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * 3. The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | # include "pass1.h"
|
|---|
| 31 |
|
|---|
| 32 | int lastloc = -1;
|
|---|
| 33 |
|
|---|
| 34 | /*
|
|---|
| 35 | * Define everything needed to print out some data (or text).
|
|---|
| 36 | * This means segment, alignment, visibility, etc.
|
|---|
| 37 | */
|
|---|
| 38 | void
|
|---|
| 39 | defloc(struct symtab *sp)
|
|---|
| 40 | {
|
|---|
| 41 | static char *loctbl[] = { "text", "data", "data" };
|
|---|
| 42 | TWORD t;
|
|---|
| 43 | char *n;
|
|---|
| 44 | int s;
|
|---|
| 45 |
|
|---|
| 46 | if (sp == NULL) {
|
|---|
| 47 | lastloc = -1;
|
|---|
| 48 | return;
|
|---|
| 49 | }
|
|---|
| 50 | t = sp->stype;
|
|---|
| 51 | s = ISFTN(t) ? PROG : ISCON(cqual(t, sp->squal)) ? RDATA : DATA;
|
|---|
| 52 | if (s != lastloc)
|
|---|
| 53 | printf(" .%s\n", loctbl[s]);
|
|---|
| 54 | lastloc = s;
|
|---|
| 55 | while (ISARY(t))
|
|---|
| 56 | t = DECREF(t);
|
|---|
| 57 | n = sp->soname ? sp->soname : exname(sp->sname);
|
|---|
| 58 | if (sp->sclass == EXTDEF)
|
|---|
| 59 | printf(" .globl %s\n", n);
|
|---|
| 60 | if (ISFTN(sp->stype) || talign(sp->stype, sp->ssue) > ALCHAR)
|
|---|
| 61 | printf(".even\n");
|
|---|
| 62 | if (sp->slevel == 0) {
|
|---|
| 63 | printf("%s:\n", n);
|
|---|
| 64 | } else {
|
|---|
| 65 | printf(LABFMT ":\n", sp->soffset);
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | * code for the end of a function
|
|---|
| 71 | * deals with struct return here
|
|---|
| 72 | */
|
|---|
| 73 | void
|
|---|
| 74 | efcode()
|
|---|
| 75 | {
|
|---|
| 76 | NODE *p, *q;
|
|---|
| 77 |
|
|---|
| 78 | if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
|
|---|
| 79 | return;
|
|---|
| 80 | /* Create struct assignment */
|
|---|
| 81 | q = block(OREG, NIL, NIL, PTR+STRTY, 0, cftnsp->ssue);
|
|---|
| 82 | q->n_rval = R5;
|
|---|
| 83 | q->n_lval = 8; /* return buffer offset */
|
|---|
| 84 | q = buildtree(UMUL, q, NIL);
|
|---|
| 85 | p = block(REG, NIL, NIL, PTR+STRTY, 0, cftnsp->ssue);
|
|---|
| 86 | p = buildtree(UMUL, p, NIL);
|
|---|
| 87 | p = buildtree(ASSIGN, q, p);
|
|---|
| 88 | ecomp(p);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /*
|
|---|
| 92 | * code for the beginning of a function; a is an array of
|
|---|
| 93 | * indices in symtab for the arguments; n is the number
|
|---|
| 94 | */
|
|---|
| 95 | void
|
|---|
| 96 | bfcode(struct symtab **sp, int cnt)
|
|---|
| 97 | {
|
|---|
| 98 | struct symtab *sp2;
|
|---|
| 99 | NODE *n;
|
|---|
| 100 | int i;
|
|---|
| 101 |
|
|---|
| 102 | if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
|
|---|
| 103 | /* Function returns struct, adjust arg offset */
|
|---|
| 104 | for (i = 0; i < cnt; i++)
|
|---|
| 105 | sp[i]->soffset += SZPOINT(INT);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (xtemps == 0)
|
|---|
| 109 | return;
|
|---|
| 110 |
|
|---|
| 111 | /* put arguments in temporaries */
|
|---|
| 112 | for (i = 0; i < cnt; i++) {
|
|---|
| 113 | if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY ||
|
|---|
| 114 | cisreg(sp[i]->stype) == 0)
|
|---|
| 115 | continue;
|
|---|
| 116 | sp2 = sp[i];
|
|---|
| 117 | n = tempnode(0, sp[i]->stype, sp[i]->sdf, sp[i]->ssue);
|
|---|
| 118 | n = buildtree(ASSIGN, n, nametree(sp2));
|
|---|
| 119 | sp[i]->soffset = regno(n->n_left);
|
|---|
| 120 | sp[i]->sflags |= STNODE;
|
|---|
| 121 | ecomp(n);
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | /*
|
|---|
| 127 | * by now, the automatics and register variables are allocated
|
|---|
| 128 | */
|
|---|
| 129 | void
|
|---|
| 130 | bccode()
|
|---|
| 131 | {
|
|---|
| 132 | SETOFF(autooff, SZINT);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* called just before final exit */
|
|---|
| 136 | /* flag is 1 if errors, 0 if none */
|
|---|
| 137 | void
|
|---|
| 138 | ejobcode(int flag )
|
|---|
| 139 | {
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | void
|
|---|
| 143 | bjobcode()
|
|---|
| 144 | {
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /*
|
|---|
| 148 | * Called with a function call with arguments as argument.
|
|---|
| 149 | * This is done early in buildtree() and only done once.
|
|---|
| 150 | * Returns p.
|
|---|
| 151 | */
|
|---|
| 152 | NODE *
|
|---|
| 153 | funcode(NODE *p)
|
|---|
| 154 | {
|
|---|
| 155 | NODE *r, *l;
|
|---|
| 156 |
|
|---|
| 157 | /* Fix function call arguments. On x86, just add funarg */
|
|---|
| 158 | for (r = p->n_right; r->n_op == CM; r = r->n_left) {
|
|---|
| 159 | if (r->n_right->n_op != STARG)
|
|---|
| 160 | r->n_right = block(FUNARG, r->n_right, NIL,
|
|---|
| 161 | r->n_right->n_type, r->n_right->n_df,
|
|---|
| 162 | r->n_right->n_sue);
|
|---|
| 163 | }
|
|---|
| 164 | if (r->n_op != STARG) {
|
|---|
| 165 | l = talloc();
|
|---|
| 166 | *l = *r;
|
|---|
| 167 | r->n_op = FUNARG;
|
|---|
| 168 | r->n_left = l;
|
|---|
| 169 | r->n_type = l->n_type;
|
|---|
| 170 | }
|
|---|
| 171 | return p;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /*
|
|---|
| 175 | * return the alignment of field of type t
|
|---|
| 176 | */
|
|---|
| 177 | int
|
|---|
| 178 | fldal(unsigned int t)
|
|---|
| 179 | {
|
|---|
| 180 | uerror("illegal field type");
|
|---|
| 181 | return(ALINT);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /* fix up type of field p */
|
|---|
| 185 | void
|
|---|
| 186 | fldty(struct symtab *p)
|
|---|
| 187 | {
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /*
|
|---|
| 191 | * XXX - fix genswitch.
|
|---|
| 192 | */
|
|---|
| 193 | int
|
|---|
| 194 | mygenswitch(int num, TWORD type, struct swents **p, int n)
|
|---|
| 195 | {
|
|---|
| 196 | return 0;
|
|---|
| 197 | }
|
|---|