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 -------------------------------------------------------------------------*/
29 /*-----------------------------------------------------------------*/
30 /* global variables */
32 set *iCodeChain = NULL;
42 symbol *returnLabel; /* function return label */
43 symbol *entryLabel; /* function entry label */
45 /*-----------------------------------------------------------------*/
46 /* forward definition of some functions */
47 operand *geniCodeDivision (operand *, operand *);
48 operand *geniCodeAssign (operand *, operand *, int);
49 operand *geniCodeArray (operand *, operand *,int);
50 operand *geniCodeArray2Ptr (operand *);
51 operand *geniCodeRValue (operand *, bool);
52 operand *geniCodeDerefPtr (operand *,int);
53 int isLvaluereq(int lvl);
55 #define PRINTFUNC(x) void x (FILE *of, iCode *ic, char *s)
56 /* forward definition of ic print functions */
57 PRINTFUNC (picGetValueAtAddr);
58 PRINTFUNC (picSetValueAtAddr);
59 PRINTFUNC (picAddrOf);
60 PRINTFUNC (picGeneric);
61 PRINTFUNC (picGenericOne);
63 PRINTFUNC (picAssign);
67 PRINTFUNC (picJumpTable);
68 PRINTFUNC (picInline);
69 PRINTFUNC (picReceive);
71 iCodeTable codeTable[] =
73 {'!', "not", picGenericOne, NULL},
74 {'~', "~", picGenericOne, NULL},
75 {RRC, "rrc", picGenericOne, NULL},
76 {RLC, "rlc", picGenericOne, NULL},
77 {GETHBIT, "ghbit", picGenericOne, NULL},
78 {UNARYMINUS, "-", picGenericOne, NULL},
79 {IPUSH, "push", picGenericOne, NULL},
80 {IPOP, "pop", picGenericOne, NULL},
81 {CALL, "call", picGenericOne, NULL},
82 {PCALL, "pcall", picGenericOne, NULL},
83 {FUNCTION, "proc", picGenericOne, NULL},
84 {ENDFUNCTION, "eproc", picGenericOne, NULL},
85 {RETURN, "ret", picGenericOne, NULL},
86 {'+', "+", picGeneric, NULL},
87 {'-', "-", picGeneric, NULL},
88 {'*', "*", picGeneric, NULL},
89 {'/', "/", picGeneric, NULL},
90 {'%', "%", picGeneric, NULL},
91 {'>', ">", picGeneric, NULL},
92 {'<', "<", picGeneric, NULL},
93 {LE_OP, "<=", picGeneric, NULL},
94 {GE_OP, ">=", picGeneric, NULL},
95 {EQ_OP, "==", picGeneric, NULL},
96 {NE_OP, "!=", picGeneric, NULL},
97 {AND_OP, "&&", picGeneric, NULL},
98 {OR_OP, "||", picGeneric, NULL},
99 {'^', "^", picGeneric, NULL},
100 {'|', "|", picGeneric, NULL},
101 {BITWISEAND, "&", picGeneric, NULL},
102 {LEFT_OP, "<<", picGeneric, NULL},
103 {RIGHT_OP, ">>", picGeneric, NULL},
104 {GET_VALUE_AT_ADDRESS, "@", picGetValueAtAddr, NULL},
105 {ADDRESS_OF, "&", picAddrOf, NULL},
106 {CAST, "<>", picCast, NULL},
107 {'=', ":=", picAssign, NULL},
108 {LABEL, "", picLabel, NULL},
109 {GOTO, "", picGoto, NULL},
110 {JUMPTABLE, "jtab", picJumpTable, NULL},
111 {IFX, "if", picIfx, NULL},
112 {INLINEASM, "", picInline, NULL},
113 {RECEIVE, "recv", picReceive, NULL},
114 {SEND, "send", picGenericOne, NULL},
115 {ARRAYINIT, "arrayInit", picGenericOne, NULL},
118 /*-----------------------------------------------------------------*/
119 /* checkConstantRange: check a constant against the type */
120 /*-----------------------------------------------------------------*/
122 /* pedantic=0: allmost anything is allowed as long as the absolute
123 value is within the bit range of the type, and -1 is treated as
124 0xf..f for unsigned types (e.g. in assign)
125 pedantic=1: -1==-1, not allowed for unsigned types (e.g. in compare)
126 pedantic>1: "char c=200" is not allowed (evaluates to -56)
129 void checkConstantRange(sym_link *ltype, value *val, char *msg,
136 max = pow ((double)2.0, (double)bitsForType(ltype));
138 if (SPEC_LONG(val->type)) {
139 if (SPEC_USIGN(val->type)) {
140 v=SPEC_CVAL(val->type).v_ulong;
142 v=SPEC_CVAL(val->type).v_long;
145 if (SPEC_USIGN(val->type)) {
146 v=SPEC_CVAL(val->type).v_uint;
148 v=SPEC_CVAL(val->type).v_int;
154 // this could be a good idea
155 if (options.pedantic)
159 if (SPEC_NOUN(ltype)==FLOAT) {
164 if (!SPEC_USIGN(val->type) && v<0) {
166 if (SPEC_USIGN(ltype) && (pedantic>1)) {
172 // if very pedantic: "char c=200" is not allowed
173 if (pedantic>1 && !SPEC_USIGN(ltype)) {
174 max = max/2 + negative;
181 #if 0 // temporary disabled, leaving the warning as a reminder
183 sprintf (message, "for %s %s in %s",
184 SPEC_USIGN(ltype) ? "unsigned" : "signed",
185 nounName(ltype), msg);
186 werror (W_CONST_RANGE, message);
194 /*-----------------------------------------------------------------*/
195 /* operandName - returns the name of the operand */
196 /*-----------------------------------------------------------------*/
198 printOperand (operand * op, FILE * file)
215 opetype = getSpec (operandType (op));
216 if (SPEC_NOUN (opetype) == V_FLOAT)
217 fprintf (file, "%g {", SPEC_CVAL (opetype).v_float);
219 fprintf (file, "0x%x {", (int) floatFromVal (op->operand.valOperand));
220 printTypeChain (operandType (op), file);
227 fprintf (file, "%s [k%d lr%d:%d so:%d]{ ia%d re%d rm%d nos%d ru%d dp%d}", /*{ar%d rm%d ru%d p%d a%d u%d i%d au%d k%d ks%d}" , */
228 (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name),
230 OP_LIVEFROM (op), OP_LIVETO (op),
231 OP_SYMBOL (op)->stack,
232 op->isaddr, OP_SYMBOL (op)->isreqv,
233 OP_SYMBOL (op)->remat,OP_SYMBOL(op)->noSpilLoc,
234 OP_SYMBOL(op)->ruonly,OP_SYMBOL(op)->dptr
238 printTypeChain (operandType (op), file);
239 if (SPIL_LOC (op) && IS_ITEMP (op))
240 fprintf (file, "}{ sir@ %s", SPIL_LOC (op)->rname);
245 /* if assigned to registers */
246 if (OP_SYMBOL (op)->nRegs)
248 if (OP_SYMBOL (op)->isspilt)
250 if (!OP_SYMBOL (op)->remat)
251 if (OP_SYMBOL (op)->usl.spillLoc)
252 fprintf (file, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ?
253 OP_SYMBOL (op)->usl.spillLoc->rname :
254 OP_SYMBOL (op)->usl.spillLoc->name));
256 fprintf (file, "[err]");
258 fprintf (file, "[remat]");
264 for (i = 0; i < OP_SYMBOL (op)->nRegs; i++)
265 fprintf (file, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i]));
270 fprintf (file, "%s", (OP_SYMBOL (op)->rname[0] ?
271 OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name));
272 /* if assigned to registers */
273 if (OP_SYMBOL (op)->nRegs && !OP_SYMBOL (op)->isspilt)
277 for (i = 0; i < OP_SYMBOL (op)->nRegs; i++)
278 fprintf (file, "%s ", (OP_SYMBOL (op)->regs[i] ?
279 OP_SYMBOL (op)->regs[i]->name :
288 printTypeChain (op->operand.typeOperand, file);
294 fprintf (file, "\n");
299 /*-----------------------------------------------------------------*/
300 /* print functions */
301 /*-----------------------------------------------------------------*/
302 PRINTFUNC (picGetValueAtAddr)
305 printOperand (IC_RESULT (ic), of);
308 printOperand (IC_LEFT (ic), of);
314 PRINTFUNC (picSetValueAtAddr)
318 printOperand (IC_LEFT (ic), of);
319 fprintf (of, "] = ");
320 printOperand (IC_RIGHT (ic), of);
324 PRINTFUNC (picAddrOf)
327 printOperand (IC_RESULT (ic), of);
328 if (IS_ITEMP (IC_LEFT (ic)))
331 fprintf (of, " = &[");
332 printOperand (IC_LEFT (ic), of);
335 if (IS_ITEMP (IC_LEFT (ic)))
336 fprintf (of, " offsetAdd ");
339 printOperand (IC_RIGHT (ic), of);
341 if (IS_ITEMP (IC_LEFT (ic)))
347 PRINTFUNC (picJumpTable)
352 fprintf (of, "%s\t", s);
353 printOperand (IC_JTCOND (ic), of);
355 for (sym = setFirstItem (IC_JTLABELS (ic)); sym;
356 sym = setNextItem (IC_JTLABELS (ic)))
357 fprintf (of, "\t\t\t%s\n", sym->name);
360 PRINTFUNC (picGeneric)
363 printOperand (IC_RESULT (ic), of);
365 printOperand (IC_LEFT (ic), of);
366 fprintf (of, " %s ", s);
367 printOperand (IC_RIGHT (ic), of);
371 PRINTFUNC (picGenericOne)
376 printOperand (IC_RESULT (ic), of);
382 fprintf (of, "%s ", s);
383 printOperand (IC_LEFT (ic), of);
386 if (!IC_RESULT (ic) && !IC_LEFT (ic))
389 if (ic->op == SEND || ic->op == RECEIVE) {
390 fprintf(of,"{argreg = %d}",ic->argreg);
398 printOperand (IC_RESULT (ic), of);
400 printOperand (IC_LEFT (ic), of);
401 printOperand (IC_RIGHT (ic), of);
406 PRINTFUNC (picAssign)
410 if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic)))
413 printOperand (IC_RESULT (ic), of);
415 if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic)))
418 fprintf (of, " %s ", s);
419 printOperand (IC_RIGHT (ic), of);
426 fprintf (of, " %s($%d) :\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key);
432 fprintf (of, " goto %s($%d)\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key);
439 printOperand (IC_COND (ic), of);
442 fprintf (of, " == 0 goto %s($%d)\n", IC_FALSE (ic)->name, IC_FALSE (ic)->key);
445 fprintf (of, " != 0 goto %s($%d)\n", IC_TRUE (ic)->name, IC_TRUE (ic)->key);
447 fprintf (of, "\tzzgoto %s\n", IC_FALSE (ic)->name);
451 PRINTFUNC (picInline)
453 fprintf (of, "%s", IC_INLINE (ic));
456 PRINTFUNC (picReceive)
458 printOperand (IC_RESULT (ic), of);
459 fprintf (of, " = %s ", s);
460 printOperand (IC_LEFT (ic), of);
464 /*-----------------------------------------------------------------*/
465 /* piCode - prints one iCode */
466 /*-----------------------------------------------------------------*/
468 piCode (void *item, FILE * of)
476 icTab = getTableEntry (ic->op);
477 fprintf (stdout, "%s(%d:%d:%d:%d:%d)\t",
478 ic->filename, ic->lineno,
479 ic->seq, ic->key, ic->depth, ic->supportRtn);
480 icTab->iCodePrint (of, ic, icTab->printName);
486 printiCChain(ic,stdout);
488 /*-----------------------------------------------------------------*/
489 /* printiCChain - prints intermediate code for humans */
490 /*-----------------------------------------------------------------*/
492 printiCChain (iCode * icChain, FILE * of)
499 for (loop = icChain; loop; loop = loop->next)
501 if ((icTab = getTableEntry (loop->op)))
503 fprintf (of, "%s(%d:%d:%d:%d:%d)\t",
504 loop->filename, loop->lineno,
505 loop->seq, loop->key, loop->depth, loop->supportRtn);
507 icTab->iCodePrint (of, loop, icTab->printName);
513 /*-----------------------------------------------------------------*/
514 /* newOperand - allocate, init & return a new iCode */
515 /*-----------------------------------------------------------------*/
521 op = Safe_alloc ( sizeof (operand));
527 /*-----------------------------------------------------------------*/
528 /* newiCode - create and return a new iCode entry initialised */
529 /*-----------------------------------------------------------------*/
531 newiCode (int op, operand * left, operand * right)
535 ic = Safe_alloc ( sizeof (iCode));
538 ic->filename = filename;
540 ic->level = scopeLevel;
542 ic->key = iCodeKey++;
544 IC_RIGHT (ic) = right;
549 /*-----------------------------------------------------------------*/
550 /* newiCode for conditional statements */
551 /*-----------------------------------------------------------------*/
553 newiCodeCondition (operand * condition,
559 if (IS_VOID(operandType(condition))) {
560 werror(E_VOID_VALUE_USED);
563 ic = newiCode (IFX, NULL, NULL);
564 IC_COND (ic) = condition;
565 IC_TRUE (ic) = trueLabel;
566 IC_FALSE (ic) = falseLabel;
570 /*-----------------------------------------------------------------*/
571 /* newiCodeLabelGoto - unconditional goto statement| label stmnt */
572 /*-----------------------------------------------------------------*/
574 newiCodeLabelGoto (int op, symbol * label)
578 ic = newiCode (op, NULL, NULL);
580 ic->argLabel.label = label;
582 IC_RIGHT (ic) = NULL;
583 IC_RESULT (ic) = NULL;
587 /*-----------------------------------------------------------------*/
588 /* newiTemp - allocate & return a newItemp Variable */
589 /*-----------------------------------------------------------------*/
596 sprintf (buffer, "%s", s);
598 sprintf (buffer, "iTemp%d", iTempNum++);
599 itmp = newSymbol (buffer, 1);
600 strcpy (itmp->rname, itmp->name);
606 /*-----------------------------------------------------------------*/
607 /* newiTempLabel - creates a temp variable label */
608 /*-----------------------------------------------------------------*/
610 newiTempLabel (char *s)
614 /* check if this alredy exists */
615 if (s && (itmplbl = findSym (LabelTab, NULL, s)))
619 itmplbl = newSymbol (s, 1);
622 sprintf (buffer, "iTempLbl%d", iTempLblNum++);
623 itmplbl = newSymbol (buffer, 1);
628 itmplbl->key = labelKey++;
629 addSym (LabelTab, itmplbl, itmplbl->name, 0, 0, 0);
633 /*-----------------------------------------------------------------*/
634 /* newiTempPreheaderLabel - creates a new preheader label */
635 /*-----------------------------------------------------------------*/
637 newiTempPreheaderLabel ()
641 sprintf (buffer, "preHeaderLbl%d", iTempLblNum++);
642 itmplbl = newSymbol (buffer, 1);
646 itmplbl->key = labelKey++;
647 addSym (LabelTab, itmplbl, itmplbl->name, 0, 0, 0);
652 /*-----------------------------------------------------------------*/
653 /* initiCode - initialises some iCode related stuff */
654 /*-----------------------------------------------------------------*/
661 /*-----------------------------------------------------------------*/
662 /* copyiCode - make a copy of the iCode given */
663 /*-----------------------------------------------------------------*/
665 copyiCode (iCode * ic)
667 iCode *nic = newiCode (ic->op, NULL, NULL);
669 nic->lineno = ic->lineno;
670 nic->filename = ic->filename;
671 nic->block = ic->block;
672 nic->level = ic->level;
673 nic->parmBytes = ic->parmBytes;
675 /* deal with the special cases first */
679 IC_COND (nic) = operandFromOperand (IC_COND (ic));
680 IC_TRUE (nic) = IC_TRUE (ic);
681 IC_FALSE (nic) = IC_FALSE (ic);
685 IC_JTCOND (nic) = operandFromOperand (IC_JTCOND (ic));
686 IC_JTLABELS (nic) = IC_JTLABELS (ic);
691 IC_RESULT (nic) = operandFromOperand (IC_RESULT (ic));
692 IC_LEFT (nic) = operandFromOperand (IC_LEFT (ic));
696 IC_INLINE (nic) = IC_INLINE (ic);
700 IC_ARRAYILIST(nic) = IC_ARRAYILIST(ic);
704 IC_RESULT (nic) = operandFromOperand (IC_RESULT (ic));
705 IC_LEFT (nic) = operandFromOperand (IC_LEFT (ic));
706 IC_RIGHT (nic) = operandFromOperand (IC_RIGHT (ic));
712 /*-----------------------------------------------------------------*/
713 /* getTableEntry - gets the table entry for the given operator */
714 /*-----------------------------------------------------------------*/
716 getTableEntry (int oper)
720 for (i = 0; i < (sizeof (codeTable) / sizeof (iCodeTable)); i++)
721 if (oper == codeTable[i].icode)
722 return &codeTable[i];
727 /*-----------------------------------------------------------------*/
728 /* newiTempOperand - new intermediate temp operand */
729 /*-----------------------------------------------------------------*/
731 newiTempOperand (sym_link * type, char throwType)
734 operand *op = newOperand ();
738 itmp = newiTemp (NULL);
740 etype = getSpec (type);
742 if (IS_LITERAL (etype))
745 /* copy the type information */
747 itmp->etype = getSpec (itmp->type = (throwType ? type :
748 copyLinkChain (type)));
749 if (IS_LITERAL (itmp->etype))
751 SPEC_SCLS (itmp->etype) = S_REGISTER;
752 SPEC_OCLS (itmp->etype) = reg;
755 op->operand.symOperand = itmp;
756 op->key = itmp->key = ++operandKey;
760 /*-----------------------------------------------------------------*/
761 /* operandType - returns the type chain for an operand */
762 /*-----------------------------------------------------------------*/
764 operandType (operand * op)
766 /* depending on type of operand */
771 return op->operand.valOperand->type;
774 return op->operand.symOperand->type;
777 return op->operand.typeOperand;
779 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
780 " operand type not known ");
781 assert (0); /* should never come here */
782 /* Just to keep the compiler happy */
783 return (sym_link *) 0;
787 /*-----------------------------------------------------------------*/
788 /* isParamterToCall - will return 1 if op is a parameter to args */
789 /*-----------------------------------------------------------------*/
791 isParameterToCall (value * args, operand * op)
798 isSymbolEqual (op->operand.symOperand, tval->sym))
805 /*-----------------------------------------------------------------*/
806 /* isOperandGlobal - return 1 if operand is a global variable */
807 /*-----------------------------------------------------------------*/
809 isOperandGlobal (operand * op)
817 if (op->type == SYMBOL &&
818 (op->operand.symOperand->level == 0 ||
819 IS_STATIC (op->operand.symOperand->etype) ||
820 IS_EXTERN (op->operand.symOperand->etype))
827 /*-----------------------------------------------------------------*/
828 /* isOperandVolatile - return 1 if the operand is volatile */
829 /*-----------------------------------------------------------------*/
831 isOperandVolatile (operand * op, bool chkTemp)
836 if (IS_ITEMP (op) && !chkTemp)
839 opetype = getSpec (optype = operandType (op));
841 if (IS_PTR (optype) && DCL_PTR_VOLATILE (optype))
844 if (IS_VOLATILE (opetype))
849 /*-----------------------------------------------------------------*/
850 /* isOperandLiteral - returns 1 if an operand contains a literal */
851 /*-----------------------------------------------------------------*/
853 isOperandLiteral (operand * op)
860 opetype = getSpec (operandType (op));
862 if (IS_LITERAL (opetype))
868 /*-----------------------------------------------------------------*/
869 /* isOperandInFarSpace - will return true if operand is in farSpace */
870 /*-----------------------------------------------------------------*/
872 isOperandInFarSpace (operand * op)
882 if (!IS_TRUE_SYMOP (op))
885 etype = SPIL_LOC (op)->etype;
891 etype = getSpec (operandType (op));
893 return (IN_FARSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
896 /*------------------------------------------------------------------*/
897 /* isOperandInDirSpace - will return true if operand is in dirSpace */
898 /*------------------------------------------------------------------*/
900 isOperandInDirSpace (operand * op)
910 if (!IS_TRUE_SYMOP (op))
913 etype = SPIL_LOC (op)->etype;
919 etype = getSpec (operandType (op));
921 return (IN_DIRSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
924 /*--------------------------------------------------------------------*/
925 /* isOperandInCodeSpace - will return true if operand is in codeSpace */
926 /*--------------------------------------------------------------------*/
928 isOperandInCodeSpace (operand * op)
938 etype = getSpec (operandType (op));
940 if (!IS_TRUE_SYMOP (op))
943 etype = SPIL_LOC (op)->etype;
949 etype = getSpec (operandType (op));
951 return (IN_CODESPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
954 /*-----------------------------------------------------------------*/
955 /* isOperandOnStack - will return true if operand is on stack */
956 /*-----------------------------------------------------------------*/
958 isOperandOnStack (operand * op)
968 etype = getSpec (operandType (op));
969 if (IN_STACK (etype) ||
970 OP_SYMBOL(op)->onStack ||
971 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
977 /*-----------------------------------------------------------------*/
978 /* operandLitValue - literal value of an operand */
979 /*-----------------------------------------------------------------*/
981 operandLitValue (operand * op)
983 assert (isOperandLiteral (op));
985 return floatFromVal (op->operand.valOperand);
988 /*-----------------------------------------------------------------*/
989 /* getBuiltInParms - returns parameters to a builtin functions */
990 /*-----------------------------------------------------------------*/
991 iCode *getBuiltinParms (iCode *ic, int *pcount, operand **parms)
996 /* builtin functions uses only SEND for parameters */
997 while (ic->op != CALL) {
998 assert(ic->op == SEND && ic->builtinSEND);
999 ic->generated = 1; /* mark the icode as generated */
1000 parms[*pcount] = IC_LEFT(ic);
1006 /* make sure this is a builtin function call */
1007 assert(IS_SYMOP(IC_LEFT(ic)));
1008 ftype = operandType(IC_LEFT(ic));
1009 assert(IFFUNC_ISBUILTIN(ftype));
1013 /*-----------------------------------------------------------------*/
1014 /* operandOperation - perforoms operations on operands */
1015 /*-----------------------------------------------------------------*/
1017 operandOperation (operand * left, operand * right,
1018 int op, sym_link * type)
1020 sym_link *let , *ret=NULL;
1021 operand *retval = (operand *) 0;
1023 assert (isOperandLiteral (left));
1024 let = getSpec(operandType(left));
1026 assert (isOperandLiteral (right));
1027 ret = getSpec(operandType(left));
1033 retval = operandFromValue (valCastLiteral (type,
1034 operandLitValue (left) +
1035 operandLitValue (right)));
1038 retval = operandFromValue (valCastLiteral (type,
1039 operandLitValue (left) -
1040 operandLitValue (right)));
1043 retval = operandFromValue (valCastLiteral (type,
1044 operandLitValue (left) *
1045 operandLitValue (right)));
1048 if ((unsigned long) operandLitValue (right) == 0)
1050 werror (E_DIVIDE_BY_ZERO);
1055 retval = operandFromValue (valCastLiteral (type,
1056 operandLitValue (left) /
1057 operandLitValue (right)));
1060 if ((unsigned long) operandLitValue (right) == 0) {
1061 werror (E_DIVIDE_BY_ZERO);
1065 retval = operandFromLit ((SPEC_USIGN(let) ?
1066 (unsigned long) operandLitValue (left) :
1067 (long) operandLitValue (left)) %
1069 (unsigned long) operandLitValue (right) :
1070 (long) operandLitValue (right)));
1074 retval = operandFromLit ((SPEC_USIGN(let) ?
1075 (unsigned long) operandLitValue (left) :
1076 (long) operandLitValue (left)) <<
1078 (unsigned long) operandLitValue (right) :
1079 (long) operandLitValue (right)));
1082 double lval = operandLitValue(left), rval = operandLitValue(right);
1084 switch ((SPEC_USIGN(let) ? 2 : 0) + (SPEC_USIGN(ret) ? 1 : 0))
1086 case 0: // left=unsigned right=unsigned
1087 res=(unsigned long)lval >> (unsigned long)rval;
1089 case 1: // left=unsigned right=signed
1090 res=(unsigned long)lval >> (signed long)rval;
1092 case 2: // left=signed right=unsigned
1093 res=(signed long)lval >> (unsigned long)rval;
1095 case 3: // left=signed right=signed
1096 res=(signed long)lval >> (signed long)rval;
1099 retval = operandFromLit (res);
1103 retval = operandFromLit (operandLitValue (left) ==
1104 operandLitValue (right));
1107 retval = operandFromLit (operandLitValue (left) <
1108 operandLitValue (right));
1111 retval = operandFromLit (operandLitValue (left) <=
1112 operandLitValue (right));
1115 retval = operandFromLit (operandLitValue (left) !=
1116 operandLitValue (right));
1119 retval = operandFromLit (operandLitValue (left) >
1120 operandLitValue (right));
1123 retval = operandFromLit (operandLitValue (left) >=
1124 operandLitValue (right));
1127 retval = operandFromLit ((long)operandLitValue(left) &
1128 (long)operandLitValue(right));
1131 retval = operandFromLit ((long)operandLitValue (left) |
1132 (long)operandLitValue (right));
1135 retval = operandFromLit ((long)operandLitValue (left) ^
1136 (long)operandLitValue (right));
1139 retval = operandFromLit (operandLitValue (left) &&
1140 operandLitValue (right));
1143 retval = operandFromLit (operandLitValue (left) ||
1144 operandLitValue (right));
1148 long i = (long) operandLitValue (left);
1150 retval = operandFromLit ((i >> (getSize (operandType (left)) * 8 - 1)) |
1156 long i = (long) operandLitValue (left);
1158 retval = operandFromLit ((i << (getSize (operandType (left)) * 8 - 1)) |
1164 retval = operandFromLit (-1 * operandLitValue (left));
1168 retval = operandFromLit (~((long) operandLitValue (left)));
1172 retval = operandFromLit (!operandLitValue (left));
1176 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1177 " operandOperation invalid operator ");
1185 /*-----------------------------------------------------------------*/
1186 /* isOperandEqual - compares two operand & return 1 if they r = */
1187 /*-----------------------------------------------------------------*/
1189 isOperandEqual (operand * left, operand * right)
1191 /* if the pointers are equal then they are equal */
1195 /* if either of them null then false */
1196 if (!left || !right)
1199 if (left->type != right->type)
1202 if (IS_SYMOP (left) && IS_SYMOP (right))
1203 return left->key == right->key;
1205 /* if types are the same */
1209 return isSymbolEqual (left->operand.symOperand,
1210 right->operand.symOperand);
1212 return (floatFromVal (left->operand.valOperand) ==
1213 floatFromVal (right->operand.valOperand));
1215 if (compareType (left->operand.typeOperand,
1216 right->operand.typeOperand) == 1)
1223 /*-------------------------------------------------------------------*/
1224 /* isiCodeEqual - compares two iCodes are equal, returns true if yes */
1225 /*-------------------------------------------------------------------*/
1227 isiCodeEqual (iCode * left, iCode * right)
1229 /* if the same pointer */
1233 /* if either of them null */
1234 if (!left || !right)
1237 /* if operand are the same */
1238 if (left->op == right->op)
1241 /* compare all the elements depending on type */
1242 if (left->op != IFX)
1244 if (!isOperandEqual (IC_LEFT (left), IC_LEFT (right)))
1246 if (!isOperandEqual (IC_RIGHT (left), IC_RIGHT (right)))
1252 if (!isOperandEqual (IC_COND (left), IC_COND (right)))
1254 if (!isSymbolEqual (IC_TRUE (left), IC_TRUE (right)))
1256 if (!isSymbolEqual (IC_FALSE (left), IC_FALSE (right)))
1265 /*-----------------------------------------------------------------*/
1266 /* newiTempFromOp - create a temp Operand with same attributes */
1267 /*-----------------------------------------------------------------*/
1269 newiTempFromOp (operand * op)
1279 nop = newiTempOperand (operandType (op), TRUE);
1280 nop->isaddr = op->isaddr;
1281 nop->isvolatile = op->isvolatile;
1282 nop->isGlobal = op->isGlobal;
1283 nop->isLiteral = op->isLiteral;
1284 nop->usesDefs = op->usesDefs;
1285 nop->isParm = op->isParm;
1289 /*-----------------------------------------------------------------*/
1290 /* operand from operand - creates an operand holder for the type */
1291 /*-----------------------------------------------------------------*/
1293 operandFromOperand (operand * op)
1299 nop = newOperand ();
1300 nop->type = op->type;
1301 nop->isaddr = op->isaddr;
1303 nop->isvolatile = op->isvolatile;
1304 nop->isGlobal = op->isGlobal;
1305 nop->isLiteral = op->isLiteral;
1306 nop->usesDefs = op->usesDefs;
1307 nop->isParm = op->isParm;
1312 nop->operand.symOperand = op->operand.symOperand;
1315 nop->operand.valOperand = op->operand.valOperand;
1318 nop->operand.typeOperand = op->operand.typeOperand;
1325 /*-----------------------------------------------------------------*/
1326 /* opFromOpWithDU - makes a copy of the operand and DU chains */
1327 /*-----------------------------------------------------------------*/
1329 opFromOpWithDU (operand * op, bitVect * defs, bitVect * uses)
1331 operand *nop = operandFromOperand (op);
1333 if (nop->type == SYMBOL)
1335 OP_SYMBOL (nop)->defs = bitVectCopy (defs);
1336 OP_SYMBOL (nop)->uses = bitVectCopy (uses);
1342 /*-----------------------------------------------------------------*/
1343 /* operandFromSymbol - creates an operand from a symbol */
1344 /*-----------------------------------------------------------------*/
1346 operandFromSymbol (symbol * sym)
1351 /* if the symbol's type is a literal */
1352 /* then it is an enumerator type */
1353 if (IS_LITERAL (sym->etype) && SPEC_ENUM (sym->etype))
1354 return operandFromValue (valFromType (sym->etype));
1357 sym->key = ++operandKey;
1359 /* if this an implicit variable, means struct/union */
1360 /* member so just return it */
1361 if (sym->implicit || IS_FUNC (sym->type))
1365 op->operand.symOperand = sym;
1367 op->isvolatile = isOperandVolatile (op, TRUE);
1368 op->isGlobal = isOperandGlobal (op);
1372 /* under the following conditions create a
1373 register equivalent for a local symbol */
1374 if (sym->level && sym->etype && SPEC_OCLS (sym->etype) &&
1375 (IN_FARSPACE (SPEC_OCLS (sym->etype)) &&
1376 (!(options.model == MODEL_FLAT24)) ) &&
1377 options.stackAuto == 0)
1380 if (!IS_AGGREGATE (sym->type) && /* not an aggregate */
1381 !IS_FUNC (sym->type) && /* not a function */
1382 !sym->_isparm && /* not a parameter */
1383 sym->level && /* is a local variable */
1384 !sym->addrtaken && /* whose address has not been taken */
1385 !sym->reqv && /* does not already have a reg equivalence */
1386 !IS_VOLATILE (sym->etype) && /* not declared as volatile */
1387 !IS_STATIC (sym->etype) && /* and not declared static */
1388 !sym->islbl && /* not a label */
1389 ok && /* farspace check */
1390 !IS_BITVAR (sym->etype) /* not a bit variable */
1394 /* we will use it after all optimizations
1395 and before liveRange calculation */
1396 sym->reqv = newiTempOperand (sym->type, 0);
1397 sym->reqv->key = sym->key;
1398 OP_SYMBOL (sym->reqv)->key = sym->key;
1399 OP_SYMBOL (sym->reqv)->isreqv = 1;
1400 OP_SYMBOL (sym->reqv)->islocal = 1;
1401 OP_SYMBOL (sym->reqv)->onStack = sym->onStack;
1402 SPIL_LOC (sym->reqv) = sym;
1405 if (!IS_AGGREGATE (sym->type))
1409 op->operand.symOperand = sym;
1412 op->isvolatile = isOperandVolatile (op, TRUE);
1413 op->isGlobal = isOperandGlobal (op);
1414 op->isPtr = IS_PTR (operandType (op));
1415 op->isParm = sym->_isparm;
1420 /* itemp = &[_symbol] */
1422 ic = newiCode (ADDRESS_OF, newOperand (), NULL);
1423 IC_LEFT (ic)->type = SYMBOL;
1424 IC_LEFT (ic)->operand.symOperand = sym;
1425 IC_LEFT (ic)->key = sym->key;
1426 (IC_LEFT (ic))->isvolatile = isOperandVolatile (IC_LEFT (ic), TRUE);
1427 (IC_LEFT (ic))->isGlobal = isOperandGlobal (IC_LEFT (ic));
1428 IC_LEFT (ic)->isPtr = IS_PTR (operandType (IC_LEFT (ic)));
1431 IC_RESULT (ic) = newiTempOperand (sym->type, 0);
1432 if (IS_ARRAY (sym->type))
1434 IC_RESULT (ic) = geniCodeArray2Ptr (IC_RESULT (ic));
1435 IC_RESULT (ic)->isaddr = 0;
1438 IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (sym->type));
1442 return IC_RESULT (ic);
1445 /*-----------------------------------------------------------------*/
1446 /* operandFromValue - creates an operand from value */
1447 /*-----------------------------------------------------------------*/
1449 operandFromValue (value * val)
1453 /* if this is a symbol then do the symbol thing */
1455 return operandFromSymbol (val->sym);
1457 /* this is not a symbol */
1460 op->operand.valOperand = val;
1461 op->isLiteral = isOperandLiteral (op);
1465 /*-----------------------------------------------------------------*/
1466 /* operandFromLink - operand from typeChain */
1467 /*-----------------------------------------------------------------*/
1469 operandFromLink (sym_link * type)
1473 /* operand from sym_link */
1479 op->operand.typeOperand = copyLinkChain (type);
1483 /*-----------------------------------------------------------------*/
1484 /* operandFromLit - makes an operand from a literal value */
1485 /*-----------------------------------------------------------------*/
1487 operandFromLit (double i)
1489 return operandFromValue (valueFromLit (i));
1492 /*-----------------------------------------------------------------*/
1493 /* operandFromAst - creates an operand from an ast */
1494 /*-----------------------------------------------------------------*/
1496 operandFromAst (ast * tree,int lvl)
1502 /* depending on type do */
1506 return ast2iCode (tree,lvl+1);
1510 return operandFromValue (tree->opval.val);
1514 return operandFromLink (tree->opval.lnk);
1518 /* Just to keep the comiler happy */
1519 return (operand *) 0;
1522 /*-----------------------------------------------------------------*/
1523 /* setOperandType - sets the operand's type to the given type */
1524 /*-----------------------------------------------------------------*/
1526 setOperandType (operand * op, sym_link * type)
1528 /* depending on the type of operand */
1533 op->operand.valOperand->etype =
1534 getSpec (op->operand.valOperand->type =
1535 copyLinkChain (type));
1539 if (op->operand.symOperand->isitmp)
1540 op->operand.symOperand->etype =
1541 getSpec (op->operand.symOperand->type =
1542 copyLinkChain (type));
1544 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1545 "attempt to modify type of source");
1549 op->operand.typeOperand = copyLinkChain (type);
1554 /*-----------------------------------------------------------------*/
1555 /* Get size in byte of ptr need to access an array */
1556 /*-----------------------------------------------------------------*/
1558 getArraySizePtr (operand * op)
1560 sym_link *ltype = operandType(op);
1564 int size = getSize(ltype);
1565 return(IS_GENPTR(ltype)?(size-1):size);
1570 sym_link *letype = getSpec(ltype);
1571 switch (PTR_TYPE (SPEC_OCLS (letype)))
1583 return (GPTRSIZE-1);
1592 /*-----------------------------------------------------------------*/
1593 /* perform "usual unary conversions" */
1594 /*-----------------------------------------------------------------*/
1596 usualUnaryConversions (operand * op)
1598 if (IS_INTEGRAL (operandType (op)))
1600 if (getSize (operandType (op)) < (unsigned int) INTSIZE)
1603 return geniCodeCast (INTTYPE, op, TRUE);
1609 /*-----------------------------------------------------------------*/
1610 /* perform "usual binary conversions" */
1611 /*-----------------------------------------------------------------*/
1613 usualBinaryConversions (operand ** op1, operand ** op2)
1616 sym_link *rtype = operandType (*op2);
1617 sym_link *ltype = operandType (*op1);
1619 ctype = computeType (ltype, rtype);
1620 *op1 = geniCodeCast (ctype, *op1, TRUE);
1621 *op2 = geniCodeCast (ctype, *op2, TRUE);
1626 /*-----------------------------------------------------------------*/
1627 /* geniCodeValueAtAddress - generate intermeditate code for value */
1629 /*-----------------------------------------------------------------*/
1631 geniCodeRValue (operand * op, bool force)
1634 sym_link *type = operandType (op);
1635 sym_link *etype = getSpec (type);
1637 /* if this is an array & already */
1638 /* an address then return this */
1639 if (IS_AGGREGATE (type) ||
1640 (IS_PTR (type) && !force && !op->isaddr))
1641 return operandFromOperand (op);
1643 /* if this is not an address then must be */
1644 /* rvalue already so return this one */
1648 /* if this is not a temp symbol then */
1649 if (!IS_ITEMP (op) &&
1651 !IN_FARSPACE (SPEC_OCLS (etype)))
1653 op = operandFromOperand (op);
1658 if (IS_SPEC (type) &&
1659 IS_TRUE_SYMOP (op) &&
1660 (!IN_FARSPACE (SPEC_OCLS (etype)) ||
1661 (options.model == MODEL_FLAT24) ))
1663 op = operandFromOperand (op);
1668 ic = newiCode (GET_VALUE_AT_ADDRESS, op, NULL);
1669 if (IS_PTR (type) && op->isaddr && force)
1672 type = copyLinkChain (type);
1674 IC_RESULT (ic) = newiTempOperand (type, 1);
1675 IC_RESULT (ic)->isaddr = 0;
1677 /* ic->supportRtn = ((IS_GENPTR(type) | op->isGptr) & op->isaddr); */
1681 return IC_RESULT (ic);
1684 /*-----------------------------------------------------------------*/
1685 /* geniCodeCast - changes the value from one type to another */
1686 /*-----------------------------------------------------------------*/
1688 geniCodeCast (sym_link * type, operand * op, bool implicit)
1692 sym_link *opetype = getSpec (optype = operandType (op));
1696 /* one of them has size zero then error */
1697 if (IS_VOID (optype))
1699 werror (E_CAST_ZERO);
1703 /* if the operand is already the desired type then do nothing */
1704 if (compareType (type, optype) == 1)
1707 /* if this is a literal then just change the type & return */
1708 if (IS_LITERAL (opetype) && op->type == VALUE && !IS_PTR (type) && !IS_PTR (optype))
1709 return operandFromValue (valCastLiteral (type,
1710 operandLitValue (op)));
1712 /* if casting to/from pointers, do some checking */
1713 if (IS_PTR(type)) { // to a pointer
1714 if (!IS_PTR(optype) && !IS_FUNC(optype) && !IS_AGGREGATE(optype)) { // from a non pointer
1715 if (IS_INTEGRAL(optype)) {
1716 // maybe this is NULL, than it's ok.
1717 if (!(IS_LITERAL(optype) && (SPEC_CVAL(optype).v_ulong ==0))) {
1718 if (!TARGET_IS_Z80 && !TARGET_IS_GBZ80 && IS_GENPTR(type)) {
1719 // no way to set the storage
1720 if (IS_LITERAL(optype)) {
1721 werror(E_LITERAL_GENERIC);
1724 werror(E_NONPTR2_GENPTR);
1727 } else if (implicit) {
1728 werror(W_INTEGRAL2PTR_NOCAST);
1733 // shouldn't do that with float, array or structure unless to void
1734 if (!IS_VOID(getSpec(type)) &&
1735 !(IS_CODEPTR(type) && IS_FUNC(type->next) && IS_FUNC(optype))) {
1736 werror(E_INCOMPAT_TYPES);
1740 } else { // from a pointer to a pointer
1741 if (!TARGET_IS_Z80 && !TARGET_IS_GBZ80) {
1742 // if not a pointer to a function
1743 if (!(IS_CODEPTR(type) && IS_FUNC(type->next) && IS_FUNC(optype))) {
1744 if (implicit) { // if not to generic, they have to match
1745 if ((!IS_GENPTR(type) && (DCL_TYPE(optype) != DCL_TYPE(type)))) {
1746 werror(E_INCOMPAT_PTYPES);
1753 } else { // to a non pointer
1754 if (IS_PTR(optype)) { // from a pointer
1755 if (implicit) { // sneaky
1756 if (IS_INTEGRAL(type)) {
1757 werror(W_PTR2INTEGRAL_NOCAST);
1759 } else { // shouldn't do that with float, array or structure
1760 werror(E_INCOMPAT_TYPES);
1767 printFromToType (optype, type);
1770 /* if they are the same size create an assignment */
1771 if (getSize (type) == getSize (optype) &&
1772 !IS_BITFIELD (type) &&
1774 !IS_FLOAT (optype) &&
1775 ((IS_SPEC (type) && IS_SPEC (optype)) ||
1776 (!IS_SPEC (type) && !IS_SPEC (optype))))
1779 ic = newiCode ('=', NULL, op);
1780 IC_RESULT (ic) = newiTempOperand (type, 0);
1781 SPIL_LOC (IC_RESULT (ic)) =
1782 (IS_TRUE_SYMOP (op) ? OP_SYMBOL (op) : NULL);
1783 IC_RESULT (ic)->isaddr = 0;
1787 ic = newiCode (CAST, operandFromLink (type),
1788 geniCodeRValue (op, FALSE));
1790 IC_RESULT (ic) = newiTempOperand (type, 0);
1793 /* preserve the storage class & output class */
1794 /* of the original variable */
1795 restype = getSpec (operandType (IC_RESULT (ic)));
1796 if (!IS_LITERAL(opetype))
1797 SPEC_SCLS (restype) = SPEC_SCLS (opetype);
1798 SPEC_OCLS (restype) = SPEC_OCLS (opetype);
1801 return IC_RESULT (ic);
1804 /*-----------------------------------------------------------------*/
1805 /* geniCodeLabel - will create a Label */
1806 /*-----------------------------------------------------------------*/
1808 geniCodeLabel (symbol * label)
1812 ic = newiCodeLabelGoto (LABEL, label);
1816 /*-----------------------------------------------------------------*/
1817 /* geniCodeGoto - will create a Goto */
1818 /*-----------------------------------------------------------------*/
1820 geniCodeGoto (symbol * label)
1824 ic = newiCodeLabelGoto (GOTO, label);
1828 /*-----------------------------------------------------------------*/
1829 /* geniCodeMultiply - gen intermediate code for multiplication */
1830 /*-----------------------------------------------------------------*/
1832 geniCodeMultiply (operand * left, operand * right,int resultIsInt)
1839 /* if they are both literal then we know the result */
1840 if (IS_LITERAL (letype) && IS_LITERAL (retype))
1841 return operandFromValue (valMult (left->operand.valOperand,
1842 right->operand.valOperand));
1844 if (IS_LITERAL(retype)) {
1845 p2 = powof2 ((unsigned long) floatFromVal (right->operand.valOperand));
1848 resType = usualBinaryConversions (&left, &right);
1850 rtype = operandType (right);
1851 retype = getSpec (rtype);
1852 ltype = operandType (left);
1853 letype = getSpec (ltype);
1857 SPEC_NOUN(getSpec(resType))=V_INT;
1860 /* if the right is a literal & power of 2 */
1861 /* then make it a left shift */
1862 /* code generated for 1 byte * 1 byte literal = 2 bytes result is more
1863 efficient in most cases than 2 bytes result = 2 bytes << literal
1864 if port has 1 byte muldiv */
1865 if (p2 && !IS_FLOAT (letype) &&
1866 !((resultIsInt) && (getSize (resType) != getSize (ltype)) &&
1867 (port->support.muldiv == 1)))
1869 if ((resultIsInt) && (getSize (resType) != getSize (ltype)))
1871 /* LEFT_OP need same size for left and result, */
1872 left = geniCodeCast (resType, left, TRUE);
1873 ltype = operandType (left);
1875 ic = newiCode (LEFT_OP, left, operandFromLit (p2)); /* left shift */
1879 ic = newiCode ('*', left, right); /* normal multiplication */
1880 /* if the size left or right > 1 then support routine */
1881 if (getSize (ltype) > 1 || getSize (rtype) > 1)
1885 IC_RESULT (ic) = newiTempOperand (resType, 1);
1888 return IC_RESULT (ic);
1891 /*-----------------------------------------------------------------*/
1892 /* geniCodeDivision - gen intermediate code for division */
1893 /*-----------------------------------------------------------------*/
1895 geniCodeDivision (operand * left, operand * right)
1900 sym_link *rtype = operandType (right);
1901 sym_link *retype = getSpec (rtype);
1902 sym_link *ltype = operandType (left);
1903 sym_link *letype = getSpec (ltype);
1905 resType = usualBinaryConversions (&left, &right);
1907 /* if the right is a literal & power of 2 */
1908 /* then make it a right shift */
1909 if (IS_LITERAL (retype) &&
1910 !IS_FLOAT (letype) &&
1911 (p2 = powof2 ((unsigned long)
1912 floatFromVal (right->operand.valOperand)))) {
1913 ic = newiCode (RIGHT_OP, left, operandFromLit (p2)); /* right shift */
1917 ic = newiCode ('/', left, right); /* normal division */
1918 /* if the size left or right > 1 then support routine */
1919 if (getSize (ltype) > 1 || getSize (rtype) > 1)
1922 IC_RESULT (ic) = newiTempOperand (resType, 0);
1925 return IC_RESULT (ic);
1927 /*-----------------------------------------------------------------*/
1928 /* geniCodeModulus - gen intermediate code for modulus */
1929 /*-----------------------------------------------------------------*/
1931 geniCodeModulus (operand * left, operand * right)
1937 /* if they are both literal then we know the result */
1938 if (IS_LITERAL (letype) && IS_LITERAL (retype))
1939 return operandFromValue (valMod (left->operand.valOperand,
1940 right->operand.valOperand));
1942 resType = usualBinaryConversions (&left, &right);
1944 /* now they are the same size */
1945 ic = newiCode ('%', left, right);
1947 /* if the size left or right > 1 then support routine */
1948 if (getSize (ltype) > 1 || getSize (rtype) > 1)
1950 IC_RESULT (ic) = newiTempOperand (resType, 0);
1953 return IC_RESULT (ic);
1956 /*-----------------------------------------------------------------*/
1957 /* geniCodePtrPtrSubtract - subtracts pointer from pointer */
1958 /*-----------------------------------------------------------------*/
1960 geniCodePtrPtrSubtract (operand * left, operand * right)
1966 /* if they are both literals then */
1967 if (IS_LITERAL (letype) && IS_LITERAL (retype))
1969 result = operandFromValue (valMinus (left->operand.valOperand,
1970 right->operand.valOperand));
1974 ic = newiCode ('-', left, right);
1976 IC_RESULT (ic) = result = newiTempOperand (newIntLink (), 1);
1980 if (IS_VOID(ltype->next) || IS_VOID(rtype->next)) {
1984 // should we really do this? is this ANSI?
1985 return geniCodeDivision (result,
1986 operandFromLit (getSize (ltype->next)));
1989 /*-----------------------------------------------------------------*/
1990 /* geniCodeSubtract - generates code for subtraction */
1991 /*-----------------------------------------------------------------*/
1993 geniCodeSubtract (operand * left, operand * right)
2000 /* if they both pointers then */
2001 if ((IS_PTR (ltype) || IS_ARRAY (ltype)) &&
2002 (IS_PTR (rtype) || IS_ARRAY (rtype)))
2003 return geniCodePtrPtrSubtract (left, right);
2005 /* if they are both literal then we know the result */
2006 if (IS_LITERAL (letype) && IS_LITERAL (retype)
2007 && left->isLiteral && right->isLiteral)
2008 return operandFromValue (valMinus (left->operand.valOperand,
2009 right->operand.valOperand));
2011 /* if left is an array or pointer */
2012 if (IS_PTR (ltype) || IS_ARRAY (ltype))
2014 isarray = left->isaddr;
2015 right = geniCodeMultiply (right,
2016 operandFromLit (getSize (ltype->next)), (getArraySizePtr(left) >= INTSIZE));
2017 resType = copyLinkChain (IS_ARRAY (ltype) ? ltype->next : ltype);
2020 { /* make them the same size */
2021 resType = usualBinaryConversions (&left, &right);
2024 ic = newiCode ('-', left, right);
2026 IC_RESULT (ic) = newiTempOperand (resType, 1);
2027 IC_RESULT (ic)->isaddr = (isarray ? 1 : 0);
2029 /* if left or right is a float */
2030 if (IS_FLOAT (ltype) || IS_FLOAT (rtype))
2034 return IC_RESULT (ic);
2037 /*-----------------------------------------------------------------*/
2038 /* geniCodeAdd - generates iCode for addition */
2039 /*-----------------------------------------------------------------*/
2041 geniCodeAdd (operand * left, operand * right, int lvl)
2049 /* if left is an array then array access */
2050 if (IS_ARRAY (ltype))
2051 return geniCodeArray (left, right,lvl);
2053 /* if the right side is LITERAL zero */
2054 /* return the left side */
2055 if (IS_LITERAL (retype) && right->isLiteral && !floatFromVal (valFromType (retype)))
2058 /* if left is literal zero return right */
2059 if (IS_LITERAL (letype) && left->isLiteral && !floatFromVal (valFromType (letype)))
2062 /* if left is a pointer then size */
2065 isarray = left->isaddr;
2066 // there is no need to multiply with 1
2067 if (getSize(ltype->next)!=1) {
2068 size = operandFromLit (getSize (ltype->next));
2069 right = geniCodeMultiply (right, size, (getArraySizePtr(left) >= INTSIZE));
2071 resType = copyLinkChain (ltype);
2074 { // make them the same size
2075 resType = usualBinaryConversions (&left, &right);
2078 /* if they are both literals then we know */
2079 if (IS_LITERAL (letype) && IS_LITERAL (retype)
2080 && left->isLiteral && right->isLiteral)
2081 return operandFromValue (valPlus (valFromType (letype),
2082 valFromType (retype)));
2084 ic = newiCode ('+', left, right);
2086 IC_RESULT (ic) = newiTempOperand (resType, 1);
2087 IC_RESULT (ic)->isaddr = (isarray ? 1 : 0);
2089 /* if left or right is a float then support
2091 if (IS_FLOAT (ltype) || IS_FLOAT (rtype))
2096 return IC_RESULT (ic);
2100 /*-----------------------------------------------------------------*/
2101 /* aggrToPtr - changes an aggregate to pointer to an aggregate */
2102 /*-----------------------------------------------------------------*/
2104 aggrToPtr (sym_link * type, bool force)
2110 if (IS_PTR (type) && !force)
2113 etype = getSpec (type);
2117 /* if the output class is generic */
2118 if ((DCL_TYPE (ptype) = PTR_TYPE (SPEC_OCLS (etype))) == CPOINTER)
2119 DCL_PTR_CONST (ptype) = port->mem.code_ro;
2121 /* if the variable was declared a constant */
2122 /* then the pointer points to a constant */
2123 if (IS_CONSTANT (etype))
2124 DCL_PTR_CONST (ptype) = 1;
2126 /* the variable was volatile then pointer to volatile */
2127 if (IS_VOLATILE (etype))
2128 DCL_PTR_VOLATILE (ptype) = 1;
2132 /*-----------------------------------------------------------------*/
2133 /* geniCodeArray2Ptr - array to pointer */
2134 /*-----------------------------------------------------------------*/
2136 geniCodeArray2Ptr (operand * op)
2138 sym_link *optype = operandType (op);
2139 sym_link *opetype = getSpec (optype);
2141 /* set the pointer depending on the storage class */
2142 if ((DCL_TYPE (optype) = PTR_TYPE (SPEC_OCLS (opetype))) == CPOINTER)
2143 DCL_PTR_CONST (optype) = port->mem.code_ro;
2146 /* if the variable was declared a constant */
2147 /* then the pointer points to a constant */
2148 if (IS_CONSTANT (opetype))
2149 DCL_PTR_CONST (optype) = 1;
2151 /* the variable was volatile then pointer to volatile */
2152 if (IS_VOLATILE (opetype))
2153 DCL_PTR_VOLATILE (optype) = 1;
2159 /*-----------------------------------------------------------------*/
2160 /* geniCodeArray - array access */
2161 /*-----------------------------------------------------------------*/
2163 geniCodeArray (operand * left, operand * right,int lvl)
2166 sym_link *ltype = operandType (left);
2170 if (IS_PTR (ltype->next) && left->isaddr)
2172 left = geniCodeRValue (left, FALSE);
2174 return geniCodeDerefPtr (geniCodeAdd (left, right, lvl), lvl);
2177 right = geniCodeMultiply (right,
2178 operandFromLit (getSize (ltype->next)), (getArraySizePtr(left) >= INTSIZE));
2180 /* we can check for limits here */
2181 if (isOperandLiteral (right) &&
2184 (operandLitValue (right) / getSize (ltype->next)) >= DCL_ELEM (ltype))
2186 werror (E_ARRAY_BOUND);
2187 right = operandFromLit (0);
2190 ic = newiCode ('+', left, right);
2192 IC_RESULT (ic) = newiTempOperand (((IS_PTR (ltype) &&
2193 !IS_AGGREGATE (ltype->next) &&
2194 !IS_PTR (ltype->next))
2195 ? ltype : ltype->next), 0);
2197 IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (ltype->next));
2199 return IC_RESULT (ic);
2202 /*-----------------------------------------------------------------*/
2203 /* geniCodeStruct - generates intermediate code for structres */
2204 /*-----------------------------------------------------------------*/
2206 geniCodeStruct (operand * left, operand * right, bool islval)
2209 sym_link *type = operandType (left);
2210 sym_link *etype = getSpec (type);
2212 symbol *element = getStructElement (SPEC_STRUCT (etype),
2213 right->operand.symOperand);
2215 /* add the offset */
2216 ic = newiCode ('+', left, operandFromLit (element->offset));
2218 IC_RESULT (ic) = newiTempOperand (element->type, 0);
2220 /* preserve the storage & output class of the struct */
2221 /* as well as the volatile attribute */
2222 retype = getSpec (operandType (IC_RESULT (ic)));
2223 SPEC_SCLS (retype) = SPEC_SCLS (etype);
2224 SPEC_OCLS (retype) = SPEC_OCLS (etype);
2225 SPEC_VOLATILE (retype) |= SPEC_VOLATILE (etype);
2227 if (IS_PTR (element->type))
2228 setOperandType (IC_RESULT (ic), aggrToPtr (operandType (IC_RESULT (ic)), TRUE));
2230 IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (element->type));
2234 return (islval ? IC_RESULT (ic) : geniCodeRValue (IC_RESULT (ic), TRUE));
2237 /*-----------------------------------------------------------------*/
2238 /* geniCodePostInc - generate int code for Post increment */
2239 /*-----------------------------------------------------------------*/
2241 geniCodePostInc (operand * op)
2245 sym_link *optype = operandType (op);
2247 operand *rv = (IS_ITEMP (op) ?
2248 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2250 sym_link *rvtype = operandType (rv);
2253 /* if this is not an address we have trouble */
2256 werror (E_LVALUE_REQUIRED, "++");
2260 rOp = newiTempOperand (rvtype, 0);
2261 OP_SYMBOL(rOp)->noSpilLoc = 1;
2264 OP_SYMBOL(rv)->noSpilLoc = 1;
2266 geniCodeAssign (rOp, rv, 0);
2268 size = (IS_PTR (rvtype) ? getSize (rvtype->next) : 1);
2269 if (IS_FLOAT (rvtype))
2270 ic = newiCode ('+', rv, operandFromValue (constFloatVal ("1.0")));
2272 ic = newiCode ('+', rv, operandFromLit (size));
2274 IC_RESULT (ic) = result = newiTempOperand (rvtype, 0);
2277 geniCodeAssign (op, result, 0);
2283 /*-----------------------------------------------------------------*/
2284 /* geniCodePreInc - generate code for preIncrement */
2285 /*-----------------------------------------------------------------*/
2287 geniCodePreInc (operand * op)
2290 sym_link *optype = operandType (op);
2291 operand *rop = (IS_ITEMP (op) ?
2292 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2294 sym_link *roptype = operandType (rop);
2300 werror (E_LVALUE_REQUIRED, "++");
2305 size = (IS_PTR (roptype) ? getSize (roptype->next) : 1);
2306 if (IS_FLOAT (roptype))
2307 ic = newiCode ('+', rop, operandFromValue (constFloatVal ("1.0")));
2309 ic = newiCode ('+', rop, operandFromLit (size));
2310 IC_RESULT (ic) = result = newiTempOperand (roptype, 0);
2314 return geniCodeAssign (op, result, 0);
2317 /*-----------------------------------------------------------------*/
2318 /* geniCodePostDec - generates code for Post decrement */
2319 /*-----------------------------------------------------------------*/
2321 geniCodePostDec (operand * op)
2325 sym_link *optype = operandType (op);
2327 operand *rv = (IS_ITEMP (op) ?
2328 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2330 sym_link *rvtype = operandType (rv);
2333 /* if this is not an address we have trouble */
2336 werror (E_LVALUE_REQUIRED, "--");
2340 rOp = newiTempOperand (rvtype, 0);
2341 OP_SYMBOL(rOp)->noSpilLoc = 1;
2344 OP_SYMBOL(rv)->noSpilLoc = 1;
2346 geniCodeAssign (rOp, rv, 0);
2348 size = (IS_PTR (rvtype) ? getSize (rvtype->next) : 1);
2349 if (IS_FLOAT (rvtype))
2350 ic = newiCode ('-', rv, operandFromValue (constFloatVal ("1.0")));
2352 ic = newiCode ('-', rv, operandFromLit (size));
2354 IC_RESULT (ic) = result = newiTempOperand (rvtype, 0);
2357 geniCodeAssign (op, result, 0);
2363 /*-----------------------------------------------------------------*/
2364 /* geniCodePreDec - generate code for pre decrement */
2365 /*-----------------------------------------------------------------*/
2367 geniCodePreDec (operand * op)
2370 sym_link *optype = operandType (op);
2371 operand *rop = (IS_ITEMP (op) ?
2372 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2374 sym_link *roptype = operandType (rop);
2380 werror (E_LVALUE_REQUIRED, "--");
2385 size = (IS_PTR (roptype) ? getSize (roptype->next) : 1);
2386 if (IS_FLOAT (roptype))
2387 ic = newiCode ('-', rop, operandFromValue (constFloatVal ("1.0")));
2389 ic = newiCode ('-', rop, operandFromLit (size));
2390 IC_RESULT (ic) = result = newiTempOperand (roptype, 0);
2394 return geniCodeAssign (op, result, 0);
2398 /*-----------------------------------------------------------------*/
2399 /* geniCodeBitwise - gen int code for bitWise operators */
2400 /*-----------------------------------------------------------------*/
2402 geniCodeBitwise (operand * left, operand * right,
2403 int oper, sym_link * resType)
2407 left = geniCodeCast (resType, left, TRUE);
2408 right = geniCodeCast (resType, right, TRUE);
2410 ic = newiCode (oper, left, right);
2411 IC_RESULT (ic) = newiTempOperand (resType, 0);
2414 return IC_RESULT (ic);
2417 /*-----------------------------------------------------------------*/
2418 /* geniCodeAddressOf - gens icode for '&' address of operator */
2419 /*-----------------------------------------------------------------*/
2421 geniCodeAddressOf (operand * op)
2425 sym_link *optype = operandType (op);
2426 sym_link *opetype = getSpec (optype);
2428 /* lvalue check already done in decorateType */
2429 /* this must be a lvalue */
2430 /* if (!op->isaddr && !IS_AGGREGATE(optype)) { */
2431 /* werror (E_LVALUE_REQUIRED,"&"); */
2436 p->class = DECLARATOR;
2438 /* set the pointer depending on the storage class */
2439 if ((DCL_TYPE (p) = PTR_TYPE (SPEC_OCLS (opetype))) == CPOINTER)
2440 DCL_PTR_CONST (p) = port->mem.code_ro;
2442 /* make sure we preserve the const & volatile */
2443 if (IS_CONSTANT (opetype))
2444 DCL_PTR_CONST (p) = 1;
2446 if (IS_VOLATILE (opetype))
2447 DCL_PTR_VOLATILE (p) = 1;
2449 p->next = copyLinkChain (optype);
2451 /* if already a temp */
2454 setOperandType (op, p);
2459 /* other wise make this of the type coming in */
2460 ic = newiCode (ADDRESS_OF, op, NULL);
2461 IC_RESULT (ic) = newiTempOperand (p, 1);
2462 IC_RESULT (ic)->isaddr = 0;
2464 return IC_RESULT (ic);
2466 /*-----------------------------------------------------------------*/
2467 /* setOClass - sets the output class depending on the pointer type */
2468 /*-----------------------------------------------------------------*/
2470 setOClass (sym_link * ptr, sym_link * spec)
2472 switch (DCL_TYPE (ptr))
2475 SPEC_OCLS (spec) = data;
2479 SPEC_OCLS (spec) = generic;
2483 SPEC_OCLS (spec) = xdata;
2487 SPEC_OCLS (spec) = code;
2491 SPEC_OCLS (spec) = idata;
2495 SPEC_OCLS (spec) = xstack;
2499 SPEC_OCLS (spec) = eeprom;
2508 /*-----------------------------------------------------------------*/
2509 /* geniCodeDerefPtr - dereference pointer with '*' */
2510 /*-----------------------------------------------------------------*/
2512 geniCodeDerefPtr (operand * op,int lvl)
2514 sym_link *rtype, *retype;
2515 sym_link *optype = operandType (op);
2517 /* if this is a pointer then generate the rvalue */
2518 if (IS_PTR (optype))
2520 if (IS_TRUE_SYMOP (op))
2523 op = geniCodeRValue (op, TRUE);
2526 op = geniCodeRValue (op, TRUE);
2529 /* now get rid of the pointer part */
2530 if (isLvaluereq(lvl) && IS_ITEMP (op))
2532 retype = getSpec (rtype = copyLinkChain (optype));
2536 retype = getSpec (rtype = copyLinkChain (optype->next));
2539 /* if this is a pointer then outputclass needs 2b updated */
2540 if (IS_PTR (optype))
2541 setOClass (optype, retype);
2543 op->isGptr = IS_GENPTR (optype);
2545 /* if the pointer was declared as a constant */
2546 /* then we cannot allow assignment to the derefed */
2547 if (IS_PTR_CONST (optype))
2548 SPEC_CONST (retype) = 1;
2550 op->isaddr = (IS_PTR (rtype) ||
2551 IS_STRUCT (rtype) ||
2556 if (!isLvaluereq(lvl))
2557 op = geniCodeRValue (op, TRUE);
2559 setOperandType (op, rtype);
2564 /*-----------------------------------------------------------------*/
2565 /* geniCodeUnaryMinus - does a unary minus of the operand */
2566 /*-----------------------------------------------------------------*/
2568 geniCodeUnaryMinus (operand * op)
2571 sym_link *optype = operandType (op);
2573 if (IS_LITERAL (optype))
2574 return operandFromLit (-floatFromVal (op->operand.valOperand));
2576 ic = newiCode (UNARYMINUS, op, NULL);
2577 IC_RESULT (ic) = newiTempOperand (optype, 0);
2579 return IC_RESULT (ic);
2582 /*-----------------------------------------------------------------*/
2583 /* geniCodeLeftShift - gen i code for left shift */
2584 /*-----------------------------------------------------------------*/
2586 geniCodeLeftShift (operand * left, operand * right)
2590 ic = newiCode (LEFT_OP, left, right);
2591 IC_RESULT (ic) = newiTempOperand (operandType (left), 0);
2593 return IC_RESULT (ic);
2596 /*-----------------------------------------------------------------*/
2597 /* geniCodeRightShift - gen i code for right shift */
2598 /*-----------------------------------------------------------------*/
2600 geniCodeRightShift (operand * left, operand * right)
2604 ic = newiCode (RIGHT_OP, left, right);
2605 IC_RESULT (ic) = newiTempOperand (operandType (left), 0);
2607 return IC_RESULT (ic);
2610 /*-----------------------------------------------------------------*/
2611 /* geniCodeLogic- logic code */
2612 /*-----------------------------------------------------------------*/
2614 geniCodeLogic (operand * left, operand * right, int op)
2618 sym_link *rtype = operandType (right);
2619 sym_link *ltype = operandType (left);
2621 /* left is integral type and right is literal then
2622 check if the literal value is within bounds */
2623 if (IS_INTEGRAL (ltype) && IS_VALOP (right) && IS_LITERAL (rtype))
2625 checkConstantRange(ltype,
2626 OP_VALUE(right), "compare operation", 1);
2629 ctype = usualBinaryConversions (&left, &right);
2631 ic = newiCode (op, left, right);
2632 IC_RESULT (ic) = newiTempOperand (newCharLink (), 1);
2634 /* if comparing float
2635 and not a '==' || '!=' || '&&' || '||' (these
2637 if (IS_FLOAT(ctype) &&
2645 return IC_RESULT (ic);
2648 /*-----------------------------------------------------------------*/
2649 /* geniCodeUnary - for a a generic unary operation */
2650 /*-----------------------------------------------------------------*/
2652 geniCodeUnary (operand * op, int oper)
2654 iCode *ic = newiCode (oper, op, NULL);
2656 IC_RESULT (ic) = newiTempOperand (operandType (op), 0);
2658 return IC_RESULT (ic);
2661 /*-----------------------------------------------------------------*/
2662 /* geniCodeConditional - geniCode for '?' ':' operation */
2663 /*-----------------------------------------------------------------*/
2665 geniCodeConditional (ast * tree,int lvl)
2668 symbol *falseLabel = newiTempLabel (NULL);
2669 symbol *exitLabel = newiTempLabel (NULL);
2670 operand *cond = ast2iCode (tree->left,lvl+1);
2671 operand *true, *false, *result;
2673 ic = newiCodeCondition (geniCodeRValue (cond, FALSE),
2677 true = ast2iCode (tree->right->left,lvl+1);
2679 /* move the value to a new Operand */
2680 result = newiTempOperand (tree->right->ftype, 0);
2681 geniCodeAssign (result, geniCodeRValue (true, FALSE), 0);
2683 /* generate an unconditional goto */
2684 geniCodeGoto (exitLabel);
2686 /* now for the right side */
2687 geniCodeLabel (falseLabel);
2689 false = ast2iCode (tree->right->right,lvl+1);
2690 geniCodeAssign (result, geniCodeRValue (false, FALSE), 0);
2692 /* create the exit label */
2693 geniCodeLabel (exitLabel);
2698 /*-----------------------------------------------------------------*/
2699 /* geniCodeAssign - generate code for assignment */
2700 /*-----------------------------------------------------------------*/
2702 geniCodeAssign (operand * left, operand * right, int nosupdate)
2705 sym_link *ltype = operandType (left);
2706 sym_link *rtype = operandType (right);
2708 if (!left->isaddr && !IS_ITEMP (left))
2710 werror (E_LVALUE_REQUIRED, "assignment");
2714 /* left is integral type and right is literal then
2715 check if the literal value is within bounds */
2716 if (IS_INTEGRAL (ltype) && right->type == VALUE && IS_LITERAL (rtype))
2718 checkConstantRange(ltype,
2719 OP_VALUE(right), "= operation", 0);
2722 /* if the left & right type don't exactly match */
2723 /* if pointer set then make sure the check is
2724 done with the type & not the pointer */
2725 /* then cast rights type to left */
2727 /* first check the type for pointer assignement */
2728 if (left->isaddr && IS_PTR (ltype) && IS_ITEMP (left) &&
2729 compareType (ltype, rtype) <= 0)
2731 if (compareType (ltype->next, rtype) < 0)
2732 right = geniCodeCast (ltype->next, right, TRUE);
2734 else if (compareType (ltype, rtype) < 0)
2735 right = geniCodeCast (ltype, right, TRUE);
2737 /* if left is a true symbol & ! volatile
2738 create an assignment to temporary for
2739 the right & then assign this temporary
2740 to the symbol this is SSA . isn't it simple
2741 and folks have published mountains of paper on it */
2742 if (IS_TRUE_SYMOP (left) &&
2743 !isOperandVolatile (left, FALSE) &&
2744 isOperandGlobal (left))
2748 if (IS_TRUE_SYMOP (right))
2749 sym = OP_SYMBOL (right);
2750 ic = newiCode ('=', NULL, right);
2751 IC_RESULT (ic) = right = newiTempOperand (ltype, 0);
2752 SPIL_LOC (right) = sym;
2756 ic = newiCode ('=', NULL, right);
2757 IC_RESULT (ic) = left;
2760 /* if left isgptr flag is set then support
2761 routine will be required */
2765 ic->nosupdate = nosupdate;
2769 /*-----------------------------------------------------------------*/
2770 /* geniCodeSEParms - generate code for side effecting fcalls */
2771 /*-----------------------------------------------------------------*/
2773 geniCodeSEParms (ast * parms,int lvl)
2778 if (parms->type == EX_OP && parms->opval.op == PARAM)
2780 geniCodeSEParms (parms->left,lvl);
2781 geniCodeSEParms (parms->right,lvl);
2785 /* hack don't like this but too lazy to think of
2787 if (IS_ADDRESS_OF_OP (parms))
2788 parms->left->lvalue = 1;
2790 if (IS_CAST_OP (parms) &&
2791 IS_PTR (parms->ftype) &&
2792 IS_ADDRESS_OF_OP (parms->right))
2793 parms->right->left->lvalue = 1;
2795 parms->opval.oprnd =
2796 geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
2798 parms->type = EX_OPERAND;
2799 AST_ARGREG(parms) = SPEC_ARGREG(parms->etype);
2802 /*-----------------------------------------------------------------*/
2803 /* geniCodeParms - generates parameters */
2804 /*-----------------------------------------------------------------*/
2806 geniCodeParms (ast * parms, value *argVals, int *stack,
2807 sym_link * fetype, symbol * func,int lvl)
2815 if (argVals==NULL) {
2817 argVals=FUNC_ARGS(func->type);
2820 /* if this is a param node then do the left & right */
2821 if (parms->type == EX_OP && parms->opval.op == PARAM)
2823 argVals=geniCodeParms (parms->left, argVals, stack, fetype, func,lvl);
2824 argVals=geniCodeParms (parms->right, argVals, stack, fetype, func,lvl);
2828 /* get the parameter value */
2829 if (parms->type == EX_OPERAND)
2830 pval = parms->opval.oprnd;
2833 /* maybe this else should go away ?? */
2834 /* hack don't like this but too lazy to think of
2836 if (IS_ADDRESS_OF_OP (parms))
2837 parms->left->lvalue = 1;
2839 if (IS_CAST_OP (parms) &&
2840 IS_PTR (parms->ftype) &&
2841 IS_ADDRESS_OF_OP (parms->right))
2842 parms->right->left->lvalue = 1;
2844 pval = geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
2847 /* if register parm then make it a send */
2848 if ((IS_REGPARM (parms->etype) && !IFFUNC_HASVARARGS(func->type)) ||
2849 IFFUNC_ISBUILTIN(func->type))
2851 ic = newiCode (SEND, pval, NULL);
2852 ic->argreg = SPEC_ARGREG(parms->etype);
2853 ic->builtinSEND = FUNC_ISBUILTIN(func->type);
2858 /* now decide whether to push or assign */
2859 if (!(options.stackAuto || IFFUNC_ISREENT (func->type)))
2863 operand *top = operandFromSymbol (argVals->sym);
2864 /* clear useDef and other bitVectors */
2865 OP_USES (top) = OP_DEFS (top) = OP_SYMBOL(top)->clashes = NULL;
2866 geniCodeAssign (top, pval, 1);
2870 sym_link *p = operandType (pval);
2872 ic = newiCode (IPUSH, pval, NULL);
2874 /* update the stack adjustment */
2875 *stack += getSize (IS_AGGREGATE (p) ? aggrToPtr (p, FALSE) : p);
2880 argVals=argVals->next;
2884 /*-----------------------------------------------------------------*/
2885 /* geniCodeCall - generates temp code for calling */
2886 /*-----------------------------------------------------------------*/
2888 geniCodeCall (operand * left, ast * parms,int lvl)
2892 sym_link *type, *etype;
2895 if (!IS_FUNC(OP_SYMBOL(left)->type) &&
2896 !IS_CODEPTR(OP_SYMBOL(left)->type)) {
2897 werror (E_FUNCTION_EXPECTED);
2901 /* take care of parameters with side-effecting
2902 function calls in them, this is required to take care
2903 of overlaying function parameters */
2904 geniCodeSEParms (parms,lvl);
2906 /* first the parameters */
2907 geniCodeParms (parms, NULL, &stack, getSpec (operandType (left)), OP_SYMBOL (left),lvl);
2909 /* now call : if symbol then pcall */
2910 if (IS_OP_POINTER (left) || IS_ITEMP(left)) {
2911 ic = newiCode (PCALL, left, NULL);
2913 ic = newiCode (CALL, left, NULL);
2916 type = copyLinkChain (operandType (left)->next);
2917 etype = getSpec (type);
2918 SPEC_EXTR (etype) = 0;
2919 IC_RESULT (ic) = result = newiTempOperand (type, 1);
2923 /* stack adjustment after call */
2924 ic->parmBytes = stack;
2929 /*-----------------------------------------------------------------*/
2930 /* geniCodeReceive - generate intermediate code for "receive" */
2931 /*-----------------------------------------------------------------*/
2933 geniCodeReceive (value * args)
2935 /* for all arguments that are passed in registers */
2939 if (IS_REGPARM (args->etype))
2941 operand *opr = operandFromValue (args);
2943 symbol *sym = OP_SYMBOL (opr);
2946 /* we will use it after all optimizations
2947 and before liveRange calculation */
2948 if (!sym->addrtaken && !IS_VOLATILE (sym->etype))
2951 if (IN_FARSPACE (SPEC_OCLS (sym->etype)) &&
2952 options.stackAuto == 0 &&
2953 (!(options.model == MODEL_FLAT24)) )
2958 opl = newiTempOperand (args->type, 0);
2960 sym->reqv->key = sym->key;
2961 OP_SYMBOL (sym->reqv)->key = sym->key;
2962 OP_SYMBOL (sym->reqv)->isreqv = 1;
2963 OP_SYMBOL (sym->reqv)->islocal = 0;
2964 SPIL_LOC (sym->reqv) = sym;
2968 ic = newiCode (RECEIVE, NULL, NULL);
2969 ic->argreg = SPEC_ARGREG(args->etype);
2971 currFunc->recvSize = getSize (sym->type);
2974 IC_RESULT (ic) = opr;
2982 /*-----------------------------------------------------------------*/
2983 /* geniCodeFunctionBody - create the function body */
2984 /*-----------------------------------------------------------------*/
2986 geniCodeFunctionBody (ast * tree,int lvl)
2993 /* reset the auto generation */
2999 func = ast2iCode (tree->left,lvl+1);
3000 fetype = getSpec (operandType (func));
3002 savelineno = lineno;
3003 lineno = OP_SYMBOL (func)->lineDef;
3004 /* create an entry label */
3005 geniCodeLabel (entryLabel);
3006 lineno = savelineno;
3008 /* create a proc icode */
3009 ic = newiCode (FUNCTION, func, NULL);
3010 lineno=ic->lineno = OP_SYMBOL (func)->lineDef;
3014 /* for all parameters that are passed
3015 on registers add a "receive" */
3016 geniCodeReceive (tree->values.args);
3018 /* generate code for the body */
3019 ast2iCode (tree->right,lvl+1);
3021 /* create a label for return */
3022 geniCodeLabel (returnLabel);
3024 /* now generate the end proc */
3025 ic = newiCode (ENDFUNCTION, func, NULL);
3030 /*-----------------------------------------------------------------*/
3031 /* geniCodeReturn - gen icode for 'return' statement */
3032 /*-----------------------------------------------------------------*/
3034 geniCodeReturn (operand * op)
3038 /* if the operand is present force an rvalue */
3040 op = geniCodeRValue (op, FALSE);
3042 ic = newiCode (RETURN, op, NULL);
3046 /*-----------------------------------------------------------------*/
3047 /* geniCodeIfx - generates code for extended if statement */
3048 /*-----------------------------------------------------------------*/
3050 geniCodeIfx (ast * tree,int lvl)
3053 operand *condition = ast2iCode (tree->left,lvl+1);
3056 /* if condition is null then exit */
3060 condition = geniCodeRValue (condition, FALSE);
3062 cetype = getSpec (operandType (condition));
3063 /* if the condition is a literal */
3064 if (IS_LITERAL (cetype))
3066 if (floatFromVal (condition->operand.valOperand))
3068 if (tree->trueLabel)
3069 geniCodeGoto (tree->trueLabel);
3075 if (tree->falseLabel)
3076 geniCodeGoto (tree->falseLabel);
3083 if (tree->trueLabel)
3085 ic = newiCodeCondition (condition,
3090 if (tree->falseLabel)
3091 geniCodeGoto (tree->falseLabel);
3095 ic = newiCodeCondition (condition,
3102 ast2iCode (tree->right,lvl+1);
3105 /*-----------------------------------------------------------------*/
3106 /* geniCodeJumpTable - tries to create a jump table for switch */
3107 /*-----------------------------------------------------------------*/
3109 geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
3111 int min = 0, max = 0, t, cnt = 0;
3118 if (!tree || !caseVals)
3121 /* the criteria for creating a jump table is */
3122 /* all integer numbers between the maximum & minimum must */
3123 /* be present , the maximum value should not exceed 255 */
3124 min = max = (int) floatFromVal (vch = caseVals);
3125 sprintf (buffer, "_case_%d_%d",
3126 tree->values.switchVals.swNum,
3128 addSet (&labels, newiTempLabel (buffer));
3130 /* if there is only one case value then no need */
3131 if (!(vch = vch->next))
3136 if (((t = (int) floatFromVal (vch)) - max) != 1)
3138 sprintf (buffer, "_case_%d_%d",
3139 tree->values.switchVals.swNum,
3141 addSet (&labels, newiTempLabel (buffer));
3147 /* if the number of case statements <= 2 then */
3148 /* it is not economical to create the jump table */
3149 /* since two compares are needed for boundary conditions */
3150 if ((!optimize.noJTabBoundary && cnt <= 2) || max > (255 / 3))
3153 if (tree->values.switchVals.swDefault)
3154 sprintf (buffer, "_default_%d", tree->values.switchVals.swNum);
3156 sprintf (buffer, "_swBrk_%d", tree->values.switchVals.swNum);
3158 falseLabel = newiTempLabel (buffer);
3160 /* so we can create a jumptable */
3161 /* first we rule out the boundary conditions */
3162 /* if only optimization says so */
3163 if (!optimize.noJTabBoundary)
3165 sym_link *cetype = getSpec (operandType (cond));
3166 /* no need to check the lower bound if
3167 the condition is unsigned & minimum value is zero */
3168 if (!(min == 0 && SPEC_USIGN (cetype)))
3170 boundary = geniCodeLogic (cond, operandFromLit (min), '<');
3171 ic = newiCodeCondition (boundary, falseLabel, NULL);
3175 /* now for upper bounds */
3176 boundary = geniCodeLogic (cond, operandFromLit (max), '>');
3177 ic = newiCodeCondition (boundary, falseLabel, NULL);
3181 /* if the min is not zero then we no make it zero */
3184 cond = geniCodeSubtract (cond, operandFromLit (min));
3185 setOperandType (cond, UCHARTYPE);
3188 /* now create the jumptable */
3189 ic = newiCode (JUMPTABLE, NULL, NULL);
3190 IC_JTCOND (ic) = cond;
3191 IC_JTLABELS (ic) = labels;
3196 /*-----------------------------------------------------------------*/
3197 /* geniCodeSwitch - changes a switch to a if statement */
3198 /*-----------------------------------------------------------------*/
3200 geniCodeSwitch (ast * tree,int lvl)
3203 operand *cond = geniCodeRValue (ast2iCode (tree->left,lvl+1), FALSE);
3204 value *caseVals = tree->values.switchVals.swVals;
3205 symbol *trueLabel, *falseLabel;
3207 /* if we can make this a jump table */
3208 if (geniCodeJumpTable (cond, caseVals, tree))
3209 goto jumpTable; /* no need for the comparison */
3211 /* for the cases defined do */
3215 operand *compare = geniCodeLogic (cond,
3216 operandFromValue (caseVals),
3219 sprintf (buffer, "_case_%d_%d",
3220 tree->values.switchVals.swNum,
3221 (int) floatFromVal (caseVals));
3222 trueLabel = newiTempLabel (buffer);
3224 ic = newiCodeCondition (compare, trueLabel, NULL);
3226 caseVals = caseVals->next;
3231 /* if default is present then goto break else break */
3232 if (tree->values.switchVals.swDefault)
3233 sprintf (buffer, "_default_%d", tree->values.switchVals.swNum);
3235 sprintf (buffer, "_swBrk_%d", tree->values.switchVals.swNum);
3237 falseLabel = newiTempLabel (buffer);
3238 geniCodeGoto (falseLabel);
3241 ast2iCode (tree->right,lvl+1);
3244 /*-----------------------------------------------------------------*/
3245 /* geniCodeInline - intermediate code for inline assembler */
3246 /*-----------------------------------------------------------------*/
3248 geniCodeInline (ast * tree)
3252 ic = newiCode (INLINEASM, NULL, NULL);
3253 IC_INLINE (ic) = tree->values.inlineasm;
3257 /*-----------------------------------------------------------------*/
3258 /* geniCodeArrayInit - intermediate code for array initializer */
3259 /*-----------------------------------------------------------------*/
3261 geniCodeArrayInit (ast * tree, operand *array)
3265 if (!getenv("TRY_THE_NEW_INITIALIZER")) {
3266 ic = newiCode (ARRAYINIT, array, NULL);
3267 IC_ARRAYILIST (ic) = tree->values.constlist;
3269 operand *left=newOperand(), *right=newOperand();
3270 left->type=right->type=SYMBOL;
3271 OP_SYMBOL(left)=AST_SYMBOL(tree->left);
3272 OP_SYMBOL(right)=AST_SYMBOL(tree->right);
3273 ic = newiCode (ARRAYINIT, left, right);
3278 /*-----------------------------------------------------------------*/
3279 /* Stuff used in ast2iCode to modify geniCodeDerefPtr in some */
3280 /* particular case. Ie : assigning or dereferencing array or ptr */
3281 /*-----------------------------------------------------------------*/
3282 set * lvaluereqSet = NULL;
3283 typedef struct lvalItem
3290 /*-----------------------------------------------------------------*/
3291 /* addLvaluereq - add a flag for lvalreq for current ast level */
3292 /*-----------------------------------------------------------------*/
3293 void addLvaluereq(int lvl)
3295 lvalItem * lpItem = (lvalItem *)Safe_alloc ( sizeof (lvalItem));
3298 addSetHead(&lvaluereqSet,lpItem);
3301 /*-----------------------------------------------------------------*/
3302 /* delLvaluereq - del a flag for lvalreq for current ast level */
3303 /*-----------------------------------------------------------------*/
3307 lpItem = getSet(&lvaluereqSet);
3308 if(lpItem) Safe_free(lpItem);
3310 /*-----------------------------------------------------------------*/
3311 /* clearLvaluereq - clear lvalreq flag */
3312 /*-----------------------------------------------------------------*/
3313 void clearLvaluereq()
3316 lpItem = peekSet(lvaluereqSet);
3317 if(lpItem) lpItem->req = 0;
3319 /*-----------------------------------------------------------------*/
3320 /* getLvaluereq - get the last lvalreq level */
3321 /*-----------------------------------------------------------------*/
3322 int getLvaluereqLvl()
3325 lpItem = peekSet(lvaluereqSet);
3326 if(lpItem) return lpItem->lvl;
3329 /*-----------------------------------------------------------------*/
3330 /* isLvaluereq - is lvalreq valid for this level ? */
3331 /*-----------------------------------------------------------------*/
3332 int isLvaluereq(int lvl)
3335 lpItem = peekSet(lvaluereqSet);
3336 if(lpItem) return ((lpItem->req)&&(lvl <= (lpItem->lvl+1)));
3340 /*-----------------------------------------------------------------*/
3341 /* ast2iCode - creates an icodeList from an ast */
3342 /*-----------------------------------------------------------------*/
3344 ast2iCode (ast * tree,int lvl)
3346 operand *left = NULL;
3347 operand *right = NULL;
3351 /* set the global variables for filename & line number */
3353 filename = tree->filename;
3355 lineno = tree->lineno;
3357 block = tree->block;
3359 scopeLevel = tree->level;
3361 if (tree->type == EX_VALUE)
3362 return operandFromValue (tree->opval.val);
3364 if (tree->type == EX_LINK)
3365 return operandFromLink (tree->opval.lnk);
3367 /* if we find a nullop */
3368 if (tree->type == EX_OP &&
3369 (tree->opval.op == NULLOP ||
3370 tree->opval.op == BLOCK))
3372 ast2iCode (tree->left,lvl+1);
3373 ast2iCode (tree->right,lvl+1);
3377 /* special cases for not evaluating */
3378 if (tree->opval.op != ':' &&
3379 tree->opval.op != '?' &&
3380 tree->opval.op != CALL &&
3381 tree->opval.op != IFX &&
3382 tree->opval.op != LABEL &&
3383 tree->opval.op != GOTO &&
3384 tree->opval.op != SWITCH &&
3385 tree->opval.op != FUNCTION &&
3386 tree->opval.op != INLINEASM)
3389 if (IS_ASSIGN_OP (tree->opval.op) ||
3390 IS_DEREF_OP (tree) ||
3391 (tree->opval.op == '&' && !tree->right) ||
3392 tree->opval.op == PTR_OP)
3395 if ((IS_ARRAY_OP (tree->left) && IS_ARRAY_OP (tree->left->left)) ||
3396 (IS_DEREF_OP (tree) && IS_ARRAY_OP (tree->left)))
3399 left = operandFromAst (tree->left,lvl);
3401 if (IS_DEREF_OP (tree) && IS_DEREF_OP (tree->left))
3402 left = geniCodeRValue (left, TRUE);
3406 left = operandFromAst (tree->left,lvl);
3408 if (tree->opval.op == INC_OP ||
3409 tree->opval.op == DEC_OP)
3412 right = operandFromAst (tree->right,lvl);
3417 right = operandFromAst (tree->right,lvl);
3421 /* now depending on the type of operand */
3422 /* this will be a biggy */
3423 switch (tree->opval.op)
3426 case '[': /* array operation */
3428 //sym_link *ltype = operandType (left);
3429 //left = geniCodeRValue (left, IS_PTR (ltype->next) ? TRUE : FALSE);
3430 left = geniCodeRValue (left, FALSE);
3431 right = geniCodeRValue (right, TRUE);
3434 return geniCodeArray (left, right,lvl);
3436 case '.': /* structure dereference */
3437 if (IS_PTR (operandType (left)))
3438 left = geniCodeRValue (left, TRUE);
3440 left = geniCodeRValue (left, FALSE);
3442 return geniCodeStruct (left, right, tree->lvalue);
3444 case PTR_OP: /* structure pointer dereference */
3447 pType = operandType (left);
3448 left = geniCodeRValue (left, TRUE);
3450 setOClass (pType, getSpec (operandType (left)));
3453 return geniCodeStruct (left, right, tree->lvalue);
3455 case INC_OP: /* increment operator */
3457 return geniCodePostInc (left);
3459 return geniCodePreInc (right);
3461 case DEC_OP: /* decrement operator */
3463 return geniCodePostDec (left);
3465 return geniCodePreDec (right);
3467 case '&': /* bitwise and or address of operator */
3469 { /* this is a bitwise operator */
3470 left = geniCodeRValue (left, FALSE);
3471 right = geniCodeRValue (right, FALSE);
3472 return geniCodeBitwise (left, right, BITWISEAND, tree->ftype);
3475 return geniCodeAddressOf (left);
3477 case '|': /* bitwise or & xor */
3479 return geniCodeBitwise (geniCodeRValue (left, FALSE),
3480 geniCodeRValue (right, FALSE),
3485 return geniCodeDivision (geniCodeRValue (left, FALSE),
3486 geniCodeRValue (right, FALSE));
3489 return geniCodeModulus (geniCodeRValue (left, FALSE),
3490 geniCodeRValue (right, FALSE));
3493 return geniCodeMultiply (geniCodeRValue (left, FALSE),
3494 geniCodeRValue (right, FALSE),IS_INT(tree->ftype));
3496 return geniCodeDerefPtr (geniCodeRValue (left, FALSE),lvl);
3500 return geniCodeSubtract (geniCodeRValue (left, FALSE),
3501 geniCodeRValue (right, FALSE));
3503 return geniCodeUnaryMinus (geniCodeRValue (left, FALSE));
3507 return geniCodeAdd (geniCodeRValue (left, FALSE),
3508 geniCodeRValue (right, FALSE),lvl);
3510 return geniCodeRValue (left, FALSE); /* unary '+' has no meaning */
3513 return geniCodeLeftShift (geniCodeRValue (left, FALSE),
3514 geniCodeRValue (right, FALSE));
3517 return geniCodeRightShift (geniCodeRValue (left, FALSE),
3518 geniCodeRValue (right, FALSE));
3520 return geniCodeCast (operandType (left),
3521 geniCodeRValue (right, FALSE), FALSE);
3527 return geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
3531 operand *op = geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
3532 setOperandType (op, UCHARTYPE);
3543 return geniCodeLogic (geniCodeRValue (left, FALSE),
3544 geniCodeRValue (right, FALSE),
3547 return geniCodeConditional (tree,lvl);
3550 return operandFromLit (getSize (tree->right->ftype));
3554 sym_link *rtype = operandType (right);
3555 sym_link *ltype = operandType (left);
3556 if (IS_PTR (rtype) && IS_ITEMP (right)
3557 && right->isaddr && compareType (rtype->next, ltype) == 1)
3558 right = geniCodeRValue (right, TRUE);
3560 right = geniCodeRValue (right, FALSE);
3562 geniCodeAssign (left, right, 0);
3567 geniCodeAssign (left,
3568 geniCodeMultiply (geniCodeRValue (operandFromOperand (left),
3570 geniCodeRValue (right, FALSE),FALSE), 0);
3574 geniCodeAssign (left,
3575 geniCodeDivision (geniCodeRValue (operandFromOperand (left),
3577 geniCodeRValue (right, FALSE)), 0);
3580 geniCodeAssign (left,
3581 geniCodeModulus (geniCodeRValue (operandFromOperand (left),
3583 geniCodeRValue (right, FALSE)), 0);
3586 sym_link *rtype = operandType (right);
3587 sym_link *ltype = operandType (left);
3588 if (IS_PTR (rtype) && IS_ITEMP (right)
3589 && right->isaddr && compareType (rtype->next, ltype) == 1)
3590 right = geniCodeRValue (right, TRUE);
3592 right = geniCodeRValue (right, FALSE);
3595 return geniCodeAssign (left,
3596 geniCodeAdd (geniCodeRValue (operandFromOperand (left),
3602 sym_link *rtype = operandType (right);
3603 sym_link *ltype = operandType (left);
3604 if (IS_PTR (rtype) && IS_ITEMP (right)
3605 && right->isaddr && compareType (rtype->next, ltype) == 1)
3607 right = geniCodeRValue (right, TRUE);
3611 right = geniCodeRValue (right, FALSE);
3614 geniCodeAssign (left,
3615 geniCodeSubtract (geniCodeRValue (operandFromOperand (left),
3621 geniCodeAssign (left,
3622 geniCodeLeftShift (geniCodeRValue (operandFromOperand (left)
3624 geniCodeRValue (right, FALSE)), 0);
3627 geniCodeAssign (left,
3628 geniCodeRightShift (geniCodeRValue (operandFromOperand (left)
3630 geniCodeRValue (right, FALSE)), 0);
3633 geniCodeAssign (left,
3634 geniCodeBitwise (geniCodeRValue (operandFromOperand (left),
3636 geniCodeRValue (right, FALSE),
3638 operandType (left)), 0);
3641 geniCodeAssign (left,
3642 geniCodeBitwise (geniCodeRValue (operandFromOperand (left),
3644 geniCodeRValue (right, FALSE),
3646 operandType (left)), 0);
3649 geniCodeAssign (left,
3650 geniCodeBitwise (geniCodeRValue (operandFromOperand (left)
3652 geniCodeRValue (right, FALSE),
3654 operandType (left)), 0);
3656 return geniCodeRValue (right, FALSE);
3659 return geniCodeCall (ast2iCode (tree->left,lvl+1),
3662 geniCodeLabel (ast2iCode (tree->left,lvl+1)->operand.symOperand);
3663 return ast2iCode (tree->right,lvl+1);
3666 geniCodeGoto (ast2iCode (tree->left,lvl+1)->operand.symOperand);
3667 return ast2iCode (tree->right,lvl+1);
3670 geniCodeFunctionBody (tree,lvl);
3674 geniCodeReturn (right);
3678 geniCodeIfx (tree,lvl);
3682 geniCodeSwitch (tree,lvl);
3686 geniCodeInline (tree);
3690 geniCodeArrayInit(tree, ast2iCode (tree->left,lvl+1));
3697 /*-----------------------------------------------------------------*/
3698 /* reverseICChain - gets from the list and creates a linkedlist */
3699 /*-----------------------------------------------------------------*/
3706 while ((loop = getSet (&iCodeChain)))
3718 /*-----------------------------------------------------------------*/
3719 /* iCodeFromAst - given an ast will convert it to iCode */
3720 /*-----------------------------------------------------------------*/
3722 iCodeFromAst (ast * tree)
3724 returnLabel = newiTempLabel ("_return");
3725 entryLabel = newiTempLabel ("_entry");
3727 return reverseiCChain ();