1 #include "../cc/cc.h" 2 #include "../vc/v.out.h" 3 4 #define TINT TLONG 5 #define TFIELD TLONG 6 #define SZ_CHAR 1 7 #define SZ_SHORT 2 8 #define SZ_LONG 4 9 #define SZ_VLONG 8 10 #define SZ_IND 4 11 #define SZ_FLOAT 4 12 #define SZ_DOUBLE 8 13 #define SU_ALLIGN SZ_LONG 14 #define SU_PAD SZ_LONG 15 #define FNX 100 16 #define INLINE (5*SZ_LONG) 17 18 typedef struct Adr Adr; 19 typedef struct Prog Prog; 20 typedef struct Case Case; 21 typedef struct C1 C1; 22 typedef struct Multab Multab; 23 typedef struct Hintab Hintab; 24 typedef struct Bits Bits; 25 typedef struct Var Var; 26 typedef struct Reg Reg; 27 typedef struct Rgn Rgn; 28 29 30 struct Adr 31 { 32 union 33 { 34 long offset; 35 double dval; 36 char sval[NSNAME]; 37 Ieee ieee; 38 }; 39 Sym* sym; 40 char type; 41 char reg; 42 char name; 43 char etype; 44 }; 45 #define A ((Adr*)0) 46 47 #define INDEXED 9 48 struct Prog 49 { 50 Adr from; 51 Adr to; 52 Prog* link; 53 long lineno; 54 char as; 55 char reg; 56 }; 57 #define P ((Prog*)0) 58 59 struct Case 60 { 61 Case* link; 62 long val; 63 long label; 64 char def; 65 }; 66 #define C ((Case*)0) 67 68 struct C1 69 { 70 long val; 71 long label; 72 }; 73 74 struct Multab 75 { 76 long val; 77 char code[20]; 78 }; 79 80 struct Hintab 81 { 82 ushort val; 83 char hint[10]; 84 }; 85 86 #define BITS 6 87 #define NVAR (BITS*sizeof(ulong)*8) 88 struct Bits 89 { 90 ulong b[BITS]; 91 }; 92 93 struct Var 94 { 95 long offset; 96 Sym* sym; 97 char name; 98 char etype; 99 }; 100 101 struct Reg 102 { 103 long pc; 104 105 Bits set; 106 Bits use1; 107 Bits use2; 108 109 Bits refbehind; 110 Bits refahead; 111 Bits calbehind; 112 Bits calahead; 113 Bits regdiff; 114 Bits act; 115 116 long regu; 117 long loop; /* could be shorter */ 118 119 union { 120 Reg* log5; 121 int active; 122 }; 123 Reg* p1; 124 Reg* p2; 125 Reg* p2link; 126 Reg* s1; 127 Reg* s2; 128 Reg* link; 129 Prog* prog; 130 }; 131 #define R ((Reg*)0) 132 133 #define NRGN 600 134 struct Rgn 135 { 136 Reg* enter; 137 short cost; 138 short varno; 139 short regno; 140 }; 141 142 long breakpc; 143 Case* cases; 144 Node constnode; 145 Node fconstnode; 146 long continpc; 147 long curarg; 148 long cursafe; 149 Prog* firstp; 150 Prog* lastp; 151 long maxargsafe; 152 int mnstring; 153 Multab multab[20]; 154 int retok; 155 int hintabsize; 156 Node* nodrat; 157 Node* nodret; 158 Node* nodsafe; 159 long nrathole; 160 long nstring; 161 Prog* p; 162 long pc; 163 Node regnode; 164 char string[NSNAME]; 165 Sym* symrathole; 166 Node znode; 167 Prog zprog; 168 char reg[NREG+NREG]; 169 long exregoffset; 170 long exfregoffset; 171 172 #define BLOAD(r) band(bnot(r->refbehind), r->refahead) 173 #define BSTORE(r) band(bnot(r->calbehind), r->calahead) 174 #define LOAD(r) (~r->refbehind.b[z] & r->refahead.b[z]) 175 #define STORE(r) (~r->calbehind.b[z] & r->calahead.b[z]) 176 177 #define bset(a,n) ((a).b[(n)/32]&(1L<<(n)%32)) 178 179 #define CLOAD 4 180 #define CREF 5 181 #define CINF 1000 182 #define LOOP 3 183 184 Rgn region[NRGN]; 185 Rgn* rgp; 186 int nregion; 187 int nvar; 188 189 Bits externs; 190 Bits params; 191 Bits consts; 192 Bits addrs; 193 Bits zbits; 194 195 long regbits; 196 long exregbits; 197 198 int change; 199 200 Reg* firstr; 201 Reg* lastr; 202 Reg zreg; 203 Reg* freer; 204 Var var[NVAR]; 205 206 extern char* anames[]; 207 extern Hintab hintab[]; 208 209 /* 210 * sgen.c 211 */ 212 void codgen(Node*, Node*); 213 void gen(Node*); 214 void noretval(int); 215 void xcom(Node*); 216 void bcomplex(Node*); 217 218 /* 219 * cgen.c 220 */ 221 void cgen(Node*, Node*); 222 void reglcgen(Node*, Node*, Node*); 223 void lcgen(Node*, Node*); 224 void bcgen(Node*, int); 225 void boolgen(Node*, int, Node*); 226 void sugen(Node*, Node*, long); 227 void layout(Node*, Node*, int, int, Node*); 228 229 /* 230 * txt.c 231 */ 232 void ginit(void); 233 void gclean(void); 234 void nextpc(void); 235 void gargs(Node*, Node*, Node*); 236 void garg1(Node*, Node*, Node*, int, Node**); 237 Node* nodconst(long); 238 Node* nod32const(vlong); 239 Node* nodfconst(double); 240 void nodreg(Node*, Node*, int); 241 void regret(Node*, Node*); 242 void regalloc(Node*, Node*, Node*); 243 void regfree(Node*); 244 void regialloc(Node*, Node*, Node*); 245 void regsalloc(Node*, Node*); 246 void regaalloc1(Node*, Node*); 247 void regaalloc(Node*, Node*); 248 void regind(Node*, Node*); 249 void gprep(Node*, Node*); 250 void raddr(Node*, Prog*); 251 void naddr(Node*, Adr*); 252 void gmove(Node*, Node*); 253 void gins(int a, Node*, Node*); 254 void gopcode(int, Node*, Node*, Node*); 255 int samaddr(Node*, Node*); 256 void gbranch(int); 257 void patch(Prog*, long); 258 int sconst(Node*); 259 int sval(long); 260 void gpseudo(int, Sym*, Node*); 261 262 /* 263 * swt.c 264 */ 265 int swcmp(void*, void*); 266 void doswit(Node*); 267 void swit1(C1*, int, long, Node*, Node*); 268 void cas(void); 269 void bitload(Node*, Node*, Node*, Node*, Node*); 270 void bitstore(Node*, Node*, Node*, Node*, Node*); 271 long outstring(char*, long); 272 int vlog(Node*); 273 int mulcon(Node*, Node*); 274 Multab* mulcon0(long); 275 void nullwarn(Node*, Node*); 276 void sextern(Sym*, Node*, long, long); 277 void gextern(Sym*, Node*, long, long); 278 void outcode(void); 279 void ieeedtod(Ieee*, double); 280 281 /* 282 * list 283 */ 284 void listinit(void); 285 int Pconv(void*, Fconv*); 286 int Aconv(void*, Fconv*); 287 int Dconv(void*, Fconv*); 288 int Sconv(void*, Fconv*); 289 int Nconv(void*, Fconv*); 290 int Bconv(void*, Fconv*); 291 292 /* 293 * reg.c 294 */ 295 Reg* rega(void); 296 int rcmp(void*, void*); 297 void regopt(Prog*); 298 void addmove(Reg*, int, int, int); 299 Bits mkvar(Adr*, int); 300 void prop(Reg*, Bits, Bits); 301 int loopit(Reg*); 302 void synch(Reg*, Bits); 303 ulong allreg(ulong, Rgn*); 304 void paint1(Reg*, int); 305 ulong paint2(Reg*, int); 306 void paint3(Reg*, int, long, int); 307 void addreg(Adr*, int); 308 309 /* 310 * peep.c 311 */ 312 void peep(void); 313 void excise(Reg*); 314 Reg* uniqp(Reg*); 315 Reg* uniqs(Reg*); 316 int regtyp(Adr*); 317 int regzer(Adr*); 318 int anyvar(Adr*); 319 int subprop(Reg*); 320 int copyprop(Reg*); 321 int copy1(Adr*, Adr*, Reg*, int); 322 int copyu(Prog*, Adr*, Adr*); 323 324 int copyas(Adr*, Adr*); 325 int copyau(Adr*, Adr*); 326 int copyau1(Prog*, Adr*); 327 int copysub(Adr*, Adr*, Adr*, int); 328 int copysub1(Prog*, Adr*, Adr*, int); 329 330 long RtoB(int); 331 long FtoB(int); 332 int BtoR(long); 333 int BtoF(long); 334 335 /* 336 * bits.c 337 */ 338 Bits bor(Bits, Bits); 339 Bits band(Bits, Bits); 340 Bits bnot(Bits); 341 int bany(Bits*); 342 int bnum(Bits); 343 Bits blsh(unsigned); 344 int beq(Bits, Bits); 345