1 /*-------------------------------------------------------------------------
3 SDCCicode.c - intermediate code generation etc.
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
27 /*-----------------------------------------------------------------*/
28 /* global variables */
30 set *iCodeChain = NULL ;
41 symbol *returnLabel ; /* function return label */
42 symbol *entryLabel ; /* function entry label */
43 /*-----------------------------------------------------------------*/
44 /* forward definition of some functions */
45 operand *geniCodeDivision (operand *,operand *);
46 operand *geniCodeAssign (operand *,operand *,int);
47 operand *geniCodeArray (operand *,operand *);
48 operand *geniCodeArray2Ptr (operand *);
49 operand *geniCodeRValue (operand *, bool );
50 operand *geniCodeDerefPtr (operand *);
52 #define PRINTFUNC(x) void x (FILE *of, iCode *ic, char *s)
53 /* forward definition of print functions */
54 PRINTFUNC(picGetValueAtAddr);
55 PRINTFUNC(picSetValueAtAddr);
57 PRINTFUNC(picGeneric);
58 PRINTFUNC(picGenericOne);
64 PRINTFUNC(picJumpTable);
66 PRINTFUNC(picReceive);
68 iCodeTable codeTable[] = {
69 { '!' , "not", picGenericOne , NULL },
70 { '~' , "~" , picGenericOne , NULL },
71 { RRC , "rrc", picGenericOne , NULL },
72 { RLC , "rlc", picGenericOne , NULL },
73 { GETHBIT ,"ghbit", picGenericOne , NULL },
74 { UNARYMINUS , "-" , picGenericOne , NULL },
75 { IPUSH , "push",picGenericOne , NULL },
76 { IPOP , "pop", picGenericOne , NULL },
77 { CALL , "call",picGenericOne , NULL },
78 { PCALL , "pcall",picGenericOne , NULL },
79 { FUNCTION , "proc", picGenericOne , NULL },
80 { ENDFUNCTION ,"eproc", picGenericOne , NULL },
81 { RETURN , "ret", picGenericOne , NULL },
82 { '+' , "+" , picGeneric , NULL },
83 { '-' , "-" , picGeneric , NULL },
84 { '*' , "*" , picGeneric , NULL },
85 { '/' , "/" , picGeneric , NULL },
86 { '%' , "%" , picGeneric , NULL },
87 { '>' , ">" , picGeneric , NULL },
88 { '<' , "<" , picGeneric , NULL },
89 { LE_OP , "<=" , picGeneric , NULL },
90 { GE_OP , ">=" , picGeneric , NULL },
91 { EQ_OP , "==" , picGeneric , NULL },
92 { NE_OP , "!=" , picGeneric , NULL },
93 { AND_OP , "&&" , picGeneric , NULL },
94 { OR_OP , "||" , picGeneric , NULL },
95 { '^' , "^" , picGeneric , NULL },
96 { '|' , "|" , picGeneric , NULL },
97 { BITWISEAND , "&" , picGeneric , NULL },
98 { LEFT_OP , "<<" , picGeneric , NULL },
99 { RIGHT_OP , ">>" , picGeneric , NULL },
100 { GET_VALUE_AT_ADDRESS, "@" , picGetValueAtAddr, NULL },
101 { ADDRESS_OF , "&" , picAddrOf , NULL },
102 { CAST , "<>" , picCast , NULL },
103 { '=' , ":=" , picAssign , NULL },
104 { LABEL , "" , picLabel , NULL },
105 { GOTO , "" , picGoto , NULL },
106 { JUMPTABLE ,"jtab" , picJumpTable , NULL },
107 { IFX , "if" , picIfx , NULL },
108 { INLINEASM , "" , picInline , NULL },
109 { RECEIVE , "recv", picReceive , NULL },
110 { SEND , "send", picGenericOne , NULL }
114 /*-----------------------------------------------------------------*/
115 /* operandName - returns the name of the operand */
116 /*-----------------------------------------------------------------*/
117 int printOperand (operand *op, FILE *file)
132 opetype = getSpec (operandType(op));
133 if (SPEC_NOUN(opetype) == V_FLOAT)
134 fprintf (file,"%g {", SPEC_CVAL(opetype).v_float);
136 fprintf (file,"0x%x {",(int) floatFromVal(op->operand.valOperand));
137 printTypeChain(operandType(op),file);
144 fprintf (file,"%s [k%d lr%d:%d so:%d]{ ia%d re%d rm%d}",/*{ar%d rm%d ru%d p%d a%d u%d i%d au%d k%d ks%d}" ,*/
145 (OP_SYMBOL(op)->rname[0] ? OP_SYMBOL(op)->rname : OP_SYMBOL(op)->name),
147 OP_LIVEFROM(op),OP_LIVETO(op),
148 OP_SYMBOL(op)->stack,
149 op->isaddr, OP_SYMBOL(op)->isreqv,OP_SYMBOL(op)->remat
152 fprintf(file,"{"); printTypeChain(operandType(op),file);
153 if (SPIL_LOC(op) && IS_ITEMP(op))
154 fprintf(file,"}{ sir@ %s",SPIL_LOC(op)->rname);
159 /* if assigned to registers */
160 if (OP_SYMBOL(op)->nRegs) {
161 if (OP_SYMBOL(op)->isspilt) {
162 if (!OP_SYMBOL(op)->remat)
163 if (OP_SYMBOL(op)->usl.spillLoc)
164 fprintf(file,"[%s]",(OP_SYMBOL(op)->usl.spillLoc->rname[0] ?
165 OP_SYMBOL(op)->usl.spillLoc->rname :
166 OP_SYMBOL(op)->usl.spillLoc->name));
168 fprintf(file,"[err]");
170 fprintf(file,"[remat]");
175 for(i=0;i<OP_SYMBOL(op)->nRegs;i++)
176 fprintf(file,"%s ", port->getRegName(OP_SYMBOL(op)->regs[i]));
181 fprintf(file,"%s",(OP_SYMBOL(op)->rname[0] ?
182 OP_SYMBOL(op)->rname : OP_SYMBOL(op)->name));
183 /* if assigned to registers */
184 if (OP_SYMBOL(op)->nRegs && !OP_SYMBOL(op)->isspilt) {
187 for(i=0;i<OP_SYMBOL(op)->nRegs;i++)
188 fprintf(file,"%s ",(OP_SYMBOL(op)->regs[i] ?
189 OP_SYMBOL(op)->regs[i]->name :
198 printTypeChain(op->operand.typeOperand,file);
209 /*-----------------------------------------------------------------*/
210 /* print functions */
211 /*-----------------------------------------------------------------*/
212 PRINTFUNC(picGetValueAtAddr)
215 printOperand (IC_RESULT(ic),of);
218 printOperand (IC_LEFT(ic), of);
224 PRINTFUNC(picSetValueAtAddr)
228 printOperand(IC_LEFT(ic),of);
230 printOperand(IC_RIGHT(ic),of);
237 printOperand(IC_RESULT(ic),of);
238 if (IS_ITEMP(IC_LEFT(ic)))
242 printOperand(IC_LEFT(ic),of);
244 if (IS_ITEMP(IC_LEFT(ic)))
245 fprintf(of," offsetAdd ");
248 printOperand(IC_RIGHT(ic),of);
250 if (IS_ITEMP(IC_LEFT(ic)))
256 PRINTFUNC(picJumpTable)
261 fprintf(of,"%s\t",s);
262 printOperand(IC_JTCOND(ic),of);
264 for ( sym = setFirstItem(IC_JTLABELS(ic)); sym;
265 sym = setNextItem(IC_JTLABELS(ic)))
266 fprintf(of,"\t\t\t%s\n",sym->name);
269 PRINTFUNC(picGeneric)
272 printOperand(IC_RESULT(ic),of);
274 printOperand(IC_LEFT(ic),of);
275 fprintf(of," %s ",s);
276 printOperand(IC_RIGHT(ic),of);
280 PRINTFUNC(picGenericOne)
283 if ( IC_RESULT(ic) ) {
284 printOperand(IC_RESULT(ic),of);
289 fprintf (of,"%s ",s);
290 printOperand(IC_LEFT(ic),of);
293 if (! IC_RESULT(ic) && !IC_LEFT(ic))
302 printOperand(IC_RESULT(ic),of);
304 printOperand(IC_LEFT(ic),of);
305 printOperand(IC_RIGHT(ic),of);
314 if (IC_RESULT(ic)->isaddr && IS_ITEMP(IC_RESULT(ic)))
317 printOperand(IC_RESULT(ic),of);
319 if (IC_RESULT(ic)->isaddr && IS_ITEMP(IC_RESULT(ic)))
322 fprintf(of," %s ", s);
323 printOperand (IC_RIGHT(ic),of);
330 fprintf(of," %s($%d) :\n",IC_LABEL(ic)->name,IC_LABEL(ic)->key);
336 fprintf (of," goto %s($%d)\n", IC_LABEL(ic)->name,IC_LABEL(ic)->key);
343 printOperand(IC_COND(ic),of);
346 fprintf (of," == 0 goto %s($%d)\n",IC_FALSE(ic)->name,IC_FALSE(ic)->key);
348 fprintf (of," != 0 goto %s($%d)\n",IC_TRUE(ic)->name,IC_TRUE(ic)->key);
350 fprintf (of,"\tzzgoto %s\n",IC_FALSE(ic)->name);
356 fprintf(of,"%s",IC_INLINE(ic));
359 PRINTFUNC(picReceive)
361 printOperand(IC_RESULT(ic),of);
362 fprintf(of," = %s ",s);
363 printOperand(IC_LEFT(ic),of);
367 /*-----------------------------------------------------------------*/
368 /* piCode - prints one iCode */
369 /*-----------------------------------------------------------------*/
370 int piCode (void *item, FILE *of)
378 icTab = getTableEntry(ic->op) ;
379 fprintf(stdout,"%s(%d:%d:%d:%d:%d)\t",
380 ic->filename,ic->lineno,
381 ic->seq,ic->key,ic->depth,ic->supportRtn);
382 icTab->iCodePrint(of,ic,icTab->printName);
386 /*-----------------------------------------------------------------*/
387 /* printiCChain - prints intermediate code for humans */
388 /*-----------------------------------------------------------------*/
389 void printiCChain (iCode *icChain, FILE *of)
396 for ( loop = icChain ; loop ; loop = loop->next ) {
397 if ((icTab = getTableEntry (loop->op ))) {
398 fprintf(of,"%s(%d:%d:%d:%d:%d)\t",
399 loop->filename,loop->lineno,
400 loop->seq,loop->key,loop->depth,loop->supportRtn);
402 icTab->iCodePrint (of,loop,icTab->printName);
408 /*-----------------------------------------------------------------*/
409 /* newOperand - allocate, init & return a new iCode */
410 /*-----------------------------------------------------------------*/
411 operand *newOperand ()
415 ALLOC(op,sizeof(operand));
421 /*-----------------------------------------------------------------*/
422 /* newiCode - create and return a new iCode entry initialised */
423 /*-----------------------------------------------------------------*/
424 iCode *newiCode (int op, operand *left, operand *right)
428 ALLOC(ic,sizeof(iCode));
430 ic->lineno = lineno ;
431 ic->filename= filename ;
433 ic->level = scopeLevel;
435 ic->key= iCodeKey++ ;
442 /*-----------------------------------------------------------------*/
443 /* newiCode for conditional statements */
444 /*-----------------------------------------------------------------*/
445 iCode *newiCodeCondition (operand *condition,
451 ic = newiCode(IFX,NULL,NULL);
452 IC_COND(ic) = condition ;
453 IC_TRUE(ic) = trueLabel ;
454 IC_FALSE(ic) = falseLabel;
458 /*-----------------------------------------------------------------*/
459 /* newiCodeLabelGoto - unconditional goto statement| label stmnt */
460 /*-----------------------------------------------------------------*/
461 iCode *newiCodeLabelGoto (int op, symbol *label)
465 ic = newiCode(op,NULL,NULL);
467 ic->argLabel.label = label ;
469 IC_RIGHT(ic) = NULL ;
470 IC_RESULT(ic) = NULL ;
474 /*-----------------------------------------------------------------*/
475 /* newiTemp - allocate & return a newItemp Variable */
476 /*-----------------------------------------------------------------*/
477 symbol *newiTemp (char *s)
482 sprintf(buffer,"%s",s);
484 sprintf (buffer,"iTemp%d",iTempNum++);
485 itmp = newSymbol (buffer,1);
486 strcpy(itmp->rname,itmp->name);
492 /*-----------------------------------------------------------------*/
493 /* newiTempLabel - creates a temp variable label */
494 /*-----------------------------------------------------------------*/
495 symbol *newiTempLabel (char *s)
499 /* check if this alredy exists */
500 if (s && (itmplbl = findSym(LabelTab, NULL, s)))
504 itmplbl = newSymbol(s,1);
506 sprintf(buffer,"iTempLbl%d",iTempLblNum++);
507 itmplbl = newSymbol(buffer,1);
512 itmplbl->key = labelKey++ ;
513 addSym (LabelTab, itmplbl, itmplbl->name,0,0);
517 /*-----------------------------------------------------------------*/
518 /* newiTempPreheaderLabel - creates a new preheader label */
519 /*-----------------------------------------------------------------*/
520 symbol *newiTempPreheaderLabel()
524 sprintf(buffer,"preHeaderLbl%d",iTempLblNum++);
525 itmplbl = newSymbol(buffer,1);
529 itmplbl->key = labelKey++ ;
530 addSym (LabelTab, itmplbl, itmplbl->name,0,0);
535 /*-----------------------------------------------------------------*/
536 /* initiCode - initialises some iCode related stuff */
537 /*-----------------------------------------------------------------*/
543 /*-----------------------------------------------------------------*/
544 /* copyiCode - make a copy of the iCode given */
545 /*-----------------------------------------------------------------*/
546 iCode *copyiCode (iCode *ic)
548 iCode *nic = newiCode(ic->op,NULL,NULL);
550 nic->lineno = ic->lineno ;
551 nic->filename= ic->filename ;
552 nic->block = ic->block;
553 nic->level = ic->level;
555 /* deal with the special cases first */
558 IC_COND(nic) = operandFromOperand(IC_COND(ic));
559 IC_TRUE(nic) = IC_TRUE(ic);
560 IC_FALSE(nic)= IC_FALSE(ic);
564 IC_JTCOND(nic) = operandFromOperand(IC_JTCOND(ic));
565 IC_JTLABELS(nic) = IC_JTLABELS(ic);
570 IC_RESULT(nic) = operandFromOperand(IC_RESULT(ic));
571 IC_LEFT(nic) = operandFromOperand(IC_LEFT(ic));
572 IC_ARGS(nic) = IC_ARGS(ic);
576 IC_INLINE(nic) = IC_INLINE(ic);
580 IC_RESULT(nic) = operandFromOperand(IC_RESULT(ic));
581 IC_LEFT(nic) = operandFromOperand(IC_LEFT(ic));
582 IC_RIGHT(nic)= operandFromOperand(IC_RIGHT(ic));
588 /*-----------------------------------------------------------------*/
589 /* getTableEntry - gets the table entry for the given operator */
590 /*-----------------------------------------------------------------*/
591 iCodeTable *getTableEntry (int oper )
595 for ( i = 0 ; i < (sizeof(codeTable)/sizeof(iCodeTable)); i++ )
596 if (oper == codeTable[i].icode)
597 return &codeTable[i] ;
602 /*-----------------------------------------------------------------*/
603 /* newiTempOperand - new intermediate temp operand */
604 /*-----------------------------------------------------------------*/
605 operand *newiTempOperand (link *type, char throwType)
608 operand *op = newOperand();
612 itmp = newiTemp(NULL);
614 etype = getSpec(type);
616 if (IS_LITERAL(etype) )
619 /* copy the type information */
621 itmp->etype = getSpec (itmp->type = (throwType ? type :
622 copyLinkChain(type)));
623 if (IS_LITERAL(itmp->etype)) {
624 SPEC_SCLS(itmp->etype) = S_REGISTER ;
625 SPEC_OCLS(itmp->etype) = reg;
628 op->operand.symOperand = itmp;
629 op->key = itmp->key = ++operandKey ;
633 /*-----------------------------------------------------------------*/
634 /* operandType - returns the type chain for an operand */
635 /*-----------------------------------------------------------------*/
636 link *operandType (operand *op)
638 /* depending on type of operand */
642 return op->operand.valOperand->type ;
645 return op->operand.symOperand->type ;
648 return op->operand.typeOperand ;
650 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
651 " operand type not known ");
652 assert (0) ; /* should never come here */
653 /* Just to keep the compiler happy */
658 /*-----------------------------------------------------------------*/
659 /* isParamterToCall - will return 1 if op is a parameter to args */
660 /*-----------------------------------------------------------------*/
661 int isParameterToCall (value *args, operand *op)
667 isSymbolEqual(op->operand.symOperand,tval->sym))
674 /*-----------------------------------------------------------------*/
675 /* isOperandGlobal - return 1 if operand is a global variable */
676 /*-----------------------------------------------------------------*/
677 int isOperandGlobal ( operand *op )
685 if (op->type == SYMBOL &&
686 (op->operand.symOperand->level == 0 ||
687 IS_STATIC(op->operand.symOperand->etype) ||
688 IS_EXTERN(op->operand.symOperand->etype))
695 /*-----------------------------------------------------------------*/
696 /* isOperandVolatile - return 1 if the operand is volatile */
697 /*-----------------------------------------------------------------*/
698 int isOperandVolatile ( operand *op , bool chkTemp)
703 if (IS_ITEMP(op) && !chkTemp)
706 opetype = getSpec(optype = operandType(op));
708 if (IS_PTR(optype) && DCL_PTR_VOLATILE(optype))
711 if (IS_VOLATILE(opetype))
716 /*-----------------------------------------------------------------*/
717 /* isOperandLiteral - returns 1 if an operand contains a literal */
718 /*-----------------------------------------------------------------*/
719 int isOperandLiteral ( operand *op )
726 opetype = getSpec (operandType(op));
728 if (IS_LITERAL(opetype))
733 /*-----------------------------------------------------------------*/
734 /* isOperandInFarSpace - will return true if operand is in farSpace*/
735 /*-----------------------------------------------------------------*/
736 bool isOperandInFarSpace (operand *op)
746 if (!IS_TRUE_SYMOP(op)) {
748 etype = SPIL_LOC(op)->etype;
754 etype = getSpec(operandType(op));
756 return (IN_FARSPACE(SPEC_OCLS(etype)) ? TRUE : FALSE);
759 /*-----------------------------------------------------------------*/
760 /* operandLitValue - literal value of an operand */
761 /*-----------------------------------------------------------------*/
762 double operandLitValue ( operand *op )
764 assert(isOperandLiteral(op));
766 return floatFromVal(op->operand.valOperand);
769 /*-----------------------------------------------------------------*/
770 /* operandOperation - perforoms operations on operands */
771 /*-----------------------------------------------------------------*/
772 operand *operandOperation (operand *left,operand *right,
775 operand *retval = (operand *)0;
777 assert(isOperandLiteral(left));
779 assert(isOperandLiteral(right));
783 retval = operandFromValue (valCastLiteral(type,
784 operandLitValue(left) +
785 operandLitValue(right)));
788 retval = operandFromValue(valCastLiteral(type,
789 operandLitValue(left) -
790 operandLitValue(right)));
793 retval = operandFromValue(valCastLiteral(type,
794 operandLitValue(left) *
795 operandLitValue(right)));
798 if ((unsigned long) operandLitValue(right) == 0){
799 werror(E_DIVIDE_BY_ZERO);
804 retval = operandFromValue (valCastLiteral(type,
805 operandLitValue(left) /
806 operandLitValue(right)));
809 if ((unsigned long) operandLitValue(right) == 0){
810 werror(E_DIVIDE_BY_ZERO);
814 retval = operandFromLit ((unsigned long) operandLitValue(left) %
815 (unsigned long) operandLitValue(right));
818 retval = operandFromLit ((unsigned long) operandLitValue(left) <<
819 (unsigned long) operandLitValue(right));
822 retval = operandFromLit ((unsigned long) operandLitValue(left) >>
823 (unsigned long) operandLitValue(right));
826 retval = operandFromLit (operandLitValue(left) ==
827 operandLitValue(right));
830 retval = operandFromLit (operandLitValue(left) <
831 operandLitValue(right));
834 retval = operandFromLit (operandLitValue(left) <=
835 operandLitValue(right));
838 retval = operandFromLit (operandLitValue(left) !=
839 operandLitValue(right));
842 retval = operandFromLit (operandLitValue(left) >
843 operandLitValue(right));
846 retval = operandFromLit (operandLitValue(left) >=
847 operandLitValue(right));
850 retval = operandFromLit ((unsigned long) operandLitValue(left) &
851 (unsigned long) operandLitValue(right));
854 retval = operandFromLit ((unsigned long) operandLitValue(left) |
855 (unsigned long) operandLitValue(right));
858 retval = operandFromLit ((unsigned long) operandLitValue(left) ^
859 (unsigned long) operandLitValue(right));
862 retval = operandFromLit (operandLitValue(left) &&
863 operandLitValue(right));
866 retval = operandFromLit (operandLitValue(left) ||
867 operandLitValue(right));
871 long i = operandLitValue(left);
873 retval = operandFromLit ((i >> (getSize(operandType(left))*8 - 1)) |
879 long i = operandLitValue(left);
881 retval = operandFromLit ((i << (getSize(operandType(left))*8 - 1)) |
887 retval = operandFromLit(-1 * operandLitValue(left));
891 retval = operandFromLit(~ ((long) operandLitValue(left)));
895 retval = operandFromLit(! operandLitValue(left));
899 werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
900 " operandOperation invalid operator ");
908 /*-----------------------------------------------------------------*/
909 /* isOperandEqual - compares two operand & return 1 if they r = */
910 /*-----------------------------------------------------------------*/
911 int isOperandEqual (operand *left, operand *right)
913 /* if the pointers are equal then they are equal */
917 /* if either of them null then false */
918 if ( !left || !right)
921 if (left->type != right->type)
924 if (IS_SYMOP(left) && IS_SYMOP(right))
925 return left->key == right->key ;
927 /* if types are the same */
928 switch (left->type) {
930 return isSymbolEqual(left->operand.symOperand,
931 right->operand.symOperand);
933 return (floatFromVal(left->operand.valOperand) ==
934 floatFromVal(right->operand.valOperand));
936 if (checkType(left->operand.typeOperand,
937 right->operand.typeOperand) == 1)
944 /*-----------------------------------------------------------------*/
945 /* isiCodeEqual - comapres two iCodes are returns true if yes */
946 /*-----------------------------------------------------------------*/
947 int isiCodeEqual (iCode *left, iCode *right)
949 /* if the same pointer */
953 /* if either of them null */
957 /* if operand are the same */
958 if ( left->op == right->op ) {
960 /* compare all the elements depending on type */
961 if (left->op != IFX ) {
962 if (!isOperandEqual(IC_LEFT(left),IC_LEFT(right)))
964 if (!isOperandEqual(IC_RIGHT(left),IC_RIGHT(right)))
968 if (!isOperandEqual(IC_COND(left),IC_COND(right)))
970 if (!isSymbolEqual (IC_TRUE(left),IC_TRUE(right)))
972 if (!isSymbolEqual(IC_FALSE(left),IC_FALSE(right)))
980 /*-----------------------------------------------------------------*/
981 /* newiTempFromOp - create a temp Operand with same attributes */
982 /*-----------------------------------------------------------------*/
983 operand *newiTempFromOp (operand *op)
993 nop = newiTempOperand(operandType(op),TRUE);
994 nop->isaddr = op->isaddr ;
995 nop->isvolatile = op->isvolatile ;
996 nop->isGlobal = op->isGlobal ;
997 nop->isLiteral= op->isLiteral ;
998 nop->noSpilLoc= op->noSpilLoc;
999 nop->usesDefs = op->usesDefs;
1000 nop->isParm = op->isParm;
1001 nop->parmBytes = op->parmBytes;
1005 /*-----------------------------------------------------------------*/
1006 /* operand from operand - creates an operand holder for the type */
1007 /*-----------------------------------------------------------------*/
1008 operand *operandFromOperand (operand *op)
1015 nop->type = op->type;
1016 nop->isaddr = op->isaddr ;
1017 nop->key = op->key ;
1018 nop->isvolatile = op->isvolatile ;
1019 nop->isGlobal = op->isGlobal ;
1020 nop->isLiteral= op->isLiteral ;
1021 nop->noSpilLoc= op->noSpilLoc;
1022 nop->usesDefs = op->usesDefs;
1023 nop->isParm = op->isParm;
1024 nop->parmBytes = op->parmBytes;
1026 switch (nop->type) {
1028 nop->operand.symOperand = op->operand.symOperand ;
1031 nop->operand.valOperand = op->operand.valOperand;
1034 nop->operand.typeOperand = op->operand.typeOperand ;
1041 /*-----------------------------------------------------------------*/
1042 /* opFromOpWithDU - makes a copy of the operand and DU chains */
1043 /*-----------------------------------------------------------------*/
1044 operand *opFromOpWithDU (operand *op, bitVect *defs, bitVect *uses)
1046 operand *nop = operandFromOperand(op);
1048 if (nop->type == SYMBOL) {
1049 OP_SYMBOL(nop)->defs = bitVectCopy(defs);
1050 OP_SYMBOL(nop)->uses = bitVectCopy(uses);
1056 /*-----------------------------------------------------------------*/
1057 /* operandFromSymbol - creates an operand from a symbol */
1058 /*-----------------------------------------------------------------*/
1059 operand *operandFromSymbol (symbol *sym)
1064 /* if the symbol's type is a literal */
1065 /* then it is an enumerator type */
1066 if (IS_LITERAL(sym->etype) && SPEC_ENUM(sym->etype))
1067 return operandFromValue (valFromType(sym->etype));
1070 sym->key = ++operandKey ;
1072 /* if this an implicit variable, means struct/union */
1073 /* member so just return it */
1074 if (sym->implicit || IS_FUNC(sym->type)) {
1077 op->operand.symOperand = sym;
1078 op->key = sym->key ;
1079 op->isvolatile = isOperandVolatile(op,TRUE);
1080 op->isGlobal = isOperandGlobal(op);
1081 op->parmBytes = sym->argStack;
1085 /* under the following conditions create a
1086 register equivalent for a local symbol */
1087 if (sym->level && sym->etype && SPEC_OCLS(sym->etype) &&
1088 (IN_FARSPACE(SPEC_OCLS(sym->etype)) && (!IS_DS390_PORT)) &&
1089 options.stackAuto == 0)
1092 if (!IS_AGGREGATE(sym->type) && /* not an aggregate */
1093 !IS_FUNC(sym->type) && /* not a function */
1094 !sym->_isparm && /* not a parameter */
1095 sym->level && /* is a local variable */
1096 !sym->addrtaken && /* whose address has not been taken */
1097 !sym->reqv && /* does not already have a register euivalence */
1098 !IS_VOLATILE(sym->etype) && /* not declared as volatile */
1099 !IS_STATIC(sym->etype) && /* and not declared static */
1100 !sym->islbl && /* not a label */
1101 ok && /* farspace check */
1102 !IS_BITVAR(sym->etype) /* not a bit variable */
1105 /* we will use it after all optimizations
1106 and before liveRange calculation */
1107 sym->reqv = newiTempOperand(sym->type,0);
1108 sym->reqv->key = sym->key ;
1109 OP_SYMBOL(sym->reqv)->key = sym->key;
1110 OP_SYMBOL(sym->reqv)->isreqv = 1;
1111 OP_SYMBOL(sym->reqv)->islocal = 1;
1112 SPIL_LOC(sym->reqv) = sym;
1115 if (!IS_AGGREGATE(sym->type)) {
1118 op->operand.symOperand = sym;
1121 op->isvolatile = isOperandVolatile(op,TRUE);
1122 op->isGlobal = isOperandGlobal(op);
1123 op->isPtr = IS_PTR(operandType(op));
1124 op->isParm = sym->_isparm ;
1129 /* itemp = &[_symbol] */
1131 ic = newiCode(ADDRESS_OF,newOperand(),NULL);
1132 IC_LEFT(ic)->type = SYMBOL ;
1133 IC_LEFT(ic)->operand.symOperand = sym ;
1134 IC_LEFT(ic)->key = sym->key;
1135 (IC_LEFT(ic))->isvolatile = isOperandVolatile(IC_LEFT(ic),TRUE);
1136 (IC_LEFT(ic))->isGlobal = isOperandGlobal(IC_LEFT(ic));
1137 IC_LEFT(ic)->isPtr = IS_PTR(operandType(IC_LEFT(ic)));
1140 IC_RESULT(ic) = newiTempOperand(sym->type,0);
1141 if (IS_ARRAY(sym->type)) {
1142 IC_RESULT(ic) = geniCodeArray2Ptr (IC_RESULT(ic));
1143 IC_RESULT(ic)->isaddr = 0;
1145 IC_RESULT(ic)->isaddr = (!IS_AGGREGATE(sym->type));
1147 IC_RESULT(ic)->operand.symOperand->args = sym->args;
1151 return IC_RESULT(ic) ;
1154 /*-----------------------------------------------------------------*/
1155 /* operandFromValue - creates an operand from value */
1156 /*-----------------------------------------------------------------*/
1157 operand *operandFromValue (value *val)
1161 /* if this is a symbol then do the symbol thing */
1163 return operandFromSymbol (val->sym);
1165 /* this is not a symbol */
1168 op->operand.valOperand = val ;
1169 op->isLiteral = isOperandLiteral(op);
1173 /*-----------------------------------------------------------------*/
1174 /* operandFromLink - operand from typeChain */
1175 /*-----------------------------------------------------------------*/
1176 operand *operandFromLink (link *type)
1180 /* operand from link */
1186 op->operand.typeOperand = copyLinkChain(type);
1190 /*-----------------------------------------------------------------*/
1191 /* operandFromLit - makes an operand from a literal value */
1192 /*-----------------------------------------------------------------*/
1193 operand *operandFromLit ( float i)
1195 return operandFromValue (valueFromLit (i));
1198 /*-----------------------------------------------------------------*/
1199 /* operandFromAst - creates an operand from an ast */
1200 /*-----------------------------------------------------------------*/
1201 operand *operandFromAst ( ast *tree )
1207 /* depending on type do */
1208 switch (tree->type ) {
1210 return ast2iCode (tree) ;
1214 return operandFromValue(tree->opval.val) ;
1218 return operandFromLink (tree->opval.lnk) ;
1222 /* Just to keep the comiler happy */
1223 return (operand *)0;
1226 /*-----------------------------------------------------------------*/
1227 /* setOperandType - sets the operand's type to the given type */
1228 /*-----------------------------------------------------------------*/
1229 void setOperandType (operand *op, link *type)
1231 /* depending on the type of operand */
1235 op->operand.valOperand->etype =
1236 getSpec( op->operand.valOperand->type =
1237 copyLinkChain (type )) ;
1241 if (op->operand.symOperand->isitmp )
1242 op->operand.symOperand->etype =
1243 getSpec( op->operand.symOperand->type =
1244 copyLinkChain (type )) ;
1246 werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
1247 "attempt to modify type of source");
1251 op->operand.typeOperand = copyLinkChain (type);
1257 /*-----------------------------------------------------------------*/
1258 /* geniCodeValueAtAddress - generate intermeditate code for value */
1260 /*-----------------------------------------------------------------*/
1261 operand *geniCodeRValue (operand *op, bool force)
1264 link *type = operandType(op);
1265 link *etype= getSpec(type);
1267 /* if this is an array & already */
1268 /* an address then return this */
1269 if (IS_AGGREGATE(type) ||
1270 (IS_PTR(type) && !force && !op->isaddr))
1271 return operandFromOperand(op);
1273 /* if this is not an address then must be */
1274 /* rvalue already so return this one */
1278 /* if this is not a temp symbol then */
1279 if (!IS_ITEMP(op) &&
1281 !IN_FARSPACE(SPEC_OCLS(etype))) {
1282 op = operandFromOperand(op);
1287 if (IS_SPEC(type) &&
1288 IS_TRUE_SYMOP(op) &&
1289 !IN_FARSPACE(SPEC_OCLS(etype))) {
1290 op = operandFromOperand(op);
1295 ic = newiCode(GET_VALUE_AT_ADDRESS,op,NULL);
1296 if (IS_PTR(type) && op->isaddr && force)
1299 type = copyLinkChain(type);
1301 IC_RESULT(ic) = newiTempOperand (type,1);
1302 IC_RESULT(ic)->isaddr = 0;
1304 /* ic->supportRtn = ((IS_GENPTR(type) | op->isGptr) & op->isaddr); */
1306 /* if the right is a symbol */
1307 if (op->type == SYMBOL)
1308 IC_RESULT(ic)->operand.symOperand->args =
1309 op->operand.symOperand->args ;
1312 return IC_RESULT(ic) ;
1315 /*-----------------------------------------------------------------*/
1316 /* geniCodeCast - changes the value from one type to another */
1317 /*-----------------------------------------------------------------*/
1318 operand *geniCodeCast (link *type, operand *op, bool implicit)
1322 link *opetype = getSpec(optype = operandType(op));
1325 /* one of them has size zero then error */
1326 if (IS_VOID(optype)) {
1327 werror(E_CAST_ZERO);
1331 /* if the operand is already the desired type then do nothing */
1332 if ( checkType (type,optype) == 1)
1335 /* if this is a literal then just change the type & return */
1336 if (IS_LITERAL(opetype) && op->type == VALUE && !IS_PTR(type) && !IS_PTR(optype))
1337 return operandFromValue(valCastLiteral(type,
1338 operandLitValue(op)));
1340 /* if casting to some pointer type &&
1341 the destination is not a generic pointer
1342 then give a warning : (only for implicit casts)*/
1343 if (IS_PTR(optype) && implicit &&
1344 (DCL_TYPE(optype) != DCL_TYPE(type)) &&
1346 werror(E_INCOMPAT_CAST);
1347 werror(E_CONTINUE,"from type '");
1348 printTypeChain(optype,stderr);fprintf(stderr,"' to type '");
1349 printTypeChain(type,stderr);fprintf(stderr,"'\n");
1352 /* if they are the same size create an assignment */
1353 if (getSize(type) == getSize(optype) &&
1354 !IS_BITFIELD(type) &&
1356 !IS_FLOAT(optype) &&
1357 ((IS_SPEC(type) && IS_SPEC(optype)) ||
1358 (!IS_SPEC(type) && !IS_SPEC(optype)))) {
1360 ic = newiCode('=',NULL,op);
1361 IC_RESULT(ic) = newiTempOperand(type,0);
1362 SPIL_LOC(IC_RESULT(ic)) =
1363 (IS_TRUE_SYMOP(op) ? OP_SYMBOL(op) : NULL);
1364 IC_RESULT(ic)->isaddr = 0;
1366 ic = newiCode(CAST,operandFromLink(type),
1367 geniCodeRValue(op,FALSE));
1369 IC_RESULT(ic)= newiTempOperand(type,0);
1372 /* preserve the storage class & output class */
1373 /* of the original variable */
1374 restype = getSpec(operandType(IC_RESULT(ic)));
1375 SPEC_SCLS(restype) = SPEC_SCLS(opetype);
1376 SPEC_OCLS(restype) = SPEC_OCLS(opetype);
1379 return IC_RESULT(ic) ;
1382 /*-----------------------------------------------------------------*/
1383 /* geniCodeLabel - will create a Label */
1384 /*-----------------------------------------------------------------*/
1385 void geniCodeLabel (symbol *label)
1389 ic = newiCodeLabelGoto(LABEL,label);
1393 /*-----------------------------------------------------------------*/
1394 /* geniCodeGoto - will create a Goto */
1395 /*-----------------------------------------------------------------*/
1396 void geniCodeGoto (symbol *label)
1400 ic = newiCodeLabelGoto(GOTO,label);
1404 /*-----------------------------------------------------------------*/
1405 /* geniCodeMultiply - gen intermediate code for multiplication */
1406 /*-----------------------------------------------------------------*/
1407 operand *geniCodeMultiply (operand *left, operand *right)
1414 /* if they are both literal then we know the result */
1415 if (IS_LITERAL(letype) && IS_LITERAL(retype))
1416 return operandFromValue (valMult(left->operand.valOperand,
1417 right->operand.valOperand));
1419 resType = computeType (ltype,rtype) ;
1420 left = geniCodeCast(resType,left,TRUE);
1421 right= geniCodeCast(resType,right,TRUE);
1423 /* if the right is a literal & power of 2 */
1424 /* then make it a left shift */
1425 if (IS_LITERAL(retype) && !IS_FLOAT(letype) &&
1426 (p2 = powof2 ((unsigned long)floatFromVal(right->operand.valOperand))))
1427 ic = newiCode(LEFT_OP, left,operandFromLit(p2)); /* left shift */
1429 ic = newiCode('*',left,right); /* normal multiplication */
1430 /* if the size left or right > 1 then support routine */
1431 if (getSize(ltype) > 1 || getSize(rtype) > 1)
1435 IC_RESULT(ic) = newiTempOperand(resType,1);
1438 return IC_RESULT(ic) ;
1441 /*-----------------------------------------------------------------*/
1442 /* geniCodeDivision - gen intermediate code for division */
1443 /*-----------------------------------------------------------------*/
1444 operand *geniCodeDivision (operand *left, operand *right)
1449 link *rtype = operandType(right);
1450 link *retype= getSpec(rtype);
1451 link *ltype = operandType(left);
1452 link *letype= getSpec(ltype);
1454 resType = computeType (ltype,rtype) ;
1455 left = geniCodeCast(resType,left,TRUE);
1456 right= geniCodeCast(resType,right,TRUE);
1458 /* if the right is a literal & power of 2 */
1459 /* then make it a right shift */
1460 if (IS_LITERAL(retype) &&
1461 !IS_FLOAT(letype) &&
1462 (p2 = powof2 ((unsigned long)
1463 floatFromVal(right->operand.valOperand))))
1464 ic = newiCode(RIGHT_OP, left,operandFromLit(p2)); /* right shift */
1466 ic = newiCode('/',left,right); /* normal division */
1467 /* if the size left or right > 1 then support routine */
1468 if (getSize(ltype) > 1 || getSize(rtype) > 1)
1471 IC_RESULT(ic) = newiTempOperand(resType,0);
1474 return IC_RESULT(ic) ;
1476 /*-----------------------------------------------------------------*/
1477 /* geniCodeModulus - gen intermediate code for modulus */
1478 /*-----------------------------------------------------------------*/
1479 operand *geniCodeModulus (operand *left, operand *right)
1485 /* if they are both literal then we know the result */
1486 if (IS_LITERAL(letype) && IS_LITERAL(retype))
1487 return operandFromValue (valMod(left->operand.valOperand,
1488 right->operand.valOperand));
1490 resType = computeType (ltype,rtype) ;
1491 left = geniCodeCast(resType,left,TRUE);
1492 right= geniCodeCast(resType,right,TRUE);
1494 /* now they are the same size */
1495 ic = newiCode('%',left,right);
1497 /* if the size left or right > 1 then support routine */
1498 if (getSize(ltype) > 1 || getSize(rtype) > 1)
1500 IC_RESULT(ic) = newiTempOperand(resType,0);
1503 return IC_RESULT(ic) ;
1506 /*-----------------------------------------------------------------*/
1507 /* geniCodePtrPtrSubtract - subtracts pointer from pointer */
1508 /*-----------------------------------------------------------------*/
1509 operand *geniCodePtrPtrSubtract (operand *left, operand *right)
1515 /* if they are both literals then */
1516 if (IS_LITERAL(letype) && IS_LITERAL(retype)) {
1517 result = operandFromValue (valMinus(left->operand.valOperand,
1518 right->operand.valOperand));
1522 ic = newiCode('-',left,right);
1524 IC_RESULT(ic) = result = newiTempOperand(newIntLink(),1);
1528 return geniCodeDivision (result,
1529 operandFromLit(getSize(ltype->next)));
1532 /*-----------------------------------------------------------------*/
1533 /* geniCodeSubtract - generates code for subtraction */
1534 /*-----------------------------------------------------------------*/
1535 operand *geniCodeSubtract (operand *left, operand *right)
1542 /* if they both pointers then */
1543 if ((IS_PTR(ltype) || IS_ARRAY(ltype)) &&
1544 (IS_PTR(rtype) || IS_ARRAY(rtype)))
1545 return geniCodePtrPtrSubtract (left,right);
1547 /* if they are both literal then we know the result */
1548 if (IS_LITERAL(letype) && IS_LITERAL(retype)
1549 && left->isLiteral && right->isLiteral)
1550 return operandFromValue (valMinus(left->operand.valOperand,
1551 right->operand.valOperand));
1553 /* if left is an array or pointer */
1554 if ( IS_PTR(ltype) || IS_ARRAY(ltype) ) {
1555 isarray = left->isaddr ;
1556 right = geniCodeMultiply (right,
1557 operandFromLit(getSize(ltype->next)));
1558 resType = copyLinkChain(IS_ARRAY(ltype) ? ltype->next : ltype);
1560 else { /* make them the same size */
1561 resType = computeType (ltype,rtype) ;
1562 left = geniCodeCast(resType,left,TRUE);
1563 right= geniCodeCast(resType,right,TRUE);
1566 ic = newiCode('-',left,right);
1568 IC_RESULT(ic)= newiTempOperand(resType,1);
1569 IC_RESULT(ic)->isaddr = (isarray ? 1 : 0);
1571 /* if left or right is a float */
1572 if (IS_FLOAT(ltype) || IS_FLOAT(rtype))
1576 return IC_RESULT(ic) ;
1579 /*-----------------------------------------------------------------*/
1580 /* geniCodeAdd - generates iCode for addition */
1581 /*-----------------------------------------------------------------*/
1582 operand *geniCodeAdd (operand *left, operand *right )
1590 /* if left is an array then array access */
1591 if (IS_ARRAY(ltype))
1592 return geniCodeArray (left,right);
1594 /* if the right side is LITERAL zero */
1595 /* return the left side */
1596 if (IS_LITERAL(retype) && right->isLiteral && !floatFromVal(valFromType(retype)))
1599 /* if left is literal zero return right */
1600 if (IS_LITERAL(letype) && left->isLiteral && !floatFromVal(valFromType(letype)))
1603 /* if left is an array or pointer then size */
1604 if (IS_PTR(ltype)) {
1606 isarray = left->isaddr;
1608 operandFromLit(getSize(ltype->next));
1609 right = geniCodeMultiply (right ,size);
1610 resType = copyLinkChain(ltype);
1612 else { /* make them the same size */
1613 resType = computeType (ltype,rtype) ;
1614 left = geniCodeCast(resType,left,TRUE);
1615 right= geniCodeCast(resType,right,TRUE);
1618 /* if they are both literals then we know */
1619 if (IS_LITERAL(letype) && IS_LITERAL(retype)
1620 && left->isLiteral && right->isLiteral)
1621 return operandFromValue (valPlus(valFromType(letype),
1622 valFromType(retype)));
1624 ic = newiCode('+',left,right);
1626 IC_RESULT(ic) = newiTempOperand(resType,1);
1627 IC_RESULT(ic)->isaddr = ( isarray ? 1 : 0);
1629 /* if left or right is a float then support
1631 if (IS_FLOAT(ltype) || IS_FLOAT(rtype))
1636 return IC_RESULT(ic) ;
1640 /*-----------------------------------------------------------------*/
1641 /* aggrToPtr - changes an aggregate to pointer to an aggregate */
1642 /*-----------------------------------------------------------------*/
1643 link *aggrToPtr ( link *type, bool force)
1649 if (IS_PTR(type) && !force)
1652 etype = getSpec(type);
1656 /* if the output class is generic */
1657 if ((DCL_TYPE(ptype) = PTR_TYPE(SPEC_OCLS(etype))) == CPOINTER)
1658 DCL_PTR_CONST(ptype) = port->mem.code_ro;
1660 /* if the variable was declared a constant */
1661 /* then the pointer points to a constant */
1662 if (IS_CONSTANT(etype) )
1663 DCL_PTR_CONST(ptype) = 1;
1665 /* the variable was volatile then pointer to volatile */
1666 if (IS_VOLATILE(etype))
1667 DCL_PTR_VOLATILE(ptype) = 1;
1671 /*-----------------------------------------------------------------*/
1672 /* geniCodeArray2Ptr - array to pointer */
1673 /*-----------------------------------------------------------------*/
1674 operand *geniCodeArray2Ptr (operand *op)
1676 link *optype = operandType(op);
1677 link *opetype = getSpec(optype);
1679 /* set the pointer depending on the storage class */
1680 if ((DCL_TYPE(optype) = PTR_TYPE(SPEC_OCLS(opetype))) == CPOINTER)
1681 DCL_PTR_CONST(optype) = port->mem.code_ro;
1684 /* if the variable was declared a constant */
1685 /* then the pointer points to a constant */
1686 if (IS_CONSTANT(opetype) )
1687 DCL_PTR_CONST(optype) = 1;
1689 /* the variable was volatile then pointer to volatile */
1690 if (IS_VOLATILE(opetype))
1691 DCL_PTR_VOLATILE(optype) = 1;
1696 /*-----------------------------------------------------------------*/
1697 /* geniCodeArray - array access */
1698 /*-----------------------------------------------------------------*/
1699 operand *geniCodeArray (operand *left,operand *right)
1702 link *ltype = operandType(left);
1704 if (IS_PTR(ltype)) {
1705 if (IS_PTR(ltype->next) && left->isaddr)
1706 left = geniCodeRValue(left,FALSE);
1707 return geniCodeDerefPtr(geniCodeAdd(left,right));
1711 right = geniCodeMultiply(right,
1712 operandFromLit(getSize(ltype->next)));
1714 /* we can check for limits here */
1715 if (isOperandLiteral(right) &&
1718 (operandLitValue(right)/getSize(ltype->next)) >= DCL_ELEM(ltype)) {
1719 werror(E_ARRAY_BOUND);
1720 right = operandFromLit(0);
1723 ic = newiCode('+',left,right);
1725 IC_RESULT(ic) = newiTempOperand(((IS_PTR(ltype) &&
1726 !IS_AGGREGATE(ltype->next) &&
1727 !IS_PTR(ltype->next))
1728 ? ltype : ltype->next),0);
1730 IC_RESULT(ic)->isaddr = (!IS_AGGREGATE(ltype->next));
1732 return IC_RESULT(ic) ;
1735 /*-----------------------------------------------------------------*/
1736 /* geniCodeStruct - generates intermediate code for structres */
1737 /*-----------------------------------------------------------------*/
1738 operand *geniCodeStruct (operand *left, operand *right, bool islval)
1741 link *type = operandType(left);
1742 link *etype = getSpec(type);
1744 symbol *element = getStructElement(SPEC_STRUCT(etype),
1745 right->operand.symOperand);
1747 /* add the offset */
1748 ic = newiCode('+',left,operandFromLit(element->offset));
1750 IC_RESULT(ic) = newiTempOperand(element->type,0);
1752 /* preserve the storage & output class of the struct */
1753 /* as well as the volatile attribute */
1754 retype = getSpec(operandType(IC_RESULT(ic)));
1755 SPEC_SCLS(retype) = SPEC_SCLS(etype);
1756 SPEC_OCLS(retype) = SPEC_OCLS(etype);
1757 SPEC_VOLATILE(retype) |= SPEC_VOLATILE(etype);
1759 if (IS_PTR(element->type))
1760 setOperandType(IC_RESULT(ic),aggrToPtr(operandType(IC_RESULT(ic)),TRUE));
1762 IC_RESULT(ic)->isaddr = (!IS_AGGREGATE(element->type));
1766 return (islval ? IC_RESULT(ic) : geniCodeRValue(IC_RESULT(ic),TRUE));
1769 /*-----------------------------------------------------------------*/
1770 /* geniCodePostInc - generate int code for Post increment */
1771 /*-----------------------------------------------------------------*/
1772 operand *geniCodePostInc (operand *op)
1776 link *optype = operandType(op);
1778 operand *rv = (IS_ITEMP(op) ?
1779 geniCodeRValue(op,(IS_PTR(optype) ? TRUE : FALSE)) :
1781 link *rvtype = operandType(rv);
1784 /* if this is not an address we have trouble */
1785 if ( ! op->isaddr ) {
1786 werror (E_LVALUE_REQUIRED,"++");
1790 rOp = newiTempOperand(rvtype,0);
1796 geniCodeAssign(rOp,rv,0);
1798 size = (IS_PTR(rvtype) ? getSize(rvtype->next) : 1);
1799 ic = newiCode('+',rv,operandFromLit(size));
1800 IC_RESULT(ic) = result =newiTempOperand(rvtype,0);
1803 geniCodeAssign(op,result,0);
1809 /*-----------------------------------------------------------------*/
1810 /* geniCodePreInc - generate code for preIncrement */
1811 /*-----------------------------------------------------------------*/
1812 operand *geniCodePreInc (operand *op)
1815 link *optype = operandType(op);
1816 operand *rop = (IS_ITEMP(op) ?
1817 geniCodeRValue (op,(IS_PTR(optype) ? TRUE : FALSE)) :
1819 link *roptype = operandType(rop);
1823 if ( ! op->isaddr ) {
1824 werror(E_LVALUE_REQUIRED,"++");
1829 size = (IS_PTR(roptype) ? getSize(roptype->next) : 1);
1830 ic = newiCode('+',rop,operandFromLit(size));
1831 IC_RESULT(ic) = result = newiTempOperand(roptype,0) ;
1835 return geniCodeAssign(op,result,0) ;
1838 /*-----------------------------------------------------------------*/
1839 /* geniCodePostDec - generates code for Post decrement */
1840 /*-----------------------------------------------------------------*/
1841 operand *geniCodePostDec (operand *op)
1845 link *optype = operandType(op);
1847 operand *rv = (IS_ITEMP(op) ?
1848 geniCodeRValue(op,(IS_PTR(optype) ? TRUE : FALSE)) :
1850 link *rvtype = operandType(rv);
1853 /* if this is not an address we have trouble */
1854 if ( ! op->isaddr ) {
1855 werror (E_LVALUE_REQUIRED,"++");
1859 rOp = newiTempOperand(rvtype,0);
1865 geniCodeAssign(rOp,rv,0);
1867 size = (IS_PTR(rvtype) ? getSize(rvtype->next) : 1);
1868 ic = newiCode('-',rv,operandFromLit(size));
1869 IC_RESULT(ic) = result =newiTempOperand(rvtype,0);
1872 geniCodeAssign(op,result,0);
1878 /*-----------------------------------------------------------------*/
1879 /* geniCodePreDec - generate code for pre decrement */
1880 /*-----------------------------------------------------------------*/
1881 operand *geniCodePreDec (operand *op)
1884 link *optype = operandType(op);
1885 operand *rop = (IS_ITEMP(op) ?
1886 geniCodeRValue (op,(IS_PTR(optype) ? TRUE : FALSE)) :
1888 link *roptype = operandType(rop);
1892 if ( ! op->isaddr ) {
1893 werror(E_LVALUE_REQUIRED,"++");
1898 size = (IS_PTR(roptype) ? getSize(roptype->next) : 1);
1899 ic = newiCode('-',rop,operandFromLit(size));
1900 IC_RESULT(ic) = result = newiTempOperand(roptype,0) ;
1904 return geniCodeAssign(op,result,0) ;
1908 /*-----------------------------------------------------------------*/
1909 /* geniCodeBitwise - gen int code for bitWise operators */
1910 /*-----------------------------------------------------------------*/
1911 operand *geniCodeBitwise (operand *left, operand *right,
1912 int oper, link *resType)
1916 left = geniCodeCast(resType,left,TRUE);
1917 right= geniCodeCast(resType,right,TRUE);
1919 ic = newiCode(oper,left,right);
1920 IC_RESULT(ic) = newiTempOperand(resType,0);
1923 return IC_RESULT(ic) ;
1926 /*-----------------------------------------------------------------*/
1927 /* geniCodeAddressOf - gens icode for '&' address of operator */
1928 /*-----------------------------------------------------------------*/
1929 operand *geniCodeAddressOf (operand *op)
1933 link *optype = operandType(op);
1934 link *opetype= getSpec(optype);
1936 /* lvalue check already done in decorateType */
1937 /* this must be a lvalue */
1938 /* if (!op->isaddr && !IS_AGGREGATE(optype)) { */
1939 /* werror (E_LVALUE_REQUIRED,"&"); */
1944 p->class = DECLARATOR ;
1946 /* set the pointer depending on the storage class */
1947 if ((DCL_TYPE(p) = PTR_TYPE(SPEC_OCLS(opetype))) == CPOINTER)
1948 DCL_PTR_CONST(p) = port->mem.code_ro;
1950 /* make sure we preserve the const & volatile */
1951 if (IS_CONSTANT(opetype))
1952 DCL_PTR_CONST(p) = 1;
1954 if (IS_VOLATILE(opetype))
1955 DCL_PTR_VOLATILE(p) = 1;
1957 p->next = copyLinkChain(optype);
1959 /* if already a temp */
1961 setOperandType (op,p);
1966 /* other wise make this of the type coming in */
1967 ic = newiCode(ADDRESS_OF,op,NULL);
1968 IC_RESULT(ic) = newiTempOperand(p,1);
1969 IC_RESULT(ic)->isaddr = 0;
1971 return IC_RESULT(ic);
1973 /*-----------------------------------------------------------------*/
1974 /* setOClass - sets the output class depending on the pointer type */
1975 /*-----------------------------------------------------------------*/
1976 void setOClass (link *ptr, link *spec)
1978 switch (DCL_TYPE(ptr)) {
1980 SPEC_OCLS(spec) = data ;
1984 SPEC_OCLS(spec) = generic;
1988 SPEC_OCLS(spec) = xdata ;
1992 SPEC_OCLS(spec) = code ;
1996 SPEC_OCLS(spec) = idata;
2000 SPEC_OCLS(spec) = xstack;
2004 SPEC_OCLS(spec) = eeprom;
2013 /*-----------------------------------------------------------------*/
2014 /* geniCodeDerefPtr - dereference pointer with '*' */
2015 /*-----------------------------------------------------------------*/
2016 operand *geniCodeDerefPtr (operand *op)
2018 link *rtype , *retype ;
2019 link *optype = operandType(op);
2021 /* if this is a pointer then generate the rvalue */
2022 if (IS_PTR(optype)) {
2023 if (IS_TRUE_SYMOP(op)) {
2025 op = geniCodeRValue(op,TRUE);
2028 op = geniCodeRValue(op,TRUE);
2031 /* now get rid of the pointer part */
2032 if (lvaluereq && IS_ITEMP(op) )
2034 retype = getSpec(rtype = copyLinkChain(optype)) ;
2038 retype = getSpec(rtype = copyLinkChain(optype->next)) ;
2041 /* if this is a pointer then outputclass needs 2b updated */
2043 setOClass(optype,retype);
2045 op->isGptr = IS_GENPTR(optype);
2047 /* if the pointer was declared as a constant */
2048 /* then we cannot allow assignment to the derefed */
2049 if (IS_PTR_CONST(optype))
2050 SPEC_CONST(retype) = 1;
2052 op->isaddr = (IS_PTR(rtype) ||
2059 op = geniCodeRValue(op,TRUE);
2061 setOperandType(op,rtype);
2066 /*-----------------------------------------------------------------*/
2067 /* geniCodeUnaryMinus - does a unary minus of the operand */
2068 /*-----------------------------------------------------------------*/
2069 operand *geniCodeUnaryMinus (operand *op)
2072 link *optype = operandType(op);
2074 if (IS_LITERAL(optype))
2075 return operandFromLit(- floatFromVal(op->operand.valOperand));
2077 ic = newiCode(UNARYMINUS,op,NULL);
2078 IC_RESULT(ic) = newiTempOperand(optype,0);
2080 return IC_RESULT(ic);
2083 /*-----------------------------------------------------------------*/
2084 /* geniCodeLeftShift - gen i code for left shift */
2085 /*-----------------------------------------------------------------*/
2086 operand *geniCodeLeftShift (operand *left, operand *right)
2089 link *ltype = operandType(left);
2091 ic = newiCode(LEFT_OP,left,right);
2092 IC_RESULT(ic) = newiTempOperand(ltype,0);
2094 return IC_RESULT(ic) ;
2097 /*-----------------------------------------------------------------*/
2098 /* geniCodeRightShift - gen i code for right shift */
2099 /*-----------------------------------------------------------------*/
2100 operand *geniCodeRightShift (operand *left, operand *right)
2103 link *ltype = operandType(left);
2105 ic = newiCode(RIGHT_OP,left,right);
2106 IC_RESULT(ic) = newiTempOperand(ltype,0);
2108 return IC_RESULT(ic) ;
2112 #define LONG_LONG __int64
2114 #define LONG_LONG long long
2117 /*-----------------------------------------------------------------*/
2118 /* geniCodeLogic- logic code */
2119 /*-----------------------------------------------------------------*/
2120 operand *geniCodeLogic (operand *left, operand *right, int op )
2124 link *rtype = operandType(right);
2125 link *ltype = operandType(left);
2127 /* left is integral type and right is literal then
2128 check if the literal value is within bounds */
2129 if (IS_INTEGRAL(ltype) && IS_LITERAL(rtype)) {
2130 int nbits = bitsForType(ltype);
2131 long v = operandLitValue(right);
2133 if (v > ((LONG_LONG) 1 << nbits) && v > 0)
2134 werror(W_CONST_RANGE," compare operation ");
2137 ctype = computeType(ltype,rtype);
2138 left = geniCodeCast(ctype,left,TRUE);
2139 right= geniCodeCast(ctype,right,TRUE);
2141 ic = newiCode(op,left,right);
2142 IC_RESULT(ic) = newiTempOperand (newCharLink(),1);
2144 /* if comparing anything greater than one byte
2145 and not a '==' || '!=' || '&&' || '||' (these
2147 if (getSize(ctype) > 1 &&
2155 return IC_RESULT(ic);
2158 /*-----------------------------------------------------------------*/
2159 /* geniCodeUnary - for a a generic unary operation */
2160 /*-----------------------------------------------------------------*/
2161 operand *geniCodeUnary (operand *op, int oper )
2163 iCode *ic = newiCode (oper,op,NULL);
2165 IC_RESULT(ic)= newiTempOperand(operandType(op),0);
2167 return IC_RESULT(ic) ;
2170 /*-----------------------------------------------------------------*/
2171 /* geniCodeConditional - geniCode for '?' ':' operation */
2172 /*-----------------------------------------------------------------*/
2173 operand *geniCodeConditional (ast *tree)
2176 symbol *falseLabel = newiTempLabel(NULL);
2177 symbol *exitLabel = newiTempLabel(NULL);
2178 operand *cond = ast2iCode(tree->left);
2179 operand *true, *false , *result;
2181 ic = newiCodeCondition(geniCodeRValue(cond,FALSE),
2185 true = ast2iCode(tree->right->left);
2187 /* move the value to a new Operand */
2188 result = newiTempOperand(operandType(true),0);
2189 geniCodeAssign(result,geniCodeRValue(true,FALSE),0);
2191 /* generate an unconditional goto */
2192 geniCodeGoto(exitLabel);
2194 /* now for the right side */
2195 geniCodeLabel(falseLabel);
2197 false = ast2iCode(tree->right->right);
2198 geniCodeAssign(result,geniCodeRValue(false,FALSE),0);
2200 /* create the exit label */
2201 geniCodeLabel(exitLabel);
2206 /*-----------------------------------------------------------------*/
2207 /* geniCodeAssign - generate code for assignment */
2208 /*-----------------------------------------------------------------*/
2209 operand *geniCodeAssign (operand *left, operand *right, int nosupdate)
2212 link *ltype = operandType(left);
2213 link *rtype = operandType(right);
2215 if (!left->isaddr && !IS_ITEMP(left)) {
2216 werror(E_LVALUE_REQUIRED,"assignment");
2220 /* left is integral type and right is literal then
2221 check if the literal value is within bounds */
2222 if (IS_INTEGRAL(ltype) && right->type == VALUE && IS_LITERAL(rtype)) {
2223 int nbits = bitsForType(ltype);
2224 long v = operandLitValue(right);
2226 if (v > ((LONG_LONG)1 << nbits) && v > 0)
2227 werror(W_CONST_RANGE," = operation");
2230 /* if the left & right type don't exactly match */
2231 /* if pointer set then make sure the check is
2232 done with the type & not the pointer */
2233 /* then cast rights type to left */
2235 /* first check the type for pointer assignement */
2236 if (left->isaddr && IS_PTR(ltype) && IS_ITEMP(left) &&
2237 checkType(ltype,rtype)<0) {
2238 if (checkType(ltype->next,rtype) < 0)
2239 right = geniCodeCast(ltype->next,right,TRUE);
2241 if (checkType(ltype,rtype) < 0 )
2242 right = geniCodeCast(ltype,right,TRUE);
2244 /* if left is a true symbol & ! volatile
2245 create an assignment to temporary for
2246 the right & then assign this temporary
2247 to the symbol this is SSA . isn't it simple
2248 and folks have published mountains of paper on it */
2249 if (IS_TRUE_SYMOP(left) &&
2250 !isOperandVolatile(left,FALSE) &&
2251 isOperandGlobal(left)) {
2254 if (IS_TRUE_SYMOP(right))
2255 sym = OP_SYMBOL(right);
2256 ic = newiCode('=',NULL,right);
2257 IC_RESULT(ic) = right = newiTempOperand(ltype,0);
2258 SPIL_LOC(right) = sym ;
2262 ic = newiCode('=',NULL,right);
2263 IC_RESULT(ic) = left;
2266 /* if left isgptr flag is set then support
2267 routine will be required */
2271 ic->nosupdate = nosupdate;
2275 /*-----------------------------------------------------------------*/
2276 /* geniCodeSEParms - generate code for side effecting fcalls */
2277 /*-----------------------------------------------------------------*/
2278 static void geniCodeSEParms (ast *parms)
2283 if (parms->type == EX_OP && parms->opval.op == PARAM) {
2284 geniCodeSEParms (parms->left) ;
2285 geniCodeSEParms (parms->right);
2289 /* hack don't like this but too lazy to think of
2291 if (IS_ADDRESS_OF_OP(parms))
2292 parms->left->lvalue = 1;
2294 if (IS_CAST_OP(parms) &&
2295 IS_PTR(parms->ftype) &&
2296 IS_ADDRESS_OF_OP(parms->right))
2297 parms->right->left->lvalue = 1;
2299 parms->opval.oprnd =
2300 geniCodeRValue(ast2iCode (parms),FALSE);
2302 parms->type = EX_OPERAND ;
2305 /*-----------------------------------------------------------------*/
2306 /* geniCodeParms - generates parameters */
2307 /*-----------------------------------------------------------------*/
2308 static void geniCodeParms ( ast *parms , int *stack, link *fetype)
2316 /* if this is a param node then do the left & right */
2317 if (parms->type == EX_OP && parms->opval.op == PARAM) {
2318 geniCodeParms (parms->left, stack,fetype) ;
2319 geniCodeParms (parms->right, stack,fetype);
2323 /* get the parameter value */
2324 if (parms->type == EX_OPERAND)
2325 pval = parms->opval.oprnd ;
2327 /* maybe this else should go away ?? */
2328 /* hack don't like this but too lazy to think of
2330 if (IS_ADDRESS_OF_OP(parms))
2331 parms->left->lvalue = 1;
2333 if (IS_CAST_OP(parms) &&
2334 IS_PTR(parms->ftype) &&
2335 IS_ADDRESS_OF_OP(parms->right))
2336 parms->right->left->lvalue = 1;
2338 pval = geniCodeRValue(ast2iCode (parms),FALSE);
2341 /* if register parm then make it a send */
2342 if (((parms->argSym && IS_REGPARM(parms->argSym->etype)) ||
2343 IS_REGPARM(parms->etype)) &&
2345 ic = newiCode(SEND,pval,NULL);
2348 /* now decide whether to push or assign */
2349 if (!(options.stackAuto || IS_RENT(fetype))) {
2352 operand *top = operandFromSymbol(parms->argSym);
2353 geniCodeAssign(top,pval,1);
2356 link *p = operandType(pval);
2358 ic = newiCode(IPUSH,pval,NULL);
2360 /* update the stack adjustment */
2361 *stack += getSize(IS_AGGREGATE(p)? aggrToPtr(p,FALSE):p);
2368 /*-----------------------------------------------------------------*/
2369 /* geniCodeCall - generates temp code for calling */
2370 /*-----------------------------------------------------------------*/
2371 operand *geniCodeCall (operand *left, ast *parms)
2378 /* take care of parameters with side-effecting
2379 function calls in them, this is required to take care
2380 of overlaying function parameters */
2381 geniCodeSEParms ( parms );
2383 /* first the parameters */
2384 geniCodeParms ( parms , &stack , getSpec(operandType(left)));
2386 /* now call : if symbol then pcall */
2388 ic = newiCode(PCALL,left,NULL);
2390 ic = newiCode(CALL,left,NULL);
2392 IC_ARGS(ic) = left->operand.symOperand->args ;
2393 type = copyLinkChain(operandType(left)->next);
2394 etype = getSpec(type);
2395 SPEC_EXTR(etype) = 0;
2396 IC_RESULT(ic) = result = newiTempOperand(type,1);
2400 /* stack adjustment after call */
2401 left->parmBytes = stack;
2406 /*-----------------------------------------------------------------*/
2407 /* geniCodeReceive - generate intermediate code for "receive" */
2408 /*-----------------------------------------------------------------*/
2409 static void geniCodeReceive (value *args)
2411 /* for all arguments that are passed in registers */
2414 if (IS_REGPARM(args->etype)) {
2415 operand *opr = operandFromValue(args);
2417 symbol *sym = OP_SYMBOL(opr);
2420 /* we will use it after all optimizations
2421 and before liveRange calculation */
2422 if (!sym->addrtaken && !IS_VOLATILE(sym->etype)) {
2424 if (IN_FARSPACE(SPEC_OCLS(sym->etype)) &&
2425 options.stackAuto == 0 &&
2428 opl = newiTempOperand(args->type,0);
2430 sym->reqv->key = sym->key ;
2431 OP_SYMBOL(sym->reqv)->key = sym->key;
2432 OP_SYMBOL(sym->reqv)->isreqv = 1;
2433 OP_SYMBOL(sym->reqv)->islocal= 0;
2434 SPIL_LOC(sym->reqv) = sym;
2438 ic = newiCode(RECEIVE,NULL,NULL);
2439 currFunc->recvSize = getSize(sym->etype);
2440 IC_RESULT(ic) = opr;
2448 /*-----------------------------------------------------------------*/
2449 /* geniCodeFunctionBody - create the function body */
2450 /*-----------------------------------------------------------------*/
2451 void geniCodeFunctionBody (ast *tree)
2458 /* reset the auto generation */
2464 func = ast2iCode(tree->left);
2465 fetype = getSpec(operandType(func));
2467 savelineno = lineno;
2468 lineno = OP_SYMBOL(func)->lineDef;
2469 /* create an entry label */
2470 geniCodeLabel(entryLabel);
2471 lineno = savelineno;
2473 /* create a proc icode */
2474 ic = newiCode(FUNCTION,func,NULL);
2475 /* if the function has parmas then */
2476 /* save the parameters information */
2477 ic->argLabel.args = tree->values.args ;
2478 ic->lineno = OP_SYMBOL(func)->lineDef;
2482 /* for all parameters that are passed
2483 on registers add a "receive" */
2484 geniCodeReceive( tree->values.args );
2486 /* generate code for the body */
2487 ast2iCode(tree->right);
2489 /* create a label for return */
2490 geniCodeLabel(returnLabel);
2492 /* now generate the end proc */
2493 ic = newiCode(ENDFUNCTION,func,NULL);
2498 /*-----------------------------------------------------------------*/
2499 /* geniCodeReturn - gen icode for 'return' statement */
2500 /*-----------------------------------------------------------------*/
2501 void geniCodeReturn (operand *op)
2505 /* if the operand is present force an rvalue */
2507 op = geniCodeRValue(op,FALSE);
2509 ic = newiCode(RETURN,op,NULL);
2513 /*-----------------------------------------------------------------*/
2514 /* geniCodeIfx - generates code for extended if statement */
2515 /*-----------------------------------------------------------------*/
2516 void geniCodeIfx (ast *tree)
2519 operand *condition = ast2iCode(tree->left);
2522 /* if condition is null then exit */
2526 condition = geniCodeRValue(condition,FALSE);
2528 cetype = getSpec(operandType(condition));
2529 /* if the condition is a literal */
2530 if (IS_LITERAL(cetype)) {
2531 if (floatFromVal(condition->operand.valOperand)) {
2532 if (tree->trueLabel)
2533 geniCodeGoto(tree->trueLabel);
2538 if (tree->falseLabel)
2539 geniCodeGoto (tree->falseLabel);
2546 if ( tree->trueLabel ) {
2547 ic = newiCodeCondition(condition,
2552 if ( tree->falseLabel)
2553 geniCodeGoto(tree->falseLabel);
2556 ic = newiCodeCondition (condition,
2563 ast2iCode(tree->right);
2566 /*-----------------------------------------------------------------*/
2567 /* geniCodeJumpTable - tries to create a jump table for switch */
2568 /*-----------------------------------------------------------------*/
2569 int geniCodeJumpTable (operand *cond, value *caseVals, ast *tree)
2571 int min = 0 ,max = 0, t, cnt = 0;
2576 set *labels = NULL ;
2578 if (!tree || !caseVals)
2581 /* the criteria for creating a jump table is */
2582 /* all integer numbers between the maximum & minimum must */
2583 /* be present , the maximum value should not exceed 255 */
2584 min = max = (int)floatFromVal(vch = caseVals);
2585 sprintf(buffer,"_case_%d_%d",
2586 tree->values.switchVals.swNum,
2588 addSet(&labels,newiTempLabel(buffer));
2590 /* if there is only one case value then no need */
2591 if (!(vch = vch->next ))
2595 if (((t = (int)floatFromVal(vch)) - max) != 1)
2597 sprintf(buffer,"_case_%d_%d",
2598 tree->values.switchVals.swNum,
2600 addSet(&labels,newiTempLabel(buffer));
2606 /* if the number of case statements <= 2 then */
2607 /* it is not economical to create the jump table */
2608 /* since two compares are needed for boundary conditions */
2609 if ((! optimize.noJTabBoundary && cnt <= 2) || max > (255/3))
2612 if ( tree->values.switchVals.swDefault )
2613 sprintf (buffer,"_default_%d",tree->values.switchVals.swNum);
2615 sprintf (buffer,"_swBrk_%d",tree->values.switchVals.swNum );
2617 falseLabel = newiTempLabel (buffer);
2619 /* so we can create a jumptable */
2620 /* first we rule out the boundary conditions */
2621 /* if only optimization says so */
2622 if ( ! optimize.noJTabBoundary ) {
2623 link *cetype = getSpec(operandType(cond));
2624 /* no need to check the lower bound if
2625 the condition is unsigned & minimum value is zero */
2626 if (!( min == 0 && SPEC_USIGN(cetype))) {
2627 boundary = geniCodeLogic (cond,operandFromLit(min),'<');
2628 ic = newiCodeCondition (boundary,falseLabel,NULL);
2632 /* now for upper bounds */
2633 boundary = geniCodeLogic(cond,operandFromLit(max),'>');
2634 ic = newiCodeCondition (boundary,falseLabel,NULL);
2638 /* if the min is not zero then we no make it zero */
2640 cond = geniCodeSubtract(cond,operandFromLit(min));
2641 setOperandType(cond, UCHARTYPE);
2644 /* now create the jumptable */
2645 ic = newiCode(JUMPTABLE,NULL,NULL);
2646 IC_JTCOND(ic) = cond;
2647 IC_JTLABELS(ic) = labels;
2652 /*-----------------------------------------------------------------*/
2653 /* geniCodeSwitch - changes a switch to a if statement */
2654 /*-----------------------------------------------------------------*/
2655 void geniCodeSwitch (ast *tree)
2658 operand *cond = geniCodeRValue(ast2iCode (tree->left),FALSE);
2659 value *caseVals = tree->values.switchVals.swVals ;
2660 symbol *trueLabel , *falseLabel;
2662 /* if we can make this a jump table */
2663 if ( geniCodeJumpTable (cond,caseVals,tree) )
2664 goto jumpTable ; /* no need for the comparison */
2666 /* for the cases defined do */
2669 operand *compare = geniCodeLogic (cond,
2670 operandFromValue(caseVals),
2673 sprintf(buffer,"_case_%d_%d",
2674 tree->values.switchVals.swNum,
2675 (int) floatFromVal(caseVals));
2676 trueLabel = newiTempLabel(buffer);
2678 ic = newiCodeCondition(compare,trueLabel,NULL);
2680 caseVals = caseVals->next;
2685 /* if default is present then goto break else break */
2686 if ( tree->values.switchVals.swDefault )
2687 sprintf (buffer,"_default_%d",tree->values.switchVals.swNum);
2689 sprintf (buffer,"_swBrk_%d",tree->values.switchVals.swNum );
2691 falseLabel = newiTempLabel (buffer);
2692 geniCodeGoto(falseLabel);
2695 ast2iCode(tree->right);
2698 /*-----------------------------------------------------------------*/
2699 /* geniCodeInline - intermediate code for inline assembler */
2700 /*-----------------------------------------------------------------*/
2701 static void geniCodeInline (ast *tree)
2705 ic = newiCode(INLINEASM,NULL,NULL);
2706 IC_INLINE(ic) = tree->values.inlineasm;
2710 /*-----------------------------------------------------------------*/
2711 /* ast2iCode - creates an icodeList from an ast */
2712 /*-----------------------------------------------------------------*/
2713 operand *ast2iCode (ast *tree)
2715 operand *left = NULL;
2716 operand *right= NULL;
2721 /* set the global variables for filename & line number */
2722 if ( tree->filename )
2723 filename = tree->filename ;
2725 lineno = tree->lineno ;
2727 block = tree->block ;
2729 scopeLevel = tree->level;
2731 if (tree->type == EX_VALUE )
2732 return operandFromValue(tree->opval.val);
2734 if (tree->type == EX_LINK )
2735 return operandFromLink (tree->opval.lnk);
2737 /* if we find a nullop */
2738 if (tree->type == EX_OP &&
2739 ( tree->opval.op == NULLOP ||
2740 tree->opval.op == BLOCK )) {
2741 ast2iCode (tree->left);
2742 ast2iCode (tree->right);
2746 /* special cases for not evaluating */
2747 if ( tree->opval.op != ':' &&
2748 tree->opval.op != '?' &&
2749 tree->opval.op != CALL &&
2750 tree->opval.op != IFX &&
2751 tree->opval.op != LABEL &&
2752 tree->opval.op != GOTO &&
2753 tree->opval.op != SWITCH &&
2754 tree->opval.op != FUNCTION &&
2755 tree->opval.op != INLINEASM ) {
2757 if (IS_ASSIGN_OP(tree->opval.op) ||
2758 IS_DEREF_OP(tree) ||
2759 (tree->opval.op == '&' && !tree->right) ||
2760 tree->opval.op == PTR_OP) {
2762 if ((IS_ARRAY_OP(tree->left) && IS_ARRAY_OP(tree->left->left)) ||
2763 (IS_DEREF_OP(tree) && IS_ARRAY_OP(tree->left)))
2765 int olvr = lvaluereq ;
2767 left = operandFromAst(tree->left);
2768 lvaluereq = olvr - 1;
2770 left = operandFromAst(tree->left);
2773 if (IS_DEREF_OP(tree) && IS_DEREF_OP(tree->left))
2774 left = geniCodeRValue(left,TRUE);
2776 left = operandFromAst(tree->left);
2778 if (tree->opval.op == INC_OP ||
2779 tree->opval.op == DEC_OP) {
2781 right= operandFromAst(tree->right);
2784 right= operandFromAst(tree->right);
2788 /* now depending on the type of operand */
2789 /* this will be a biggy */
2790 switch (tree->opval.op) {
2792 case '[' : /* array operation */
2794 link *ltype = operandType(left);
2795 left= geniCodeRValue (left,IS_PTR(ltype->next) ? TRUE : FALSE);
2796 right=geniCodeRValue (right,TRUE);
2799 return geniCodeArray (left,right);
2801 case '.' : /* structure dereference */
2802 if (IS_PTR(operandType(left)))
2803 left = geniCodeRValue(left,TRUE);
2805 left = geniCodeRValue(left,FALSE);
2807 return geniCodeStruct (left,right,tree->lvalue);
2809 case PTR_OP: /* structure pointer dereference */
2812 pType = operandType(left);
2813 left = geniCodeRValue(left,TRUE);
2815 setOClass (pType,getSpec(operandType(left)));
2818 return geniCodeStruct (left, right,tree->lvalue);
2820 case INC_OP: /* increment operator */
2822 return geniCodePostInc (left);
2824 return geniCodePreInc (right);
2826 case DEC_OP: /* decrement operator */
2828 return geniCodePostDec (left);
2830 return geniCodePreDec (right);
2832 case '&' : /* bitwise and or address of operator */
2833 if ( right ) { /* this is a bitwise operator */
2834 left= geniCodeRValue(left,FALSE);
2835 right= geniCodeRValue(right,FALSE);
2836 return geniCodeBitwise (left,right,BITWISEAND,tree->ftype);
2838 return geniCodeAddressOf (left);
2840 case '|': /* bitwise or & xor */
2842 return geniCodeBitwise (geniCodeRValue(left,FALSE),
2843 geniCodeRValue(right,FALSE),
2848 return geniCodeDivision (geniCodeRValue(left,FALSE),
2849 geniCodeRValue(right,FALSE));
2852 return geniCodeModulus (geniCodeRValue(left,FALSE),
2853 geniCodeRValue(right,FALSE));
2856 return geniCodeMultiply (geniCodeRValue(left,FALSE),
2857 geniCodeRValue(right,FALSE));
2859 return geniCodeDerefPtr (geniCodeRValue(left,FALSE));
2863 return geniCodeSubtract (geniCodeRValue(left,FALSE),
2864 geniCodeRValue(right,FALSE));
2866 return geniCodeUnaryMinus (geniCodeRValue(left,FALSE));
2870 return geniCodeAdd (geniCodeRValue(left,FALSE),
2871 geniCodeRValue(right,FALSE));
2873 return geniCodeRValue(left,FALSE) ; /* unary '+' has no meaning */
2876 return geniCodeLeftShift (geniCodeRValue(left,FALSE),
2877 geniCodeRValue(right,FALSE));
2880 return geniCodeRightShift (geniCodeRValue(left,FALSE),
2881 geniCodeRValue(right,FALSE));
2883 return geniCodeCast (operandType(left),
2884 geniCodeRValue(right,FALSE),FALSE);
2890 return geniCodeUnary (geniCodeRValue(left,FALSE),tree->opval.op);
2894 operand *op = geniCodeUnary (geniCodeRValue(left,FALSE),tree->opval.op);
2895 setOperandType(op, UCHARTYPE);
2906 return geniCodeLogic (geniCodeRValue(left,FALSE),
2907 geniCodeRValue(right,FALSE),
2910 return geniCodeConditional (tree);
2913 return operandFromLit(getSize(tree->right->ftype));
2917 link *rtype = operandType(right);
2918 link *ltype = operandType(left);
2919 if (IS_PTR(rtype) && IS_ITEMP(right)
2920 && right->isaddr && checkType(rtype->next,ltype)==1)
2921 right = geniCodeRValue(right,TRUE);
2923 right = geniCodeRValue(right,FALSE);
2925 geniCodeAssign (left,right,0);
2930 geniCodeAssign(left,
2931 geniCodeMultiply(geniCodeRValue (operandFromOperand(left),
2933 geniCodeRValue(right,FALSE)),0);
2937 geniCodeAssign(left,
2938 geniCodeDivision(geniCodeRValue(operandFromOperand(left),
2940 geniCodeRValue(right,FALSE)),0);
2943 geniCodeAssign(left,
2944 geniCodeModulus(geniCodeRValue(operandFromOperand(left),
2946 geniCodeRValue(right,FALSE)),0);
2949 link *rtype = operandType(right);
2950 link *ltype = operandType(left);
2951 if (IS_PTR(rtype) && IS_ITEMP(right)
2952 && right->isaddr && checkType(rtype->next,ltype)==1)
2953 right = geniCodeRValue(right,TRUE);
2955 right = geniCodeRValue(right,FALSE);
2958 return geniCodeAssign(left,
2959 geniCodeAdd (geniCodeRValue(operandFromOperand(left),
2965 link *rtype = operandType(right);
2966 link *ltype = operandType(left);
2967 if (IS_PTR(rtype) && IS_ITEMP(right)
2968 && right->isaddr && checkType(rtype->next,ltype)==1) {
2969 right = geniCodeRValue(right,TRUE);
2972 right = geniCodeRValue(right,FALSE);
2975 geniCodeAssign (left,
2976 geniCodeSubtract(geniCodeRValue(operandFromOperand(left),
2982 geniCodeAssign (left,
2983 geniCodeLeftShift(geniCodeRValue(operandFromOperand(left)
2985 geniCodeRValue(right,FALSE)),0);
2988 geniCodeAssign(left,
2989 geniCodeRightShift(geniCodeRValue(operandFromOperand(left)
2991 geniCodeRValue(right,FALSE)),0);
2994 geniCodeAssign (left,
2995 geniCodeBitwise(geniCodeRValue(operandFromOperand(left),
2997 geniCodeRValue(right,FALSE),
2999 operandType(left)),0);
3002 geniCodeAssign (left,
3003 geniCodeBitwise (geniCodeRValue(operandFromOperand(left),
3005 geniCodeRValue(right,FALSE),
3007 operandType(left)),0);
3010 geniCodeAssign (left,
3011 geniCodeBitwise (geniCodeRValue(operandFromOperand(left)
3013 geniCodeRValue(right,FALSE),
3015 operandType(left)),0);
3017 return geniCodeRValue(right,FALSE);
3020 return geniCodeCall (ast2iCode(tree->left),
3023 geniCodeLabel(ast2iCode(tree->left)->operand.symOperand);
3024 return ast2iCode (tree->right);
3027 geniCodeGoto (ast2iCode(tree->left)->operand.symOperand);
3028 return ast2iCode (tree->right);
3031 geniCodeFunctionBody ( tree );
3035 geniCodeReturn (right);
3043 geniCodeSwitch (tree);
3047 geniCodeInline (tree);
3054 /*-----------------------------------------------------------------*/
3055 /* reverseICChain - gets from the list and creates a linkedlist */
3056 /*-----------------------------------------------------------------*/
3057 iCode *reverseiCChain ()
3059 iCode *loop = NULL ;
3060 iCode *prev = NULL ;
3062 while ((loop = getSet(&iCodeChain))) {
3073 /*-----------------------------------------------------------------*/
3074 /* iCodeFromAst - given an ast will convert it to iCode */
3075 /*-----------------------------------------------------------------*/
3076 iCode *iCodeFromAst ( ast *tree )
3078 returnLabel = newiTempLabel("_return");
3079 entryLabel = newiTempLabel("_entry") ;
3081 return reverseiCChain ();