PulkoMandy | 17fc759 | 2022-07-28 18:27:54 +0200 | [diff] [blame] | 1 | /* Code generator for SAB c16x microcontrollers. */ |
| 2 | |
| 3 | #include "supp.h" |
| 4 | |
| 5 | static char FILE_[]=__FILE__; |
| 6 | /* Public data that MUST be there. */ |
| 7 | |
| 8 | /* Name and copyright. */ |
| 9 | char cg_copyright[]="vbcc code-generator for c16x V0.3 (c) in 1998-2005 by Volker Barthelmann"; |
| 10 | |
| 11 | /* Commandline-flags the code-generator accepts */ |
| 12 | int g_flags[MAXGF]={VALFLAG,0,0,0, |
| 13 | 0,0,0,0, |
| 14 | 0}; |
| 15 | char *g_flags_name[MAXGF]={"cpu","int32","no-delayed-popping","const-in-data", |
| 16 | "merge-constants","no-peephole","mtiny","mlarge", |
| 17 | "mhuge","tasking"}; |
| 18 | union ppi g_flags_val[MAXGF]; |
| 19 | |
| 20 | /* Typenames (needed because of HAVE_EXT_TYPES). */ |
| 21 | char *typname[]={"strange","bit","char","short","int","long","long long", |
| 22 | "float","double","long double","void", |
| 23 | "near-pointer","far-pointer","huge-pointer", |
| 24 | "array","struct","union","enum","function"}; |
| 25 | |
| 26 | /* Alignment-requirements for all types in bytes. */ |
| 27 | zmax align[MAX_TYPE+1]; |
| 28 | |
| 29 | /* Alignment that is sufficient for every object. */ |
| 30 | zmax maxalign; |
| 31 | |
| 32 | /* CHAR_BIT of the target machine. */ |
| 33 | zmax char_bit; |
| 34 | |
| 35 | /* Sizes of all elementary types in bytes. */ |
| 36 | zmax sizetab[MAX_TYPE+1]; |
| 37 | |
| 38 | /* Minimum and Maximum values each type can have. */ |
| 39 | /* Must be initialized in init_cg(). */ |
| 40 | zmax t_min[MAX_TYPE+1]; |
| 41 | zumax t_max[MAX_TYPE+1]; |
| 42 | zumax tu_max[MAX_TYPE+1]; |
| 43 | |
| 44 | /* Names of all registers. */ |
| 45 | char *regnames[]={"noreg","R0","R1","R2","R3","R4","R5","R6","R7", |
| 46 | "R8","R9","R10","R11","R12","R13","R14","R15", |
| 47 | "R6/R7","R7/R8","R8/R9","R4/R5", |
| 48 | "R14/R15","R13/R14","R12/R13", |
| 49 | "R3/R4","R2/R3", |
| 50 | "MAXR+1","MAXR+2","ZEROS","ONES"}; |
| 51 | |
| 52 | /* The Size of each register in bytes. */ |
| 53 | zmax regsize[MAXR+1]; |
| 54 | |
| 55 | /* Type which can store each register. */ |
| 56 | struct Typ *regtype[MAXR+1]; |
| 57 | |
| 58 | /* regsa[reg]!=0 if a certain register is allocated and should */ |
| 59 | /* not be used by the compiler pass. */ |
| 60 | int regsa[MAXR+1]; |
| 61 | |
| 62 | /* Specifies which registers may be scratched by functions. */ |
| 63 | int regscratch[MAXR+1]={0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1}; |
| 64 | |
| 65 | int reg_prio[MAXR+1]={0,0,0,0,0,2,3,1,1,1,1,0,0,4,5,6,7,1,1,1,2,6,5,4,0,0}; |
| 66 | |
| 67 | struct reg_handle empty_reg_handle={0}; |
| 68 | |
| 69 | /* Names of target-specific variable attributes. */ |
| 70 | char *g_attr_name[]={"__interrupt","__dpp0","__dpp1","__dpp2","__dpp3",0}; |
| 71 | #define INTERRUPT 1 |
| 72 | #define DPP0 2 |
| 73 | #define DPP1 4 |
| 74 | #define DPP2 8 |
| 75 | #define DPP3 16 |
| 76 | |
| 77 | |
| 78 | /****************************************/ |
| 79 | /* Some private data and functions. */ |
| 80 | /****************************************/ |
| 81 | |
| 82 | static char *bregnames[]={"nobreg","RL0","RL1","RL2","RL3","RL4","RL5","RL6","RL7", |
| 83 | "e8","e9","e10","e11","e12","e13","e14","e15","e16", |
| 84 | "e17","e18","e19","e20","e21","e22" |
| 85 | }; |
| 86 | |
| 87 | static long malign[MAX_TYPE+1]= {1,1,1,2,2,2,2,2,2,2,1,2,2,2,1,1,1,2,1}; |
| 88 | static long msizetab[MAX_TYPE+1]={0,1,1,2,2,4,8,4,8,8,0,2,4,4,0,0,0,2,0}; |
| 89 | |
| 90 | struct Typ ityp={SHORT},ltyp={LONG}; |
| 91 | |
| 92 | #define INT32 (g_flags[1]&USEDFLAG) |
| 93 | #define TINY (g_flags[6]&USEDFLAG) |
| 94 | #define LARGE (g_flags[7]&USEDFLAG) |
| 95 | #define HUGE (g_flags[8]&USEDFLAG) |
| 96 | #define TASKING (g_flags[9]&USEDFLAG) |
| 97 | |
| 98 | |
| 99 | #define NDATA 0 |
| 100 | #define NBSS 1 |
| 101 | #define NCDATA 2 |
| 102 | #define FDATA 3 |
| 103 | #define FBSS 4 |
| 104 | #define FCDATA 5 |
| 105 | #define HDATA 6 |
| 106 | #define HBSS 7 |
| 107 | #define HCDATA 8 |
| 108 | #define CODE 9 |
| 109 | #define SPECIAL 10 |
| 110 | #define BITS 11 |
| 111 | |
| 112 | #define SEG r2 |
| 113 | #define SOF r1 |
| 114 | |
| 115 | #define MTMP1 MAXR+1 |
| 116 | #define MTMP2 MAXR+2 |
| 117 | #define ZEROS MAXR+3 |
| 118 | #define ONES MAXR+4 |
| 119 | |
| 120 | static int section=-1,newobj,scnt; |
| 121 | static char *codename="SCODE\tSECTION CODE WORD PUBLIC 'CPROGRAM'\n",*ecode="SCODE\tends\n", |
| 122 | *ndataname="NDATA\tSECTION LDAT WORD PUBLIC\n",*endata="NDATA\tends\n", |
| 123 | *fdataname="FDATA\tSECTION LDAT WORD PUBLIC\n",*efdata="FDATA\tends\n", |
| 124 | *hdataname="HDATA\tSECTION LDAT WORD PUBLIC\n",*ehdata="HDATA\tends\n", |
| 125 | *nbssname="NBSS\tSECTION LDAT WORD PUBLIC\n",*enbss="NBSS\tends\n", |
| 126 | *fbssname="FBSS\tSECTION LDAT WORD PUBLIC\n",*efbss="FBSS\tends\n", |
| 127 | *hbssname="HBSS\tSECTION LDAT WORD PUBLIC\n",*ehbss="HBSS\tends\n", |
| 128 | *ncdataname="NCDATA\tSECTION LDAT WORD PUBLIC\n",*encdata="NCDATA\tends\n", |
| 129 | *fcdataname="FCDATA\tSECTION LDAT WORD PUBLIC\n",*efcdata="FCDATA\tends\n", |
| 130 | *hcdataname="HCDATA\tSECTION LDAT WORD PUBLIC\n",*ehcdata="HCDATA\tends\n", |
| 131 | |
| 132 | *bitsname="BITS\tSECTION BIT BIT PUBLIC 'CBITS'\n",*ebits="BITS\tends\n"; |
| 133 | |
| 134 | static char *even="\teven\n",*public="public",*comment="; "; |
| 135 | |
| 136 | #define IMM_IND 1 |
| 137 | #define VAR_IND 2 |
| 138 | #define POST_INC 3 |
| 139 | #define PRE_DEC 4 |
| 140 | |
| 141 | static char sec_end[32]; |
| 142 | /* (user)stack-pointer, pointer-tmp, int-tmp; reserved for compiler */ |
| 143 | static const int sp=1,tp=2,ti=11,ti2=12,r4=5,r5=6,r4r5=20; |
| 144 | static int tmp1,tmp2,tmp3,tmp4; |
| 145 | static void pr(FILE *,struct IC *); |
| 146 | static void function_top(FILE *,struct Var *,long); |
| 147 | static void function_bottom(FILE *f,struct Var *,long); |
| 148 | static int scratchreg(int,struct IC *); |
| 149 | static struct Var nvar,fvar; |
| 150 | |
| 151 | static char *marray[]={0, |
| 152 | "__far=__attr(\"far\")", |
| 153 | "__near=__attr(\"near\")", |
| 154 | "__huge=__attr(\"huge\")", |
| 155 | "__bit=__attr(\"bit\") unsigned char", |
| 156 | /*"__section(x)=__vattr(\"section(\"x\")\")",*/ |
| 157 | "__section(x,y)=__vattr(\"section(\"#x\",\"#y\")\")", |
| 158 | "__rbank(x)=__vattr(\"rbank(\"__str(x)\")\")", |
| 159 | "__interrupt(x)=__interrupt __vattr(\"ivec(\"__str(x)\")\")", |
| 160 | "__sysstack(x)=__stack2(x)", |
| 161 | "__usrstack(x)=__stack(x)", |
| 162 | "__C16X__", |
| 163 | "__C167__", |
| 164 | "__ST10__", |
| 165 | "__sfr(x)=__vattr(\"sfr(\"__str(x)\")\") extern", |
| 166 | "__sfrbit(x,y)=__vattr(\"sfrbit(\"__str(x)\",\"__str(y)\")\") extern", |
| 167 | "__esfr(x)=__vattr(\"sfre(\"__str(x)\")\") extern", |
| 168 | "__esfrbit(x,y)=__vattr(\"sfrbite(\"__str(x)\",\"__str(y)\")\") extern", |
| 169 | "__SIZE_T_INT=1", |
| 170 | "__str(x)=#x", |
| 171 | 0}; |
| 172 | |
| 173 | #define isreg(x) ((p->x.flags&(REG|DREFOBJ))==REG) |
| 174 | #define isconst(x) ((p->x.flags&(KONST|DREFOBJ))==KONST) |
| 175 | #define issfrv(v) (v->vattr&&strstr(v->vattr,"sfr(")) |
| 176 | #define issfrbitv(v) (v->vattr&&strstr(v->vattr,"sfrbit(")) |
| 177 | #define isesfrv(v) (v->vattr&&strstr(v->vattr,"sfre(")) |
| 178 | #define isesfrbitv(v) (v->vattr&&strstr(v->vattr,"sfrbite(")) |
| 179 | #define issfr(x) ((p->x.flags&(VAR|DREFOBJ))==VAR&&issfrv(p->x.v)) |
| 180 | #define issfrbit(x) ((p->x.flags&(VAR|DREFOBJ))==VAR&&issfrbitv(p->x.v)) |
| 181 | #define isesfr(x) ((p->x.flags&(VAR|DREFOBJ))==VAR&&isesfrv(p->x.v)) |
| 182 | #define isesfrbit(x) ((p->x.flags&(VAR|DREFOBJ))==VAR&&isesfrbitv(p->x.v)) |
| 183 | |
| 184 | static long loff,sysstackoffset,usrstackoffset,notpopped,dontpop, |
| 185 | usrmaxpushed,sysmaxpushed,sysstack,usrstack; |
| 186 | |
| 187 | static char *x_t[]={"?","","b","","","","","","","","","","","","","",""}; |
| 188 | static char *ccs[]={"z","nz","lt","ge","le","gt"}; |
| 189 | static char *logicals[]={"or","xor","and"}; |
| 190 | static char *arithmetics[]={"shl","shr","add","sub","mul","div","mod"}; |
| 191 | static char *dct[]={"","dbit","db","dw","dw","dw","dw","dw","dw","dw", |
| 192 | "(void)","dw","dsptr","dsptr"}; |
| 193 | static char *vdct[]={"",".bit",".byte",".short",".short",".short",".short",".short",".short",".short", |
| 194 | "(void)",".short",".long",".long"}; |
| 195 | static int pushedsize,pushorder=2; |
| 196 | static char *idprefix="_",*labprefix="l"; |
| 197 | static int ti2_used; |
| 198 | static struct rpair qp; |
| 199 | static int exit_label,have_frame,stackchecklabel,stack_valid; |
| 200 | static char *ret,*call,*jump; |
| 201 | static int frame_used; |
| 202 | |
| 203 | static char *dppprefix="___DPP_"; |
| 204 | static int romdpp=0,ramdpp=1; |
| 205 | |
| 206 | #define STR_NEAR "near" |
| 207 | #define STR_FAR "far" |
| 208 | #define STR_HUGE "huge" |
| 209 | #define STR_BADDR "baddr" |
| 210 | |
| 211 | #define ISNULL() (zmeqto(vmax,l2zm(0L))&&zumeqto(vumax,ul2zum(0UL))&&zldeqto(vldouble,d2zld(0.0))) |
| 212 | #define ISLWORD(t) ((t&NQ)==LONG||(t&NQ)==FPOINTER||(t&NQ)==HPOINTER||(t&NQ)==FLOAT) |
| 213 | #define ISHWORD(t) ((t&NQ)==INT||(t&NQ)==SHORT||(t&NQ)==NPOINTER) |
| 214 | #define ISSTATIC(v) ((v)->storage_class==EXTERN||(v)->storage_class==STATIC) |
| 215 | #define ISBADDR(v) ((v)->vtyp->attr&&strstr(STR_BADDR,(v)->vtyp->attr)) |
| 216 | |
| 217 | static int dppuse(struct Var *v,int section) |
| 218 | { |
| 219 | if(v->tattr&DPP0) return 0; |
| 220 | if(v->tattr&DPP1) return 1; |
| 221 | if(v->tattr&DPP2) return 2; |
| 222 | if(v->tattr&DPP3) return 3; |
| 223 | if(section==NCDATA) return romdpp; |
| 224 | if(section==NDATA||section==NBSS) return ramdpp; |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | static int ISFAR(struct Var *v) |
| 229 | { |
| 230 | struct Typ *vt; |
| 231 | if(v==&nvar) return 0; |
| 232 | if(v==&fvar) return 1; |
| 233 | if(issfrv(v)||isesfrv(v)||issfrbitv(v)||isesfrbitv(v)) |
| 234 | return 0; |
| 235 | vt=v->vtyp; |
| 236 | while(ISARRAY(vt->flags)) vt=vt->next; |
| 237 | if(vt->attr&&strstr(STR_NEAR,vt->attr)) |
| 238 | return 0; |
| 239 | if(HUGE||LARGE) |
| 240 | return 1; |
| 241 | if(vt->attr&&(strstr(STR_FAR,vt->attr)||strstr(STR_HUGE,vt->attr))) |
| 242 | return 1; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int special_section(FILE *f,struct Var *v) |
| 247 | { |
| 248 | char *sec,*e; |
| 249 | if(!v->vattr) return 0; |
| 250 | sec=strstr(v->vattr,"section("); |
| 251 | if(!sec) return 0; |
| 252 | sec+=strlen("section("); |
| 253 | if(f&&TASKING){ |
| 254 | emit(f,sec_end); |
| 255 | e=sec_end; |
| 256 | while(*sec&&*sec!=')') { |
| 257 | *e++=*sec; |
| 258 | emit_char(f,*sec++); |
| 259 | } |
| 260 | *e=0; |
| 261 | strcat(sec_end,"\tends\n"); |
| 262 | emit(f,"\tSECTION LDAT WORD PUBLIC\n"); |
| 263 | }else{ |
| 264 | emit(f,"\t.section\t"); |
| 265 | while(*sec&&*sec!=')') emit_char(f,*sec++); |
| 266 | emit(f,"\n"); |
| 267 | } |
| 268 | if(f) section=SPECIAL; |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | static struct fpconstlist { |
| 273 | struct fpconstlist *next; |
| 274 | int label,typ; |
| 275 | union atyps val; |
| 276 | } *firstfpc; |
| 277 | |
| 278 | static int addfpconst(struct obj *o,int t) |
| 279 | { |
| 280 | struct fpconstlist *p=firstfpc; |
| 281 | t&=NQ; |
| 282 | if(g_flags[4]&USEDFLAG){ |
| 283 | for(p=firstfpc;p;p=p->next){ |
| 284 | if(t==p->typ){ |
| 285 | eval_const(&p->val,t); |
| 286 | if(t==FLOAT&&zldeqto(vldouble,zf2zld(o->val.vfloat))) return p->label; |
| 287 | if(t==DOUBLE&&zldeqto(vldouble,zd2zld(o->val.vdouble))) return p->label; |
| 288 | if(t==LDOUBLE&&zldeqto(vldouble,o->val.vldouble)) return p->label; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | p=mymalloc(sizeof(struct fpconstlist)); |
| 293 | p->next=firstfpc; |
| 294 | p->label=++label; |
| 295 | p->typ=t; |
| 296 | p->val=o->val; |
| 297 | firstfpc=p; |
| 298 | return p->label; |
| 299 | } |
| 300 | static void callee_push(long usr,long sys) |
| 301 | { |
| 302 | if(usr-usrstackoffset>usrstack) |
| 303 | usrstack=usr-usrstackoffset; |
| 304 | if(sys-sysstackoffset>sysstack) |
| 305 | sysstack=sys-sysstackoffset; |
| 306 | } |
| 307 | static void push(long usr,long sys) |
| 308 | { |
| 309 | usrstackoffset-=usr; |
| 310 | if(usrstackoffset<usrmaxpushed) |
| 311 | usrmaxpushed=usrstackoffset; |
| 312 | if(-usrmaxpushed>usrstack) usrstack=-usrmaxpushed; |
| 313 | sysstackoffset-=sys; |
| 314 | if(sysstackoffset<sysmaxpushed) |
| 315 | sysmaxpushed=sysstackoffset; |
| 316 | if(-sysmaxpushed>sysstack) sysstack=-sysmaxpushed; |
| 317 | } |
| 318 | static void pop(long usr,long sys) |
| 319 | { |
| 320 | usrstackoffset+=usr; |
| 321 | sysstackoffset+=sys; |
| 322 | } |
| 323 | int pointer_type(struct Typ *p) |
| 324 | { |
| 325 | struct Typ *merk=p; |
| 326 | if(!p) ierror(0); |
| 327 | while(ISARRAY(p->flags)||ISFUNC(p->flags)) p=p->next; |
| 328 | if(p->attr){ |
| 329 | if(strstr(p->attr,STR_HUGE)) return HPOINTER; |
| 330 | if(strstr(p->attr,STR_FAR)) return FPOINTER; |
| 331 | if(strstr(p->attr,STR_NEAR)) return NPOINTER; |
| 332 | } |
| 333 | if((merk->flags&NQ)==FUNKT){ |
| 334 | if(TINY) |
| 335 | return NPOINTER; |
| 336 | else |
| 337 | return HPOINTER; |
| 338 | } |
| 339 | if(LARGE) |
| 340 | return FPOINTER; |
| 341 | else if(HUGE) |
| 342 | return HPOINTER; |
| 343 | else |
| 344 | return NPOINTER; |
| 345 | } |
| 346 | static long voff(struct obj *p) |
| 347 | { |
| 348 | if(p->v->offset<0) |
| 349 | return loff-zm2l(p->v->offset)+zm2l(p->val.vmax)-usrstackoffset+pushedsize-2; |
| 350 | return zm2l(p->v->offset)+zm2l(p->val.vmax)-usrstackoffset; |
| 351 | } |
| 352 | static int alignment(struct obj *o) |
| 353 | { |
| 354 | if(o->flags&DREFOBJ) return 1; |
| 355 | if(!(o->flags&VAR)) ierror(0); |
| 356 | if(ISSTATIC(o->v)) return zm2l(o->val.vmax)&1; |
| 357 | return voff(o)&1; |
| 358 | } |
| 359 | |
| 360 | static int isseg; |
| 361 | |
| 362 | static void emit_obj(FILE *f,struct obj *p,int t) |
| 363 | /* Gibt Objekt auf Bildschirm aus */ |
| 364 | { |
| 365 | if(p->am){ |
| 366 | static struct rpair bp; |
| 367 | int base; |
| 368 | if(reg_pair(p->am->base,&bp)) |
| 369 | base=bp.SOF; |
| 370 | else |
| 371 | base=p->am->base; |
| 372 | if(p->am->flags==POST_INC){ |
| 373 | emit(f,"[%s+]",regnames[base]); |
| 374 | return; |
| 375 | }else if(p->am->flags==PRE_DEC){ |
| 376 | emit(f,"[-%s]",regnames[base]); |
| 377 | return; |
| 378 | }else{ |
| 379 | emit(f,"[%s+#%ld",regnames[base],p->am->offset); |
| 380 | if(p->am->v){ |
| 381 | if(p->am->v->storage_class==STATIC) |
| 382 | emit(f,"+%s%ld",labprefix,zm2l(p->am->v->offset)); |
| 383 | else |
| 384 | emit(f,"+%s%s",idprefix,p->am->v->identifier); |
| 385 | } |
| 386 | emit(f,"]"); |
| 387 | return; |
| 388 | } |
| 389 | } |
| 390 | if((p->flags&(KONST|DREFOBJ))==(KONST|DREFOBJ)){ |
| 391 | emitval(f,&p->val,p->dtyp&NU); |
| 392 | return; |
| 393 | } |
| 394 | if((p->flags&(DREFOBJ|REG))==(DREFOBJ|REG)) emit(f,"["); |
| 395 | if(p->flags&VARADR) emit(f,"#"); |
| 396 | if((p->flags&(VAR|REG))==VAR){ |
| 397 | if(p->v==&nvar||p->v==&fvar){ |
| 398 | if(p->v==&fvar) emit(f,"SOF "); |
| 399 | emitval(f,&p->val,p->dtyp&NU); |
| 400 | return; |
| 401 | }else if(issfrv(p->v)||isesfrv(p->v)||issfrbitv(p->v)||isesfrbitv(p->v)){ |
| 402 | emit(f,"%s",p->v->identifier); |
| 403 | }else if(p->v->storage_class==AUTO||p->v->storage_class==REGISTER){ |
| 404 | if(voff(p)) |
| 405 | emit(f,"[%s+#%ld]",regnames[sp],voff(p)); |
| 406 | else emit(f,"[%s]",regnames[sp]); |
| 407 | }else{ |
| 408 | if(!isseg&&!ISFUNC(p->v->vtyp->flags)){ |
| 409 | if(ISFAR(p->v)) |
| 410 | emit(f,"SOF "); |
| 411 | else |
| 412 | emit(f,"DPPX:"); |
| 413 | } |
| 414 | isseg=0; |
| 415 | if(!zmeqto(l2zm(0L),p->val.vmax)){emitval(f,&p->val,LONG);emit(f,"+");} |
| 416 | if(p->v->storage_class==STATIC){ |
| 417 | emit(f,"%s%ld",labprefix,zm2l(p->v->offset)); |
| 418 | }else{ |
| 419 | emit(f,"%s%s",idprefix,p->v->identifier); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | if(p->flags®){ |
| 424 | if(p->reg==MTMP1||p->reg==MTMP2){ |
| 425 | if(!reg_pair(p->reg,&rp)) ierror(0); |
| 426 | emit(f,"%s",regnames[rp.SOF]); |
| 427 | }else{ |
| 428 | emit(f,"%s",regnames[p->reg]); |
| 429 | if((t&NQ)==BIT) emit(f,".0"); |
| 430 | } |
| 431 | } |
| 432 | /* sometimes we just or a REG into a KONST */ |
| 433 | if((p->flags&(KONST|REG))==KONST){ |
| 434 | if(ISFLOAT(t)){ |
| 435 | emit(f,"%s%d",labprefix,addfpconst(p,t)); |
| 436 | }else{ |
| 437 | emit(f,"#");emitval(f,&p->val,t&NU); |
| 438 | } |
| 439 | } |
| 440 | if((p->flags&(DREFOBJ|REG))==(DREFOBJ|REG)) emit(f,"]"); |
| 441 | } |
| 442 | |
| 443 | static void pr(FILE *f,struct IC *p) |
| 444 | { |
| 445 | } |
| 446 | static int switched_bank; |
| 447 | static void function_top(FILE *f,struct Var *v,long offset) |
| 448 | /* erzeugt Funktionskopf */ |
| 449 | { |
| 450 | int i;char *tmp; |
| 451 | static int wrote_header; |
| 452 | if(f&&!wrote_header){ |
| 453 | wrote_header=1; |
| 454 | if(!TASKING){ |
| 455 | emit(f,"\t.sfr\tZEROS,0xfe,0x8e\n"); |
| 456 | emit(f,"\t.sfr\tONES,0xfe,0x8f\n"); |
| 457 | emit(f,"\t.sfr\tCP,0xfe,0x08\n"); |
| 458 | emit(f,"\t.sfr\tMDH,0xfe,0x06\n"); |
| 459 | emit(f,"\t.sfr\tMDL,0xfe,0x07\n"); |
| 460 | emit(f,"\t.sfr\tPSW,0xfe,0x88\n"); |
| 461 | } |
| 462 | } |
| 463 | have_frame=0;stack_valid=1; |
| 464 | pushedsize=0; |
| 465 | if(v->vattr&&(tmp=strstr(v->vattr,"ivec("))){ |
| 466 | int vec;char c;int rc; |
| 467 | rc=sscanf(tmp+5,"%i%c",&vec,&c); |
| 468 | if(rc!=2||c!=')') error(324,"illegal vector number"); |
| 469 | emit(f,"\t.section\t\".ivec%d\",\"axr\"\n",vec); |
| 470 | emit(f,"\t.global\tivec%d\nivec%d:\n",vec,vec); |
| 471 | if(v->storage_class==EXTERN) |
| 472 | emit(f,"\tjmps\t%s%s\n",idprefix,v->identifier); |
| 473 | else |
| 474 | emit(f,"\tjmps\t%s%ld\n",labprefix,zm2l(v->offset)); |
| 475 | section=-1; |
| 476 | } |
| 477 | if(!special_section(f,v)&§ion!=CODE){ |
| 478 | if(f&&TASKING){ |
| 479 | emit(f,sec_end);emit(f,codename); |
| 480 | section=CODE; |
| 481 | strcpy(sec_end,ecode); |
| 482 | }else |
| 483 | emit(f,codename); |
| 484 | } |
| 485 | if(TASKING){ |
| 486 | if(v->storage_class==EXTERN){ |
| 487 | if((v->flags&(INLINEFUNC|INLINEEXT))!=INLINEFUNC) |
| 488 | emit(f,"\tpublic\t%s%s\n",idprefix,v->identifier); |
| 489 | if(TINY) |
| 490 | emit(f,"%s%s\tproc\tnear\n",idprefix,v->identifier); |
| 491 | else |
| 492 | emit(f,"%s%s\tproc\tfar\n",idprefix,v->identifier); |
| 493 | }else{ |
| 494 | if(TINY) |
| 495 | emit(f,"%s%ld\tproc\tnear\n",labprefix,zm2l(v->offset)); |
| 496 | else |
| 497 | emit(f,"%s%ld\tproc\tfar\n",labprefix,zm2l(v->offset)); |
| 498 | } |
| 499 | }else{ |
| 500 | if(v->storage_class==EXTERN){ |
| 501 | if((v->flags&(INLINEFUNC|INLINEEXT))!=INLINEFUNC) |
| 502 | emit(f,"\t.global\t%s%s\n",idprefix,v->identifier); |
| 503 | emit(f,"\t.align\t1\n%s%s:\n",idprefix,v->identifier); |
| 504 | }else{ |
| 505 | emit(f,"\t.align\t1\n%s%ld:\n",labprefix,zm2l(v->offset)); |
| 506 | } |
| 507 | } |
| 508 | if(v->vattr&&(tmp=strstr(v->vattr,"rbank("))){ |
| 509 | char *p; |
| 510 | tmp+=strlen("rbank("); |
| 511 | emit(f,"\tmov\t%s",idprefix); |
| 512 | for(p=tmp;*p&&*p!=')';p++) emit_char(f,*p); |
| 513 | emit(f,",%s\n",regnames[sp]); |
| 514 | emit(f,"\tscxt\tCP,#%s",idprefix); |
| 515 | for(p=tmp;*p&&*p!=')';p++) emit_char(f,*p); |
| 516 | emit(f,"\n"); |
| 517 | switched_bank=1; |
| 518 | }else |
| 519 | switched_bank=0; |
| 520 | if(v->tattr&INTERRUPT){ |
| 521 | /*FIXME?*/ |
| 522 | emit(f,"\tpush\tMDL\n"); |
| 523 | emit(f,"\tpush\tMDH\n"); |
| 524 | } |
| 525 | if(stack_check){ |
| 526 | stackchecklabel=++label; |
| 527 | BSET(regs_modified,tp); |
| 528 | emit(f,"\tmov\t%s,#%s%d\n",regnames[tp],labprefix,stackchecklabel); |
| 529 | emit(f,"\t%s\t%s__stack_check\n",call,idprefix);/*FIXME:usrstack*/ |
| 530 | } |
| 531 | for(i=1;i<=16;i++){ |
| 532 | if(regused[i]&&!regscratch[i]&&!regsa[i]){ |
| 533 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[i]); |
| 534 | push(2,0); |
| 535 | have_frame=1;pushedsize+=2; |
| 536 | } |
| 537 | } |
| 538 | if(offset){ |
| 539 | emit(f,"\tsub\t%s,#%ld\n",regnames[sp],offset); |
| 540 | have_frame=1; |
| 541 | } |
| 542 | } |
| 543 | static void function_bottom(FILE *f,struct Var *v,long offset) |
| 544 | /* erzeugt Funktionsende */ |
| 545 | { |
| 546 | int i; |
| 547 | if(offset) emit(f,"\tadd\t%s,#%ld\n",regnames[sp],offset); |
| 548 | for(i=16;i>0;i--){ |
| 549 | if(regused[i]&&!regscratch[i]&&!regsa[i]){ |
| 550 | emit(f,"\tmov\t%s,[%s+]\n",regnames[i],regnames[sp]); |
| 551 | pop(2,0); |
| 552 | } |
| 553 | } |
| 554 | if(v->tattr&INTERRUPT){ |
| 555 | emit(f,"\tpop\tMDH\n"); |
| 556 | emit(f,"\tpop\tMDL\n"); |
| 557 | } |
| 558 | if(switched_bank) |
| 559 | emit(f,"\tpop\tCP\n"); |
| 560 | if(ret) emit(f,"\t%s\n",ret); |
| 561 | if(TASKING){ |
| 562 | if(v->storage_class==EXTERN){ |
| 563 | emit(f,"%s%s\tendp\n",idprefix,v->identifier); |
| 564 | if(!strcmp("main",v->identifier)) emit(f,"\textern\t__CSTART:far\n"); |
| 565 | }else |
| 566 | emit(f,"%s%ld\tendp\n",labprefix,zm2l(v->offset)); |
| 567 | }else{ |
| 568 | if(v->storage_class==EXTERN){ |
| 569 | emit(f,"\t.type\t%s%s,@function\n",idprefix,v->identifier); |
| 570 | emit(f,"\t.size\t%s%s,$-%s%s\n",idprefix,v->identifier,idprefix,v->identifier); |
| 571 | }else{ |
| 572 | emit(f,"\t.type\t%s%ld,@function\n",labprefix,zm2l(v->offset)); |
| 573 | emit(f,"\t.size\t%s%ld,$-%s%ld\n",labprefix,zm2l(v->offset),labprefix,zm2l(v->offset)); |
| 574 | } |
| 575 | } |
| 576 | if(stack_check) |
| 577 | emit(f,"%s%d\tequ\t%ld\n",labprefix,stackchecklabel,offset+pushedsize-usrmaxpushed); |
| 578 | if(stack_valid){ |
| 579 | long ustack=usrstack+offset+pushedsize,sstack=sysstack; |
| 580 | if(!v->fi) v->fi=new_fi(); |
| 581 | if(v->fi->flags&ALL_STACK){ |
| 582 | if(v->fi->stack1!=ustack) |
| 583 | if(f) error(319,"user-",ustack,v->fi->stack1); |
| 584 | if(v->fi->stack2!=sstack) |
| 585 | if(f) error(319,"system-",sstack,v->fi->stack2); |
| 586 | }else{ |
| 587 | v->fi->flags|=ALL_STACK; |
| 588 | v->fi->stack1=ustack; |
| 589 | v->fi->stack2=sstack; |
| 590 | } |
| 591 | #if 0 |
| 592 | emit(f,"%s usrstacksize=%ld\n",comment,zm2l(v->fi->stack1)); |
| 593 | emit(f,"%s sysstacksize=%ld\n",comment,zm2l(v->fi->stack2)); |
| 594 | #endif |
| 595 | if(TASKING){ |
| 596 | }else{ |
| 597 | emit(f,"\t.equ\t%s__ustack_%s,%ld\n",idprefix,v->identifier,zm2l(v->fi->stack1)); |
| 598 | emit(f,"\t.equ\t%s__sstack_%s,%ld\n",idprefix,v->identifier,zm2l(v->fi->stack2)); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | static int compare_objects(struct obj *o1,struct obj *o2) |
| 603 | { |
| 604 | if(o1->flags==o2->flags&&o1->am==o2->am){ |
| 605 | if(!(o1->flags&VAR)||(o1->v==o2->v&&zmeqto(o1->val.vmax,o2->val.vmax))){ |
| 606 | if(!(o1->flags®)||o1->reg==o2->reg){ |
| 607 | return 1; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | #define EXT_IC_CMPIA 1 |
| 614 | #define EXT_IC_BTST 2 |
| 615 | #define EXT_IC_CMPIB 3 |
| 616 | static void clear_ext_ic(struct ext_ic *p) |
| 617 | { |
| 618 | p->flags=0; |
| 619 | p->r=0; |
| 620 | p->offset=0; |
| 621 | } |
| 622 | static long pof2(zumax x) |
| 623 | /* Yields log2(x)+1 oder 0. */ |
| 624 | { |
| 625 | zumax p;int ln=1; |
| 626 | p=ul2zum(1L); |
| 627 | while(ln<=32&&zumleq(p,x)){ |
| 628 | if(zumeqto(x,p)) return ln; |
| 629 | ln++;p=zumadd(p,p); |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | static void peephole(struct IC *p) |
| 634 | { |
| 635 | int c,c2,r,t;struct IC *p2; |
| 636 | struct AddressingMode *am; |
| 637 | frame_used=0; |
| 638 | for(;p;p=p->next){ |
| 639 | c=p->code; |
| 640 | if(!frame_used){ |
| 641 | if((p->q1.flags&(REG|VAR))==VAR&&!ISSTATIC(p->q1.v)) frame_used=1; |
| 642 | if((p->q2.flags&(REG|VAR))==VAR&&!ISSTATIC(p->q2.v)) frame_used=1; |
| 643 | if((p->z.flags&(REG|VAR))==VAR&&!ISSTATIC(p->z.v)) frame_used=1; |
| 644 | } |
| 645 | /* letztes Label merken */ |
| 646 | if(c!=FREEREG&&c!=ALLOCREG&&(c!=SETRETURN||!isreg(q1)||p->q1.reg!=p->z.reg)) exit_label=0; |
| 647 | if(c==LABEL) exit_label=p->typf; |
| 648 | /* and x,#const;bne/beq */ |
| 649 | if(c==AND&&isconst(q2)&&isreg(z)&&!ISLWORD(ztyp(p))){ |
| 650 | long bit; |
| 651 | eval_const(&p->q2.val,p->typf); |
| 652 | if(bit=pof2(vumax)){ |
| 653 | struct IC *cmp=0;int fr=0; |
| 654 | for(p2=p->next;p2;p2=p2->next){ |
| 655 | c2=p2->code; |
| 656 | if(c2==TEST){ |
| 657 | if((p2->q1.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==p->z.reg){ |
| 658 | cmp=p2;continue; |
| 659 | } |
| 660 | } |
| 661 | if(c2==COMPARE&&(p2->q1.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==p->z.reg&&(p2->q2.flags&(KONST|DREFOBJ))==KONST){ |
| 662 | eval_const(&p2->q2.val,p2->typf); |
| 663 | if(ISNULL()){ |
| 664 | cmp=p2;continue; |
| 665 | } |
| 666 | break; |
| 667 | } |
| 668 | if(c2==FREEREG&&p2->q1.reg==p->z.reg) {fr++;continue;} |
| 669 | if((c2==BNE||c2==BEQ)&&cmp&&fr==1){ |
| 670 | p->ext.flags=EXT_IC_BTST; |
| 671 | p2->ext.flags=EXT_IC_BTST; |
| 672 | p2->ext.offset=bit-1; |
| 673 | cmp->code=NOP; |
| 674 | cmp->q1.flags=cmp->q2.flags=cmp->z.flags=0; |
| 675 | break; |
| 676 | } |
| 677 | if(((p2->q1.flags®)&&p2->q1.reg==p->z.reg)||((p2->q2.flags®)&&p2->q2.reg==p->z.reg)||((p2->z.flags®)&&p2->z.reg==p->z.reg)) break; |
| 678 | if(c2==CALL||c2==LABEL||(c2>=BEQ&&c2<=BRA)) break; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | /* [Rx+] in q1 */ |
| 683 | if(!p->q1.am&&(p->q1.flags&(REG|DREFOBJ))==(REG|DREFOBJ)){ |
| 684 | r=p->q1.reg; t=q1typ(p); |
| 685 | if((!(p->q2.flags®)||p->q2.reg!=r)&&(!(p->z.flags®)||p->z.reg!=r)){ |
| 686 | for(p2=p->next;p2;p2=p2->next){ |
| 687 | c2=p2->code; |
| 688 | if((c2==ADD||(c2==ADDI2P&&(p2->typf2&NQ)!=HPOINTER))&&(p2->q1.flags&(REG|DREFOBJ))==REG&&(p2->z.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==r&&p2->z.reg==r&&(p2->q2.flags&(KONST|DREFOBJ))==KONST){ |
| 689 | eval_const(&p2->q2.val,p2->typf2); |
| 690 | if((zmeqto(vmax,l2zm(1L))&&(t&NQ)==CHAR)||(zmeqto(vmax,l2zm(2L))&&ISHWORD(t))){ |
| 691 | p2->code=NOP; |
| 692 | p2->q1.flags=p2->q2.flags=p2->z.flags=0; |
| 693 | p->q1.am=mymalloc(sizeof(*am)); |
| 694 | p->q1.am->flags=POST_INC; |
| 695 | p->q1.am->base=r; |
| 696 | }else break; |
| 697 | } |
| 698 | if(c2==CALL||c2==LABEL||(c2>=BEQ&&c2<=BRA)) break; |
| 699 | if(((p2->q1.flags®)&&p2->q1.reg==r)||((p2->q2.flags®)&&p2->q2.reg==r)||((p2->z.flags®)&&p2->z.reg==r)) break; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | /* [Rx+] in q2 */ |
| 704 | if(!p->q2.am&&(p->q2.flags&(REG|DREFOBJ))==(REG|DREFOBJ)){ |
| 705 | r=p->q2.reg; t=q2typ(p); |
| 706 | if((!(p->q1.flags®)||p->q1.reg!=r)&&(!(p->z.flags®)||p->z.reg!=r)){ |
| 707 | for(p2=p->next;p2;p2=p2->next){ |
| 708 | c2=p2->code; |
| 709 | if((c2==ADD||(c2==ADDI2P&&(p2->typf2&NQ)!=HPOINTER))&&(p2->q1.flags&(REG|DREFOBJ))==REG&&(p2->z.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==r&&p2->z.reg==r&&(p2->q2.flags&(KONST|DREFOBJ))==KONST){ |
| 710 | eval_const(&p2->q2.val,p2->typf2); |
| 711 | if((zmeqto(vmax,l2zm(1L))&&(t&NQ)==CHAR)||(zmeqto(vmax,l2zm(2L))&&ISHWORD(t))){ |
| 712 | p2->code=NOP; |
| 713 | p2->q1.flags=p2->q2.flags=p2->z.flags=0; |
| 714 | p->q2.am=mymalloc(sizeof(*am)); |
| 715 | p->q2.am->flags=POST_INC; |
| 716 | p->q2.am->base=r; |
| 717 | }else break; |
| 718 | } |
| 719 | if(c2==CALL||c2==LABEL||(c2>=BEQ&&c2<=BRA)) break; |
| 720 | if(((p2->q1.flags®)&&p2->q1.reg==r)||((p2->q2.flags®)&&p2->q2.reg==r)||((p2->z.flags®)&&p2->z.reg==r)) break; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | /* move x->t; add/sub x,#1/-1/2/-2-> x; test/cmp t,#0; freereg t => cmpid12 */ |
| 725 | if(c==ASSIGN&&isreg(q1)&&isreg(z)&&ISHWORD(p->typf)){ |
| 726 | p2=p->next;if(p2) c2=p2->code; |
| 727 | if(p2&&(c2==ADD||c2==SUB||c2==ADDI2P||c2==SUBIFP)&&(p2->q1.flags&(REG|DREFOBJ))==REG&&(p2->z.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==p->q1.reg&&p2->z.reg==p2->q1.reg&&(p2->q2.flags&(KONST|DREFOBJ))==KONST&&ISHWORD(p2->typf)){ |
| 728 | long l; |
| 729 | eval_const(&p2->q2.val,p2->typf); |
| 730 | l=zm2l(vmax); |
| 731 | if((l==1||l==2||l==-1||l==-2)&&p2->next){ |
| 732 | struct IC *p3=p2->next; |
| 733 | if((p3->code==TEST||(p3->code==COMPARE&&(p3->q2.flags&(KONST|DREFOBJ))==KONST))&&(p3->q1.flags&(REG|DREFOBJ))==REG&&p3->q1.reg==p->z.reg&&scratchreg(p->z.reg,p3)&&ISHWORD(p3->typf)){ |
| 734 | if(c2==SUB||c2==SUBIFP) l=-l; |
| 735 | p3->q1=p->q1; |
| 736 | p->code=c=NOP; |
| 737 | p->q1.flags=p->q2.flags=p->z.flags=0; |
| 738 | p2->code=NOP; |
| 739 | p2->q1.flags=p2->q2.flags=p2->z.flags=0; |
| 740 | p3->ext.flags=EXT_IC_CMPIB; |
| 741 | p3->ext.offset=l; |
| 742 | p3->ext.r=p->q1.reg; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | /* add/sub x,#1/2/-1/-2 ->x; cmp x,#c => cmpid12 x,#c+-12 */ |
| 748 | if((c==ADD||c==SUB||c==ADDI2P||c==SUBIFP)&&isreg(q1)&&isreg(z)&&p->q1.reg==p->z.reg&&isconst(q2)&&ISHWORD(p->typf)&&!reg_pair(p->q1.reg,&rp)){ |
| 749 | eval_const(&p->q2.val,p->typf); |
| 750 | if(zmeqto(vmax,l2zm(-1L))||zmeqto(vmax,l2zm(-2L))||zmeqto(vmax,l2zm(1L))||zmeqto(vmax,l2zm(2L))){ |
| 751 | long l=zm2l(vmax); |
| 752 | if(c==SUB||c==SUBIFP) l=-l; |
| 753 | r=p->q1.reg; |
| 754 | for(p2=p->next;p2;p2=p2->next){ |
| 755 | c2=p2->code; |
| 756 | if((p2->q1.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==r&&(c2==TEST||(c2==COMPARE&&(p2->q2.flags&(KONST|DREFOBJ))==KONST))&&ISHWORD(p2->typf)){ |
| 757 | unsigned long ul; |
| 758 | eval_const(&p2->q2.val,q2typ(p2)); |
| 759 | ul=zum2ul(vumax); |
| 760 | if(ul<65534){ |
| 761 | p2->ext.flags=EXT_IC_CMPIA; |
| 762 | p2->ext.offset=l; |
| 763 | p2->ext.r=r; |
| 764 | p->code=c=NOP; |
| 765 | p->q1.flags=p->q2.flags=p->z.flags=0; |
| 766 | } |
| 767 | break; |
| 768 | |
| 769 | } |
| 770 | if(c2==CALL||c2==LABEL||(c2>=BEQ&&c2<=BRA)) break; |
| 771 | if((p2->q1.flags®)&&p2->q1.reg==r) break; |
| 772 | if((p2->q2.flags®)&&p2->q2.reg==r) break; |
| 773 | if((p2->z.flags®)&&p2->z.reg==r) break; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | /* [Rx+#const] */ |
| 778 | if((c==ADDI2P||c==SUBIFP)&&((p->typf2&NQ)==NPOINTER||(p->typf2&NQ)==FPOINTER)&&isreg(z)&&((p->q2.flags&(KONST|DREFOBJ))==KONST||(p->q1.flags&VARADR))){ |
| 779 | int base;zmax of;struct obj *o;struct Var *v; |
| 780 | if(p->q1.flags&VARADR){ |
| 781 | v=p->q1.v; |
| 782 | of=p->q1.val.vmax; |
| 783 | r=p->z.reg; |
| 784 | if(isreg(q2)) base=p->q2.reg; else base=r; |
| 785 | }else{ |
| 786 | eval_const(&p->q2.val,p->typf); |
| 787 | if(c==SUBIFP) of=zmsub(l2zm(0L),vmax); else of=vmax; |
| 788 | v=0; |
| 789 | r=p->z.reg; |
| 790 | if(isreg(q1)) base=p->q1.reg; else base=r; |
| 791 | } |
| 792 | o=0; |
| 793 | for(p2=p->next;p2;p2=p2->next){ |
| 794 | c2=p2->code; |
| 795 | if(c2==CALL||c2==LABEL||(c2>=BEQ&&c2<=BRA)) break; |
| 796 | if(c2!=FREEREG&&(p2->q1.flags&(REG|DREFOBJ))==REG&&p2->q1.reg==r) break; |
| 797 | if(c2!=FREEREG&&(p2->q2.flags&(REG|DREFOBJ))==REG&&p2->q2.reg==r) break; |
| 798 | if(c2!=CALL&&(c2<LABEL||c2>BRA)/*&&c2!=ADDRESS*/){ |
| 799 | if(!p2->q1.am&&(p2->q1.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&p2->q1.reg==r){ |
| 800 | if(o) break; |
| 801 | o=&p2->q1; |
| 802 | } |
| 803 | if(!p2->q2.am&&(p2->q2.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&p2->q2.reg==r){ |
| 804 | if(o) break; |
| 805 | o=&p2->q2; |
| 806 | } |
| 807 | if(!p2->z.am&&(p2->z.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&p2->z.reg==r){ |
| 808 | if(o) break; |
| 809 | o=&p2->z; |
| 810 | } |
| 811 | } |
| 812 | if(c2==FREEREG||(p2->z.flags&(REG|DREFOBJ))==REG){ |
| 813 | int m; |
| 814 | if(c2==FREEREG) |
| 815 | m=p2->q1.reg; |
| 816 | else |
| 817 | m=p2->z.reg; |
| 818 | if(m==r){ |
| 819 | if(o){ |
| 820 | o->am=am=mymalloc(sizeof(*am)); |
| 821 | am->flags=IMM_IND; |
| 822 | am->base=base; |
| 823 | am->offset=zm2l(of); |
| 824 | am->v=v; |
| 825 | if(!v){ |
| 826 | if(isreg(q1)){ |
| 827 | p->code=c=NOP;p->q1.flags=p->q2.flags=p->z.flags=0; |
| 828 | }else{ |
| 829 | p->code=c=ASSIGN;p->q2.flags=0; |
| 830 | p->typf=p->typf2;p->q2.val.vmax=sizetab[p->typf2&NQ]; |
| 831 | } |
| 832 | }else{ |
| 833 | if(isreg(q2)){ |
| 834 | p->code=c=NOP;p->q1.flags=p->q2.flags=p->z.flags=0; |
| 835 | }else{ |
| 836 | p->code=c=ASSIGN;p->q1=p->q2;p->q2.flags=0; |
| 837 | p->q2.val.vmax=sizetab[p->typf&NQ]; |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | break; |
| 842 | } |
| 843 | if(/*get_reg!! c2!=FREEREG&&*/m==base) break; |
| 844 | continue; |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | static struct obj *cam(int flags,int base,long offset,struct Var *v) |
| 852 | /* Initializes an addressing-mode structure and returns a pointer to */ |
| 853 | /* that object. Will not survive a second call! */ |
| 854 | { |
| 855 | static struct obj obj; |
| 856 | static struct AddressingMode am; |
| 857 | obj.am=&am; |
| 858 | am.flags=flags; |
| 859 | am.base=base; |
| 860 | am.offset=offset; |
| 861 | am.v=v; |
| 862 | return &obj; |
| 863 | } |
| 864 | /* prints label types used by tasking assembler */ |
| 865 | static void emit_label_type(FILE *f,struct Var *v) |
| 866 | { |
| 867 | if(TASKING){ |
| 868 | if(ISFUNC(v->vtyp->flags)){ |
| 869 | if(TINY) |
| 870 | emit(f,"near"); |
| 871 | else |
| 872 | emit(f,"far"); |
| 873 | }else{ |
| 874 | if((v->vtyp->flags&NQ)==BIT) |
| 875 | emit(f,"bit"); |
| 876 | else if((v->vtyp->flags&NQ)==CHAR) |
| 877 | emit(f,"byte"); |
| 878 | else |
| 879 | emit(f,"word"); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | static void move(FILE *f,struct obj *q,int qr,struct obj *z,int zr,int t) |
| 884 | /* Generates code to move object q (or register qr) into object z (or */ |
| 885 | /* register zr). One must be a register and DREFOBJ only allowed with */ |
| 886 | /* registers. */ |
| 887 | { |
| 888 | long l=0; |
| 889 | t&=NQ; |
| 890 | if(q&&(q->flags&(REG|DREFOBJ))==REG) qr=q->reg; |
| 891 | if(z&&(z->flags&(REG|DREFOBJ))==REG) zr=z->reg; |
| 892 | if(qr&&qr==zr) return; |
| 893 | if((t&NQ)==BIT){ |
| 894 | if(!zr&&((z->flags&(VAR|REG|DREFOBJ|VARADR))!=VAR||!ISSTATIC(z->v))) ierror(0); |
| 895 | if(q&&(q->flags&(KONST|DREFOBJ))==KONST){ |
| 896 | if(z&&(z->flags&(VAR|DREFOBJ))==VAR&&isesfrbitv(z->v)) |
| 897 | emit(f,"\textr\t#1\n"); |
| 898 | eval_const(&q->val,BIT); |
| 899 | if(zmeqto(vmax,l2zm(0L))) |
| 900 | emit(f,"\tbclr\t"); |
| 901 | else |
| 902 | emit(f,"\tbset\t"); |
| 903 | if(zr) emit(f,"%s.0",regnames[zr]); else emit_obj(f,z,t); |
| 904 | emit(f,"\n"); |
| 905 | }else{ |
| 906 | if(!qr&&((q->flags&(VAR|REG|DREFOBJ|VARADR))!=VAR||!ISSTATIC(q->v))) ierror(0); |
| 907 | if((q->flags&(VAR|DREFOBJ))==VAR&&isesfrbitv(q->v)){ |
| 908 | if(!zr&&((z->flags&(VAR|DREFOBJ))!=VAR||!isesfrbitv(z->v))){ |
| 909 | emit(f,"\textr\t#1\n"); |
| 910 | emit(f,"\tbmov\t%s.0,",regnames[ti]); |
| 911 | if(qr) emit(f,"%s.0",regnames[qr]); else emit_obj(f,q,t); |
| 912 | emit(f,"\n"); |
| 913 | emit(f,"\tbmov\t"); |
| 914 | if(zr) emit(f,"%s.0",regnames[zr]); else emit_obj(f,z,t); |
| 915 | emit(f,",%s.0\n",regnames[ti]); |
| 916 | return; |
| 917 | }else |
| 918 | emit(f,"\textr\t#1\n"); |
| 919 | }else if(z&&(z->flags&(VAR|DREFOBJ))==VAR&&isesfrbitv(z->v)){ |
| 920 | if(!qr){ |
| 921 | emit(f,"\tbmov\t%s.0,",regnames[ti]); |
| 922 | if(qr) emit(f,"%s.0",regnames[qr]); else emit_obj(f,q,t); |
| 923 | emit(f,"\n"); |
| 924 | emit(f,"\textr\t#1\n"); |
| 925 | emit(f,"\tbmov\t");emit_obj(f,z,t); |
| 926 | emit(f,",%s.0\n",regnames[ti]); |
| 927 | return; |
| 928 | }else |
| 929 | emit(f,"\textr\t#1\n"); |
| 930 | } |
| 931 | emit(f,"\tbmov\t"); |
| 932 | if(zr) emit(f,"%s.0",regnames[zr]); else emit_obj(f,z,t); |
| 933 | emit(f,","); |
| 934 | if(qr) emit(f,"%s.0",regnames[qr]); else emit_obj(f,q,t); |
| 935 | emit(f,"\n"); |
| 936 | } |
| 937 | return; |
| 938 | } |
| 939 | if(q){ |
| 940 | if(!zr) ierror(0); |
| 941 | if(q->am&®_pair(q->am->base,&rp)){ |
| 942 | l=rp.SOF; |
| 943 | if(zr>16) |
| 944 | emit(f,"\texts\t%s,#2\n",regnames[rp.SEG]); |
| 945 | else |
| 946 | emit(f,"\texts\t%s,#1\n",regnames[rp.SEG]); |
| 947 | }else if((q->flags&DREFOBJ)&®_pair(q->reg,&rp)){ |
| 948 | l=rp.SOF; |
| 949 | if(zr>16) |
| 950 | emit(f,"\texts\t%s,#2\n",regnames[rp.SEG]); |
| 951 | else |
| 952 | emit(f,"\texts\t%s,#1\n",regnames[rp.SEG]); |
| 953 | }else if((q->flags&(VAR|REG|VARADR))==VAR&&ISSTATIC(q->v)&&ISFAR(q->v)){ |
| 954 | emit(f,"\texts\t#SEG "); |
| 955 | isseg=1;emit_obj(f,q,INT); |
| 956 | if(zr>16) |
| 957 | emit(f,",#2\n"); |
| 958 | else |
| 959 | emit(f,",#1\n"); |
| 960 | } |
| 961 | if(reg_pair(zr,&rp)){ |
| 962 | if(q->am){ |
| 963 | emit(f,"\tmov\t%s,",regnames[rp.r1]); |
| 964 | emit_obj(f,q,t);emit(f,"\n"); |
| 965 | q->am->offset+=2; |
| 966 | emit(f,"\tmov\t%s,",regnames[rp.r2]); |
| 967 | emit_obj(f,q,t);emit(f,"\n"); |
| 968 | q->am->offset-=2; |
| 969 | return; |
| 970 | }else if(q->flags&DREFOBJ){ |
| 971 | int tmp=0; |
| 972 | if(!(q->flags®)) ierror(0); |
| 973 | if(!l) l=q->reg; |
| 974 | if(l==rp.r1){ |
| 975 | tmp=rp.r1; |
| 976 | /*FIXME: tp hier immer frei? */ |
| 977 | BSET(regs_modified,tp); |
| 978 | rp.r1=tp; |
| 979 | } |
| 980 | /*FIXME: test auf scratchreg*/ |
| 981 | if(q->reg>MAXR){ |
| 982 | emit(f,"\tmov\t%s,[%s+]\n",regnames[rp.r1],regnames[l]); |
| 983 | emit(f,"\tmov\t%s,[%s]\n",regnames[rp.r2],regnames[l]); |
| 984 | }else{ |
| 985 | emit(f,"\tmov\t%s,[%s]\n",regnames[rp.r1],regnames[l]); |
| 986 | emit(f,"\tmov\t%s,[%s+#2]\n",regnames[rp.r2],regnames[l]); |
| 987 | } |
| 988 | if(tmp) emit(f,"\tmov\t%s,%s\n",regnames[tmp],regnames[rp.r1]); |
| 989 | return; |
| 990 | }else if(q->flags&VARADR){ |
| 991 | q->flags&=~VARADR; |
| 992 | emit(f,"\tmov\t%s,#",regnames[rp.r1]);emit_obj(f,q,t);emit(f,"\n"); |
| 993 | emit(f,"\tmov\t%s,#SEG ",regnames[rp.r2]);isseg=1;emit_obj(f,q,t);emit(f,"\n"); |
| 994 | q->flags|=VARADR; |
| 995 | return; |
| 996 | }else if((q->flags&(KONST|DREFOBJ))==KONST){ |
| 997 | long l2; |
| 998 | if(ISFLOAT(t)) ierror(0); |
| 999 | eval_const(&q->val,t); |
| 1000 | l=zm2zl(zmand(vmax,l2zm(65535L))); |
| 1001 | emit(f,"\tmov\t%s,#%ld\n",regnames[rp.r1],l); |
| 1002 | l2=zm2l(zmrshift(vmax,l2zm(16L))); |
| 1003 | if(l2==l) emit(f,"\tmov\t%s,%s\n",regnames[rp.r2],regnames[rp.r1]); |
| 1004 | else emit(f,"\tmov\t%s,#%ld\n",regnames[rp.r2],l2); |
| 1005 | return; |
| 1006 | }else if(qr){ |
| 1007 | if(!reg_pair(qr,&qp)) ierror(0); |
| 1008 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r1],regnames[qp.r1]); |
| 1009 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r2],regnames[qp.r2]); |
| 1010 | return; |
| 1011 | }else{ |
| 1012 | if(!(q->flags&VAR)) ierror(0); |
| 1013 | if(q->v->storage_class==AUTO||q->v->storage_class==REGISTER){ |
| 1014 | l=voff(q); |
| 1015 | if(l) |
| 1016 | emit(f,"\tmov\t%s,[%s+#%ld]\n",regnames[rp.r1],regnames[sp],l); |
| 1017 | else |
| 1018 | emit(f,"\tmov\t%s,[%s]\n",regnames[rp.r1],regnames[sp]); |
| 1019 | emit(f,"\tmov\t%s,[%s+#%ld]\n",regnames[rp.r2],regnames[sp],l+2); |
| 1020 | return; |
| 1021 | }else{ |
| 1022 | emit(f,"\tmov\t%s,",regnames[rp.r1]);emit_obj(f,q,t);emit(f,"\n"); |
| 1023 | emit(f,"\tmov\t%s,",regnames[rp.r2]);emit_obj(f,q,t);emit(f,"+2\n"); |
| 1024 | return; |
| 1025 | } |
| 1026 | } |
| 1027 | ierror(0); |
| 1028 | }else{ |
| 1029 | emit(f,"\tmov%s\t%s,",x_t[t&NQ],(t==CHAR?bregnames[zr]:regnames[zr])); |
| 1030 | if(qr&&t==CHAR) |
| 1031 | emit(f,"%s",bregnames[qr]); |
| 1032 | else if(!q->am&&(q->flags&(REG|DREFOBJ))==(REG|DREFOBJ)&®_pair(q->reg,&rp)) |
| 1033 | emit(f,"[%s]",regnames[rp.SOF]); |
| 1034 | else |
| 1035 | emit_obj(f,q,t); |
| 1036 | emit(f,"\n"); |
| 1037 | return; |
| 1038 | } |
| 1039 | } |
| 1040 | if(z){ |
| 1041 | if(!qr) ierror(0); |
| 1042 | if(z->am&®_pair(z->am->base,&rp)){ |
| 1043 | l=rp.SOF; |
| 1044 | if(qr>16) |
| 1045 | emit(f,"\texts\t%s,#2\n",regnames[rp.SEG]); |
| 1046 | else |
| 1047 | emit(f,"\texts\t%s,#1\n",regnames[rp.SEG]); |
| 1048 | }else if((z->flags&DREFOBJ)&®_pair(z->reg,&rp)){ |
| 1049 | l=rp.SOF; |
| 1050 | if(qr>16) |
| 1051 | emit(f,"\texts\t%s,#2\n",regnames[rp.SEG]); |
| 1052 | else |
| 1053 | emit(f,"\texts\t%s,#1\n",regnames[rp.SEG]); |
| 1054 | }else if((z->flags&(VAR|REG|VARADR))==VAR&&ISSTATIC(z->v)&&ISFAR(z->v)){ |
| 1055 | emit(f,"\texts\t#SEG "); |
| 1056 | isseg=1;emit_obj(f,z,INT); |
| 1057 | if(qr>16) |
| 1058 | emit(f,",#2\n"); |
| 1059 | else |
| 1060 | emit(f,",#1\n"); |
| 1061 | } |
| 1062 | if(reg_pair(qr,&rp)){ |
| 1063 | if(z->am){ |
| 1064 | emit(f,"\tmov\t");emit_obj(f,z,t);emit(f,",%s\n",regnames[rp.r1]); |
| 1065 | z->am->offset+=2; |
| 1066 | emit(f,"\tmov\t");emit_obj(f,z,t);emit(f,",%s\n",regnames[rp.r2]); |
| 1067 | z->am->offset-=2; |
| 1068 | return; |
| 1069 | }else if(z->flags&DREFOBJ){ |
| 1070 | if(!(z->flags®)) ierror(0); |
| 1071 | if(!l) l=z->reg; |
| 1072 | /*FIXME: test auf scratchreg statt >MAXR*/ |
| 1073 | if(z->reg>MAXR&&l!=rp.r2){ |
| 1074 | emit(f,"\tmov\t[%s+],%s\n",regnames[l],regnames[rp.r1]); |
| 1075 | emit(f,"\tmov\t[%s],%s\n",regnames[l],regnames[rp.r2]); |
| 1076 | }else{ |
| 1077 | emit(f,"\tmov\t[%s],%s\n",regnames[l],regnames[rp.r1]); |
| 1078 | emit(f,"\tmov\t[%s+#2],%s\n",regnames[l],regnames[rp.r2]); |
| 1079 | } |
| 1080 | return; |
| 1081 | }else if(zr){ |
| 1082 | if(!reg_pair(zr,&qp)) ierror(0); |
| 1083 | emit(f,"\tmov\t%s,%s\n",regnames[qp.r1],regnames[rp.r1]); |
| 1084 | emit(f,"\tmov\t%s,%s\n",regnames[qp.r2],regnames[rp.r2]); |
| 1085 | return; |
| 1086 | }else{ |
| 1087 | if(!(z->flags&VAR)) ierror(0); |
| 1088 | if(z->v->storage_class==AUTO||z->v->storage_class==REGISTER){ |
| 1089 | l=voff(z); |
| 1090 | if(l) |
| 1091 | emit(f,"\tmov\t[%s+#%ld],%s\n",regnames[sp],l,regnames[rp.r1]); |
| 1092 | else |
| 1093 | emit(f,"\tmov\t[%s],%s\n",regnames[sp],regnames[rp.r1]); |
| 1094 | emit(f,"\tmov\t[%s+#%ld],%s\n",regnames[sp],l+2,regnames[rp.r2]); |
| 1095 | return; |
| 1096 | }else{ |
| 1097 | emit(f,"\tmov\t");emit_obj(f,z,t);emit(f,",%s\n",regnames[rp.r1]); |
| 1098 | emit(f,"\tmov\t");emit_obj(f,z,t);emit(f,"+2,%s\n",regnames[rp.r2]); |
| 1099 | return; |
| 1100 | } |
| 1101 | } |
| 1102 | ierror(0); |
| 1103 | }else{ |
| 1104 | emit(f,"\tmov%s\t",x_t[t&NQ]); |
| 1105 | if(!z->am&&(z->flags&(REG|DREFOBJ))==(REG|DREFOBJ)&®_pair(z->reg,&rp)) |
| 1106 | emit(f,"[%s]",regnames[rp.SOF]); |
| 1107 | else if(zr&&t==CHAR) |
| 1108 | emit(f,"%s",bregnames[zr]); |
| 1109 | else |
| 1110 | emit_obj(f,z,t); |
| 1111 | emit(f,",%s\n",((t==CHAR)?bregnames[qr]:regnames[qr])); |
| 1112 | return; |
| 1113 | } |
| 1114 | } |
| 1115 | /*reg->reg*/ |
| 1116 | if(t==CHAR){ |
| 1117 | emit(f,"\tmovb\t%s,%s\n",bregnames[zr],bregnames[qr]); |
| 1118 | }else if(ISLWORD(t)){ |
| 1119 | if(!reg_pair(qr,&qp)) ierror(0); |
| 1120 | if(!reg_pair(zr,&rp)) ierror(0); |
| 1121 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r1],regnames[qp.r1]); |
| 1122 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r2],regnames[qp.r2]); |
| 1123 | }else{ |
| 1124 | emit(f,"\tmov\t%s,%s\n",regnames[zr],regnames[qr]); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | static int get_reg(FILE *f,struct IC *p) |
| 1129 | { |
| 1130 | int i; |
| 1131 | for(i=1;i<=16;i++){ |
| 1132 | if(!regs[i]&®scratch[i]){ |
| 1133 | BSET(regs_modified,i); |
| 1134 | return i; |
| 1135 | } |
| 1136 | } |
| 1137 | ierror(0); |
| 1138 | } |
| 1139 | |
| 1140 | static void save_result(FILE *f,int r,struct IC *p,int t) |
| 1141 | /* Saves result in register r to object o. May use tp or ti. */ |
| 1142 | { |
| 1143 | if((p->z.flags&(REG|DREFOBJ))==DREFOBJ){ |
| 1144 | BSET(regs_modified,tp); |
| 1145 | if(ISLWORD(p->z.dtyp)){ |
| 1146 | if(!ti2_used) tmp4=ti2; else tmp4=get_reg(f,p); |
| 1147 | BSET(regs_modified,tmp3); |
| 1148 | tmp3=tp; |
| 1149 | p->z.flags&=~DREFOBJ; |
| 1150 | move(f,&p->z,0,0,MTMP2,FPOINTER); |
| 1151 | p->z.flags=(DREFOBJ|REG); |
| 1152 | p->z.reg=MTMP2; |
| 1153 | }else if(!(p->z.flags&KONST)){ |
| 1154 | int tmp=(r==tp)?ti:tp; |
| 1155 | BSET(regs_modified,tmp); |
| 1156 | p->z.flags&=~DREFOBJ; |
| 1157 | move(f,&p->z,0,0,tmp,NPOINTER); |
| 1158 | p->z.flags=(DREFOBJ|REG); |
| 1159 | p->z.reg=tmp; |
| 1160 | } |
| 1161 | } |
| 1162 | move(f,0,r,&p->z,0,t); |
| 1163 | } |
| 1164 | |
| 1165 | static int scratchreg(int r,struct IC *p) |
| 1166 | { |
| 1167 | int c; |
| 1168 | if(r==tp||r==ti||r==ti2||r==MTMP1||r==MTMP2) |
| 1169 | return 1; |
| 1170 | while(1){ |
| 1171 | p=p->next; |
| 1172 | if(!p||((c=p->code)==FREEREG&&p->q1.reg==r)) return 1; |
| 1173 | if(c==CALL||(c>=BEQ&&c<=BRA)) return 0; |
| 1174 | if((p->q1.flags®)&&p->q1.reg==r) return 0; |
| 1175 | if((p->q2.flags®)&&p->q2.reg==r) return 0; |
| 1176 | if((p->z.flags®)&&p->z.reg==r) return 0; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | static char *longcmd(char *s) |
| 1181 | { |
| 1182 | if(!strcmp(s,"add")) return "addc"; |
| 1183 | if(!strcmp(s,"sub")) return "subc"; |
| 1184 | /*FIXME*/ |
| 1185 | return s; |
| 1186 | } |
| 1187 | |
| 1188 | /* perform long arithmetic using library functions */ |
| 1189 | /* the functions take arguments in r4/r5 and r10/r11 */ |
| 1190 | /* giving the result in r4/r5 */ |
| 1191 | /* returns , if the operation was performed, 0 if */ |
| 1192 | /* the IC should be handled by inline-code */ |
| 1193 | static int lib_larith(FILE *f,struct IC *p) |
| 1194 | { |
| 1195 | int r4p,r5p,c=p->code,t=ztyp(p);char *s; |
| 1196 | if(regs[r4]&&(!isreg(z)||p->z.reg==r4r5)){ |
| 1197 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[r4]); |
| 1198 | push(2,0); |
| 1199 | r4p=1; |
| 1200 | }else |
| 1201 | r4p=0; |
| 1202 | if(regs[r5]&&(!isreg(z)||p->z.reg==r4r5)){ |
| 1203 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[r5]); |
| 1204 | push(2,0); |
| 1205 | r5p=1; |
| 1206 | }else |
| 1207 | r5p=0; |
| 1208 | if(!p->q1.am&&(p->q1.flags&DREFOBJ)){ |
| 1209 | if(!(p->q1.flags&(REG))){ |
| 1210 | if(ISLWORD(p->q1.dtyp)){ |
| 1211 | tmp3=ti2;tmp4=ti;ti2_used=1; |
| 1212 | BSET(regs_modified,ti); |
| 1213 | BSET(regs_modified,ti2); |
| 1214 | p->q1.flags&=~DREFOBJ; |
| 1215 | move(f,&p->q1,0,0,MTMP2,FPOINTER); |
| 1216 | p->q1.reg=MTMP2; |
| 1217 | p->q1.flags=(REG|DREFOBJ); |
| 1218 | }else if(!(p->q1.flags&KONST)){ |
| 1219 | BSET(regs_modified,ti); |
| 1220 | p->q1.flags&=~DREFOBJ; |
| 1221 | move(f,&p->q1,0,0,ti,NPOINTER); |
| 1222 | p->q1.reg=ti; |
| 1223 | p->q1.flags=(REG|DREFOBJ); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | BSET(regs_modified,r4r5); |
| 1228 | move(f,&p->q1,0,0,r4r5,q1typ(p)); |
| 1229 | if(!p->q2.am&&(p->q2.flags&DREFOBJ)){ |
| 1230 | if(!(p->q2.flags&(REG))){ |
| 1231 | if(ISLWORD(p->q2.dtyp)){ |
| 1232 | BSET(regs_modified,ti); |
| 1233 | BSET(regs_modified,ti2); |
| 1234 | tmp3=ti2;tmp4=ti;ti2_used=1; |
| 1235 | p->q2.flags&=~DREFOBJ; |
| 1236 | move(f,&p->q2,0,0,MTMP2,FPOINTER); |
| 1237 | p->q2.reg=MTMP2; |
| 1238 | p->q2.flags=(REG|DREFOBJ); |
| 1239 | }else if(!(p->q2.flags&KONST)){ |
| 1240 | BSET(regs_modified,ti); |
| 1241 | p->q2.flags&=~DREFOBJ; |
| 1242 | move(f,&p->q2,0,0,ti,NPOINTER); |
| 1243 | p->q2.reg=ti; |
| 1244 | p->q2.flags=(REG|DREFOBJ); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | /* ti/ti2 must be r10/r11 */ |
| 1249 | tmp3=ti;tmp4=ti2;ti2_used=1; |
| 1250 | BSET(regs_modified,ti); |
| 1251 | BSET(regs_modified,ti2); |
| 1252 | move(f,&p->q2,0,0,MTMP2,q2typ(p)); |
| 1253 | if(c==MULT) |
| 1254 | s="_mul"; |
| 1255 | else if(c==DIV&&(t&UNSIGNED)) |
| 1256 | s="_udil"; |
| 1257 | else if(c==DIV&&!(t&UNSIGNED)) |
| 1258 | s="_sdil"; |
| 1259 | else if(c==MOD&&(t&UNSIGNED)) |
| 1260 | s="_umol"; |
| 1261 | else if(c==MOD&&!(t&UNSIGNED)) |
| 1262 | s="_smol"; |
| 1263 | else |
| 1264 | ierror(0); |
| 1265 | if(TASKING){ |
| 1266 | emit(f,"\textern\t%s%s:%s\n",idprefix,s,TINY?"near":"far"); |
| 1267 | emit(f,"\t%s\t%s%s\n",call,idprefix,s); |
| 1268 | } |
| 1269 | save_result(f,r4r5,p,ztyp(p)); |
| 1270 | if(r5p){ |
| 1271 | emit(f,"\tmov\t%s,[%s+]\n",regnames[r5],regnames[sp]); |
| 1272 | pop(2,0); |
| 1273 | } |
| 1274 | if(r4p){ |
| 1275 | emit(f,"\tmov\t%s,[%s+]\n",regnames[r4],regnames[sp]); |
| 1276 | pop(2,0); |
| 1277 | } |
| 1278 | return 1; |
| 1279 | } |
| 1280 | |
| 1281 | /****************************************/ |
| 1282 | /* End of private fata and functions. */ |
| 1283 | /****************************************/ |
| 1284 | |
| 1285 | int emit_peephole(void) |
| 1286 | { |
| 1287 | int entries,i;long x,y,z; |
| 1288 | char *asmline[EMIT_BUF_DEPTH]; |
| 1289 | i=emit_l; |
| 1290 | if(emit_f==0) |
| 1291 | entries=i-emit_f+1; |
| 1292 | else |
| 1293 | entries=EMIT_BUF_DEPTH; |
| 1294 | asmline[0]=emit_buffer[i]; |
| 1295 | if(entries>=1){ |
| 1296 | if(sscanf(asmline[0],"\tshl\tR%ld,#%ld\n",&x,&y)==2&&(y<0||y>15)){ |
| 1297 | sprintf(asmline[0],"\tmov\tR%ld,#0\n",x); |
| 1298 | return 1; |
| 1299 | } |
| 1300 | if(sscanf(asmline[0],"\tshr\tR%ld,#%ld\n",&x,&y)==2&&(y<0||y>15)){ |
| 1301 | sprintf(asmline[0],"\tmov\tR%ld,#0\n",x); |
| 1302 | return 1; |
| 1303 | } |
| 1304 | } |
| 1305 | if(entries>=2){ |
| 1306 | i--; |
| 1307 | if(i<0) i=EMIT_BUF_DEPTH-1; |
| 1308 | asmline[1]=emit_buffer[i]; |
| 1309 | if(sscanf(asmline[0],"\tadd\tR0,#%ld",&x)==1&&sscanf(asmline[1],"\tadd\tR0,#%ld",&y)==1){ |
| 1310 | sprintf(asmline[1],"\tadd\tR0,#%ld\n",x+y); |
| 1311 | remove_asm(); |
| 1312 | return 1; |
| 1313 | } |
| 1314 | if(sscanf(asmline[1],"\tmov\tR11,#%ld",&x)==1&&sscanf(asmline[0],"\texts\tR11,#%ld",&y)==1){ |
| 1315 | sprintf(asmline[1],"\texts\t#%ld,#%ld\n",x,y); |
| 1316 | remove_asm(); |
| 1317 | return 1; |
| 1318 | } |
| 1319 | } |
| 1320 | if(entries>=3){ |
| 1321 | i--; |
| 1322 | if(i<0) i=EMIT_BUF_DEPTH-1; |
| 1323 | asmline[2]=emit_buffer[i]; |
| 1324 | if(sscanf(asmline[2],"\tmov\tR1,#%ld",&x)==1&&sscanf(asmline[1],"\texts\t#%ld,#1",&y)==1&&sscanf(asmline[0],"\tmov\t[R1],R%ld",&z)==1){ |
| 1325 | strcpy(asmline[2],asmline[1]); |
| 1326 | sprintf(asmline[1],"\tmov\t%ld,R%ld\n",x,z); |
| 1327 | remove_asm(); |
| 1328 | return 1; |
| 1329 | } |
| 1330 | } |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | int init_cg(void) |
| 1335 | /* Does necessary initializations for the code-generator. Gets called */ |
| 1336 | /* once at the beginning and should return 0 in case of problems. */ |
| 1337 | { |
| 1338 | int i; |
| 1339 | /* Initialize some values which cannot be statically initialized */ |
| 1340 | /* because they are stored in the target's arithmetic. */ |
| 1341 | maxalign=l2zm(2L); |
| 1342 | char_bit=l2zm(8L); |
| 1343 | for(i=0;i<=MAX_TYPE;i++){ |
| 1344 | sizetab[i]=l2zm(msizetab[i]); |
| 1345 | align[i]=l2zm(malign[i]); |
| 1346 | } |
| 1347 | for(i=1;i<=MAXR;i++){ |
| 1348 | if(i<=16){ |
| 1349 | regsize[i]=l2zm(2L);regtype[i]=&ityp; |
| 1350 | }else{ |
| 1351 | regsize[i]=l2zm(4L);regtype[i]=<yp; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /* Initialize the min/max-settings. Note that the types of the */ |
| 1356 | /* host system may be different from the target system and you may */ |
| 1357 | /* only use the smallest maximum values ANSI guarantees if you */ |
| 1358 | /* want to be portable. */ |
| 1359 | /* That's the reason for the subtraction in t_min[INT]. Long could */ |
| 1360 | /* be unable to represent -2147483648 on the host system. */ |
| 1361 | t_min[CHAR]=l2zm(-128L); |
| 1362 | t_min[SHORT]=l2zm(-32768L); |
| 1363 | t_min[INT]=t_min[SHORT]; |
| 1364 | t_min[LONG]=zmsub(l2zm(-2147483647L),l2zm(1L)); |
| 1365 | t_min[LLONG]=zmlshift(l2zm(1L),l2zm(63L)); |
| 1366 | t_min[MAXINT]=t_min(LLONG); |
| 1367 | t_max[CHAR]=ul2zum(127L); |
| 1368 | t_max[SHORT]=ul2zum(32767UL); |
| 1369 | t_max[INT]=t_max[SHORT]; |
| 1370 | t_max[LONG]=ul2zum(2147483647UL); |
| 1371 | t_max[LLONG]=zumrshift(zumkompl(ul2zum(0UL)),ul2zum(1UL)); |
| 1372 | t_max[MAXINT]=t_max(LLONG); |
| 1373 | tu_max[CHAR]=ul2zum(255UL); |
| 1374 | tu_max[SHORT]=ul2zum(65535UL); |
| 1375 | tu_max[INT]=tu_max[SHORT]; |
| 1376 | tu_max[LONG]=ul2zum(4294967295UL); |
| 1377 | tu_max[LLONG]=zumkompl(ul2zum(0UL)); |
| 1378 | tu_max[MAXINT]=t_max(UNSIGNED|LLONG); |
| 1379 | |
| 1380 | /* Reserve a few registers for use by the code-generator. */ |
| 1381 | regsa[sp]=regsa[tp]=regsa[ti]=regsa[ti2]=1; |
| 1382 | regscratch[sp]=regscratch[tp]=regscratch[ti]=regscratch[ti2]=0; |
| 1383 | target_macros=marray; |
| 1384 | if(TINY) marray[0]="__TINY__"; |
| 1385 | else if(LARGE) marray[0]="__LARGE__"; |
| 1386 | else if(HUGE) marray[0]="__HUGE__"; |
| 1387 | else marray[0]="__MEDIUM__"; |
| 1388 | |
| 1389 | if(!TASKING){ |
| 1390 | codename="\t.section\t.text,\"carx1\"\n"; |
| 1391 | ndataname="\t.section\t.ndata,\"darw1\"\n"; |
| 1392 | fdataname="\t.section\t.fdata,\"darw1\"\n"; |
| 1393 | hdataname="\t.section\t.hdata,\"darw1\"\n"; |
| 1394 | nbssname="\t.section\t.nbss,\"uarw1\"\n"; |
| 1395 | fbssname="\t.section\t.fbss,\"uarw1\"\n"; |
| 1396 | hbssname="\t.section\t.hbss,\"uarw1\"\n"; |
| 1397 | ncdataname="\t.section\t.ncdata,\"dar1\"\n"; |
| 1398 | fcdataname="\t.section\t.fcdata,\"dar1\"\n"; |
| 1399 | hcdataname="\t.section\t.hcdata,\"dar1\"\n"; |
| 1400 | bitsname="\t.section\t.bits,\"darw0\"\n"; |
| 1401 | even="\t.align\t1\n"; |
| 1402 | public=".global"; |
| 1403 | comment="; "; |
| 1404 | } |
| 1405 | |
| 1406 | nvar.storage_class=STATIC; |
| 1407 | fvar.storage_class=STATIC; |
| 1408 | |
| 1409 | |
| 1410 | /* TODO: set argument registers */ |
| 1411 | declare_builtin("__mulint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1412 | declare_builtin("__addint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1413 | declare_builtin("__subint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1414 | declare_builtin("__andint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1415 | declare_builtin("__orint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1416 | declare_builtin("__eorint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1417 | declare_builtin("__negint64",LLONG,LLONG,0,0,0,1,0); |
| 1418 | declare_builtin("__lslint64",LLONG,LLONG,0,INT,0,1,0); |
| 1419 | |
| 1420 | declare_builtin("__divsint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1421 | declare_builtin("__divuint64",UNSIGNED|LLONG,UNSIGNED|LLONG,0,UNSIGNED|LLONG,0,1,0); |
| 1422 | declare_builtin("__modsint64",LLONG,LLONG,0,LLONG,0,1,0); |
| 1423 | declare_builtin("__moduint64",UNSIGNED|LLONG,UNSIGNED|LLONG,0,UNSIGNED|LLONG,0,1,0); |
| 1424 | declare_builtin("__lsrsint64",LLONG,LLONG,0,INT,0,1,0); |
| 1425 | declare_builtin("__lsruint64",UNSIGNED|LLONG,UNSIGNED|LLONG,0,INT,0,1,0); |
| 1426 | declare_builtin("__cmpsint64",INT,LLONG,0,LLONG,0,1,0); |
| 1427 | declare_builtin("__cmpuint64",INT,UNSIGNED|LLONG,0,UNSIGNED|LLONG,0,1,0); |
| 1428 | declare_builtin("__sint64toflt32",FLOAT,LLONG,0,0,0,1,0); |
| 1429 | declare_builtin("__uint64toflt32",FLOAT,UNSIGNED|LLONG,0,0,0,1,0); |
| 1430 | declare_builtin("__sint64toflt64",DOUBLE,LLONG,0,0,0,1,0); |
| 1431 | declare_builtin("__uint64toflt64",DOUBLE,UNSIGNED|LLONG,0,0,0,1,0); |
| 1432 | declare_builtin("__flt32tosint64",LLONG,FLOAT,0,0,0,1,0); |
| 1433 | declare_builtin("__flt32touint64",UNSIGNED|LLONG,FLOAT,0,0,0,1,0); |
| 1434 | declare_builtin("__flt64tosint64",LLONG,DOUBLE,0,0,0,1,0); |
| 1435 | declare_builtin("__flt64touint64",UNSIGNED|LLONG,DOUBLE,0,0,0,1,0); |
| 1436 | |
| 1437 | declare_builtin("__flt32toflt64",DOUBLE,FLOAT,0,0,0,1,0); |
| 1438 | declare_builtin("__flt64toflt32",FLOAT,DOUBLE,0,0,0,1,0); |
| 1439 | |
| 1440 | |
| 1441 | declare_builtin("__addflt32",FLOAT,FLOAT,0,FLOAT,0,1,0); |
| 1442 | declare_builtin("__subflt32",FLOAT,FLOAT,0,FLOAT,0,1,0); |
| 1443 | declare_builtin("__mulflt32",FLOAT,FLOAT,0,FLOAT,0,1,0); |
| 1444 | declare_builtin("__divflt32",FLOAT,FLOAT,0,FLOAT,0,1,0); |
| 1445 | declare_builtin("__negflt32",FLOAT,FLOAT,0,FLOAT,0,1,0); |
| 1446 | declare_builtin("__cmpflt32",INT,FLOAT,0,FLOAT,0,1,0); |
| 1447 | |
| 1448 | declare_builtin("__addflt64",DOUBLE,DOUBLE,0,DOUBLE,0,1,0); |
| 1449 | declare_builtin("__subflt64",DOUBLE,DOUBLE,0,DOUBLE,0,1,0); |
| 1450 | declare_builtin("__mulflt64",DOUBLE,DOUBLE,0,DOUBLE,0,1,0); |
| 1451 | declare_builtin("__divflt64",DOUBLE,DOUBLE,0,DOUBLE,0,1,0); |
| 1452 | declare_builtin("__negflt64",DOUBLE,DOUBLE,0,DOUBLE,0,1,0); |
| 1453 | declare_builtin("__cmpflt64",INT,DOUBLE,0,DOUBLE,0,1,0); |
| 1454 | |
| 1455 | return 1; |
| 1456 | } |
| 1457 | |
| 1458 | int freturn(struct Typ *t) |
| 1459 | /* Returns the register in which variables of type t are returned. */ |
| 1460 | /* If the value cannot be returned in a register returns 0. */ |
| 1461 | { |
| 1462 | int f=t->flags&NQ; |
| 1463 | if(f==LLONG||f==DOUBLE||f==LDOUBLE) |
| 1464 | return 0; |
| 1465 | if(ISSCALAR(f)){ |
| 1466 | if(f==LONG||f==FPOINTER||f==HPOINTER) |
| 1467 | return 20; |
| 1468 | else |
| 1469 | return 5; |
| 1470 | } |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | int regok(int r,int t,int mode) |
| 1475 | /* Returns 0 if register r cannot store variables of */ |
| 1476 | /* type t. If t==POINTER and mode!=0 then it returns */ |
| 1477 | /* non-zero only if the register can store a pointer */ |
| 1478 | /* and dereference a pointer to mode. */ |
| 1479 | { |
| 1480 | t&=NQ; |
| 1481 | if(r==0) return(0); |
| 1482 | if(mode>0&&!ISPOINTER(t)) return 0; |
| 1483 | if(t==NPOINTER&&mode>0){ |
| 1484 | if(r<=4) return 1; else return 0; |
| 1485 | } |
| 1486 | if(t<=CHAR&&r>8) return 0; |
| 1487 | if((t<LONG||t==NPOINTER)&&r<=16) return 1; |
| 1488 | if(t==LONG||t==FLOAT||t==FPOINTER||t==HPOINTER){ |
| 1489 | if(r>16) return 1; |
| 1490 | } |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | int reg_pair(int r,struct rpair *p) |
| 1495 | /* Returns 0 if the register is no register pair. If r */ |
| 1496 | /* is a register pair non-zero will be returned and the */ |
| 1497 | /* structure pointed to p will be filled with the two */ |
| 1498 | /* elements. */ |
| 1499 | { |
| 1500 | if(r<=16) return 0; |
| 1501 | if(p){ |
| 1502 | switch(r){ |
| 1503 | case 17: p->r1=7;p->r2=8;break; |
| 1504 | case 18: p->r1=8;p->r2=9;break; |
| 1505 | case 19: p->r1=9;p->r2=10;break; |
| 1506 | case 20: p->r1=5;p->r2=6;break; |
| 1507 | case 21: p->r1=15;p->r2=16;break; |
| 1508 | case 22: p->r1=14;p->r2=15;break; |
| 1509 | case 23: p->r1=13;p->r2=14;break; |
| 1510 | case 24: p->r1=4;p->r2=5;break; |
| 1511 | case 25: p->r1=3;p->r2=4;break; |
| 1512 | |
| 1513 | /* pseudos */ |
| 1514 | case MTMP1: p->r1=tmp1;p->r2=tmp2;break; |
| 1515 | case MTMP2: p->r1=tmp3;p->r2=tmp4;break; |
| 1516 | case ZEROS: return 0; |
| 1517 | case ONES: return 0; |
| 1518 | default: ierror(0); |
| 1519 | } |
| 1520 | } |
| 1521 | return 1; |
| 1522 | } |
| 1523 | |
| 1524 | int cost_savings(struct IC *p,int r,struct obj *o) |
| 1525 | { |
| 1526 | int c=p->code; |
| 1527 | if((o->flags&DREFOBJ)){ |
| 1528 | if(o->flags&VKONST) return 1; |
| 1529 | if(r<=4&&p->q2.flags&&o!=&p->z) |
| 1530 | return 6; |
| 1531 | else |
| 1532 | return 4; |
| 1533 | }else if(o->flags&VKONST){ |
| 1534 | struct obj *co=&o->v->cobj; |
| 1535 | if((p->code==ASSIGN&&(p->z.flags&DREFOBJ))||p->code==PUSH) |
| 1536 | return 4; |
| 1537 | if(co->flags&VARADR) |
| 1538 | return 4; |
| 1539 | if(o==&p->q1) |
| 1540 | eval_const(&co->val,q1typ(p)); |
| 1541 | else |
| 1542 | eval_const(&co->val,q2typ(p)); |
| 1543 | /*FIXME*/ |
| 1544 | return 0; |
| 1545 | }else if(c==GETRETURN&&p->q1.reg==r){ |
| 1546 | return 4; |
| 1547 | }else if(c==SETRETURN&&p->z.reg==r){ |
| 1548 | return 4; |
| 1549 | }else if(c==CONVERT&&((p->typf&NQ)==CHAR||(p->typf2&NQ)==CHAR)&®ok(r,CHAR,0)){ |
| 1550 | return 3; |
| 1551 | } |
| 1552 | return 2; |
| 1553 | } |
| 1554 | |
| 1555 | int dangerous_IC(struct IC *p) |
| 1556 | /* Returns zero if the IC p can be safely executed */ |
| 1557 | /* without danger of exceptions or similar things. */ |
| 1558 | /* vbcc may generate code in which non-dangerous ICs */ |
| 1559 | /* are sometimes executed although control-flow may */ |
| 1560 | /* never reach them (mainly when moving computations */ |
| 1561 | /* out of loops). */ |
| 1562 | /* Typical ICs that generate exceptions on some */ |
| 1563 | /* machines are: */ |
| 1564 | /* - accesses via pointers */ |
| 1565 | /* - division/modulo */ |
| 1566 | /* - overflow on signed integer/floats */ |
| 1567 | { |
| 1568 | int c=p->code; |
| 1569 | if((p->q1.flags&DREFOBJ)||(p->q2.flags&DREFOBJ)||(p->z.flags&DREFOBJ)) |
| 1570 | return 1; |
| 1571 | if((c==DIV||c==MOD)&&!isconst(q2)) |
| 1572 | return 1; |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | int must_convert(int o,int t,int const_expr) |
| 1577 | /* Returns zero if code for converting np to type t */ |
| 1578 | /* can be omitted. */ |
| 1579 | /* In this generic 32bit RISC cpu pointers and 32bit */ |
| 1580 | /* integers have the same representation and can use */ |
| 1581 | /* the same registers. */ |
| 1582 | { |
| 1583 | int op=o&NQ,tp=t&NQ; |
| 1584 | if(op==tp) return 0; |
| 1585 | if(ISHWORD(op)&&ISHWORD(tp)) return 0; |
| 1586 | if(ISFLOAT(op)||ISFLOAT(tp)) return 1; |
| 1587 | if(ISLWORD(op)&&ISLWORD(tp)) return 0; |
| 1588 | return 1; |
| 1589 | } |
| 1590 | |
| 1591 | void gen_ds(FILE *f,zmax size,struct Typ *t) |
| 1592 | /* This function has to create <size> bytes of storage */ |
| 1593 | /* initialized with zero. */ |
| 1594 | { |
| 1595 | #if 0 |
| 1596 | emit(f,"\tds\t%ld\n",zm2l(size)); |
| 1597 | #endif |
| 1598 | /*FIXME: we currently do not use ds because of initialization */ |
| 1599 | long l=zm2l(size); |
| 1600 | if(TASKING){ |
| 1601 | if(t&&(t->flags&NQ)==BIT){ |
| 1602 | emit(f,"\tdbit\n"); |
| 1603 | return; |
| 1604 | } |
| 1605 | /*FIXME:alignment while(size>=4) {emit(f,"\tddw\t0\n");size-=4;}*/ |
| 1606 | while(size--) emit(f,"\tdb\t0\n"); |
| 1607 | }else{ |
| 1608 | if(newobj&§ion!=SPECIAL) |
| 1609 | emit(f,"%ld\n",zm2l(size)); |
| 1610 | else |
| 1611 | emit(f,"\t.space\t%ld\n",zm2l(size)); |
| 1612 | newobj=0; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | void gen_align(FILE *f,zmax align) |
| 1617 | /* This function has to make sure the next data is */ |
| 1618 | /* aligned to multiples of <align> bytes. */ |
| 1619 | { |
| 1620 | if(section!=BITS&&!zmleq(align,l2zm(1L))) |
| 1621 | emit(f,even); |
| 1622 | } |
| 1623 | static void new_section(FILE *f,int nsec) |
| 1624 | { |
| 1625 | if(!f||section==nsec) return; |
| 1626 | if(TASKING) |
| 1627 | emit(f,sec_end); |
| 1628 | section=nsec; |
| 1629 | if(nsec==HDATA){ |
| 1630 | emit(f,hdataname); |
| 1631 | strcpy(sec_end,ehdata); |
| 1632 | }else if(nsec==FDATA){ |
| 1633 | emit(f,fdataname); |
| 1634 | strcpy(sec_end,efdata); |
| 1635 | }else if(nsec==NDATA){ |
| 1636 | emit(f,ndataname); |
| 1637 | strcpy(sec_end,endata); |
| 1638 | }else if(nsec==HCDATA){ |
| 1639 | emit(f,hcdataname); |
| 1640 | strcpy(sec_end,ehcdata); |
| 1641 | }else if(nsec==FCDATA){ |
| 1642 | emit(f,fcdataname); |
| 1643 | strcpy(sec_end,efcdata); |
| 1644 | }else if(nsec==NCDATA){ |
| 1645 | emit(f,ncdataname); |
| 1646 | strcpy(sec_end,encdata); |
| 1647 | }else if(nsec==HBSS){ |
| 1648 | emit(f,hbssname); |
| 1649 | strcpy(sec_end,ehbss); |
| 1650 | }else if(nsec==FBSS){ |
| 1651 | emit(f,fbssname); |
| 1652 | strcpy(sec_end,efbss); |
| 1653 | }else if(nsec==NBSS){ |
| 1654 | emit(f,nbssname); |
| 1655 | strcpy(sec_end,enbss); |
| 1656 | }else if(nsec==BITS){ |
| 1657 | emit(f,bitsname); |
| 1658 | strcpy(sec_end,ebits); |
| 1659 | } |
| 1660 | } |
| 1661 | void gen_var_head(FILE *f,struct Var *v) |
| 1662 | /* This function has to create the head of a variable */ |
| 1663 | /* definition, i.e. the label and information for */ |
| 1664 | /* linkage etc. */ |
| 1665 | { |
| 1666 | int constflag; |
| 1667 | char *attr;struct Typ *tv; |
| 1668 | tv=v->vtyp; |
| 1669 | while(tv->flags==ARRAY) tv=tv->next; |
| 1670 | attr=tv->attr; |
| 1671 | if(v->clist) constflag=is_const(v->vtyp); |
| 1672 | if(v->storage_class==STATIC){ |
| 1673 | if(ISFUNC(v->vtyp->flags)) return; |
| 1674 | if(!TASKING){ |
| 1675 | emit(f,"\t.type\t%s%ld,@object\n",labprefix,zm2l(v->offset)); |
| 1676 | emit(f,"\t.size\t%s%ld,%ld\n",labprefix,zm2l(v->offset),zm2l(szof(v->vtyp))); |
| 1677 | } |
| 1678 | if(!special_section(f,v)){ |
| 1679 | if((v->vtyp->flags&NQ)==BIT){ |
| 1680 | new_section(f,BITS); |
| 1681 | }else{ |
| 1682 | if(v->clist&&(!constflag||(g_flags[3]&USEDFLAG))){ |
| 1683 | if(attr&&strstr(attr,STR_HUGE)) |
| 1684 | new_section(f,HDATA); |
| 1685 | else if(attr&&strstr(attr,STR_FAR)) |
| 1686 | new_section(f,FDATA); |
| 1687 | else if(attr&&strstr(attr,STR_NEAR)) |
| 1688 | new_section(f,NDATA); |
| 1689 | else{ |
| 1690 | if(HUGE) |
| 1691 | new_section(f,HDATA); |
| 1692 | else if(LARGE) |
| 1693 | new_section(f,FDATA); |
| 1694 | else |
| 1695 | new_section(f,NDATA); |
| 1696 | } |
| 1697 | } |
| 1698 | if(v->clist&&constflag&&!(g_flags[3]&USEDFLAG)){ |
| 1699 | if(attr&&strstr(attr,STR_HUGE)) |
| 1700 | new_section(f,HCDATA); |
| 1701 | else if(attr&&strstr(attr,STR_FAR)) |
| 1702 | new_section(f,FCDATA); |
| 1703 | else if(attr&&strstr(attr,STR_NEAR)) |
| 1704 | new_section(f,NCDATA); |
| 1705 | else{ |
| 1706 | if(HUGE) |
| 1707 | new_section(f,HCDATA); |
| 1708 | else if(LARGE) |
| 1709 | new_section(f,FCDATA); |
| 1710 | else |
| 1711 | new_section(f,NCDATA); |
| 1712 | } |
| 1713 | } |
| 1714 | if(!v->clist){ |
| 1715 | if(attr&&strstr(attr,STR_HUGE)) |
| 1716 | new_section(f,HBSS); |
| 1717 | else if(attr&&strstr(attr,STR_FAR)) |
| 1718 | new_section(f,FBSS); |
| 1719 | else if(attr&&strstr(attr,STR_NEAR)) |
| 1720 | new_section(f,NBSS); |
| 1721 | else{ |
| 1722 | if(HUGE) |
| 1723 | new_section(f,HBSS); |
| 1724 | else if(LARGE) |
| 1725 | new_section(f,FBSS); |
| 1726 | else |
| 1727 | new_section(f,NBSS); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | } |
| 1732 | gen_align(f,falign(v->vtyp)); |
| 1733 | if(TASKING){ |
| 1734 | emit(f,"%s%ld\tlabel\t",labprefix,zm2l(v->offset)); |
| 1735 | emit_label_type(f,v); |
| 1736 | emit(f,"\n"); |
| 1737 | }else{ |
| 1738 | if(dppuse(v,section)>=0) |
| 1739 | emit(f,"\t.set\t%s%s%ld,%d\n",dppprefix,labprefix,zm2l(v->offset),dppuse(v,section)); |
| 1740 | emit(f,"%s%ld:\n",labprefix,zm2l(v->offset)); |
| 1741 | } |
| 1742 | } |
| 1743 | if(v->storage_class==EXTERN){ |
| 1744 | if(v->flags&(DEFINED|TENTATIVE)){ |
| 1745 | if(!TASKING){ |
| 1746 | emit(f,"\t.type\t%s%s,@object\n",idprefix,v->identifier); |
| 1747 | emit(f,"\t.size\t%s%s,%ld\n",idprefix,v->identifier,zm2l(szof(v->vtyp))); |
| 1748 | } |
| 1749 | emit(f,"\t%s\t%s%s\n",public,idprefix,v->identifier); |
| 1750 | if(!special_section(f,v)){ |
| 1751 | if((v->vtyp->flags&NQ)==BIT){ |
| 1752 | if(f&§ion!=BITS){ |
| 1753 | if(TASKING) |
| 1754 | emit(f,sec_end); |
| 1755 | strcpy(sec_end,ebits); |
| 1756 | emit(f,bitsname); |
| 1757 | section=BITS; |
| 1758 | } |
| 1759 | }else{ |
| 1760 | if(v->clist&&(!constflag||(g_flags[3]&USEDFLAG))){ |
| 1761 | if(attr&&strstr(attr,STR_HUGE)) |
| 1762 | new_section(f,HDATA); |
| 1763 | else if(attr&&strstr(attr,STR_FAR)) |
| 1764 | new_section(f,FDATA); |
| 1765 | else if(attr&&strstr(attr,STR_NEAR)) |
| 1766 | new_section(f,NDATA); |
| 1767 | else{ |
| 1768 | if(HUGE) |
| 1769 | new_section(f,HDATA); |
| 1770 | else if(LARGE) |
| 1771 | new_section(f,FDATA); |
| 1772 | else |
| 1773 | new_section(f,NDATA); |
| 1774 | } |
| 1775 | } |
| 1776 | if(v->clist&&constflag&&!(g_flags[3]&USEDFLAG)){ |
| 1777 | if(attr&&strstr(attr,STR_HUGE)) |
| 1778 | new_section(f,HCDATA); |
| 1779 | else if(attr&&strstr(attr,STR_FAR)) |
| 1780 | new_section(f,FCDATA); |
| 1781 | else if(attr&&strstr(attr,STR_NEAR)) |
| 1782 | new_section(f,NCDATA); |
| 1783 | else{ |
| 1784 | if(HUGE) |
| 1785 | new_section(f,HCDATA); |
| 1786 | else if(LARGE) |
| 1787 | new_section(f,FCDATA); |
| 1788 | else |
| 1789 | new_section(f,NCDATA); |
| 1790 | } |
| 1791 | } |
| 1792 | if(!v->clist){ |
| 1793 | if(attr&&strstr(attr,STR_HUGE)) |
| 1794 | new_section(f,HBSS); |
| 1795 | else if(attr&&strstr(attr,STR_FAR)) |
| 1796 | new_section(f,FBSS); |
| 1797 | else if(attr&&strstr(attr,STR_NEAR)) |
| 1798 | new_section(f,NBSS); |
| 1799 | else{ |
| 1800 | if(HUGE) |
| 1801 | new_section(f,HBSS); |
| 1802 | else if(LARGE) |
| 1803 | new_section(f,FBSS); |
| 1804 | else |
| 1805 | new_section(f,NBSS); |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | gen_align(f,falign(v->vtyp)); |
| 1811 | if(TASKING){ |
| 1812 | emit(f,"%s%s\tlabel\t",idprefix,v->identifier); |
| 1813 | emit_label_type(f,v); |
| 1814 | emit(f,"\n"); |
| 1815 | }else{ |
| 1816 | if(dppuse(v,section)>=0){ |
| 1817 | emit(f,"\t.global\t%s%s%s\n",dppprefix,idprefix,v->identifier); |
| 1818 | emit(f,"\t.set\t%s%s%s,%d\n",dppprefix,idprefix,v->identifier,dppuse(v,section)); |
| 1819 | } |
| 1820 | emit(f,"%s%s:\n",idprefix,v->identifier); |
| 1821 | } |
| 1822 | }else if(strcmp(v->identifier,"__va_start")&&!issfrv(v)&&!isesfrv(v)&&!issfrbitv(v)&&!isesfrbitv(v)){ |
| 1823 | if(TASKING){ |
| 1824 | emit(f,"\textern\t%s%s:",idprefix,v->identifier); |
| 1825 | emit_label_type(f,v); |
| 1826 | emit(f,"\n"); |
| 1827 | }else{ |
| 1828 | emit(f,"\t.global\t%s%s\n",idprefix,v->identifier); |
| 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | void gen_dc(FILE *f,int t,struct const_list *p) |
| 1835 | /* This function has to create static storage */ |
| 1836 | /* initialized with const-list p. */ |
| 1837 | { |
| 1838 | if(ISPOINTER(t)){ |
| 1839 | if(p->tree) |
| 1840 | emit(f,"\t%s\t",TASKING?dct[t&NQ]:vdct[t&NQ]); |
| 1841 | if(ISLWORD(t)) |
| 1842 | t=UNSIGNED|LONG; |
| 1843 | else |
| 1844 | t=UNSIGNED|SHORT; |
| 1845 | if(!p->tree) |
| 1846 | emit(f,"\t%s\t",TASKING?dct[t&NQ]:vdct[t&NQ]); |
| 1847 | }else{ |
| 1848 | emit(f,"\t%s\t",TASKING?dct[t&NQ]:vdct[t&NQ]); |
| 1849 | } |
| 1850 | if(!p->tree){ |
| 1851 | if(ISFLOAT(t)){ |
| 1852 | /* auch wieder nicht sehr schoen und IEEE noetig */ |
| 1853 | unsigned char *ip; |
| 1854 | |
| 1855 | ip=(unsigned char *)&p->val.vdouble; |
| 1856 | emit(f,"0x%02x%02x%02x%02x",ip[3],ip[2],ip[1],ip[0]); |
| 1857 | if((t&NQ)==DOUBLE||(t&NQ)==LDOUBLE){ |
| 1858 | emit(f,",0x%02x%02x%02x%02x",ip[7],ip[6],ip[5],ip[4]); |
| 1859 | } |
| 1860 | }else{ |
| 1861 | if(ISLWORD(t)){ |
| 1862 | long l; |
| 1863 | eval_const(&p->val,t); |
| 1864 | l=zm2l(zmand(p->val.vmax,l2zm(65535L))); |
| 1865 | emit(f,"%ld",l); |
| 1866 | l=zm2l(zmand(zmrshift(p->val.vmax,l2zm(16L)),l2zm(65535L))); |
| 1867 | emit(f,",%ld",l); |
| 1868 | }else if((t&NQ)!=BIT) |
| 1869 | /*FIXME: initialization of bits impossible */ |
| 1870 | emitval(f,&p->val,(t&NU)|UNSIGNED); |
| 1871 | } |
| 1872 | }else{ |
| 1873 | int m=p->tree->o.flags; |
| 1874 | p->tree->o.flags&=~VARADR; |
| 1875 | emit_obj(f,&p->tree->o,t&NU); |
| 1876 | p->tree->o.flags=m; |
| 1877 | } |
| 1878 | emit(f,"\n"); |
| 1879 | } |
| 1880 | |
| 1881 | /* The main code-generation routine. */ |
| 1882 | /* f is the stream the code should be written to. */ |
| 1883 | /* p is a pointer to a doubly linked list of ICs */ |
| 1884 | /* containing the function body to generate code for. */ |
| 1885 | /* v is a pointer to the function. */ |
| 1886 | /* offset is the size of the stackframe the function */ |
| 1887 | /* needs for local variables. */ |
| 1888 | void gen_code(FILE *f,struct IC *p,struct Var *v,zmax offset) |
| 1889 | { |
| 1890 | int c,t,lastcomp=0,reg,short_add,bit_reverse,need_return=0; |
| 1891 | struct obj *bit_obj;char *bit_reg; |
| 1892 | static int idone; |
| 1893 | struct obj o,*cc=0;int cc_t; |
| 1894 | struct IC *p2; |
| 1895 | if(TINY){ |
| 1896 | ret="ret"; |
| 1897 | call="calla\tcc_uc,"; |
| 1898 | jump="jmpa\tcc_uc,"; |
| 1899 | }else{ |
| 1900 | ret="rets"; |
| 1901 | call="calls"; |
| 1902 | jump="jmps\t"; |
| 1903 | } |
| 1904 | if(v->tattr&INTERRUPT) |
| 1905 | ret="reti"; |
| 1906 | if(DEBUG&1) printf("gen_code()\n"); |
| 1907 | if(!v->fi) v->fi=new_fi(); |
| 1908 | v->fi->flags|=ALL_REGS; |
| 1909 | if(f&&TASKING&&!idone){ |
| 1910 | emit(f,"$EXTEND\n"); |
| 1911 | emit(f,"$MODEL(SMALL)\n"); |
| 1912 | idone=1; |
| 1913 | } |
| 1914 | for(p2=p;p2;p2=p2->next) clear_ext_ic(&p2->ext); |
| 1915 | if(!(g_flags[5]&USEDFLAG)){ |
| 1916 | peephole(p); |
| 1917 | if(!frame_used) offset=l2zm(0L); |
| 1918 | } |
| 1919 | for(c=1;c<=15;c++) regs[c]=regsa[c]; |
| 1920 | regs[16]=0; |
| 1921 | for(c=1;c<=MAXR;c++){ |
| 1922 | if(regscratch[c]&&(regsa[c]||regused[c])){ |
| 1923 | BSET(regs_modified,c); |
| 1924 | } |
| 1925 | } |
| 1926 | loff=((zm2l(offset)+1)/2)*2; |
| 1927 | function_top(f,v,loff); |
| 1928 | usrstackoffset=sysstackoffset=notpopped=dontpop=usrmaxpushed=sysmaxpushed=0; |
| 1929 | sysstack=usrstack=0; |
| 1930 | for(;p;pr(f,p),p=p->next){ |
| 1931 | if((p->q1.flags&(KONST|DREFOBJ))==(KONST|DREFOBJ)){ |
| 1932 | p->q1.flags=VAR; |
| 1933 | p->q1.v=ISLWORD(p->q1.dtyp)?&fvar:&nvar; |
| 1934 | } |
| 1935 | if((p->q2.flags&(KONST|DREFOBJ))==(KONST|DREFOBJ)){ |
| 1936 | p->q2.flags=VAR; |
| 1937 | p->q2.v=ISLWORD(p->q2.dtyp)?&fvar:&nvar; |
| 1938 | } |
| 1939 | if((p->z.flags&(KONST|DREFOBJ))==(KONST|DREFOBJ)){ |
| 1940 | p->z.flags=VAR; |
| 1941 | p->z.v=ISLWORD(p->z.dtyp)?&fvar:&nvar; |
| 1942 | } |
| 1943 | if(!TASKING){ |
| 1944 | int rn,rb;char *rs; |
| 1945 | if(issfr(q1)){ |
| 1946 | rs=strstr(p->q1.v->vattr,"sfr("); |
| 1947 | sscanf(rs+4,"%i",&rn); |
| 1948 | if(rn<=255) rn=0xfe00+2*rn; |
| 1949 | emit(f,"\t.sfr\t%s,%d\n",p->q1.v->identifier,rn); |
| 1950 | } |
| 1951 | if(issfr(q2)){ |
| 1952 | rs=strstr(p->q2.v->vattr,"sfr("); |
| 1953 | sscanf(rs+4,"%i",&rn); |
| 1954 | if(rn<=255) rn=0xfe00+2*rn; |
| 1955 | emit(f,"\t.sfr\t%s,%d\n",p->q2.v->identifier,rn); |
| 1956 | } |
| 1957 | if(issfr(z)){ |
| 1958 | rs=strstr(p->z.v->vattr,"sfr("); |
| 1959 | sscanf(rs+4,"%i",&rn); |
| 1960 | if(rn<=255) rn=0xfe00+2*rn; |
| 1961 | emit(f,"\t.sfr\t%s,%d\n",p->z.v->identifier,rn); |
| 1962 | } |
| 1963 | if(issfrbit(q1)){ |
| 1964 | rs=strstr(p->q1.v->vattr,"sfrbit("); |
| 1965 | sscanf(rs+7,"%i,%i",&rn,&rb); |
| 1966 | if(rn<=255) rn=0xfe00+2*rn; |
| 1967 | emit(f,"\t.sfr\t%s,%d,%d\n",p->q1.v->identifier,rn,rb); |
| 1968 | } |
| 1969 | if(issfrbit(q2)){ |
| 1970 | rs=strstr(p->q2.v->vattr,"sfrbit("); |
| 1971 | sscanf(rs+7,"%i,%i",&rn,&rb); |
| 1972 | if(rn<=255) rn=0xfe00+2*rn; |
| 1973 | emit(f,"\t.sfr\t%s,%d,%d\n",p->q2.v->identifier,rn,rb); |
| 1974 | } |
| 1975 | if(issfrbit(z)){ |
| 1976 | rs=strstr(p->z.v->vattr,"sfrbit("); |
| 1977 | sscanf(rs+7,"%i,%i",&rn,&rb); |
| 1978 | if(rn<=255) rn=0xfe00+2*rn; |
| 1979 | emit(f,"\t.sfr\t%s,%d,%d\n",p->z.v->identifier,rn,rb); |
| 1980 | } |
| 1981 | if(isesfr(q1)){ |
| 1982 | rs=strstr(p->q1.v->vattr,"sfre("); |
| 1983 | sscanf(rs+5,"%i",&rn); |
| 1984 | if(rn<=255) rn=0xf000+2*rn; |
| 1985 | emit(f,"\t.equ\t%s,%d\n",p->q1.v->identifier,rn); |
| 1986 | } |
| 1987 | if(isesfr(q2)){ |
| 1988 | rs=strstr(p->q2.v->vattr,"sfre("); |
| 1989 | sscanf(rs+5,"%i",&rn); |
| 1990 | if(rn<=255) rn=0xf000+2*rn; |
| 1991 | emit(f,"\t.equ\t%s,%d\n",p->q2.v->identifier,rn); |
| 1992 | } |
| 1993 | if(isesfr(z)){ |
| 1994 | rs=strstr(p->z.v->vattr,"sfre("); |
| 1995 | sscanf(rs+5,"%i",&rn); |
| 1996 | if(rn<=255) rn=0xf000+2*rn; |
| 1997 | emit(f,"\t.equ\t%s,%d\n",p->z.v->identifier,rn); |
| 1998 | } |
| 1999 | if(isesfrbit(q1)){ |
| 2000 | rs=strstr(p->q1.v->vattr,"sfrbite("); |
| 2001 | sscanf(rs+8,"%i,%i",&rn,&rb); |
| 2002 | if(rn<=255) rn=0xf000+2*rn; |
| 2003 | emit(f,"\t.sfr\t%s,%d,%d\n",p->q1.v->identifier,rn,rb); |
| 2004 | } |
| 2005 | if(isesfrbit(q2)){ |
| 2006 | rs=strstr(p->q2.v->vattr,"sfrbite("); |
| 2007 | sscanf(rs+8,"%i,%i",&rn,&rb); |
| 2008 | if(rn<=255) rn=0xf000+2*rn; |
| 2009 | emit(f,"\t.sfr\t%s,%d,%d\n",p->q2.v->identifier,rn,rb); |
| 2010 | } |
| 2011 | if(isesfrbit(z)){ |
| 2012 | rs=strstr(p->z.v->vattr,"sfrbite("); |
| 2013 | sscanf(rs+8,"%i,%i",&rn,&rb); |
| 2014 | if(rn<=255) rn=0xf000+2*rn; |
| 2015 | emit(f,"\t.sfr\t%s,%d,%d\n",p->z.v->identifier,rn,rb); |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | c=p->code;t=p->typf; |
| 2020 | ti2_used=0; short_add=0; |
| 2021 | if(c==NOP) continue; |
| 2022 | if(c==ALLOCREG){ |
| 2023 | regs[p->q1.reg]=1; |
| 2024 | if(reg_pair(p->q1.reg,&rp)) regs[rp.r1]=regs[rp.r2]=1; |
| 2025 | BSET(regs_modified,p->q1.reg); |
| 2026 | continue; |
| 2027 | } |
| 2028 | if(c==FREEREG){ |
| 2029 | regs[p->q1.reg]=0; |
| 2030 | if(reg_pair(p->q1.reg,&rp)) regs[rp.r1]=regs[rp.r2]=0; |
| 2031 | continue; |
| 2032 | } |
| 2033 | if(notpopped&&!dontpop){ |
| 2034 | int flag=0; |
| 2035 | if(c==LABEL||c==COMPARE||c==TEST||c==BRA){ |
| 2036 | emit(f,"\tadd\t%s,#%ld\n",regnames[sp],notpopped); |
| 2037 | pop(notpopped,0);notpopped=0;cc=0; |
| 2038 | } |
| 2039 | } |
| 2040 | if(c==LABEL) {cc=0;emit(f,"%s%d:\n",labprefix,t);continue;} |
| 2041 | if(c>=BEQ&&c<=BGT&&t==exit_label) need_return=1; |
| 2042 | if(c==BRA){ |
| 2043 | if(p->typf==exit_label&&!have_frame){ |
| 2044 | emit(f,"\t%s\n",ret); |
| 2045 | }else{ |
| 2046 | if(t==exit_label) need_return=1; |
| 2047 | emit(f,"\tjmpr\tcc_uc,%s%d\n",labprefix,t); |
| 2048 | } |
| 2049 | cc=0;continue; |
| 2050 | } |
| 2051 | if(c==BEQ||c==BNE){ |
| 2052 | if(p->ext.flags==EXT_IC_BTST){ |
| 2053 | if(bit_reg){ |
| 2054 | emit(f,"\tj%sb\t%s.%ld,%s%d\n",(c==BEQ)?"n":"",bit_reg,p->ext.offset,labprefix,t); |
| 2055 | }else{ |
| 2056 | if(!bit_obj) ierror(0); |
| 2057 | emit(f,"\tj%sb\t",(c==BEQ)?"n":""); |
| 2058 | emit_obj(f,bit_obj,t);emit(f,".%ld,%s%d\n",p->ext.offset,labprefix,t); |
| 2059 | } |
| 2060 | }else if(lastcomp==BIT){ |
| 2061 | if(c==BEQ) bit_reverse=1-bit_reverse; |
| 2062 | emit(f,"\tj%sb\t",(bit_reverse==1)?"n":""); |
| 2063 | if(bit_obj) |
| 2064 | emit_obj(f,bit_obj,t); |
| 2065 | else |
| 2066 | emit(f,"%s",bit_reg); |
| 2067 | emit(f,",%s%d\n",labprefix,t); |
| 2068 | }else |
| 2069 | emit(f,"\tjmpr\tcc_%s,%s%d\n",ccs[c-BEQ],labprefix,t); |
| 2070 | cc=0;continue; |
| 2071 | } |
| 2072 | if(c>BNE&&c<BRA){ |
| 2073 | if(p->ext.flags==EXT_IC_BTST||lastcomp==BIT) ierror(0); |
| 2074 | if(lastcomp&UNSIGNED) emit(f,"\tjmpr\tcc_u%s,%s%d\n",ccs[c-BEQ],labprefix,t); |
| 2075 | else emit(f,"\tjmpr\tcc_s%s,%s%d\n",ccs[c-BEQ],labprefix,t); |
| 2076 | cc=0;continue; |
| 2077 | } |
| 2078 | if(c==MOVETOREG){ |
| 2079 | move(f,&p->q1,0,0,p->z.reg,SHORT); |
| 2080 | cc=&p->q1;cc_t=SHORT;continue; |
| 2081 | } |
| 2082 | if(c==MOVEFROMREG){ |
| 2083 | move(f,0,p->q1.reg,&p->z,0,SHORT); |
| 2084 | cc=&p->z;cc_t=SHORT;continue; |
| 2085 | } |
| 2086 | |
| 2087 | if((t&NQ)==DOUBLE) {pric2(stdout,p);ierror(0);} |
| 2088 | if((t&NQ)==BIT){ |
| 2089 | cc=0; |
| 2090 | if(c==ASSIGN){ |
| 2091 | if(!isreg(z)&&((p->z.flags&(VAR|REG|DREFOBJ|VARADR))!=VAR||!ISSTATIC(p->z.v))) ierror(0); |
| 2092 | move(f,&p->q1,0,&p->z,0,t); |
| 2093 | continue; |
| 2094 | } |
| 2095 | if(c==COMPARE){ |
| 2096 | if(!isconst(q2)){ |
| 2097 | if(!isreg(q2)&&((p->q2.flags&(VAR|REG|DREFOBJ|VARADR))!=VAR||!ISSTATIC(p->q2.v))) ierror(0); |
| 2098 | bit_reg="PSW.1";bit_obj=0;lastcomp=BIT;bit_reverse=1; |
| 2099 | if(isesfrbit(q1)){ |
| 2100 | if(!isesfrbit(q2)&&!isreg(q2)){ |
| 2101 | move(f,&p->q1,0,0,ti,t); |
| 2102 | emit(f,"\tbcmp\t%s.0,",regnames[ti]); |
| 2103 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 2104 | continue; |
| 2105 | }else |
| 2106 | emit(f,"\textr\t#1\n"); |
| 2107 | }else if(isesfrbit(q2)){ |
| 2108 | if(!isreg(q1)){ |
| 2109 | move(f,&p->q2,0,0,ti,t); |
| 2110 | emit(f,"\tbcmp\t"); |
| 2111 | emit_obj(f,&p->q1,t); |
| 2112 | emit(f,",%s.0\n",regnames[ti]); |
| 2113 | continue; |
| 2114 | }else |
| 2115 | emit(f,"\textr\t#1\n"); |
| 2116 | } |
| 2117 | emit(f,"\tbcmp\t");emit_obj(f,&p->q1,t); |
| 2118 | emit(f,",");emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 2119 | }else{ |
| 2120 | bit_reg=0;bit_obj=&p->q1;lastcomp=BIT; |
| 2121 | eval_const(&p->q2.val,t); |
| 2122 | if(ISNULL()) |
| 2123 | bit_reverse=0; |
| 2124 | else |
| 2125 | bit_reverse=1; |
| 2126 | if(isesfrbit(q1)) emit(f,"\textr\t#1\n"); |
| 2127 | } |
| 2128 | continue; |
| 2129 | } |
| 2130 | if(c==TEST){ |
| 2131 | bit_reg=0;bit_obj=&p->q1;bit_reverse=0;lastcomp=BIT; |
| 2132 | if(isesfrbit(q1)) emit(f,"\textr\t#1\n"); |
| 2133 | continue; |
| 2134 | } |
| 2135 | if(c==AND||c==OR||c==XOR){ |
| 2136 | char *s; |
| 2137 | if(compare_objects(&p->z,&p->q2)){ |
| 2138 | struct obj m; |
| 2139 | m=p->q1;p->q1=p->q2;p->q2=m; |
| 2140 | } |
| 2141 | if(!compare_objects(&p->q1,&p->z)) |
| 2142 | move(f,&p->q1,0,&p->z,0,t); |
| 2143 | /*FIXME: const, esfr etc. */ |
| 2144 | if(c==AND) s="band"; |
| 2145 | else if(c==OR) s="bor"; |
| 2146 | else s="bxor"; |
| 2147 | if(isesfrbit(q2)&&!isesfrbit(z)&&!isreg(z)){ |
| 2148 | move(f,&p->q2,0,0,ti,t); |
| 2149 | emit(f,"\t%s\t",s); |
| 2150 | emit_obj(f,&p->z,t); |
| 2151 | emit(f,",%s.0\n",regnames[ti]); |
| 2152 | }else if(isesfrbit(z)&&!isesfrbit(q2)&&!isreg(q2)){ |
| 2153 | move(f,&p->q2,0,0,ti,t); |
| 2154 | emit(f,"\textr\t#1\n"); |
| 2155 | emit(f,"\t%s\t",s); |
| 2156 | emit_obj(f,&p->z,t); |
| 2157 | emit(f,",%s.0\n",regnames[ti]); |
| 2158 | }else{ |
| 2159 | if(isesfrbit(z)) emit(f,"\textr\t#1\n"); |
| 2160 | emit(f,"\t%s\t",s); |
| 2161 | emit_obj(f,&p->z,t);emit(f,","); |
| 2162 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 2163 | } |
| 2164 | continue; |
| 2165 | } |
| 2166 | if(c!=CONVERT) ierror(0); |
| 2167 | } |
| 2168 | |
| 2169 | if(c==TEST){ |
| 2170 | lastcomp=t; |
| 2171 | if(p->ext.flags==EXT_IC_CMPIA){ |
| 2172 | if(p->ext.offset<0) |
| 2173 | emit(f,"\tsub\t%s,#%ld\n",regnames[p->ext.r],-p->ext.offset); |
| 2174 | else |
| 2175 | emit(f,"\tadd\t%s,#%ld\n",regnames[p->ext.r],p->ext.offset); |
| 2176 | cc=0;continue; |
| 2177 | } |
| 2178 | if(p->ext.flags==EXT_IC_CMPIB){ |
| 2179 | if(p->ext.offset<0) |
| 2180 | emit(f,"\tcmpd%ld\t%s,#0\n",-p->ext.offset,regnames[p->ext.r]); |
| 2181 | else |
| 2182 | emit(f,"\tcmpi%ld\t%s,#0\n",p->ext.offset,regnames[p->ext.r]); |
| 2183 | cc=0;continue; |
| 2184 | } |
| 2185 | if(cc&&(cc_t&NU)==(t&NU)&&compare_objects(cc,&p->q1)){ |
| 2186 | continue; |
| 2187 | } |
| 2188 | p->code=c=COMPARE; |
| 2189 | gval.vmax=l2zm(0L); |
| 2190 | p->q2.flags=KONST; |
| 2191 | eval_const(&gval,MAXINT); |
| 2192 | insert_const(&p->q2.val,t); |
| 2193 | } |
| 2194 | if(c==COMPARE&&isconst(q1)){ |
| 2195 | struct IC *p2; |
| 2196 | o=p->q1;p->q1=p->q2;p->q2=o; |
| 2197 | p2=p->next; |
| 2198 | while(p2&&p2->code==FREEREG) p2=p2->next; |
| 2199 | if(!p2||p2->code<BEQ||p2->code>BGT) ierror(0); |
| 2200 | if(p2->code==BLT) p2->code=BGT; |
| 2201 | else if(p2->code==BGT) p2->code=BLT; |
| 2202 | else if(p2->code==BLE) p2->code=BGE; |
| 2203 | else if(p2->code==BGE) p2->code=BLE; |
| 2204 | } |
| 2205 | if(c==COMPARE&&p->ext.flags==EXT_IC_CMPIA){ |
| 2206 | long l; |
| 2207 | if(!isconst(q2)) ierror(0); |
| 2208 | eval_const(&p->q2.val,t); |
| 2209 | l=zm2l(vmax); |
| 2210 | if(p->ext.offset<0) |
| 2211 | emit(f,"\tcmpd%ld\t%s,#%ld\n",-p->ext.offset,regnames[p->ext.r],l-p->ext.offset); |
| 2212 | else |
| 2213 | emit(f,"\tcmpi%ld\t%s,#%ld\n",p->ext.offset,regnames[p->ext.r],l-p->ext.offset); |
| 2214 | cc=0;lastcomp=t;continue; |
| 2215 | } |
| 2216 | if(c==COMPARE&&p->ext.flags==EXT_IC_CMPIB){ |
| 2217 | pric2(stdout,p); |
| 2218 | if(p->ext.offset<0) |
| 2219 | emit(f,"\tcmpd%ld\t%s,",-p->ext.offset,regnames[p->ext.r]); |
| 2220 | else |
| 2221 | emit(f,"\tcmpi%ld\t%s,",p->ext.offset,regnames[p->ext.r]); |
| 2222 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 2223 | cc=0;lastcomp=t;continue; |
| 2224 | } |
| 2225 | if(c==COMPARE&&isconst(q2)){ |
| 2226 | eval_const(&p->q2.val,t); |
| 2227 | if(ISNULL()){ |
| 2228 | if(cc&&(cc_t&NU)==(t&NU)&&compare_objects(cc,&p->q1)){ |
| 2229 | lastcomp=t;continue; |
| 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if(c==SUBPFP){ |
| 2235 | p->code=c=SUB; |
| 2236 | if((p->typf2&NQ)==NPOINTER) p->typf=t=INT; |
| 2237 | else if((p->typf2&NQ)==HPOINTER) p->typf=t=LONG; |
| 2238 | else ierror(0); |
| 2239 | if((p->typf2&NQ)==NPOINTER){ |
| 2240 | cc=&p->z;cc_t=NPOINTER; |
| 2241 | }else{ |
| 2242 | cc=0; |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | if(c==ADDI2P||c==SUBIFP){ |
| 2247 | /*if(c==ADDI2P) p->code=c=ADD; else p->code=c=SUB;*/ |
| 2248 | if((p->typf2&NQ)!=HPOINTER){ |
| 2249 | p->typf=t=(UNSIGNED|SHORT); |
| 2250 | short_add=2; |
| 2251 | if(isreg(q2)&®_pair(p->q2.reg,&rp)){ |
| 2252 | /*FIXME:warning*/ |
| 2253 | p->q2.reg=rp.r1; |
| 2254 | } |
| 2255 | }else if(ISHWORD(t)){ |
| 2256 | p->typf=t=(UNSIGNED|LONG); |
| 2257 | short_add=1; |
| 2258 | } |
| 2259 | if((p->typf2&NQ)==NPOINTER){ |
| 2260 | cc=&p->z;cc_t=NPOINTER; |
| 2261 | }else{ |
| 2262 | cc=0; |
| 2263 | } |
| 2264 | } |
| 2265 | /* try to avoid z==q2 */ |
| 2266 | if((c==ADD||c==AND||c==OR||c==XOR||(c==ADDI2P&&!short_add))&&compare_objects(&p->q2,&p->z)){ |
| 2267 | o=p->q1;p->q1=p->q2;p->q2=o; |
| 2268 | } |
| 2269 | /* DREFOBJs nach q2, um evtl. op reg,[ri] zu nutzen */ |
| 2270 | if(c==ADD||c==MULT||c==OR||c==AND||c==XOR){ |
| 2271 | if(isreg(q2)&&scratchreg(p->q2.reg,p)){ |
| 2272 | o=p->q1;p->q1=p->q2;p->q2=o; |
| 2273 | } |
| 2274 | if((p->q1.flags&DREFOBJ)&&(!(p->q1.flags®)||p->q1.reg<=4)&&(!p->q1.am||p->q1.am->flags!=IMM_IND)){ |
| 2275 | if(!((p->q2.flags&DREFOBJ)&&(!p->q2.am||p->q2.am->flags!=IMM_IND))){ |
| 2276 | struct obj o; |
| 2277 | o=p->q1;p->q1=p->q2;p->q2=o; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | /*FIXME: ICs mit mehreren Typen*/ |
| 2282 | if(switch_IC(p)&&isreg(z)){ |
| 2283 | reg=p->z.reg; |
| 2284 | }else if(isreg(q1)&&(!ISLWORD(t)||reg_pair(p->q1.reg,&rp))&&(scratchreg(p->q1.reg,p)||(isreg(z)&&p->z.reg==p->q1.reg)||(!ISLWORD(t)&&c==COMPARE))){ |
| 2285 | reg=p->q1.reg; |
| 2286 | }else{ |
| 2287 | if(ISLWORD(ztyp(p))){ |
| 2288 | tmp1=ti;tmp2=ti2;ti2_used=1; |
| 2289 | BSET(regs_modified,ti); |
| 2290 | BSET(regs_modified,ti2); |
| 2291 | reg=MTMP1; |
| 2292 | }else{ |
| 2293 | if((t&NQ)==CHAR){ |
| 2294 | BSET(regs_modified,tp); |
| 2295 | reg=tp; |
| 2296 | }else{ |
| 2297 | BSET(regs_modified,ti); |
| 2298 | reg=ti; |
| 2299 | } |
| 2300 | } |
| 2301 | } |
| 2302 | /* op reg,mem/const */ |
| 2303 | if(issfr(z)&&(c==ADD||c==SUB||c==AND||c==OR||c==XOR||c==ADDI2P)&&(compare_objects(&p->q1,&p->z)||((c!=SUB&&c!=SUBIFP)&&compare_objects(&p->q2,&p->z)))){ |
| 2304 | char *s; |
| 2305 | if(!compare_objects(&p->q1,&p->z)){o=p->q1;p->q1=p->q2;p->q2=o;} |
| 2306 | if((p->q2.flags&(KONST|DREFOBJ))==KONST||((p->q2.flags&(VAR|DREFOBJ))==VAR&&ISSTATIC(p->q2.v))){ |
| 2307 | if(c>=OR&&c<=AND){ |
| 2308 | s=logicals[c-OR]; |
| 2309 | }else{ |
| 2310 | if(c==ADDI2P) |
| 2311 | s=arithmetics[ADD-LSHIFT]; |
| 2312 | else if(c==SUBIFP) |
| 2313 | s=arithmetics[SUB-LSHIFT]; |
| 2314 | else |
| 2315 | s=arithmetics[c-LSHIFT]; |
| 2316 | } |
| 2317 | if((p->q2.flags&VAR)&&ISFAR(p->q2.v)){ |
| 2318 | emit(f,"\texts\t#SEG ");isseg=1;emit_obj(f,&p->q2,t);emit(f,",#1\n"); |
| 2319 | } |
| 2320 | emit(f,"\t%s%s\t",s,(t&NQ)==CHAR?"b":""); |
| 2321 | emit_obj(f,&p->q1,t); |
| 2322 | emit(f,","); |
| 2323 | emit_obj(f,&p->q2,t); |
| 2324 | emit(f,"\n"); |
| 2325 | cc=&p->z;cc_t=t; |
| 2326 | continue; |
| 2327 | } |
| 2328 | } |
| 2329 | /* op mem,reg */ |
| 2330 | if((c==ADD||c==SUB||c==AND||c==OR||c==XOR||c==ADDI2P)&&(compare_objects(&p->q1,&p->z)||((c!=SUB&&c!=SUBIFP)&&compare_objects(&p->q2,&p->z)))&&(p->z.flags&(VAR|VARADR|DREFOBJ|REG))==VAR&&ISSTATIC(p->z.v)){ |
| 2331 | char *s; |
| 2332 | if(!compare_objects(&p->q1,&p->z)){o=p->q1;p->q1=p->q2;p->q2=o;} |
| 2333 | cc=&p->z;cc_t=t; |
| 2334 | if(isreg(q2)){ |
| 2335 | reg=p->q2.reg; |
| 2336 | }else if(issfr(q2)){ |
| 2337 | reg=0; |
| 2338 | }else if(isconst(q2)){ |
| 2339 | eval_const(&p->q2.val,t); |
| 2340 | if(ISLWORD(t)){ |
| 2341 | unsigned long ul; |
| 2342 | reg=MTMP1; |
| 2343 | if((c==ADD||c==SUB||c==ADDI2P||c==SUBIFP)&&zumeqto(vumax,ul2zum(1UL))){ |
| 2344 | if(c==ADD) c=SUB; |
| 2345 | else if(c==SUB) c=ADD; |
| 2346 | else if(c==ADDI2P) c=SUBIFP; |
| 2347 | else c=ADDI2P; |
| 2348 | tmp1=ONES;tmp2=ONES; |
| 2349 | }else{ |
| 2350 | ul=zum2ul(zumand(vumax,ul2zum(65535UL))); |
| 2351 | if(ul==0) tmp1=ZEROS; |
| 2352 | else if(ul==0xffff) tmp1=ONES; |
| 2353 | else{ |
| 2354 | BSET(regs_modified,ti); |
| 2355 | tmp1=ti; |
| 2356 | emit(f,"\tmov\t%s,#%lu\n",regnames[ti],ul); |
| 2357 | } |
| 2358 | ul=zum2ul(zumand(zumrshift(vumax,ul2zum(16UL)),ul2zum(65535UL))); |
| 2359 | if(ul==0) tmp2=ZEROS; |
| 2360 | else if(ul==0xffff) tmp2=ONES; |
| 2361 | else{ |
| 2362 | BSET(regs_modified,ti2); |
| 2363 | tmp2=ti2; |
| 2364 | emit(f,"\tmov\t%s,#%lu\n",regnames[ti2],ul); |
| 2365 | } |
| 2366 | } |
| 2367 | }else{ |
| 2368 | long l; |
| 2369 | if(c==OR&&(l=pof2(vumax))&&ISBADDR(p->z.v)){ |
| 2370 | emit(f,"\tbset\t");emit_obj(f,&p->z,t); |
| 2371 | emit(f,".%ld\n",l-1); |
| 2372 | cc=0;continue; |
| 2373 | }else if(c==AND&&(l=pof2(zumkompl(vumax)))&&ISBADDR(p->z.v)){ |
| 2374 | emit(f,"\tbclr\t");emit_obj(f,&p->z,t); |
| 2375 | emit(f,".%ld\n",l-1); |
| 2376 | cc=0;continue; |
| 2377 | }else if((c==ADD||c==SUB||c==ADDI2P||c==SUBIFP)&&zumeqto(vumax,ul2zum(1UL))){ |
| 2378 | if(c==ADD) c=SUB; |
| 2379 | else if(c==SUB) c=ADD; |
| 2380 | else if(c==ADDI2P) c=SUBIFP; |
| 2381 | else c=ADDI2P; |
| 2382 | reg=ONES; |
| 2383 | }else{ |
| 2384 | if(zumeqto(vumax,ul2zum(0UL))) reg=ZEROS; |
| 2385 | else if(zumeqto(vumax,ul2zum(0xffffUL))) reg=ONES; |
| 2386 | else{ |
| 2387 | BSET(regs_modified,tp); |
| 2388 | reg=tp; |
| 2389 | move(f,&p->q2,0,0,reg,t); |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | }else{ |
| 2394 | /*FIXME:ones/zeros nutzen*/ |
| 2395 | if(ISLWORD(t)){ |
| 2396 | tmp1=ti;tmp2=ti2; |
| 2397 | BSET(regs_modified,ti); |
| 2398 | BSET(regs_modified,ti2); |
| 2399 | reg=MTMP1; |
| 2400 | }else{ |
| 2401 | BSET(regs_modified,tp); |
| 2402 | reg=tp; |
| 2403 | } |
| 2404 | if((p->q2.flags&(DREFOBJ|REG))==DREFOBJ){ |
| 2405 | if(ISLWORD(p->q2.dtyp)){ |
| 2406 | tmp3=ti2;tmp4=ti; |
| 2407 | BSET(regs_modified,ti); |
| 2408 | BSET(regs_modified,ti2); |
| 2409 | p->q2.flags&=~DREFOBJ; |
| 2410 | move(f,&p->q2,0,0,MTMP2,HPOINTER); |
| 2411 | p->q2.flags=(REG|DREFOBJ);p->q2.reg=MTMP2; |
| 2412 | }else if(!(p->q2.flags&KONST)){ |
| 2413 | BSET(regs_modified,ti); |
| 2414 | move(f,&p->q2,0,0,ti,NPOINTER); |
| 2415 | p->q2.flags=REG;p->q2.reg=ti; |
| 2416 | } |
| 2417 | } |
| 2418 | move(f,&p->q2,0,0,reg,t); |
| 2419 | } |
| 2420 | if(c>=OR&&c<=AND){ |
| 2421 | s=logicals[c-OR]; |
| 2422 | }else{ |
| 2423 | if(c==ADDI2P) |
| 2424 | s=arithmetics[ADD-LSHIFT]; |
| 2425 | else if(c==SUBIFP) |
| 2426 | s=arithmetics[SUB-LSHIFT]; |
| 2427 | else |
| 2428 | s=arithmetics[c-LSHIFT]; |
| 2429 | } |
| 2430 | if(reg&®_pair(reg,&rp)){ |
| 2431 | if(ISFAR(p->z.v)){ |
| 2432 | emit(f,"\texts\t#SEG ");isseg=1;emit_obj(f,&p->z,t);emit(f,",#2\n"); |
| 2433 | } |
| 2434 | emit(f,"\t%s\t",s); |
| 2435 | emit_obj(f,&p->q1,t);emit(f,",%s\n",regnames[rp.r1]); |
| 2436 | s=longcmd(s); |
| 2437 | p->q1.val.vmax=zmadd(p->q1.val.vmax,l2zm(2L)); |
| 2438 | emit(f,"\t%s\t",s); |
| 2439 | emit_obj(f,&p->q1,t);emit(f,",%s\n",regnames[rp.r2]); |
| 2440 | p->q1.val.vmax=zmsub(p->q1.val.vmax,l2zm(2L)); |
| 2441 | }else{ |
| 2442 | if(ISFAR(p->z.v)){ |
| 2443 | emit(f,"\texts\t#SEG ");isseg=1;emit_obj(f,&p->z,t);emit(f,",#1\n"); |
| 2444 | } |
| 2445 | emit(f,"\t%s%s\t",s,(t&NQ)==CHAR?"b":""); |
| 2446 | emit_obj(f,&p->q1,t); |
| 2447 | if(reg) |
| 2448 | emit(f,",%s\n",((t&NQ)==CHAR&®<=MAXR)?bregnames[reg]:regnames[reg]); |
| 2449 | else{ |
| 2450 | emit(f,","); |
| 2451 | emit_obj(f,&p->q2,t); |
| 2452 | emit(f,"\n"); |
| 2453 | } |
| 2454 | } |
| 2455 | cc=&p->z;cc_t=t; |
| 2456 | continue; |
| 2457 | } |
| 2458 | if(p->ext.flags==EXT_IC_BTST){ |
| 2459 | if(isreg(q1)){bit_reg=regnames[p->q1.reg];continue;} |
| 2460 | if((p->q1.flags&(VAR|VARADR|DREFOBJ|REG))==VAR&&ISSTATIC(p->q1.v)&&ISBADDR(p->q1.v)){ |
| 2461 | bit_reg=0;bit_obj=&p->q1; |
| 2462 | continue; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | if((c==MULT||((c==DIV||c==MOD)&&(p->typf&UNSIGNED)))&&isconst(q2)){ |
| 2467 | long ln; |
| 2468 | eval_const(&p->q2.val,t); |
| 2469 | if(zmleq(l2zm(0L),vmax)&&zumleq(ul2zum(0UL),vumax)){ |
| 2470 | if(ln=pof2(vumax)){ |
| 2471 | if(c==MOD){ |
| 2472 | vmax=zmsub(vmax,l2zm(1L)); |
| 2473 | p->code=AND; |
| 2474 | }else{ |
| 2475 | vmax=l2zm(ln-1); |
| 2476 | if(c==DIV) p->code=RSHIFT; else p->code=LSHIFT; |
| 2477 | } |
| 2478 | c=p->code; |
| 2479 | gval.vmax=vmax; |
| 2480 | eval_const(&gval,MAXINT); |
| 2481 | if(c==AND){ |
| 2482 | insert_const(&p->q2.val,t); |
| 2483 | }else{ |
| 2484 | insert_const(&p->q2.val,INT); |
| 2485 | p->typf2=INT; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | if(ISLWORD(t)&&(c==MULT||c==DIV||c==MOD)){ |
| 2491 | if(lib_larith(f,p)) continue; |
| 2492 | } |
| 2493 | |
| 2494 | if(!p->q1.am&&(p->q1.flags&DREFOBJ)){ |
| 2495 | if(!(p->q1.flags&(REG))){ |
| 2496 | if(ISLWORD(p->q1.dtyp)){ |
| 2497 | tmp3=ti2;tmp4=ti;ti2_used=1; |
| 2498 | BSET(regs_modified,ti); |
| 2499 | BSET(regs_modified,ti2); |
| 2500 | p->q1.flags&=~DREFOBJ; |
| 2501 | move(f,&p->q1,0,0,MTMP2,FPOINTER); |
| 2502 | p->q1.reg=MTMP2; |
| 2503 | p->q1.flags=(REG|DREFOBJ); |
| 2504 | }else if(!(p->q1.flags&KONST)){ |
| 2505 | BSET(regs_modified,ti); |
| 2506 | p->q1.flags&=~DREFOBJ; |
| 2507 | move(f,&p->q1,0,0,ti,NPOINTER); |
| 2508 | p->q1.reg=ti; |
| 2509 | p->q1.flags=(REG|DREFOBJ); |
| 2510 | } |
| 2511 | } |
| 2512 | if(p->q2.flags){ |
| 2513 | move(f,&p->q1,0,0,reg,q2typ(p));/*FIXME*/ |
| 2514 | if((!isreg(q1)||p->q1.reg!=reg)&&c==COMPARE&&!ISLWORD(t)&&isconst(q2)){ |
| 2515 | /* avoid cmp, if not needed */ |
| 2516 | eval_const(&p->q2.val,t); |
| 2517 | if(ISNULL()){ |
| 2518 | lastcomp=t;continue; |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | }else{ |
| 2523 | if(p->q2.flags){ |
| 2524 | move(f,&p->q1,0,0,reg,q1typ(p));/*FIXME*/ |
| 2525 | if((!isreg(q1)||p->q1.reg!=reg)&&c==COMPARE&&!ISLWORD(t)&&isconst(q2)){ |
| 2526 | /* avoid cmp, if not needed */ |
| 2527 | eval_const(&p->q2.val,t); |
| 2528 | if(ISNULL()){ |
| 2529 | lastcomp=t;continue; |
| 2530 | } |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | if(p->ext.flags==EXT_IC_BTST){ bit_reg=regnames[reg];continue;} |
| 2535 | if(!p->q2.am&&(p->q2.flags&DREFOBJ)){ |
| 2536 | if(!(p->q2.flags&(REG))||p->q2.reg>4){ |
| 2537 | if(ISLWORD(p->q2.dtyp)){ |
| 2538 | if(ti2_used) tmp4=get_reg(f,p); else tmp4=ti2; |
| 2539 | tmp3=tp; |
| 2540 | BSET(regs_modified,tmp3); |
| 2541 | BSET(regs_modified,tmp4); |
| 2542 | p->q2.flags&=~DREFOBJ; |
| 2543 | move(f,&p->q2,0,0,MTMP2,FPOINTER); |
| 2544 | p->q2.reg=MTMP2; |
| 2545 | p->q2.flags=(REG|DREFOBJ); |
| 2546 | }else if(!(p->q2.flags&KONST)){ |
| 2547 | BSET(regs_modified,tp); |
| 2548 | p->q2.flags&=~DREFOBJ; |
| 2549 | move(f,&p->q2,0,0,tp,NPOINTER); |
| 2550 | p->q2.flags=(REG|DREFOBJ); |
| 2551 | p->q2.reg=tp; |
| 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | if(c==CONVERT&&!must_convert(p->typf,p->typf2,0)){ |
| 2556 | p->code=c=ASSIGN; |
| 2557 | p->q2.val.vmax=sizetab[p->typf&NQ]; |
| 2558 | } |
| 2559 | |
| 2560 | if(c==CONVERT){ |
| 2561 | int to=p->typf2&NU; |
| 2562 | if(to==INT) to=SHORT; |
| 2563 | if(to==(UNSIGNED|INT)||to==NPOINTER) to=(UNSIGNED|SHORT); |
| 2564 | if(to==FPOINTER||to==HPOINTER) to=(UNSIGNED|LONG); |
| 2565 | if((t&NU)==INT) t=SHORT; |
| 2566 | if((t&NU)==(UNSIGNED|INT)||(t&NU)==NPOINTER) t=(UNSIGNED|SHORT); |
| 2567 | if((t&NQ)==FPOINTER||(t&NQ)==HPOINTER) t=(UNSIGNED|LONG); |
| 2568 | if((to&NQ)<=LONG&&(t&NQ)<=LONG){ |
| 2569 | if((to&NQ)<=(t&NQ)){ |
| 2570 | if((to&NQ)==(t&NQ)) ierror(0); |
| 2571 | if((to&NQ)==BIT){ |
| 2572 | cc=0; |
| 2573 | if((p->q1.flags&(VAR|REG|DREFOBJ|VARADR))!=VAR||!ISSTATIC(p->q1.v)) ierror(0); |
| 2574 | if(reg_pair(reg,&rp)){ |
| 2575 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r1]); |
| 2576 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r2]); |
| 2577 | emit(f,"\tbmov\t%s.0,",regnames[rp.r1]); |
| 2578 | emit_obj(f,&p->q1,t);emit(f,"\n"); |
| 2579 | }else{ |
| 2580 | emit(f,"\tmov\t%s,#0\n",regnames[reg]); |
| 2581 | emit(f,"\tbmov\t%s.0,",regnames[reg]); |
| 2582 | } |
| 2583 | emit_obj(f,&p->q1,t);emit(f,"\n"); |
| 2584 | save_result(f,reg,p,t); |
| 2585 | continue; |
| 2586 | } |
| 2587 | if((p->q1.flags&(VAR|REG|VARADR))==VAR&&ISSTATIC(p->q1.v)&&ISFAR(p->q1.v)){ |
| 2588 | emit(f,"\texts\t#SEG ");isseg=1;emit_obj(f,&p->q1,t); |
| 2589 | emit(f,",#1\n"); |
| 2590 | } |
| 2591 | qp.SEG=0; |
| 2592 | if(!p->q1.am&&(p->q1.flags&DREFOBJ)&®_pair(p->q1.reg,&qp)) |
| 2593 | emit(f,"\texts\t%s,#1\n",regnames[qp.SEG]); |
| 2594 | if(p->q1.am&®_pair(p->q1.am->base,&qp)) |
| 2595 | emit(f,"\texts\t%s,#1\n",regnames[qp.SEG]); |
| 2596 | if(reg_pair(reg,&rp)){ |
| 2597 | cc=0; |
| 2598 | if((to&NQ)==CHAR){ |
| 2599 | if(isreg(q1)||((p->q1.flags&(VAR|VARADR|DREFOBJ))==VAR&&ISSTATIC(p->q1.v))){ |
| 2600 | emit(f,"\tmovb%c\t%s,",(to&UNSIGNED)?'z':'s',regnames[rp.r1]); |
| 2601 | if((to&NQ)==CHAR&&isreg(q1)) emit(f,"%s",bregnames[p->q1.reg]); |
| 2602 | else if(qp.SEG) emit(f,"[%s]",regnames[qp.SOF]); |
| 2603 | else emit_obj(f,&p->q1,to); |
| 2604 | emit(f,"\n"); |
| 2605 | }else{ |
| 2606 | BSET(regs_modified,tp); |
| 2607 | emit(f,"\tmovb\t%s,",bregnames[tp]); |
| 2608 | if(isreg(q1)) emit(f,"%s",bregnames[p->q1.reg]); |
| 2609 | else if(!p->q1.am&&qp.SEG) emit(f,"[%s]",regnames[qp.SOF]); |
| 2610 | else emit_obj(f,&p->q1,to); |
| 2611 | emit(f,"\n"); |
| 2612 | emit(f,"\tmovb%c\t%s,%s\n",(to&UNSIGNED)?'z':'s',regnames[rp.r1],bregnames[tp]); |
| 2613 | } |
| 2614 | }else{ |
| 2615 | emit(f,"\tmov\t%s,",regnames[rp.r1]); |
| 2616 | if(qp.SEG) emit(f,"[%s]",regnames[qp.SOF]); |
| 2617 | else emit_obj(f,&p->q1,to); |
| 2618 | emit(f,"\n"); |
| 2619 | } |
| 2620 | if(to&UNSIGNED) |
| 2621 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r2]); |
| 2622 | else |
| 2623 | emit(f,"\tmov\t%s,%s\n\tashr\t%s,#15\n",regnames[rp.r2],regnames[rp.r1],regnames[rp.r2]); |
| 2624 | |
| 2625 | }else{ |
| 2626 | cc=&p->z;cc_t=t; |
| 2627 | if(isreg(q1)||((p->q1.flags&(VAR|VARADR|DREFOBJ))==VAR&&ISSTATIC(p->q1.v))){ |
| 2628 | emit(f,"\tmovb%c\t%s,",(to&UNSIGNED)?'z':'s',regnames[reg]); |
| 2629 | if((to&NQ)==CHAR&&isreg(q1)) emit(f,"%s",bregnames[p->q1.reg]); |
| 2630 | else if(qp.SEG) emit(f,"[%s]",regnames[qp.SOF]); |
| 2631 | else emit_obj(f,&p->q1,to); |
| 2632 | emit(f,"\n"); |
| 2633 | }else{ |
| 2634 | BSET(regs_modified,tp); |
| 2635 | emit(f,"\tmovb\t%s,",bregnames[tp]); |
| 2636 | if(isreg(q1)) emit(f,"%s",bregnames[p->q1.reg]); |
| 2637 | else if(!p->q1.am&&qp.SEG) emit(f,"[%s]",regnames[qp.SOF]); |
| 2638 | else emit_obj(f,&p->q1,to); |
| 2639 | emit(f,"\n"); |
| 2640 | emit(f,"\tmovb%c\t%s,%s\n",(to&UNSIGNED)?'z':'s',regnames[reg],bregnames[tp]); |
| 2641 | } |
| 2642 | } |
| 2643 | save_result(f,reg,p,t); |
| 2644 | continue; |
| 2645 | }else{ |
| 2646 | if((t&NQ)==BIT){ |
| 2647 | if(!isreg(z)&&((p->z.flags&(VAR|REG|DREFOBJ|VARADR))!=VAR||!ISSTATIC(p->z.v))) ierror(0); |
| 2648 | if(isreg(q1)){ |
| 2649 | reg=p->q1.reg; |
| 2650 | }else{ |
| 2651 | if(ISLWORD(to)){ |
| 2652 | tmp1=ti;tmp2=ti2; |
| 2653 | BSET(regs_modified,ti); |
| 2654 | BSET(regs_modified,ti2); |
| 2655 | reg=MTMP1; |
| 2656 | } |
| 2657 | } |
| 2658 | move(f,&p->q1,0,0,reg,to); |
| 2659 | if(reg_pair(reg,&rp)){ |
| 2660 | if(isreg(q1)&&!scratchreg(reg,p)){ |
| 2661 | BSET(regs_modified,ti); |
| 2662 | emit(f,"\tmov\t%s,%s\n",regnames[ti],regnames[rp.r1]); |
| 2663 | emit(f,"\tor\t%s,%s\n",regnames[ti],regnames[rp.r2]); |
| 2664 | }else |
| 2665 | emit(f,"\tor\t%s,%s\n",regnames[rp.r1],regnames[rp.r2]); |
| 2666 | }else{ |
| 2667 | if(isreg(q1)) emit(f,"\tcmp%s\t%s,#0\n",(to&NQ)==CHAR?"b":"",(to&NQ)==CHAR?bregnames[reg]:regnames[reg]); |
| 2668 | } |
| 2669 | emit(f,"\tbmovn\t"); |
| 2670 | if(isesfrbit(z)) |
| 2671 | emit(f,"%s.0",regnames[ti]); |
| 2672 | else |
| 2673 | emit_obj(f,&p->z,t); |
| 2674 | emit(f,",PSW.3\n"); |
| 2675 | if(isesfrbit(z)){ |
| 2676 | emit(f,"\textr\t#1\n"); |
| 2677 | emit(f,"\tbmov\t"); |
| 2678 | emit_obj(f,&p->z,t); |
| 2679 | emit(f,",%s.0\n",regnames[ti]); |
| 2680 | } |
| 2681 | cc=0;continue; |
| 2682 | } |
| 2683 | cc=&p->z;cc_t=t; |
| 2684 | if(ISLWORD(to)){ |
| 2685 | if(isreg(q1)) {reg_pair(p->q1.reg,&rp);p->q1.reg=rp.r1;} |
| 2686 | to=SHORT; |
| 2687 | } |
| 2688 | if(isreg(q1)&®ok(p->q1.reg,t,0)) reg=p->q1.reg; |
| 2689 | if(isreg(z)) reg=p->z.reg; |
| 2690 | if(!regok(reg,t,0)){ |
| 2691 | reg=tp; |
| 2692 | BSET(regs_modified,tp); |
| 2693 | } |
| 2694 | move(f,&p->q1,0,0,reg,to); |
| 2695 | save_result(f,reg,p,t); |
| 2696 | } |
| 2697 | continue; |
| 2698 | } |
| 2699 | ierror(0); |
| 2700 | } |
| 2701 | if(c==MINUS||c==KOMPLEMENT){ |
| 2702 | move(f,&p->q1,0,0,reg,t); |
| 2703 | if(reg_pair(reg,&rp)){ |
| 2704 | emit(f,"\t%s\t%s\n",(c==MINUS?"neg":"cpl"),regnames[rp.r1]); |
| 2705 | if(c==MINUS) emit(f,"\taddc\t%s,#0\n",regnames[rp.r2]); |
| 2706 | emit(f,"\t%s\t%s\n",(c==MINUS?"neg":"cpl"),regnames[rp.r2]); |
| 2707 | cc=0; |
| 2708 | }else{ |
| 2709 | emit(f,"\t%s%s\t%s\n",(c==MINUS?"neg":"cpl"),x_t[t&NQ],regnames[reg]); |
| 2710 | cc=&p->z;cc_t=t; |
| 2711 | } |
| 2712 | save_result(f,reg,p,t); |
| 2713 | continue; |
| 2714 | } |
| 2715 | if(c==SETRETURN){ |
| 2716 | if(p->z.reg){ |
| 2717 | move(f,&p->q1,0,0,p->z.reg,t); |
| 2718 | BSET(regs_modified,p->z.reg); |
| 2719 | } |
| 2720 | cc=0; /* probably not needed */ |
| 2721 | continue; |
| 2722 | } |
| 2723 | if(c==GETRETURN){ |
| 2724 | if(p->q1.reg){ |
| 2725 | if(!isreg(z)||p->z.reg!=p->q1.reg){ cc=&p->z;cc_t=t;} |
| 2726 | save_result(f,p->q1.reg,p,t); |
| 2727 | } |
| 2728 | continue; |
| 2729 | } |
| 2730 | if(c==CALL){ |
| 2731 | int reg,jmp=0;long csstack=0,custack=0; |
| 2732 | cc=0; |
| 2733 | if((p->q1.flags&(VAR|DREFOBJ))==VAR&&!strcmp("__va_start",p->q1.v->identifier)){ |
| 2734 | long va_off=loff-usrstackoffset+pushedsize+zm2l(va_offset(v)); |
| 2735 | emit(f,"\tmov\t%s,%s\n",regnames[r4],regnames[sp]); |
| 2736 | if(va_off) |
| 2737 | emit(f,"\tadd\t%s,#%ld\n",regnames[r4],va_off); |
| 2738 | BSET(regs_modified,r4); |
| 2739 | if(LARGE||HUGE){ |
| 2740 | emit(f,"\tmov\t%s,#0\n",regnames[r5]); |
| 2741 | BSET(regs_modified,r5); |
| 2742 | } |
| 2743 | continue; |
| 2744 | } |
| 2745 | if(stack_valid){ |
| 2746 | int i; |
| 2747 | if(p->call_cnt<=0){ |
| 2748 | err_ic=p;if(f) error(320); |
| 2749 | stack_valid=0; |
| 2750 | } |
| 2751 | for(i=0;stack_valid&&i<p->call_cnt;i++){ |
| 2752 | if(p->call_list[i].v->fi&&(p->call_list[i].v->fi->flags&ALL_STACK)){ |
| 2753 | if(p->call_list[i].v->fi->stack1>custack) custack=p->call_list[i].v->fi->stack1; |
| 2754 | if(p->call_list[i].v->fi->stack2>csstack) csstack=p->call_list[i].v->fi->stack2; |
| 2755 | }else{ |
| 2756 | err_ic=p;if(f) error(317,p->call_list[i].v->identifier); |
| 2757 | stack_valid=0; |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | if(!calc_regs(p,f!=0)&&v->fi) v->fi->flags&=~ALL_REGS; |
| 2762 | if((p->q1.flags&VAR)&&p->q1.v->fi&&p->q1.v->fi->inline_asm){ |
| 2763 | emit_inline_asm(f,p->q1.v->fi->inline_asm); |
| 2764 | callee_push(custack,csstack); |
| 2765 | }else{ |
| 2766 | if(usrstackoffset==0&&!have_frame&&!(v->tattr&INTERRUPT)){ |
| 2767 | struct IC *p2; |
| 2768 | jmp=1; |
| 2769 | for(p2=p->next;p2;p2=p2->next){ |
| 2770 | if(p2->code!=FREEREG&&p2->code!=ALLOCREG&&p2->code!=LABEL&& |
| 2771 | (p2->code!=GETRETURN||(p2->z.flags&(REG|DREFOBJ))!=REG||p2->q1.reg!=p2->z.reg)&& |
| 2772 | (p2->code!=SETRETURN||(p2->q1.flags&(REG|DREFOBJ))!=REG||p2->q1.reg!=p2->z.reg)){ |
| 2773 | jmp=0;break; |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | if(p->q1.flags&DREFOBJ){ |
| 2778 | int clabel=++label; |
| 2779 | if(ISLWORD(p->q1.dtyp)){ |
| 2780 | int tmp; |
| 2781 | if(!ti2_used) tmp=ti2; else tmp=tp; |
| 2782 | BSET(regs_modified,tmp); |
| 2783 | emit(f,"\tmov\t%s,#SEG %s%d\n",regnames[tmp],labprefix,clabel); |
| 2784 | emit(f,"\tpush\t%s\n",regnames[tmp]); |
| 2785 | emit(f,"\tmov\t%s,#SOF %s%d\n",regnames[tmp],labprefix,clabel); |
| 2786 | emit(f,"\tpush\t%s\n",regnames[tmp]); |
| 2787 | push(0,4); |
| 2788 | } |
| 2789 | if(!(p->q1.flags®)) ierror(0); |
| 2790 | reg=p->q1.reg; |
| 2791 | p->q1.flags&=~DREFOBJ; |
| 2792 | if(!ISLWORD(p->q1.dtyp)){ |
| 2793 | emit(f,"\tcalli\tcc_uc,[%s]\n",regnames[reg]); |
| 2794 | push(0,2); |
| 2795 | callee_push(custack,csstack); |
| 2796 | pop(0,2); |
| 2797 | }else{ |
| 2798 | if(!reg_pair(reg,&rp)) ierror(0); |
| 2799 | emit(f,"\tpush\t%s\n",regnames[rp.r2]); |
| 2800 | emit(f,"\tpush\t%s\n",regnames[rp.r1]); |
| 2801 | push(0,4); |
| 2802 | callee_push(custack,csstack); |
| 2803 | emit(f,"\trets\n"); |
| 2804 | emit(f,"%s%d:\n",labprefix,clabel); |
| 2805 | pop(0,8); |
| 2806 | } |
| 2807 | }else{ |
| 2808 | if(jmp){ |
| 2809 | emit(f,"\t%s",jump); |
| 2810 | if(!need_return) ret=TASKING?"retv":""; |
| 2811 | callee_push(custack,csstack); |
| 2812 | }else{ |
| 2813 | emit(f,"\t%s\t",call); |
| 2814 | if(TINY) |
| 2815 | push(0,2); |
| 2816 | else |
| 2817 | push(0,4); |
| 2818 | callee_push(custack,csstack); |
| 2819 | if(TINY) |
| 2820 | pop(0,2); |
| 2821 | else |
| 2822 | pop(0,4); |
| 2823 | } |
| 2824 | emit_obj(f,&p->q1,t); |
| 2825 | emit(f,"\n"); |
| 2826 | } |
| 2827 | } |
| 2828 | if(!zmeqto(l2zm(0L),p->q2.val.vmax)){ |
| 2829 | notpopped+=zm2l(p->q2.val.vmax); |
| 2830 | dontpop-=zm2l(p->q2.val.vmax); |
| 2831 | if(!(g_flags[2]&USEDFLAG)&&usrstackoffset==-notpopped){ |
| 2832 | /* Entfernen der Parameter verzoegern */ |
| 2833 | }else{ |
| 2834 | emit(f,"\tadd\t%s,#%ld\n",regnames[sp],zm2l(p->q2.val.vmax)); |
| 2835 | pop(zm2l(p->q2.val.vmax),0); |
| 2836 | notpopped-=zm2l(p->q2.val.vmax);cc=0; |
| 2837 | } |
| 2838 | } |
| 2839 | continue; |
| 2840 | } |
| 2841 | if(c==ASSIGN||c==PUSH){ |
| 2842 | if(c==PUSH) dontpop+=zm2l(p->q2.val.vmax); |
| 2843 | if(!ISSCALAR(t)||!zmeqto(p->q2.val.vmax,sizetab[t&NQ])||!zmleq(p->q2.val.vmax,l2zm(4L))){ |
| 2844 | int alq,alz,rq,rz;unsigned long size; |
| 2845 | cc=0; |
| 2846 | size=zm2l(p->q2.val.vmax); |
| 2847 | /*FIXME: check for (s)huge */ |
| 2848 | if(malign[t&NQ]==1){ |
| 2849 | alq=alignment(&p->q1); |
| 2850 | alz=alignment(&p->z); |
| 2851 | }else{ |
| 2852 | alq=alz=0; |
| 2853 | } |
| 2854 | if(size==1||(size==2&&alq==0&&alz==0)){ |
| 2855 | if(c==PUSH&&zm2l(p->z.val.vmax)==1){ |
| 2856 | emit(f,"\tsub\t%s,#1\n",regnames[sp]); |
| 2857 | push(1,0);size=1; |
| 2858 | } |
| 2859 | BSET(regs_modified,tp); |
| 2860 | move(f,&p->q1,0,0,tp,size==1?CHAR:SHORT); |
| 2861 | if(c==PUSH){ |
| 2862 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[tp]); |
| 2863 | push(size,0); |
| 2864 | }else |
| 2865 | save_result(f,tp,p,size==1?CHAR:SHORT); |
| 2866 | continue; |
| 2867 | } |
| 2868 | if(size==4&&alq==0&&alz==0){ |
| 2869 | tmp1=ti;tmp2=ti2; |
| 2870 | BSET(regs_modified,ti); |
| 2871 | BSET(regs_modified,ti2); |
| 2872 | move(f,&p->q1,0,0,MTMP1,LONG); |
| 2873 | if(c==PUSH){ |
| 2874 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[tmp2]); |
| 2875 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[tmp1]); |
| 2876 | push(4,0); |
| 2877 | }else |
| 2878 | save_result(f,MTMP1,p,LONG); |
| 2879 | continue; |
| 2880 | } |
| 2881 | if(p->q1.am){ |
| 2882 | if(p->q1.am->flags!=IMM_IND) ierror(0); |
| 2883 | if(!scratchreg(p->q1.am->base,p)){ |
| 2884 | BSET(regs_modified,ti); |
| 2885 | emit(f,"\tmov\t%s,%s\n",regnames[ti],regnames[p->q1.am->base]); |
| 2886 | rq=ti; |
| 2887 | }else |
| 2888 | rq=p->q1.am->base; |
| 2889 | emit(f,"\tadd\t%s,#%ld",regnames[rq],p->q1.am->offset); |
| 2890 | if(p->q1.am->v){ |
| 2891 | if(p->q1.am->v->storage_class==EXTERN) |
| 2892 | emit(f,"+%s%s\n",idprefix,p->q1.am->v->identifier); |
| 2893 | |
| 2894 | else |
| 2895 | emit(f,"+%s%ld\n",labprefix,zm2l(p->q1.am->v->offset)); |
| 2896 | } |
| 2897 | emit(f,"\n"); |
| 2898 | }else if(p->q1.flags®){ |
| 2899 | if(scratchreg(p->q1.reg,p)){ |
| 2900 | rq=p->q1.reg; |
| 2901 | }else{ |
| 2902 | BSET(regs_modified,ti); |
| 2903 | emit(f,"\tmov\t%s,%s\n",regnames[ti],regnames[p->q1.reg]); |
| 2904 | rq=ti; |
| 2905 | } |
| 2906 | }else if((p->q1.flags&VAR)&&ISSTATIC(p->q1.v)){ |
| 2907 | int m=p->q1.flags; |
| 2908 | rq=ti; |
| 2909 | BSET(regs_modified,ti); |
| 2910 | if(p->q1.flags&DREFOBJ) |
| 2911 | p->q1.flags&=~DREFOBJ; |
| 2912 | else |
| 2913 | p->q1.flags|=VARADR; |
| 2914 | move(f,&p->q1,0,0,ti,NPOINTER); |
| 2915 | p->q1.flags&=~VARADR; |
| 2916 | }else{ |
| 2917 | BSET(regs_modified,ti); |
| 2918 | rq=ti; |
| 2919 | emit(f,"\tmov\t%s,%s\n",regnames[ti],regnames[sp]); |
| 2920 | if(voff(&p->q1)) |
| 2921 | emit(f,"\tadd\t%s,#%ld\n",regnames[ti],voff(&p->q1)); |
| 2922 | } |
| 2923 | if(!p->z.flags){ |
| 2924 | /* PUSH */ |
| 2925 | emit(f,"\tsub\t%s,#%lu\n",regnames[sp],size); |
| 2926 | emit(f,"\tmov\t%s,%s\n",regnames[ti2],regnames[sp]); |
| 2927 | rz=ti2; |
| 2928 | push(size,0); |
| 2929 | BSET(regs_modified,ti2); |
| 2930 | }else if(p->z.am){ |
| 2931 | if(p->z.am->flags!=IMM_IND) ierror(0); |
| 2932 | if(!scratchreg(p->z.am->base,p)){ |
| 2933 | BSET(regs_modified,ti2); |
| 2934 | emit(f,"\tmov\t%s,%s\n",regnames[ti2],regnames[p->z.am->base]); |
| 2935 | rz=ti2; |
| 2936 | }else |
| 2937 | rz=p->z.am->base; |
| 2938 | emit(f,"\tadd\t%s,#%ld",regnames[rz],p->z.am->offset); |
| 2939 | if(p->z.am->v){ |
| 2940 | if(p->z.am->v->storage_class==EXTERN) |
| 2941 | emit(f,"+%s%s",idprefix,p->z.am->v->identifier); |
| 2942 | else |
| 2943 | emit(f,"%+s%ld",labprefix,zm2l(p->z.am->v->offset)); |
| 2944 | |
| 2945 | } |
| 2946 | emit(f,"\n"); |
| 2947 | }else if(p->z.flags®){ |
| 2948 | if(scratchreg(p->z.reg,p)){ |
| 2949 | rz=p->z.reg; |
| 2950 | }else{ |
| 2951 | BSET(regs_modified,ti2); |
| 2952 | emit(f,"\tmov\t%s,%s\n",regnames[ti2],regnames[p->z.reg]); |
| 2953 | rz=ti2; |
| 2954 | } |
| 2955 | }else if(ISSTATIC(p->z.v)){ |
| 2956 | int m=p->z.flags; |
| 2957 | rz=ti2; |
| 2958 | BSET(regs_modified,ti2); |
| 2959 | if(p->z.flags&DREFOBJ) |
| 2960 | p->z.flags&=~DREFOBJ; |
| 2961 | else |
| 2962 | p->z.flags|=VARADR; |
| 2963 | move(f,&p->z,0,0,ti2,NPOINTER); |
| 2964 | p->z.flags=m; |
| 2965 | }else{ |
| 2966 | BSET(regs_modified,ti2); |
| 2967 | rz=ti2; |
| 2968 | emit(f,"\tmov\t%s,%s\n",regnames[ti2],regnames[sp]); |
| 2969 | if(voff(&p->z)) |
| 2970 | emit(f,"\tadd\t%s,#%ld\n",regnames[ti2],voff(&p->z)); |
| 2971 | } |
| 2972 | if(alq==0&&alz==0){ |
| 2973 | if(optspeed){ |
| 2974 | if((size/8)>1){ |
| 2975 | BSET(regs_modified,tp); |
| 2976 | emit(f,"\tmov\t%s,#%lu\n",regnames[tp],size/8); |
| 2977 | emit(f,"%s%d:\n",labprefix,++label); |
| 2978 | } |
| 2979 | if(size>=8){ |
| 2980 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 2981 | emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 2982 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 2983 | emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 2984 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 2985 | emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 2986 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 2987 | emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 2988 | } |
| 2989 | if((size/8)>1){ |
| 2990 | emit(f,"\tsub\t%s,#1\n",regnames[tp]); |
| 2991 | emit(f,"\tjmpr\tcc_nz,%s%d\n",labprefix,label); |
| 2992 | } |
| 2993 | size&=7; |
| 2994 | if(size>=4){ |
| 2995 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 2996 | emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 2997 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 2998 | if(size>4) emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 2999 | } |
| 3000 | size&=3; |
| 3001 | if(size>=2){ |
| 3002 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 3003 | if(size>2) emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 3004 | } |
| 3005 | if(size&1) |
| 3006 | if(size&1) emit(f,"\tmovb\t[%s],[%s]\n",regnames[rz],regnames[rq]); |
| 3007 | }else{ |
| 3008 | BSET(regs_modified,tp); |
| 3009 | emit(f,"\tmov\t%s,#%lu\n",regnames[tp],size/2); |
| 3010 | emit(f,"%s%d:\n",labprefix,++label); |
| 3011 | emit(f,"\tmov\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 3012 | emit(f,"\tadd\t%s,#2\n",regnames[rz]); |
| 3013 | emit(f,"\tsub\t%s,#1\n",regnames[tp]); |
| 3014 | emit(f,"\tjmpr\tcc_nz,%s%d\n",labprefix,label); |
| 3015 | if(size&1) emit(f,"\tmovb\t[%s],[%s]\n",regnames[rz],regnames[rq]); |
| 3016 | } |
| 3017 | }else{ |
| 3018 | BSET(regs_modified,tp); |
| 3019 | emit(f,"\tmov\t%s,#%lu\n",regnames[tp],size); |
| 3020 | emit(f,"%s%d:\n",labprefix,++label); |
| 3021 | emit(f,"\tmovb\t[%s],[%s+]\n",regnames[rz],regnames[rq]); |
| 3022 | emit(f,"\tadd\t%s,#1\n",regnames[rz]); |
| 3023 | emit(f,"\tsub\t%s,#1\n",regnames[tp]); |
| 3024 | emit(f,"\tjmpr\tcc_nz,%s%d\n",labprefix,label); |
| 3025 | } |
| 3026 | continue; |
| 3027 | } |
| 3028 | /* mov [rx],[ry] ; mov [rx+],[ry] ; mov [rx],[ry+] */ |
| 3029 | if(!ISLWORD(t)&&(p->q1.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&(p->z.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&(!p->z.am||p->z.am->flags==POST_INC)&&(!p->q1.am||p->q1.am->flags==POST_INC)&&!reg_pair(p->q1.reg,&rp)&&!reg_pair(p->z.reg,&rp)){ |
| 3030 | emit(f,"\tmov%s\t",(t&NQ)==CHAR?"b":""); |
| 3031 | emit_obj(f,&p->z,t);emit(f,","); |
| 3032 | emit_obj(f,&p->q1,t);emit(f,"\n"); |
| 3033 | cc=&p->z;cc_t=t;continue; |
| 3034 | } |
| 3035 | /* mov mem,[rx]/reg */ |
| 3036 | if(!ISLWORD(t)&&(issfr(q1)||(p->q1.flags&(REG|DREFOBJ))==(REG|DREFOBJ))&&(p->z.flags&(REG|DREFOBJ|VAR))==VAR&&!p->q1.am&&(p->z.v->storage_class==STATIC||p->z.v->storage_class==EXTERN)){ |
| 3037 | emit(f,"\tmov%s\t",(t&NQ)==CHAR?"b":""); |
| 3038 | emit_obj(f,&p->z,t);emit(f,","); |
| 3039 | emit_obj(f,&p->q1,t);emit(f,"\n"); |
| 3040 | cc=&p->z;cc_t=t;continue; |
| 3041 | } |
| 3042 | /* mov [rx]/reg,mem */ |
| 3043 | if(!ISLWORD(t)&&(issfr(z)||(p->z.flags&(REG|DREFOBJ))==(REG|DREFOBJ))&&(p->q1.flags&(REG|DREFOBJ|VAR))==VAR&&!p->z.am&&(p->q1.v->storage_class==STATIC||p->q1.v->storage_class==EXTERN)){ |
| 3044 | emit(f,"\tmov%s\t",(t&NQ)==CHAR?"b":""); |
| 3045 | emit_obj(f,&p->z,t);emit(f,","); |
| 3046 | emit_obj(f,&p->q1,t);emit(f,"\n"); |
| 3047 | cc=&p->z;cc_t=t;continue; |
| 3048 | } |
| 3049 | /* mov reg,op */ |
| 3050 | if(issfr(z)&&(p->q1.flags&KONST)){ |
| 3051 | emit(f,"\tmov%s\t",(t&NQ)==CHAR?"b":""); |
| 3052 | emit_obj(f,&p->z,t);emit(f,","); |
| 3053 | emit_obj(f,&p->q1,t);emit(f,"\n"); |
| 3054 | cc=&p->z;cc_t=t;continue; |
| 3055 | } |
| 3056 | |
| 3057 | if(isreg(q1)){ |
| 3058 | reg=p->q1.reg; |
| 3059 | }else if(isreg(z)){ |
| 3060 | reg=p->z.reg; |
| 3061 | }else{ |
| 3062 | BSET(regs_modified,ti); |
| 3063 | if(ISLWORD(t)){ |
| 3064 | BSET(regs_modified,ti2); |
| 3065 | tmp1=ti;tmp2=ti2; |
| 3066 | reg=MTMP1; |
| 3067 | }else |
| 3068 | reg=ti; |
| 3069 | } |
| 3070 | if(c==PUSH){ |
| 3071 | move(f,&p->q1,0,0,reg,t); |
| 3072 | if(reg_pair(reg,&rp)){ |
| 3073 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[rp.r2]); |
| 3074 | emit(f,"\tmov\t[-%s],%s\n",regnames[sp],regnames[rp.r1]); |
| 3075 | cc=0; |
| 3076 | }else{ |
| 3077 | emit(f,"\tmov%s\t[-%s],%s\n",x_t[t&NQ],regnames[sp],regnames[reg]); |
| 3078 | cc=&p->q1;cc_t=t; |
| 3079 | } |
| 3080 | push(zm2l(p->z.val.vmax),0); |
| 3081 | continue; |
| 3082 | } |
| 3083 | if(c==ASSIGN){ |
| 3084 | if((p->z.flags&(VAR|REG|DREFOBJ))==VAR&&ISSTATIC(p->z.v)&&isconst(q1)){ |
| 3085 | /*FIXME: long und ones */ |
| 3086 | eval_const(&p->q1.val,t); |
| 3087 | if(ISNULL()&&!ISLWORD(t)){ |
| 3088 | emit(f,"\tmov%s\t",(t&NQ)==CHAR?"b":"");emit_obj(f,&p->z,t); |
| 3089 | emit(f,",ZEROS\n"); |
| 3090 | cc=&p->z;cc_t=t;continue; |
| 3091 | } |
| 3092 | } |
| 3093 | if((t&NQ)==CHAR&&!regok(reg,CHAR,0)){ |
| 3094 | reg=tp; |
| 3095 | BSET(regs_modified,tp); |
| 3096 | } |
| 3097 | move(f,&p->q1,0,0,reg,t); |
| 3098 | save_result(f,reg,p,t); |
| 3099 | if(ISLWORD(t)){ |
| 3100 | cc=0; |
| 3101 | }else{ |
| 3102 | cc=&p->z;cc_t=t; |
| 3103 | } |
| 3104 | continue; |
| 3105 | } |
| 3106 | ierror(0); |
| 3107 | } |
| 3108 | if(c==ADDRESS){ |
| 3109 | if(reg_pair(reg,&rp)){ |
| 3110 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r1],regnames[sp]); |
| 3111 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r2]); |
| 3112 | if(voff(&p->q1)) |
| 3113 | emit(f,"\tadd\t%s,#%ld\n",regnames[rp.r1],voff(&p->q1)&0xffff); |
| 3114 | }else{ |
| 3115 | emit(f,"\tmov\t%s,%s\n",regnames[reg],regnames[sp]); |
| 3116 | if(voff(&p->q1)) emit(f,"\tadd\t%s,#%ld\n",regnames[reg],voff(&p->q1)); |
| 3117 | } |
| 3118 | save_result(f,reg,p,p->typf2); |
| 3119 | cc=0; |
| 3120 | continue; |
| 3121 | } |
| 3122 | |
| 3123 | if(c==MOD||c==DIV){ |
| 3124 | if(ISLWORD(t)) ierror(0); |
| 3125 | /*FIXME:suboptimal*/ |
| 3126 | emit(f,"\tmov\tMDL,%s\n",regnames[reg]); |
| 3127 | move(f,&p->q2,0,0,reg,t); |
| 3128 | emit(f,"\tdiv%s\t%s\n",(t&UNSIGNED)?"u":"",regnames[reg]); |
| 3129 | if(c==MOD) |
| 3130 | emit(f,"\tmov\t%s,MDH\n",regnames[reg]); |
| 3131 | else |
| 3132 | emit(f,"\tmov\t%s,MDL\n",regnames[reg]); |
| 3133 | save_result(f,reg,p,t); |
| 3134 | cc=&p->z;cc_t=t; |
| 3135 | continue; |
| 3136 | } |
| 3137 | if((c>=LSHIFT&&c<=MOD)||(c>=OR&&c<=AND)||c==COMPARE||c==ADDI2P||c==SUBIFP){ |
| 3138 | char *s; |
| 3139 | /*FIXME: nicht immer besser*/ |
| 3140 | /* rfi: better performed in LSHIFT code generation |
| 3141 | if(ISLWORD(t)&&c==LSHIFT&&isconst(q2)){ |
| 3142 | eval_const(&p->q2.val,t); |
| 3143 | if(zm2l(vmax)==1&&!p->q1.am){ |
| 3144 | p->code=c=ADD; |
| 3145 | p->q2=p->q1; |
| 3146 | } |
| 3147 | } |
| 3148 | */ |
| 3149 | if(ISLWORD(t)&&c==MULT) ierror(0); |
| 3150 | if(ISLWORD(t)&&(c==LSHIFT||c==RSHIFT)){ |
| 3151 | /* rfi: we now handle constant value greater than 16 in const shift generation |
| 3152 | if(isconst(q2)) |
| 3153 | eval_const(&p->q2.val,q2typ(p)); |
| 3154 | if(!isconst(q2)||zmleq(l2zm(16L),vmax)){ |
| 3155 | */ |
| 3156 | if(!isconst(q2)){ |
| 3157 | int cnt,lab1,lab2; |
| 3158 | if(isreg(q2)&&scratchreg(p->q2.reg,p)){ |
| 3159 | cnt=p->q2.reg; |
| 3160 | }else{ |
| 3161 | BSET(regs_modified,tp); |
| 3162 | cnt=tp; |
| 3163 | if(isreg(q2)&®_pair(p->q2.reg,&rp)) |
| 3164 | move(f,0,rp.r1,0,tp,INT); |
| 3165 | else |
| 3166 | move(f,&p->q2,0,0,tp,INT); |
| 3167 | } |
| 3168 | if(reg_pair(cnt,&rp)) |
| 3169 | cnt=rp.r1; |
| 3170 | /*move(f,&p->q1,0,0,reg,ztyp(p));*/ |
| 3171 | lab1=++label;lab2=++label; |
| 3172 | if(!reg_pair(reg,&rp)) ierror(0); |
| 3173 | if(c==LSHIFT){ |
| 3174 | emit(f,"\tjmpr\tcc_uc,%s%d\n",labprefix,lab2); |
| 3175 | emit(f,"%s%d:\n",labprefix,lab1); |
| 3176 | emit(f,"\tadd\t%s,%s\n",regnames[rp.r1],regnames[rp.r1]); |
| 3177 | emit(f,"\taddc\t%s,%s\n",regnames[rp.r2],regnames[rp.r2]); |
| 3178 | emit(f,"%s%d:\n",labprefix,lab2); |
| 3179 | /* rfi: fix off by one and handle negative shift value |
| 3180 | emit(f,"\tsub\t%s,#1\n",regnames[cnt]); |
| 3181 | emit(f,"\tjmpr\tcc_ne,%s%d\n",labprefix,lab1); |
| 3182 | */ |
| 3183 | emit(f,"\tcmpd1\t%s,#1\n",regnames[cnt]); |
| 3184 | emit(f,"\tjmpr\tcc_sge,%s%d\n",labprefix,lab1); |
| 3185 | }else{ |
| 3186 | emit(f,"\tjmpr\tcc_uc,%s%d\n",labprefix,lab2); |
| 3187 | emit(f,"%s%d:\n",labprefix,lab1); |
| 3188 | emit(f,"\tshr\t%s,#1\n",regnames[rp.r1]); |
| 3189 | emit(f,"\tbmov\t%s.15,%s.0\n",regnames[rp.r1],regnames[rp.r2]); |
| 3190 | /* rfi: use ashr for signed shift |
| 3191 | emit(f,"\tshr\t%s,#1\n",regnames[rp.r2]); |
| 3192 | */ |
| 3193 | emit(f,"\t%sshr\t%s,#1\n",(t&UNSIGNED)?"":"a",regnames[rp.r2]); |
| 3194 | emit(f,"%s%d:\n",labprefix,lab2); |
| 3195 | /* rfi: fix off by one and handle negative shift value |
| 3196 | emit(f,"\tsub\t%s,#1\n",regnames[cnt]); |
| 3197 | emit(f,"\tjmpr\tcc_ne,%s%d\n",labprefix,lab1); |
| 3198 | */ |
| 3199 | emit(f,"\tcmpd1\t%s,#1\n",regnames[cnt]); |
| 3200 | emit(f,"\tjmpr\tcc_sge,%s%d\n",labprefix,lab1); |
| 3201 | } |
| 3202 | save_result(f,reg,p,ztyp(p)); |
| 3203 | continue; |
| 3204 | } |
| 3205 | } |
| 3206 | if(c>=OR&&c<=AND){ |
| 3207 | s=logicals[c-OR]; |
| 3208 | }else{ |
| 3209 | if(c==ADDI2P) |
| 3210 | s=arithmetics[ADD-LSHIFT]; |
| 3211 | else if(c==SUBIFP) |
| 3212 | s=arithmetics[SUB-LSHIFT]; |
| 3213 | else |
| 3214 | s=arithmetics[c-LSHIFT]; |
| 3215 | } |
| 3216 | if(c==RSHIFT&&!(t&UNSIGNED)) s="ashr"; |
| 3217 | if(c==COMPARE){ |
| 3218 | lastcomp=t; |
| 3219 | if(ISLWORD(t)) s="sub"; else s="cmp"; |
| 3220 | } |
| 3221 | if((c==MULT&&!isreg(q2))){ |
| 3222 | BSET(regs_modified,tp); |
| 3223 | move(f,&p->q2,0,0,tp,t); |
| 3224 | p->q2.flags=REG; |
| 3225 | p->q2.reg=tp; |
| 3226 | } |
| 3227 | if((c==LSHIFT||c==RSHIFT)&&!isconst(q2)&&!isreg(q2)){ |
| 3228 | BSET(regs_modified,tp); |
| 3229 | move(f,&p->q2,0,0,tp,t); |
| 3230 | p->q2.flags=REG; |
| 3231 | p->q2.reg=tp; |
| 3232 | } |
| 3233 | if(isreg(q2)){ |
| 3234 | if(reg_pair(reg,&rp)){ |
| 3235 | if(!reg_pair(p->q2.reg,&qp)) qp.r1=p->q2.reg; |
| 3236 | emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r1],regnames[qp.r1]); |
| 3237 | s=longcmd(s); |
| 3238 | if(!short_add) emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r2],regnames[qp.r2]); |
| 3239 | }else{ |
| 3240 | emit(f,"\t%s%s\t%s,%s\n",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg],(t&NQ)==CHAR?bregnames[p->q2.reg]:regnames[p->q2.reg]); |
| 3241 | } |
| 3242 | }else if(p->q2.am){ |
| 3243 | if(reg_pair(p->q2.am->base,&rp)){ |
| 3244 | int seg=rp.SEG; |
| 3245 | if(!short_add&®_pair(reg,&rp)) |
| 3246 | emit(f,"\texts\t%s,#3\n",regnames[seg]); |
| 3247 | else |
| 3248 | emit(f,"\texts\t%s,#1\n",regnames[seg]); |
| 3249 | } |
| 3250 | if(reg_pair(reg,&rp)){ |
| 3251 | BSET(regs_modified,tp); |
| 3252 | emit(f,"\tmov\t%s,",regnames[tp]);emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3253 | emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r1],regnames[tp]); |
| 3254 | s=longcmd(s); |
| 3255 | if(!short_add){ |
| 3256 | p->q2.am->offset+=2; |
| 3257 | emit(f,"\tmov\t%s,",regnames[tp]);emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3258 | emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r2],regnames[tp]); |
| 3259 | p->q2.am->offset-=2; |
| 3260 | } |
| 3261 | }else{ |
| 3262 | if(p->q2.am->flags==POST_INC&&p->q2.am->base<=4){ |
| 3263 | emit(f,"\t%s%s\t%s,",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg]); |
| 3264 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3265 | }else{ |
| 3266 | BSET(regs_modified,tp); |
| 3267 | emit(f,"\tmov%s\t%s,",x_t[t&NQ],(t&NQ)==CHAR?bregnames[tp]:regnames[tp]); |
| 3268 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3269 | emit(f,"\t%s%s\t%s,%s\n",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg],(t&NQ)==CHAR?bregnames[tp]:regnames[tp]); |
| 3270 | } |
| 3271 | } |
| 3272 | }else if((p->q2.flags&(VAR|VARADR|REG))==VAR&&ISSTATIC(p->q2.v)){ |
| 3273 | if(ISFAR(p->q2.v)){ |
| 3274 | emit(f,"\texts\t#SEG ");isseg=1;emit_obj(f,&p->q2,t); |
| 3275 | emit(f,",#%d\n",(reg_pair(reg,&rp)?2:1)); |
| 3276 | } |
| 3277 | if(reg_pair(reg,&rp)){ |
| 3278 | emit(f,"\t%s\t%s,",s,regnames[rp.r1]); |
| 3279 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3280 | s=longcmd(s); |
| 3281 | if(!short_add){ |
| 3282 | emit(f,"\t%s\t%s,",s,regnames[rp.r2]); |
| 3283 | emit_obj(f,&p->q2,t);emit(f,"+2\n"); |
| 3284 | } |
| 3285 | }else{ |
| 3286 | emit(f,"\t%s%s\t%s,",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg]); |
| 3287 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3288 | } |
| 3289 | }else if(isconst(q2)){ |
| 3290 | if(ISFLOAT(t)) ierror(0); |
| 3291 | if(reg_pair(reg,&rp)){ |
| 3292 | long l; |
| 3293 | /* rfi: shift constant must be evaluated according to own type |
| 3294 | eval_const(&p->q2.val,t); |
| 3295 | */ |
| 3296 | eval_const(&p->q2.val,q2typ(p)); |
| 3297 | if(c==RSHIFT){ |
| 3298 | l=zm2l(vmax); |
| 3299 | /* rfi: new generation for constant right shift */ |
| 3300 | if( l < 0) { |
| 3301 | /* TODO: we should raise a warning */ |
| 3302 | /* Implementation dependent: negative shift is same as null shift */ |
| 3303 | /* avoid shift generation */ |
| 3304 | } else if( l > 31) { |
| 3305 | /* TODO: we should raise a warning */ |
| 3306 | /* Implementation dependent: same behaviour as non const shift */ |
| 3307 | if( t & UNSIGNED) |
| 3308 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r2]); |
| 3309 | else |
| 3310 | emit(f,"\t%s\t%s,#15\n",s,regnames[rp.r2]); |
| 3311 | |
| 3312 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r1],regnames[rp.r2]); |
| 3313 | } else if( l > 15) { |
| 3314 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r1],regnames[rp.r2]); |
| 3315 | if( t & UNSIGNED) |
| 3316 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r2]); |
| 3317 | else |
| 3318 | emit(f,"\t%s\t%s,#15\n",s,regnames[rp.r2]); |
| 3319 | |
| 3320 | l -= 16 ; |
| 3321 | if( l > 0) |
| 3322 | emit(f,"\t%s\t%s,#%ld\n",s,regnames[rp.r1],l); |
| 3323 | } else if( l == 0) { |
| 3324 | /* avoid shift generation */ |
| 3325 | } else if( l == 1) { |
| 3326 | emit(f,"\tshr\t%s,#1\n",regnames[rp.r1]); |
| 3327 | emit(f,"\tbmov\t%s.15,%s.0\n",regnames[rp.r1],regnames[rp.r2]); |
| 3328 | emit(f,"\t%s\t%s,#1\n",s,regnames[rp.r2]); |
| 3329 | } else { |
| 3330 | /* rfi: warning should be raised in corresponding case (see TODO above) |
| 3331 | if(l<1||l>15) ierror(0); |
| 3332 | */ |
| 3333 | BSET(regs_modified,tp); |
| 3334 | emit(f,"\tmov\t%s,%s\n",regnames[tp],regnames[rp.r2]); |
| 3335 | emit(f,"\tshr\t%s,#%ld\n",regnames[rp.r1],l); |
| 3336 | emit(f,"\t%s\t%s,#%ld\n",s,regnames[rp.r2],l); |
| 3337 | emit(f,"\tshl\t%s,#%ld\n",regnames[tp],16-l); |
| 3338 | emit(f,"\tor\t%s,%s\n",regnames[rp.r1],regnames[tp]); |
| 3339 | } |
| 3340 | }else if(c==LSHIFT){ |
| 3341 | l=zm2l(vmax); |
| 3342 | /* rfi: new generation for constant left shift */ |
| 3343 | if( l < 0) { |
| 3344 | /* TODO: we should raise a warning */ |
| 3345 | /* Implementation dependent: negative shift is same as null shift */ |
| 3346 | /* avoid shift generation */ |
| 3347 | } else if( l > 31) { |
| 3348 | /* TODO: we should raise a warning */ |
| 3349 | /* Implementation dependent: same behaviour as non const shift */ |
| 3350 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r1]); |
| 3351 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r2],regnames[rp.r1]); |
| 3352 | } else if( l > 15) { |
| 3353 | emit(f,"\tmov\t%s,%s\n",regnames[rp.r2],regnames[rp.r1]); |
| 3354 | emit(f,"\tmov\t%s,#0\n",regnames[rp.r1]); |
| 3355 | l -= 16 ; |
| 3356 | if( l > 0) |
| 3357 | emit(f,"\tshl\t%s,#%ld\n",regnames[rp.r2],l); |
| 3358 | } else if( l == 0) { |
| 3359 | /* avoid shift generation */ |
| 3360 | } else if( l == 1) { |
| 3361 | emit(f,"\tadd\t%s,%s\n",regnames[rp.r1],regnames[rp.r1]); |
| 3362 | emit(f,"\taddc\t%s,%s\n",regnames[rp.r2],regnames[rp.r2]); |
| 3363 | } else |
| 3364 | /* rfi: warning should be raised in corresponding case (see TODO above) |
| 3365 | if(l<1||l>15) ierror(0); |
| 3366 | */ |
| 3367 | if(l==2){ |
| 3368 | emit(f,"\tadd\t%s,%s\n",regnames[rp.r1],regnames[rp.r1]); |
| 3369 | emit(f,"\taddc\t%s,%s\n",regnames[rp.r2],regnames[rp.r2]); |
| 3370 | emit(f,"\tadd\t%s,%s\n",regnames[rp.r1],regnames[rp.r1]); |
| 3371 | emit(f,"\taddc\t%s,%s\n",regnames[rp.r2],regnames[rp.r2]); |
| 3372 | }else{ |
| 3373 | BSET(regs_modified,tp); |
| 3374 | emit(f,"\tmov\t%s,%s\n",regnames[tp],regnames[rp.r1]); |
| 3375 | emit(f,"\tshl\t%s,#%ld\n",regnames[rp.r1],l); |
| 3376 | emit(f,"\tshl\t%s,#%ld\n",regnames[rp.r2],l); |
| 3377 | emit(f,"\tshr\t%s,#%ld\n",regnames[tp],16-l); |
| 3378 | emit(f,"\tor\t%s,%s\n",regnames[rp.r2],regnames[tp]); |
| 3379 | } |
| 3380 | }else{ |
| 3381 | l=zm2l(zmand(vmax,l2zm(65535L))); |
| 3382 | emit(f,"\t%s\t%s,#%ld\n",s,regnames[rp.r1],l); |
| 3383 | l=zm2l(zmrshift(vmax,l2zm(16L))); |
| 3384 | s=longcmd(s); |
| 3385 | if(!short_add) emit(f,"\t%s\t%s,#%ld\n",s,regnames[rp.r2],l); |
| 3386 | } |
| 3387 | }else{ |
| 3388 | emit(f,"\t%s%s\t%s,",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg]); |
| 3389 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3390 | } |
| 3391 | }else if(p->q2.flags&VARADR){ |
| 3392 | p->q2.flags&=~VARADR; |
| 3393 | if(reg_pair(reg,&rp)){ |
| 3394 | emit(f,"\t%s\t%s,#SOF ",s,regnames[rp.r1]);emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3395 | s=longcmd(s); |
| 3396 | if(!short_add) |
| 3397 | emit(f,"\t%s\t%s,#SEG ",s,regnames[rp.r2]);isseg=1;emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3398 | }else{ |
| 3399 | emit(f,"\t%s\t%s,#",s,regnames[reg]);emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3400 | } |
| 3401 | p->q2.flags|=VARADR; |
| 3402 | }else if(p->q2.flags&DREFOBJ){ |
| 3403 | int seg=0,sof=0; |
| 3404 | /* if(!(p->q2.flags®)) ierror(0);*/ |
| 3405 | if(reg_pair(p->q2.reg,&rp)){ |
| 3406 | seg=rp.SEG;sof=rp.SOF; |
| 3407 | if(!short_add&®_pair(reg,&rp)){ |
| 3408 | emit(f,"\texts\t%s,#2\n",regnames[seg]); |
| 3409 | }else |
| 3410 | emit(f,"\texts\t%s,#1\n",regnames[seg]); |
| 3411 | } |
| 3412 | if(reg_pair(reg,&rp)){ |
| 3413 | if(scratchreg(p->q2.reg,p)){ |
| 3414 | emit(f,"\t%s\t%s,[%s+]\n",s,regnames[rp.r1],regnames[sof?sof:p->q2.reg]); |
| 3415 | s=longcmd(s); |
| 3416 | if(!short_add) emit(f,"\t%s\t%s,[%s]\n",s,regnames[rp.r2],regnames[sof?sof:p->q2.reg]); |
| 3417 | }else{ |
| 3418 | emit(f,"\t%s\t%s,[%s]\n",s,regnames[rp.r1],regnames[sof?sof:p->q2.reg]); |
| 3419 | s=longcmd(s); |
| 3420 | if(!short_add){ |
| 3421 | BSET(regs_modified,tp); |
| 3422 | emit(f,"\tmov\t%s,[%s+#2]\n",regnames[tp],regnames[sof?sof:p->q2.reg]); |
| 3423 | emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r2],regnames[tp]); |
| 3424 | } |
| 3425 | } |
| 3426 | }else{ |
| 3427 | emit(f,"\t%s%s\t%s,",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg]); |
| 3428 | emit_obj(f,&p->q2,t);emit(f,"\n"); |
| 3429 | } |
| 3430 | }else if((p->q2.flags&(VAR|DREFOBJ))==VAR&&(p->q2.v->storage_class==AUTO||p->q2.v->storage_class==REGISTER)){ |
| 3431 | long l=voff(&p->q2); |
| 3432 | if(reg_pair(reg,&rp)){ |
| 3433 | if(l){ |
| 3434 | BSET(regs_modified,tp); |
| 3435 | emit(f,"\tmov\t%s,[%s+#%ld]\n",regnames[tp],regnames[sp],l); |
| 3436 | emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r1],regnames[tp]); |
| 3437 | }else |
| 3438 | emit(f,"\t%s\t%s,[%s]\n",s,regnames[rp.r1],regnames[sp]); |
| 3439 | s=longcmd(s); |
| 3440 | if(!short_add){ |
| 3441 | BSET(regs_modified,tp); |
| 3442 | emit(f,"\tmov\t%s,[%s+#%ld]\n",regnames[tp],regnames[sp],l+2); |
| 3443 | emit(f,"\t%s\t%s,%s\n",s,regnames[rp.r2],regnames[tp]); |
| 3444 | } |
| 3445 | }else{ |
| 3446 | if(l){ |
| 3447 | BSET(regs_modified,tp); |
| 3448 | emit(f,"\tmov%s\t%s,[%s+#%ld]\n",x_t[t&NQ],(t&NQ)==CHAR?bregnames[tp]:regnames[tp],regnames[sp],l); |
| 3449 | emit(f,"\t%s%s\t%s,%s\n",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg],(t&NQ)==CHAR?bregnames[tp]:regnames[tp]); |
| 3450 | }else |
| 3451 | emit(f,"\t%s%s\t%s,[%s]\n",s,x_t[t&NQ],(t&NQ)==CHAR?bregnames[reg]:regnames[reg],regnames[sp]); |
| 3452 | } |
| 3453 | }else |
| 3454 | ierror(0); |
| 3455 | if(c==MULT) emit(f,"\tmov\t%s,MDL\n",regnames[reg]); |
| 3456 | /*FIXME:signed*/ |
| 3457 | if(short_add==1) emit(f,"\t%s\t%s,#0\n",s,regnames[rp.r2]); |
| 3458 | if(c!=COMPARE) save_result(f,reg,p,t); |
| 3459 | if(!ISLWORD(t)||((c==ADD||c==SUB||c==ADDI2P||c==SUBIFP)&&!short_add)){ |
| 3460 | cc=&p->z;cc_t=t; |
| 3461 | }else |
| 3462 | cc=0; |
| 3463 | continue; |
| 3464 | } |
| 3465 | ierror(0); |
| 3466 | } |
| 3467 | if(notpopped){ |
| 3468 | emit(f,"\tadd\t%s,#%ld\n",regnames[sp],notpopped); |
| 3469 | pop(notpopped,0);notpopped=0; |
| 3470 | } |
| 3471 | function_bottom(f,v,loff); |
| 3472 | } |
| 3473 | |
| 3474 | int shortcut(int c,int t) |
| 3475 | { |
| 3476 | if(c==COMPARE||c==AND||c==OR||c==XOR) return 1; |
| 3477 | if((t&NQ)!=BIT&&(c==ADD||c==SUB)) return 1; |
| 3478 | return 0; |
| 3479 | } |
| 3480 | |
| 3481 | void cleanup_cg(FILE *f) |
| 3482 | { |
| 3483 | struct fpconstlist *p; |
| 3484 | unsigned char *ip; |
| 3485 | if(f&&stack_check){ |
| 3486 | emit(f,"\t%s\t%s__stack_check\n",public,idprefix); |
| 3487 | } |
| 3488 | while(p=firstfpc){ |
| 3489 | if(f){ |
| 3490 | new_section(f,NDATA); |
| 3491 | emit(f,even); |
| 3492 | emit(f,"%s%d:\n\t%s\t",labprefix,p->label,TASKING?dct[SHORT]:vdct[SHORT]); |
| 3493 | ip=(unsigned char *)&p->val.vdouble; |
| 3494 | emit(f,"0x%02x%02x%02x%02x",ip[3],ip[2],ip[1],ip[0]); |
| 3495 | if((p->typ&NQ)==DOUBLE||(p->typ&NQ)==LDOUBLE){ |
| 3496 | emit(f,",0x%02x%02x%02x%02x",ip[7],ip[6],ip[5],ip[4]); |
| 3497 | } |
| 3498 | emit(f,"\n"); |
| 3499 | } |
| 3500 | firstfpc=p->next; |
| 3501 | free(p); |
| 3502 | } |
| 3503 | if(f&&TASKING) emit(f,"%s\tREGDEF R0-R15\n\tend\n",sec_end); |
| 3504 | } |
| 3505 | |
| 3506 | int reg_parm(struct reg_handle *p,struct Typ *t,int mode,struct Typ *fkt) |
| 3507 | { |
| 3508 | int f=t->flags&NQ; |
| 3509 | if(!ISSCALAR(f)) return 0; |
| 3510 | if(p->gpr>3||mode) return 0; |
| 3511 | if(f==LLONG||f==DOUBLE||f==LDOUBLE) |
| 3512 | return 0; |
| 3513 | else if(f==LONG||f==FPOINTER||f==HPOINTER){ |
| 3514 | if(p->gpr==0) {p->gpr=2;return 23;} |
| 3515 | if(p->gpr==1) {p->gpr=3;return 22;} |
| 3516 | if(p->gpr==2) {p->gpr=4;return 21;} |
| 3517 | return 0; |
| 3518 | }else |
| 3519 | return 13+p->gpr++; |
| 3520 | } |
| 3521 | |
| 3522 | void insert_const(union atyps *p,int t) |
| 3523 | /* Traegt Konstante in entprechendes Feld ein. */ |
| 3524 | { |
| 3525 | if(!p) ierror(0); |
| 3526 | /* *p = (union atyps) 0 ; /* rfi: clear unused bits */ |
| 3527 | t&=NU; |
| 3528 | if(t==BIT) {if(zmeqto(zc2zm(vchar),l2zm(0L))) p->vchar=zm2zc(l2zm(0L)); else p->vchar=zm2zc(l2zm(1L));return;} |
| 3529 | if(t==CHAR) {p->vchar=vchar;return;} |
| 3530 | if(t==SHORT) {p->vshort=vshort;return;} |
| 3531 | if(t==INT) {p->vint=vint;return;} |
| 3532 | if(t==LONG) {p->vlong=vlong;return;} |
| 3533 | if(t==LLONG) {p->vllong=vllong;return;} |
| 3534 | if(t==MAXINT) {p->vmax=vmax;return;} |
| 3535 | if(t==(UNSIGNED|BIT)) {if(zumeqto(zuc2zum(vuchar),ul2zum(0UL))) p->vuchar=zum2zuc(ul2zum(0UL)); else p->vuchar=zum2zuc(ul2zum(1UL));return;} |
| 3536 | if(t==(UNSIGNED|CHAR)) {p->vuchar=vuchar;return;} |
| 3537 | if(t==(UNSIGNED|SHORT)) {p->vushort=vushort;return;} |
| 3538 | if(t==(UNSIGNED|INT)) {p->vuint=vuint;return;} |
| 3539 | if(t==(UNSIGNED|LONG)) {p->vulong=vulong;return;} |
| 3540 | if(t==(UNSIGNED|LLONG)) {p->vullong=vullong;return;} |
| 3541 | if(t==(UNSIGNED|MAXINT)) {p->vumax=vumax;return;} |
| 3542 | if(t==FLOAT) {p->vfloat=vfloat;return;} |
| 3543 | if(t==DOUBLE) {p->vdouble=vdouble;return;} |
| 3544 | if(t==LDOUBLE) {p->vldouble=vldouble;return;} |
| 3545 | if(t==NPOINTER) {p->vuint=vuint;return;} |
| 3546 | if(t==FPOINTER||t==HPOINTER) {p->vulong=vulong;return;} |
| 3547 | } |
| 3548 | void eval_const(union atyps *p,int t) |
| 3549 | /* Weist bestimmten globalen Variablen Wert einer CEXPR zu. */ |
| 3550 | { |
| 3551 | int f=t&NQ; |
| 3552 | if(!p) ierror(0); |
| 3553 | if(f==MAXINT||(f>=BIT&&f<=LLONG)){ |
| 3554 | if(!(t&UNSIGNED)){ |
| 3555 | if(f==BIT){ |
| 3556 | if(zmeqto(zc2zm(p->vchar),l2zm(0L))) vmax=l2zm(0L); else vmax=l2zm(1L); |
| 3557 | }else if(f==CHAR) vmax=zc2zm(p->vchar); |
| 3558 | else if(f==SHORT)vmax=zs2zm(p->vshort); |
| 3559 | else if(f==INT) vmax=zi2zm(p->vint); |
| 3560 | else if(f==LONG) vmax=zl2zm(p->vlong); |
| 3561 | else if(f==LLONG) vmax=zll2zm(p->vllong); |
| 3562 | else if(f==MAXINT) vmax=p->vmax; |
| 3563 | else ierror(0); |
| 3564 | vumax=zm2zum(vmax); |
| 3565 | vldouble=zm2zld(vmax); |
| 3566 | }else{ |
| 3567 | if(f==BIT){ |
| 3568 | if(zumeqto(zuc2zum(p->vuchar),ul2zum(0UL))) vumax=ul2zum(0UL); else vumax=ul2zum(1UL); |
| 3569 | }else if(f==CHAR) vumax=zuc2zum(p->vuchar); |
| 3570 | else if(f==SHORT)vumax=zus2zum(p->vushort); |
| 3571 | else if(f==INT) vumax=zui2zum(p->vuint); |
| 3572 | else if(f==LONG) vumax=zul2zum(p->vulong); |
| 3573 | else if(f==LLONG) vumax=zull2zum(p->vullong); |
| 3574 | else if(f==MAXINT) vumax=p->vumax; |
| 3575 | else ierror(0); |
| 3576 | vmax=zum2zm(vumax); |
| 3577 | vldouble=zum2zld(vumax); |
| 3578 | } |
| 3579 | }else{ |
| 3580 | if(ISPOINTER(f)){ |
| 3581 | if(f==NPOINTER) |
| 3582 | vumax=zui2zum(p->vuint); |
| 3583 | else |
| 3584 | vumax=zul2zum(p->vulong); |
| 3585 | vmax=zum2zm(vumax);vldouble=zum2zld(vumax); |
| 3586 | }else{ |
| 3587 | if(f==FLOAT) vldouble=zf2zld(p->vfloat); |
| 3588 | else if(f==DOUBLE) vldouble=zd2zld(p->vdouble); |
| 3589 | else vldouble=p->vldouble; |
| 3590 | vmax=zld2zm(vldouble); |
| 3591 | vumax=zld2zum(vldouble); |
| 3592 | } |
| 3593 | } |
| 3594 | vfloat=zld2zf(vldouble); |
| 3595 | vdouble=zld2zd(vldouble); |
| 3596 | vuchar=zum2zuc(vumax); |
| 3597 | vushort=zum2zus(vumax); |
| 3598 | vuint=zum2zui(vumax); |
| 3599 | vulong=zum2zul(vumax); |
| 3600 | vullong=zum2zull(vumax); |
| 3601 | vchar=zm2zc(vmax); |
| 3602 | vshort=zm2zs(vmax); |
| 3603 | vint=zm2zi(vmax); |
| 3604 | vlong=zm2zl(vmax); |
| 3605 | vllong=zm2zll(vmax); |
| 3606 | } |
| 3607 | void printval(FILE *f,union atyps *p,int t) |
| 3608 | /* Gibt atyps aus. */ |
| 3609 | { |
| 3610 | t&=NU; |
| 3611 | if(t==BIT){fprintf(f,"B");vmax=zc2zm(p->vchar);fprintf(f,"%d",!zmeqto(vmax,l2zm(0L)));} |
| 3612 | if(t==(UNSIGNED|BIT)){fprintf(f,"UB");vumax=zuc2zum(p->vuchar);fprintf(f,"%d",!zumeqto(vmax,ul2zum(0UL)));} |
| 3613 | if(t==CHAR){fprintf(f,"C");vmax=zc2zm(p->vchar);printzm(f,vmax);} |
| 3614 | if(t==(UNSIGNED|CHAR)){fprintf(f,"UC");vumax=zuc2zum(p->vuchar);printzum(f,vumax);} |
| 3615 | if(t==SHORT){fprintf(f,"S");vmax=zs2zm(p->vshort);printzm(f,vmax);} |
| 3616 | if(t==(UNSIGNED|SHORT)){fprintf(f,"US");vumax=zus2zum(p->vushort);printzum(f,vumax);} |
| 3617 | if(t==FLOAT){fprintf(f,"F");vldouble=zf2zld(p->vfloat);printzld(f,vldouble);} |
| 3618 | if(t==DOUBLE){fprintf(f,"D");vldouble=zd2zld(p->vdouble);printzld(f,vldouble);} |
| 3619 | if(t==LDOUBLE){fprintf(f,"LD");printzld(f,p->vldouble);} |
| 3620 | if(t==INT){fprintf(f,"I");vmax=zi2zm(p->vint);printzm(f,vmax);} |
| 3621 | if(t==(UNSIGNED|INT)||t==NPOINTER){fprintf(f,"UI");vumax=zui2zum(p->vuint);printzum(f,vumax);} |
| 3622 | if(t==LONG){fprintf(f,"L");vmax=zl2zm(p->vlong);printzm(f,vmax);} |
| 3623 | if(t==(UNSIGNED|LONG)||t==FPOINTER||t==HPOINTER){fprintf(f,"UL");vumax=zul2zum(p->vulong);printzum(f,vumax);} |
| 3624 | if(t==LLONG){fprintf(f,"LL");vmax=zll2zm(p->vllong);printzm(f,vmax);} |
| 3625 | if(t==(UNSIGNED|LLONG)){fprintf(f,"ULL");vumax=zull2zum(p->vullong);printzum(f,vumax);} |
| 3626 | if(t==MAXINT) printzm(f,p->vmax); |
| 3627 | if(t==(UNSIGNED|MAXINT)) printzum(f,p->vumax); |
| 3628 | } |
| 3629 | void emitval(FILE *f,union atyps *p,int t) |
| 3630 | /* Gibt atyps aus. */ |
| 3631 | { |
| 3632 | t&=NU; |
| 3633 | if(t==BIT){vmax=zc2zm(p->vchar);emit(f,"%d",!zmeqto(vmax,l2zm(0L)));} |
| 3634 | if(t==(UNSIGNED|BIT)){vumax=zuc2zum(p->vuchar);emit(f,"%d",!zumeqto(vumax,ul2zum(0UL)));} |
| 3635 | if(t==CHAR){vmax=zc2zm(p->vchar);emitzm(f,vmax);} |
| 3636 | if(t==(UNSIGNED|CHAR)){vumax=zuc2zum(p->vuchar);emitzum(f,vumax);} |
| 3637 | if(t==SHORT){vmax=zs2zm(p->vshort);emitzm(f,vmax);} |
| 3638 | if(t==(UNSIGNED|SHORT)){vumax=zus2zum(p->vushort);emitzum(f,vumax);} |
| 3639 | if(t==FLOAT){vldouble=zf2zld(p->vfloat);emitzld(f,vldouble);} |
| 3640 | if(t==DOUBLE){vldouble=zd2zld(p->vdouble);emitzld(f,vldouble);} |
| 3641 | if(t==LDOUBLE){emitzld(f,p->vldouble);} |
| 3642 | if(t==INT){vmax=zi2zm(p->vint);emitzm(f,vmax);} |
| 3643 | if(t==(UNSIGNED|INT)||t==NPOINTER){vumax=zui2zum(p->vuint);emitzum(f,vumax);} |
| 3644 | if(t==LONG){vmax=zl2zm(p->vlong);emitzm(f,vmax);} |
| 3645 | if(t==(UNSIGNED|LONG)||t==FPOINTER||t==HPOINTER){vumax=zul2zum(p->vulong);emitzum(f,vumax);} |
| 3646 | if(t==LLONG){vmax=zll2zm(p->vllong);emitzm(f,vmax);} |
| 3647 | if(t==(UNSIGNED|LLONG)){vumax=zull2zum(p->vullong);emitzum(f,vumax);} |
| 3648 | if(t==MAXINT) emitzm(f,p->vmax); |
| 3649 | if(t==(UNSIGNED|MAXINT)) emitzum(f,p->vumax); |
| 3650 | } |
| 3651 | void conv_typ(struct Typ *p) |
| 3652 | /* Erzeugt extended types in einem Typ. */ |
| 3653 | { |
| 3654 | char *attr; |
| 3655 | while(p){ |
| 3656 | if(ISPOINTER(p->flags)){ |
| 3657 | p->flags=((p->flags&~NU)|POINTER_TYPE(p->next)); |
| 3658 | if(attr=p->next->attr){ |
| 3659 | if(strstr(attr,STR_NEAR)) |
| 3660 | p->flags=((p->flags&~NU)|NPOINTER); |
| 3661 | if(strstr(attr,STR_FAR)) |
| 3662 | p->flags=((p->flags&~NU)|FPOINTER); |
| 3663 | if(strstr(attr,STR_HUGE)) |
| 3664 | p->flags=((p->flags&~NU)|HPOINTER); |
| 3665 | } |
| 3666 | } |
| 3667 | if(ISINT(p->flags)&&(attr=p->attr)&&strstr(attr,"bit")) |
| 3668 | p->flags=((p->flags&~NU)|BIT); |
| 3669 | p=p->next; |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | void init_db(FILE *f) |
| 3674 | { |
| 3675 | } |
| 3676 | |
| 3677 | void cleanup_db(FILE *f) |
| 3678 | { |
| 3679 | } |
| 3680 | |
| 3681 | char *use_libcall(int c,int t,int t2) |
| 3682 | { |
| 3683 | static char fname[16]; |
| 3684 | char *ret=0,*tt; |
| 3685 | |
| 3686 | if(c==COMPARE){ |
| 3687 | if((t&NQ)==LLONG||ISFLOAT(t)){ |
| 3688 | sprintf(fname,"__cmp%s%s%ld",(t&UNSIGNED)?"u":"s",ISFLOAT(t)?"flt":"int",zm2l(sizetab[t&NQ])*8); |
| 3689 | ret=fname; |
| 3690 | } |
| 3691 | }else{ |
| 3692 | t&=NU; |
| 3693 | t2&=NU; |
| 3694 | if(t==LDOUBLE) t=DOUBLE; |
| 3695 | if(t2==LDOUBLE) t2=DOUBLE; |
| 3696 | if(c==CONVERT){ |
| 3697 | if(t==t2) return 0; |
| 3698 | if(t==FLOAT&&t2==DOUBLE) return "__flt64toflt32"; |
| 3699 | if(t==DOUBLE&&t2==FLOAT) return "__flt32toflt64"; |
| 3700 | |
| 3701 | if(ISFLOAT(t)){ |
| 3702 | sprintf(fname,"__%cint%ldtoflt%d",(t2&UNSIGNED)?'u':'s',zm2l(sizetab[t2&NQ])*8,(t==FLOAT)?32:64); |
| 3703 | ret=fname; |
| 3704 | } |
| 3705 | if(ISFLOAT(t2)&&(t&NU)==LLONG){ |
| 3706 | sprintf(fname,"__flt%dto%cint%ld",((t2&NU)==FLOAT)?32:64,(t&UNSIGNED)?'u':'s',zm2l(sizetab[t&NQ])*8); |
| 3707 | ret=fname; |
| 3708 | } |
| 3709 | } |
| 3710 | if((t&NQ)==LLONG||ISFLOAT(t)){ |
| 3711 | if((c>=LSHIFT&&c<=MOD)||(c>=OR&&c<=AND)||c==KOMPLEMENT||c==MINUS){ |
| 3712 | if(t==(UNSIGNED|LLONG)&&(c==DIV||c==MOD||c==RSHIFT)){ |
| 3713 | sprintf(fname,"__%suint64",ename[c]); |
| 3714 | ret=fname; |
| 3715 | }else if((t&NQ)==LLONG){ |
| 3716 | sprintf(fname,"__%sint64",ename[c]); |
| 3717 | ret=fname; |
| 3718 | }else{ |
| 3719 | sprintf(fname,"__%s%s%s%ld",ename[c],(t&UNSIGNED)?"u":"",ISFLOAT(t)?"flt":"int",zm2l(sizetab[t&NQ])*8); |
| 3720 | ret=fname; |
| 3721 | } |
| 3722 | } |
| 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | |
| 3727 | return ret; |
| 3728 | } |