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;
43 symbol *returnLabel; /* function return label */
44 symbol *entryLabel; /* function entry label */
46 /*-----------------------------------------------------------------*/
47 /* forward definition of some functions */
48 operand *geniCodeAssign (operand *, operand *, int);
49 static operand *geniCodeArray (operand *, operand *,int);
50 static operand *geniCodeArray2Ptr (operand *);
51 operand *geniCodeRValue (operand *, bool);
52 operand *geniCodeDerefPtr (operand *,int);
53 int isLvaluereq(int lvl);
54 void setOClass (sym_link * ptr, sym_link * spec);
55 static operand *geniCodeCast (sym_link *, operand *, bool);
57 #define PRINTFUNC(x) void x (FILE *of, iCode *ic, char *s)
58 /* forward definition of ic print functions */
59 PRINTFUNC (picGetValueAtAddr);
60 PRINTFUNC (picSetValueAtAddr);
61 PRINTFUNC (picAddrOf);
62 PRINTFUNC (picGeneric);
63 PRINTFUNC (picGenericOne);
65 PRINTFUNC (picAssign);
69 PRINTFUNC (picJumpTable);
70 PRINTFUNC (picInline);
71 PRINTFUNC (picReceive);
72 PRINTFUNC (picDummyRead);
73 PRINTFUNC (picCritical);
74 PRINTFUNC (picEndCritical);
76 iCodeTable codeTable[] =
78 {'!', "not", picGenericOne, NULL},
79 {'~', "~", picGenericOne, NULL},
80 {RRC, "rrc", picGenericOne, NULL},
81 {RLC, "rlc", picGenericOne, NULL},
82 {GETHBIT, "ghbit", picGenericOne, NULL},
83 {UNARYMINUS, "-", picGenericOne, NULL},
84 {IPUSH, "push", picGenericOne, NULL},
85 {IPOP, "pop", picGenericOne, NULL},
86 {CALL, "call", picGenericOne, NULL},
87 {PCALL, "pcall", picGenericOne, NULL},
88 {FUNCTION, "proc", picGenericOne, NULL},
89 {ENDFUNCTION, "eproc", picGenericOne, NULL},
90 {RETURN, "ret", picGenericOne, NULL},
91 {'+', "+", picGeneric, NULL},
92 {'-', "-", picGeneric, NULL},
93 {'*', "*", picGeneric, NULL},
94 {'/', "/", picGeneric, NULL},
95 {'%', "%", picGeneric, NULL},
96 {'>', ">", picGeneric, NULL},
97 {'<', "<", picGeneric, NULL},
98 {LE_OP, "<=", picGeneric, NULL},
99 {GE_OP, ">=", picGeneric, NULL},
100 {EQ_OP, "==", picGeneric, NULL},
101 {NE_OP, "!=", picGeneric, NULL},
102 {AND_OP, "&&", picGeneric, NULL},
103 {OR_OP, "||", picGeneric, NULL},
104 {'^', "^", picGeneric, NULL},
105 {'|', "|", picGeneric, NULL},
106 {BITWISEAND, "&", picGeneric, NULL},
107 {LEFT_OP, "<<", picGeneric, NULL},
108 {RIGHT_OP, ">>", picGeneric, NULL},
109 {GET_VALUE_AT_ADDRESS, "@", picGetValueAtAddr, NULL},
110 {ADDRESS_OF, "&", picAddrOf, NULL},
111 {CAST, "<>", picCast, NULL},
112 {'=', ":=", picAssign, NULL},
113 {LABEL, "", picLabel, NULL},
114 {GOTO, "", picGoto, NULL},
115 {JUMPTABLE, "jtab", picJumpTable, NULL},
116 {IFX, "if", picIfx, NULL},
117 {INLINEASM, "", picInline, NULL},
118 {RECEIVE, "recv", picReceive, NULL},
119 {SEND, "send", picGenericOne, NULL},
120 {ARRAYINIT, "arrayInit", picGenericOne, NULL},
121 {DUMMY_READ_VOLATILE, "dummy = (volatile)", picDummyRead, NULL},
122 {CRITICAL, "critical_start", picCritical, NULL},
123 {ENDCRITICAL, "critical_end", picEndCritical, NULL}
126 /*-----------------------------------------------------------------*/
127 /* checkConstantRange: check a constant against the type */
128 /*-----------------------------------------------------------------*/
131 /* pedantic=0: allmost anything is allowed as long as the absolute
132 value is within the bit range of the type, and -1 is treated as
133 0xf..f for unsigned types (e.g. in assign)
134 pedantic=1: -1==-1, not allowed for unsigned types (e.g. in compare)
135 pedantic>1: "char c=200" is not allowed (evaluates to -56)
138 void checkConstantRange(sym_link *ltype, value *val, char *msg,
145 max = pow ((double)2.0, (double)bitsForType(ltype));
147 if (IS_LONG(val->type)) {
148 if (IS_UNSIGNED(val->type)) {
149 v=SPEC_CVAL(val->type).v_ulong;
151 v=SPEC_CVAL(val->type).v_long;
154 if (IS_UNSIGNED(val->type)) {
155 v=SPEC_CVAL(val->type).v_uint;
157 v=SPEC_CVAL(val->type).v_int;
163 // this could be a good idea
164 if (options.pedantic)
168 if (IS_FLOAT(ltype)) {
173 if (!IS_UNSIGNED(val->type) && v<0) {
175 if (IS_UNSIGNED(ltype) && (pedantic>1)) {
181 // if very pedantic: "char c=200" is not allowed
182 if (pedantic>1 && !IS_UNSIGNED(ltype)) {
183 max = max/2 + negative;
190 #if 0 // temporary disabled, leaving the warning as a reminder
192 SNPRINTF (message, sizeof(message), "for %s %s in %s",
193 IS_UNSIGNED(ltype) ? "unsigned" : "signed",
194 nounName(ltype), msg);
195 werror (W_CONST_RANGE, message);
203 /*-----------------------------------------------------------------*/
204 /* operandName - returns the name of the operand */
205 /*-----------------------------------------------------------------*/
207 printOperand (operand * op, FILE * file)
224 opetype = getSpec (operandType (op));
225 if (IS_FLOAT (opetype))
226 fprintf (file, "%g {", SPEC_CVAL (opetype).v_float);
228 fprintf (file, "0x%x {", (unsigned) floatFromVal (op->operand.valOperand));
229 printTypeChain (operandType (op), file);
236 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}" , */
237 (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name),
239 OP_LIVEFROM (op), OP_LIVETO (op),
240 OP_SYMBOL (op)->stack,
241 op->isaddr, OP_SYMBOL (op)->isreqv,
242 OP_SYMBOL (op)->remat,OP_SYMBOL(op)->noSpilLoc,
243 OP_SYMBOL(op)->ruonly,OP_SYMBOL(op)->dptr
247 printTypeChain (operandType (op), file);
248 if (SPIL_LOC (op) && IS_ITEMP (op))
249 fprintf (file, "}{ sir@ %s", SPIL_LOC (op)->rname);
254 /* if assigned to registers */
255 if (OP_SYMBOL (op)->nRegs)
257 if (OP_SYMBOL (op)->isspilt)
259 if (!OP_SYMBOL (op)->remat)
260 if (OP_SYMBOL (op)->usl.spillLoc)
261 fprintf (file, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ?
262 OP_SYMBOL (op)->usl.spillLoc->rname :
263 OP_SYMBOL (op)->usl.spillLoc->name));
265 fprintf (file, "[err]");
267 fprintf (file, "[remat]");
273 for (i = 0; i < OP_SYMBOL (op)->nRegs; i++)
274 fprintf (file, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i]));
279 fprintf (file, "%s", (OP_SYMBOL (op)->rname[0] ?
280 OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name));
281 /* if assigned to registers */
282 if (OP_SYMBOL (op)->nRegs && !OP_SYMBOL (op)->isspilt)
286 for (i = 0; i < OP_SYMBOL (op)->nRegs; i++)
287 fprintf (file, "%s ", (OP_SYMBOL (op)->regs[i] ?
288 OP_SYMBOL (op)->regs[i]->name :
297 printTypeChain (op->operand.typeOperand, file);
303 fprintf (file, "\n");
308 /*-----------------------------------------------------------------*/
309 /* print functions */
310 /*-----------------------------------------------------------------*/
311 PRINTFUNC (picGetValueAtAddr)
314 printOperand (IC_RESULT (ic), of);
317 printOperand (IC_LEFT (ic), of);
323 PRINTFUNC (picSetValueAtAddr)
327 printOperand (IC_LEFT (ic), of);
328 fprintf (of, "] = ");
329 printOperand (IC_RIGHT (ic), of);
333 PRINTFUNC (picAddrOf)
336 printOperand (IC_RESULT (ic), of);
337 if (IS_ITEMP (IC_LEFT (ic)))
340 fprintf (of, " = &[");
341 printOperand (IC_LEFT (ic), of);
344 if (IS_ITEMP (IC_LEFT (ic)))
345 fprintf (of, " offsetAdd ");
348 printOperand (IC_RIGHT (ic), of);
350 if (IS_ITEMP (IC_LEFT (ic)))
356 PRINTFUNC (picJumpTable)
361 fprintf (of, "%s\t", s);
362 printOperand (IC_JTCOND (ic), of);
364 for (sym = setFirstItem (IC_JTLABELS (ic)); sym;
365 sym = setNextItem (IC_JTLABELS (ic)))
366 fprintf (of, "\t\t\t%s\n", sym->name);
369 PRINTFUNC (picGeneric)
372 printOperand (IC_RESULT (ic), of);
374 printOperand (IC_LEFT (ic), of);
375 fprintf (of, " %s ", s);
376 printOperand (IC_RIGHT (ic), of);
380 PRINTFUNC (picGenericOne)
385 printOperand (IC_RESULT (ic), of);
391 fprintf (of, "%s ", s);
392 printOperand (IC_LEFT (ic), of);
395 if (!IC_RESULT (ic) && !IC_LEFT (ic))
398 if (ic->op == SEND || ic->op == RECEIVE) {
399 fprintf(of,"{argreg = %d}",ic->argreg);
407 printOperand (IC_RESULT (ic), of);
409 printOperand (IC_LEFT (ic), of);
410 printOperand (IC_RIGHT (ic), of);
415 PRINTFUNC (picAssign)
419 if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic)))
422 printOperand (IC_RESULT (ic), of);
424 if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic)))
427 fprintf (of, " %s ", s);
428 printOperand (IC_RIGHT (ic), of);
435 fprintf (of, " %s($%d) :\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key);
441 fprintf (of, " goto %s($%d)\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key);
448 printOperand (IC_COND (ic), of);
451 fprintf (of, " == 0 goto %s($%d)\n", IC_FALSE (ic)->name, IC_FALSE (ic)->key);
454 fprintf (of, " != 0 goto %s($%d)\n", IC_TRUE (ic)->name, IC_TRUE (ic)->key);
456 fprintf (of, "\tzzgoto %s\n", IC_FALSE (ic)->name);
460 PRINTFUNC (picInline)
462 fprintf (of, "%s", IC_INLINE (ic));
465 PRINTFUNC (picReceive)
467 printOperand (IC_RESULT (ic), of);
468 fprintf (of, " = %s ", s);
469 printOperand (IC_LEFT (ic), of);
473 PRINTFUNC (picDummyRead)
476 fprintf (of, "%s ", s);
477 printOperand (IC_RIGHT (ic), of);
481 PRINTFUNC (picCritical)
485 printOperand (IC_RESULT (ic), of);
487 fprintf (of, "(stack)");
488 fprintf (of, " = %s ", s);
492 PRINTFUNC (picEndCritical)
495 fprintf (of, "%s = ", s);
497 printOperand (IC_RIGHT (ic), of);
499 fprintf (of, "(stack)");
503 /*-----------------------------------------------------------------*/
504 /* piCode - prints one iCode */
505 /*-----------------------------------------------------------------*/
507 piCode (void *item, FILE * of)
515 icTab = getTableEntry (ic->op);
516 fprintf (stdout, "%s(%d:%d:%d:%d:%d)\t",
517 ic->filename, ic->lineno,
518 ic->seq, ic->key, ic->depth, ic->supportRtn);
519 icTab->iCodePrint (of, ic, icTab->printName);
525 printiCChain(ic,stdout);
527 /*-----------------------------------------------------------------*/
528 /* printiCChain - prints intermediate code for humans */
529 /*-----------------------------------------------------------------*/
531 printiCChain (iCode * icChain, FILE * of)
538 for (loop = icChain; loop; loop = loop->next)
540 if ((icTab = getTableEntry (loop->op)))
542 fprintf (of, "%s(l%d:s%d:k%d:d%d:s%d)\t",
543 loop->filename, loop->lineno,
544 loop->seq, loop->key, loop->depth, loop->supportRtn);
546 icTab->iCodePrint (of, loop, icTab->printName);
552 /*-----------------------------------------------------------------*/
553 /* newOperand - allocate, init & return a new iCode */
554 /*-----------------------------------------------------------------*/
560 op = Safe_alloc ( sizeof (operand));
566 /*-----------------------------------------------------------------*/
567 /* newiCode - create and return a new iCode entry initialised */
568 /*-----------------------------------------------------------------*/
570 newiCode (int op, operand * left, operand * right)
574 ic = Safe_alloc ( sizeof (iCode));
576 ic->seqPoint = seqPoint;
578 ic->filename = filename;
580 ic->level = scopeLevel;
582 ic->key = iCodeKey++;
584 IC_RIGHT (ic) = right;
589 /*-----------------------------------------------------------------*/
590 /* newiCode for conditional statements */
591 /*-----------------------------------------------------------------*/
593 newiCodeCondition (operand * condition,
599 if (IS_VOID(operandType(condition))) {
600 werror(E_VOID_VALUE_USED);
603 ic = newiCode (IFX, NULL, NULL);
604 IC_COND (ic) = condition;
605 IC_TRUE (ic) = trueLabel;
606 IC_FALSE (ic) = falseLabel;
610 /*-----------------------------------------------------------------*/
611 /* newiCodeLabelGoto - unconditional goto statement| label stmnt */
612 /*-----------------------------------------------------------------*/
614 newiCodeLabelGoto (int op, symbol * label)
618 ic = newiCode (op, NULL, NULL);
622 IC_RIGHT (ic) = NULL;
623 IC_RESULT (ic) = NULL;
627 /*-----------------------------------------------------------------*/
628 /* newiTemp - allocate & return a newItemp Variable */
629 /*-----------------------------------------------------------------*/
637 SNPRINTF (buffer, sizeof(buffer), "%s", s);
641 SNPRINTF (buffer, sizeof(buffer), "iTemp%d", iTempNum++);
644 itmp = newSymbol (buffer, 1);
645 strncpyz (itmp->rname, itmp->name, SDCC_NAME_MAX);
651 /*-----------------------------------------------------------------*/
652 /* newiTempLabel - creates a temp variable label */
653 /*-----------------------------------------------------------------*/
655 newiTempLabel (char *s)
659 /* check if this alredy exists */
660 if (s && (itmplbl = findSym (LabelTab, NULL, s)))
665 itmplbl = newSymbol (s, 1);
669 SNPRINTF (buffer, sizeof(buffer), "iTempLbl%d", iTempLblNum++);
670 itmplbl = newSymbol (buffer, 1);
675 itmplbl->key = labelKey++;
676 addSym (LabelTab, itmplbl, itmplbl->name, 0, 0, 0);
680 /*-----------------------------------------------------------------*/
681 /* newiTempPreheaderLabel - creates a new preheader label */
682 /*-----------------------------------------------------------------*/
684 newiTempPreheaderLabel ()
688 SNPRINTF (buffer, sizeof(buffer), "preHeaderLbl%d", iTempLblNum++);
689 itmplbl = newSymbol (buffer, 1);
693 itmplbl->key = labelKey++;
694 addSym (LabelTab, itmplbl, itmplbl->name, 0, 0, 0);
699 /*-----------------------------------------------------------------*/
700 /* initiCode - initialises some iCode related stuff */
701 /*-----------------------------------------------------------------*/
708 /*-----------------------------------------------------------------*/
709 /* copyiCode - make a copy of the iCode given */
710 /*-----------------------------------------------------------------*/
712 copyiCode (iCode * ic)
714 iCode *nic = newiCode (ic->op, NULL, NULL);
716 nic->lineno = ic->lineno;
717 nic->filename = ic->filename;
718 nic->block = ic->block;
719 nic->level = ic->level;
720 nic->parmBytes = ic->parmBytes;
722 /* deal with the special cases first */
726 IC_COND (nic) = operandFromOperand (IC_COND (ic));
727 IC_TRUE (nic) = IC_TRUE (ic);
728 IC_FALSE (nic) = IC_FALSE (ic);
732 IC_JTCOND (nic) = operandFromOperand (IC_JTCOND (ic));
733 IC_JTLABELS (nic) = IC_JTLABELS (ic);
738 IC_RESULT (nic) = operandFromOperand (IC_RESULT (ic));
739 IC_LEFT (nic) = operandFromOperand (IC_LEFT (ic));
743 IC_INLINE (nic) = IC_INLINE (ic);
747 IC_ARRAYILIST(nic) = IC_ARRAYILIST(ic);
751 IC_RESULT (nic) = operandFromOperand (IC_RESULT (ic));
752 IC_LEFT (nic) = operandFromOperand (IC_LEFT (ic));
753 IC_RIGHT (nic) = operandFromOperand (IC_RIGHT (ic));
759 /*-----------------------------------------------------------------*/
760 /* getTableEntry - gets the table entry for the given operator */
761 /*-----------------------------------------------------------------*/
763 getTableEntry (int oper)
767 for (i = 0; i < (sizeof (codeTable) / sizeof (iCodeTable)); i++)
768 if (oper == codeTable[i].icode)
769 return &codeTable[i];
774 /*-----------------------------------------------------------------*/
775 /* newiTempOperand - new intermediate temp operand */
776 /*-----------------------------------------------------------------*/
778 newiTempOperand (sym_link * type, char throwType)
781 operand *op = newOperand ();
785 itmp = newiTemp (NULL);
787 etype = getSpec (type);
789 if (IS_LITERAL (etype))
792 /* copy the type information */
794 itmp->etype = getSpec (itmp->type = (throwType ? type :
795 copyLinkChain (type)));
796 if (IS_LITERAL (itmp->etype))
798 SPEC_SCLS (itmp->etype) = S_REGISTER;
799 SPEC_OCLS (itmp->etype) = reg;
802 op->operand.symOperand = itmp;
803 op->key = itmp->key = ++operandKey;
807 /*-----------------------------------------------------------------*/
808 /* operandType - returns the type chain for an operand */
809 /*-----------------------------------------------------------------*/
811 operandType (operand * op)
813 /* depending on type of operand */
818 return op->operand.valOperand->type;
821 return op->operand.symOperand->type;
824 return op->operand.typeOperand;
826 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
827 " operand type not known ");
828 assert (0); /* should never come here */
829 /* Just to keep the compiler happy */
830 return (sym_link *) 0;
834 /*-----------------------------------------------------------------*/
835 /* isParamterToCall - will return 1 if op is a parameter to args */
836 /*-----------------------------------------------------------------*/
838 isParameterToCall (value * args, operand * op)
842 wassert (IS_SYMOP(op));
847 isSymbolEqual (op->operand.symOperand, tval->sym))
854 /*-----------------------------------------------------------------*/
855 /* isOperandGlobal - return 1 if operand is a global variable */
856 /*-----------------------------------------------------------------*/
858 isOperandGlobal (operand * op)
867 (op->operand.symOperand->level == 0 ||
868 IS_STATIC (op->operand.symOperand->etype) ||
869 IS_EXTERN (op->operand.symOperand->etype))
876 /*-----------------------------------------------------------------*/
877 /* isOperandVolatile - return 1 if the operand is volatile */
878 /*-----------------------------------------------------------------*/
880 isOperandVolatile (operand * op, bool chkTemp)
885 if (IS_ITEMP (op) && !chkTemp)
888 opetype = getSpec (optype = operandType (op));
890 if (IS_PTR (optype) && DCL_PTR_VOLATILE (optype))
893 if (IS_VOLATILE (opetype))
898 /*-----------------------------------------------------------------*/
899 /* isOperandLiteral - returns 1 if an operand contains a literal */
900 /*-----------------------------------------------------------------*/
902 isOperandLiteral (operand * op)
909 opetype = getSpec (operandType (op));
911 if (IS_LITERAL (opetype))
917 /*-----------------------------------------------------------------*/
918 /* isOperandInFarSpace - will return true if operand is in farSpace */
919 /*-----------------------------------------------------------------*/
921 isOperandInFarSpace (operand * op)
931 if (!IS_TRUE_SYMOP (op))
934 etype = SPIL_LOC (op)->etype;
940 etype = getSpec (operandType (op));
942 return (IN_FARSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
945 /*------------------------------------------------------------------*/
946 /* isOperandInDirSpace - will return true if operand is in dirSpace */
947 /*------------------------------------------------------------------*/
949 isOperandInDirSpace (operand * op)
959 if (!IS_TRUE_SYMOP (op))
962 etype = SPIL_LOC (op)->etype;
968 etype = getSpec (operandType (op));
970 return (IN_DIRSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
973 /*--------------------------------------------------------------------*/
974 /* isOperandInCodeSpace - will return true if operand is in codeSpace */
975 /*--------------------------------------------------------------------*/
977 isOperandInCodeSpace (operand * op)
987 etype = getSpec (operandType (op));
989 if (!IS_TRUE_SYMOP (op))
992 etype = SPIL_LOC (op)->etype;
998 etype = getSpec (operandType (op));
1000 return (IN_CODESPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
1003 /*-----------------------------------------------------------------*/
1004 /* isOperandOnStack - will return true if operand is on stack */
1005 /*-----------------------------------------------------------------*/
1007 isOperandOnStack (operand * op)
1017 etype = getSpec (operandType (op));
1018 if (IN_STACK (etype) ||
1019 OP_SYMBOL(op)->onStack ||
1020 (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
1026 /*-----------------------------------------------------------------*/
1027 /* isOclsExpensive - will return true if accesses to an output */
1028 /* storage class are expensive */
1029 /*-----------------------------------------------------------------*/
1031 isOclsExpensive (struct memmap *oclass)
1033 if (port->oclsExpense)
1034 return port->oclsExpense (oclass) > 0;
1036 /* In the absence of port specific guidance, assume only */
1037 /* farspace is expensive. */
1038 return IN_FARSPACE (oclass);
1041 /*-----------------------------------------------------------------*/
1042 /* operandLitValue - literal value of an operand */
1043 /*-----------------------------------------------------------------*/
1045 operandLitValue (operand * op)
1047 assert (isOperandLiteral (op));
1049 return floatFromVal (op->operand.valOperand);
1052 /*-----------------------------------------------------------------*/
1053 /* getBuiltInParms - returns parameters to a builtin functions */
1054 /*-----------------------------------------------------------------*/
1055 iCode *getBuiltinParms (iCode *ic, int *pcount, operand **parms)
1060 /* builtin functions uses only SEND for parameters */
1061 while (ic->op != CALL) {
1062 assert(ic->op == SEND && ic->builtinSEND);
1063 ic->generated = 1; /* mark the icode as generated */
1064 parms[*pcount] = IC_LEFT(ic);
1070 /* make sure this is a builtin function call */
1071 assert(IS_SYMOP(IC_LEFT(ic)));
1072 ftype = operandType(IC_LEFT(ic));
1073 assert(IFFUNC_ISBUILTIN(ftype));
1077 /*-----------------------------------------------------------------*/
1078 /* operandOperation - performs operations on operands */
1079 /*-----------------------------------------------------------------*/
1081 operandOperation (operand * left, operand * right,
1082 int op, sym_link * type)
1084 sym_link *let , *ret=NULL;
1085 operand *retval = (operand *) 0;
1087 assert (isOperandLiteral (left));
1088 let = getSpec(operandType(left));
1090 assert (isOperandLiteral (right));
1091 ret = getSpec(operandType(right));
1097 retval = operandFromValue (valCastLiteral (type,
1098 operandLitValue (left) +
1099 operandLitValue (right)));
1102 retval = operandFromValue (valCastLiteral (type,
1103 operandLitValue (left) -
1104 operandLitValue (right)));
1108 retval = operandFromValue (valCastLiteral (type,
1109 operandLitValue (left) *
1110 operandLitValue (right)));
1111 This could be all we've to do, but with gcc we've to take care about
1112 overflows. Two examples:
1113 ULONG_MAX * ULONG_MAX doesn't fit into a double, some of the least
1114 significant bits are lost (52 in fraction, 63 bits would be
1115 necessary to keep full precision).
1116 If the resulting double value is greater than ULONG_MAX (resp.
1117 USHRT_MAX, ...), then 0 will be assigned to v_ulong (resp. u_uint, ...)!
1120 /* if it is not a specifier then we can assume that */
1121 /* it will be an unsigned long */
1122 if (IS_INT (type) ||
1125 /* long is handled here, because it can overflow with double */
1126 if (IS_LONG (type) ||
1128 /* signed and unsigned mul are the same, as long as the precision
1129 of the result isn't bigger than the precision of the operands. */
1130 retval = operandFromValue (valCastLiteral (type,
1131 (TYPE_UDWORD) operandLitValue (left) *
1132 (TYPE_UDWORD) operandLitValue (right)));
1133 else if (IS_UNSIGNED (type)) /* unsigned int */
1135 /* unsigned int is handled here in order to detect overflow */
1136 TYPE_UDWORD ul = (TYPE_UWORD) operandLitValue (left) *
1137 (TYPE_UWORD) operandLitValue (right);
1139 retval = operandFromValue (valCastLiteral (type, (TYPE_UWORD) ul));
1140 if (ul != (TYPE_UWORD) ul)
1143 else /* signed int */
1145 /* signed int is handled here in order to detect overflow */
1146 TYPE_DWORD l = (TYPE_WORD) operandLitValue (left) *
1147 (TYPE_WORD) operandLitValue (right);
1149 retval = operandFromValue (valCastLiteral (type, (TYPE_WORD) l));
1150 if (l != (TYPE_WORD) l)
1155 /* all others go here: */
1156 retval = operandFromValue (valCastLiteral (type,
1157 operandLitValue (left) *
1158 operandLitValue (right)));
1161 if ((TYPE_UDWORD) operandLitValue (right) == 0)
1163 werror (E_DIVIDE_BY_ZERO);
1169 if (IS_UNSIGNED (type))
1171 SPEC_USIGN (let) = 1;
1172 SPEC_USIGN (ret) = 1;
1173 retval = operandFromValue (valCastLiteral (type,
1174 (TYPE_UDWORD) operandLitValue (left) /
1175 (TYPE_UDWORD) operandLitValue (right)));
1179 retval = operandFromValue (valCastLiteral (type,
1180 operandLitValue (left) /
1181 operandLitValue (right)));
1186 if ((TYPE_UDWORD) operandLitValue (right) == 0)
1188 werror (E_DIVIDE_BY_ZERO);
1193 if (IS_UNSIGNED (type))
1194 retval = operandFromLit ((TYPE_UDWORD) operandLitValue (left) %
1195 (TYPE_UDWORD) operandLitValue (right));
1197 retval = operandFromLit ((TYPE_DWORD) operandLitValue (left) %
1198 (TYPE_DWORD) operandLitValue (right));
1202 /* The number of left shifts is always unsigned. Signed doesn't make
1203 sense here. Shifting by a negative number is impossible. */
1204 retval = operandFromValue (valCastLiteral (type,
1205 ((TYPE_UDWORD) operandLitValue (left) <<
1206 (TYPE_UDWORD) operandLitValue (right))));
1209 /* The number of right shifts is always unsigned. Signed doesn't make
1210 sense here. Shifting by a negative number is impossible. */
1211 if (IS_UNSIGNED(let))
1212 /* unsigned: logic shift right */
1213 retval = operandFromLit ((TYPE_UDWORD) operandLitValue (left) >>
1214 (TYPE_UDWORD) operandLitValue (right));
1216 /* signed: arithmetic shift right */
1217 retval = operandFromLit ((TYPE_DWORD ) operandLitValue (left) >>
1218 (TYPE_UDWORD) operandLitValue (right));
1221 if (IS_FLOAT (let) ||
1224 retval = operandFromLit (operandLitValue (left) ==
1225 operandLitValue (right));
1229 /* this op doesn't care about signedness */
1232 l = (TYPE_UDWORD) operandLitValue (left);
1233 r = (TYPE_UDWORD) operandLitValue (right);
1234 /* In order to correctly compare 'signed int' and 'unsigned int' it's
1235 neccessary to strip them to 16 bit.
1236 Literals are reduced to their cheapest type, therefore left and
1237 right might have different types. It's neccessary to find a
1238 common type: int (used for char too) or long */
1239 if (!IS_LONG (let) &&
1245 retval = operandFromLit (l == r);
1249 retval = operandFromLit (operandLitValue (left) <
1250 operandLitValue (right));
1253 retval = operandFromLit (operandLitValue (left) <=
1254 operandLitValue (right));
1257 retval = operandFromLit (operandLitValue (left) !=
1258 operandLitValue (right));
1261 retval = operandFromLit (operandLitValue (left) >
1262 operandLitValue (right));
1265 retval = operandFromLit (operandLitValue (left) >=
1266 operandLitValue (right));
1269 retval = operandFromValue (valCastLiteral (type,
1270 (TYPE_UDWORD)operandLitValue(left) &
1271 (TYPE_UDWORD)operandLitValue(right)));
1274 retval = operandFromValue (valCastLiteral (type,
1275 (TYPE_UDWORD)operandLitValue(left) |
1276 (TYPE_UDWORD)operandLitValue(right)));
1279 retval = operandFromValue (valCastLiteral (type,
1280 (TYPE_UDWORD)operandLitValue(left) ^
1281 (TYPE_UDWORD)operandLitValue(right)));
1284 retval = operandFromLit (operandLitValue (left) &&
1285 operandLitValue (right));
1288 retval = operandFromLit (operandLitValue (left) ||
1289 operandLitValue (right));
1293 TYPE_UDWORD i = (TYPE_UDWORD) operandLitValue (left);
1295 retval = operandFromLit ((i >> (getSize (operandType (left)) * 8 - 1)) |
1301 TYPE_UDWORD i = (TYPE_UDWORD) operandLitValue (left);
1303 retval = operandFromLit ((i << (getSize (operandType (left)) * 8 - 1)) |
1309 retval = operandFromValue (valCastLiteral (type,
1310 -1 * operandLitValue (left)));
1314 retval = operandFromValue (valCastLiteral (type,
1316 operandLitValue (left))));
1320 retval = operandFromLit (!operandLitValue (left));
1324 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1325 " operandOperation invalid operator ");
1333 /*-----------------------------------------------------------------*/
1334 /* isOperandEqual - compares two operand & return 1 if they r = */
1335 /*-----------------------------------------------------------------*/
1337 isOperandEqual (operand * left, operand * right)
1339 /* if the pointers are equal then they are equal */
1343 /* if either of them null then false */
1344 if (!left || !right)
1347 if (left->type != right->type)
1350 if (IS_SYMOP (left) && IS_SYMOP (right))
1351 return left->key == right->key;
1353 /* if types are the same */
1357 return isSymbolEqual (left->operand.symOperand,
1358 right->operand.symOperand);
1360 return (floatFromVal (left->operand.valOperand) ==
1361 floatFromVal (right->operand.valOperand));
1363 if (compareType (left->operand.typeOperand,
1364 right->operand.typeOperand) == 1)
1371 /*-------------------------------------------------------------------*/
1372 /* isiCodeEqual - compares two iCodes are equal, returns true if yes */
1373 /*-------------------------------------------------------------------*/
1375 isiCodeEqual (iCode * left, iCode * right)
1377 /* if the same pointer */
1381 /* if either of them null */
1382 if (!left || !right)
1385 /* if operand are the same */
1386 if (left->op == right->op)
1389 /* compare all the elements depending on type */
1390 if (left->op != IFX)
1392 if (!isOperandEqual (IC_LEFT (left), IC_LEFT (right)))
1394 if (!isOperandEqual (IC_RIGHT (left), IC_RIGHT (right)))
1400 if (!isOperandEqual (IC_COND (left), IC_COND (right)))
1402 if (!isSymbolEqual (IC_TRUE (left), IC_TRUE (right)))
1404 if (!isSymbolEqual (IC_FALSE (left), IC_FALSE (right)))
1413 /*-----------------------------------------------------------------*/
1414 /* newiTempFromOp - create a temp Operand with same attributes */
1415 /*-----------------------------------------------------------------*/
1417 newiTempFromOp (operand * op)
1427 nop = newiTempOperand (operandType (op), TRUE);
1428 nop->isaddr = op->isaddr;
1429 nop->isvolatile = op->isvolatile;
1430 nop->isGlobal = op->isGlobal;
1431 nop->isLiteral = op->isLiteral;
1432 nop->usesDefs = op->usesDefs;
1433 nop->isParm = op->isParm;
1437 /*-----------------------------------------------------------------*/
1438 /* operand from operand - creates an operand holder for the type */
1439 /*-----------------------------------------------------------------*/
1441 operandFromOperand (operand * op)
1447 nop = newOperand ();
1448 nop->type = op->type;
1449 nop->isaddr = op->isaddr;
1451 nop->isvolatile = op->isvolatile;
1452 nop->isGlobal = op->isGlobal;
1453 nop->isLiteral = op->isLiteral;
1454 nop->usesDefs = op->usesDefs;
1455 nop->isParm = op->isParm;
1460 nop->operand.symOperand = op->operand.symOperand;
1463 nop->operand.valOperand = op->operand.valOperand;
1466 nop->operand.typeOperand = op->operand.typeOperand;
1473 /*-----------------------------------------------------------------*/
1474 /* opFromOpWithDU - makes a copy of the operand and DU chains */
1475 /*-----------------------------------------------------------------*/
1477 opFromOpWithDU (operand * op, bitVect * defs, bitVect * uses)
1479 operand *nop = operandFromOperand (op);
1481 if (nop->type == SYMBOL)
1483 OP_SYMBOL (nop)->defs = bitVectCopy (defs);
1484 OP_SYMBOL (nop)->uses = bitVectCopy (uses);
1490 /*-----------------------------------------------------------------*/
1491 /* operandFromSymbol - creates an operand from a symbol */
1492 /*-----------------------------------------------------------------*/
1494 operandFromSymbol (symbol * sym)
1499 /* if the symbol's type is a literal */
1500 /* then it is an enumerator type */
1501 if (IS_LITERAL (sym->etype) && SPEC_ENUM (sym->etype))
1502 return operandFromValue (valFromType (sym->etype));
1505 sym->key = ++operandKey;
1507 /* if this an implicit variable, means struct/union */
1508 /* member so just return it */
1509 if (sym->implicit || IS_FUNC (sym->type))
1513 op->operand.symOperand = sym;
1515 op->isvolatile = isOperandVolatile (op, TRUE);
1516 op->isGlobal = isOperandGlobal (op);
1520 /* under the following conditions create a
1521 register equivalent for a local symbol */
1522 if (sym->level && sym->etype && SPEC_OCLS (sym->etype) &&
1523 (IN_FARSPACE (SPEC_OCLS (sym->etype)) &&
1525 (!(options.model == MODEL_FLAT24)) ) &&
1526 options.stackAuto == 0)
1529 if (!IS_AGGREGATE (sym->type) && /* not an aggregate */
1530 !IS_FUNC (sym->type) && /* not a function */
1531 !sym->_isparm && /* not a parameter */
1532 sym->level && /* is a local variable */
1533 !sym->addrtaken && /* whose address has not been taken */
1534 !sym->reqv && /* does not already have a reg equivalence */
1535 !IS_VOLATILE (sym->etype) && /* not declared as volatile */
1536 !IS_STATIC (sym->etype) && /* and not declared static */
1537 !sym->islbl && /* not a label */
1538 ok && /* farspace check */
1539 !IS_BITVAR (sym->etype) /* not a bit variable */
1543 /* we will use it after all optimizations
1544 and before liveRange calculation */
1545 sym->reqv = newiTempOperand (sym->type, 0);
1546 sym->reqv->key = sym->key;
1547 OP_SYMBOL (sym->reqv)->prereqv = sym;
1548 OP_SYMBOL (sym->reqv)->key = sym->key;
1549 OP_SYMBOL (sym->reqv)->isreqv = 1;
1550 OP_SYMBOL (sym->reqv)->islocal = 1;
1551 OP_SYMBOL (sym->reqv)->onStack = sym->onStack;
1552 SPIL_LOC (sym->reqv) = sym;
1555 if (!IS_AGGREGATE (sym->type))
1559 op->operand.symOperand = sym;
1562 op->isvolatile = isOperandVolatile (op, TRUE);
1563 op->isGlobal = isOperandGlobal (op);
1564 op->isPtr = IS_PTR (operandType (op));
1565 op->isParm = sym->_isparm;
1570 /* itemp = &[_symbol] */
1572 ic = newiCode (ADDRESS_OF, newOperand (), NULL);
1573 IC_LEFT (ic)->type = SYMBOL;
1574 IC_LEFT (ic)->operand.symOperand = sym;
1575 IC_LEFT (ic)->key = sym->key;
1576 (IC_LEFT (ic))->isvolatile = isOperandVolatile (IC_LEFT (ic), TRUE);
1577 (IC_LEFT (ic))->isGlobal = isOperandGlobal (IC_LEFT (ic));
1578 IC_LEFT (ic)->isPtr = IS_PTR (operandType (IC_LEFT (ic)));
1581 IC_RESULT (ic) = newiTempOperand (sym->type, 0);
1582 if (IS_ARRAY (sym->type))
1584 IC_RESULT (ic) = geniCodeArray2Ptr (IC_RESULT (ic));
1585 IC_RESULT (ic)->isaddr = 0;
1588 IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (sym->type));
1592 return IC_RESULT (ic);
1595 /*-----------------------------------------------------------------*/
1596 /* operandFromValue - creates an operand from value */
1597 /*-----------------------------------------------------------------*/
1599 operandFromValue (value * val)
1603 /* if this is a symbol then do the symbol thing */
1605 return operandFromSymbol (val->sym);
1607 /* this is not a symbol */
1610 op->operand.valOperand = val;
1611 op->isLiteral = isOperandLiteral (op);
1615 /*-----------------------------------------------------------------*/
1616 /* operandFromLink - operand from typeChain */
1617 /*-----------------------------------------------------------------*/
1619 operandFromLink (sym_link * type)
1623 /* operand from sym_link */
1629 op->operand.typeOperand = copyLinkChain (type);
1633 /*-----------------------------------------------------------------*/
1634 /* operandFromLit - makes an operand from a literal value */
1635 /*-----------------------------------------------------------------*/
1637 operandFromLit (double i)
1639 return operandFromValue (valueFromLit (i));
1642 /*-----------------------------------------------------------------*/
1643 /* operandFromAst - creates an operand from an ast */
1644 /*-----------------------------------------------------------------*/
1646 operandFromAst (ast * tree,int lvl)
1652 /* depending on type do */
1656 return ast2iCode (tree,lvl+1);
1660 return operandFromValue (tree->opval.val);
1664 return operandFromLink (tree->opval.lnk);
1671 /* Just to keep the compiler happy */
1672 return (operand *) 0;
1675 /*-----------------------------------------------------------------*/
1676 /* setOperandType - sets the operand's type to the given type */
1677 /*-----------------------------------------------------------------*/
1679 setOperandType (operand * op, sym_link * type)
1681 /* depending on the type of operand */
1686 op->operand.valOperand->etype =
1687 getSpec (op->operand.valOperand->type =
1688 copyLinkChain (type));
1692 if (op->operand.symOperand->isitmp)
1693 op->operand.symOperand->etype =
1694 getSpec (op->operand.symOperand->type =
1695 copyLinkChain (type));
1697 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1698 "attempt to modify type of source");
1702 op->operand.typeOperand = copyLinkChain (type);
1708 /*-----------------------------------------------------------------*/
1709 /* Get size in byte of ptr need to access an array */
1710 /*-----------------------------------------------------------------*/
1712 getArraySizePtr (operand * op)
1714 sym_link *ltype = operandType(op);
1718 int size = getSize(ltype);
1719 return(IS_GENPTR(ltype)?(size-1):size);
1724 sym_link *letype = getSpec(ltype);
1725 switch (PTR_TYPE (SPEC_OCLS (letype)))
1737 return (GPTRSIZE-1);
1746 /*-----------------------------------------------------------------*/
1747 /* perform "usual unary conversions" */
1748 /*-----------------------------------------------------------------*/
1751 usualUnaryConversions (operand * op)
1753 if (IS_INTEGRAL (operandType (op)))
1755 if (getSize (operandType (op)) < (unsigned int) INTSIZE)
1758 return geniCodeCast (INTTYPE, op, TRUE);
1765 /*-----------------------------------------------------------------*/
1766 /* perform "usual binary conversions" */
1767 /*-----------------------------------------------------------------*/
1770 usualBinaryConversions (operand ** op1, operand ** op2,
1771 RESULT_TYPE resultType, int op)
1774 sym_link *rtype = operandType (*op2);
1775 sym_link *ltype = operandType (*op1);
1777 ctype = computeType (ltype, rtype, resultType, op);
1784 if (IS_CHAR (getSpec (ltype)) && IS_CHAR (getSpec (rtype)))
1786 /* one byte operations: keep signedness for code generator */
1794 *op1 = geniCodeCast (ctype, *op1, TRUE);
1795 *op2 = geniCodeCast (ctype, *op2, TRUE);
1800 /*-----------------------------------------------------------------*/
1801 /* geniCodeValueAtAddress - generate intermeditate code for value */
1803 /*-----------------------------------------------------------------*/
1805 geniCodeRValue (operand * op, bool force)
1808 sym_link *type = operandType (op);
1809 sym_link *etype = getSpec (type);
1811 /* if this is an array & already */
1812 /* an address then return this */
1813 if (IS_AGGREGATE (type) ||
1814 (IS_PTR (type) && !force && !op->isaddr))
1815 return operandFromOperand (op);
1817 /* if this is not an address then must be */
1818 /* rvalue already so return this one */
1822 /* if this is not a temp symbol then */
1823 if (!IS_ITEMP (op) &&
1825 !(IN_FARSPACE (SPEC_OCLS (etype)) && !TARGET_IS_HC08))
1827 op = operandFromOperand (op);
1832 if (IS_SPEC (type) &&
1833 IS_TRUE_SYMOP (op) &&
1834 (!(IN_FARSPACE (SPEC_OCLS (etype)) && !TARGET_IS_HC08) ||
1835 (options.model == MODEL_FLAT24) ))
1837 op = operandFromOperand (op);
1842 ic = newiCode (GET_VALUE_AT_ADDRESS, op, NULL);
1843 if (IS_PTR (type) && op->isaddr && force)
1846 type = copyLinkChain (type);
1848 IC_RESULT (ic) = newiTempOperand (type, 1);
1849 IC_RESULT (ic)->isaddr = 0;
1851 /* ic->supportRtn = ((IS_GENPTR(type) | op->isGptr) & op->isaddr); */
1855 return IC_RESULT (ic);
1858 /*-----------------------------------------------------------------*/
1859 /* geniCodeCast - changes the value from one type to another */
1860 /*-----------------------------------------------------------------*/
1862 geniCodeCast (sym_link * type, operand * op, bool implicit)
1866 sym_link *opetype = getSpec (optype = operandType (op));
1870 /* one of them has size zero then error */
1871 if (IS_VOID (optype))
1873 werror (E_CAST_ZERO);
1877 if (IS_ITEMP (op) && IS_ARRAY (OP_SYMBOL (op)->type))
1879 geniCodeArray2Ptr (op);
1883 /* if the operand is already the desired type then do nothing */
1884 if (compareType (type, optype) == 1)
1887 /* if this is a literal then just change the type & return */
1888 if (IS_LITERAL (opetype) && op->type == VALUE && !IS_PTR (type) && !IS_PTR (optype))
1890 return operandFromValue (valCastLiteral (type,
1891 operandLitValue (op)));
1894 /* if casting to/from pointers, do some checking */
1895 if (IS_PTR(type)) { // to a pointer
1896 if (!IS_PTR(optype) && !IS_FUNC(optype) && !IS_AGGREGATE(optype)) { // from a non pointer
1897 if (IS_INTEGRAL(optype)) {
1898 // maybe this is NULL, than it's ok.
1899 if (!(IS_LITERAL(optype) && (SPEC_CVAL(optype).v_ulong ==0))) {
1900 if (port->s.gptr_size > port->s.fptr_size && IS_GENPTR(type)) {
1901 // no way to set the storage
1902 if (IS_LITERAL(optype)) {
1903 werror(E_LITERAL_GENERIC);
1906 werror(E_NONPTR2_GENPTR);
1909 } else if (implicit) {
1910 werror(W_INTEGRAL2PTR_NOCAST);
1915 // shouldn't do that with float, array or structure unless to void
1916 if (!IS_VOID(getSpec(type)) &&
1917 !(IS_CODEPTR(type) && IS_FUNC(type->next) && IS_FUNC(optype))) {
1918 werror(E_INCOMPAT_TYPES);
1922 } else { // from a pointer to a pointer
1923 if (port->s.gptr_size > port->s.fptr_size /*!TARGET_IS_Z80 && !TARGET_IS_GBZ80*/) {
1924 // if not a pointer to a function
1925 if (!(IS_CODEPTR(type) && IS_FUNC(type->next) && IS_FUNC(optype))) {
1926 if (implicit) { // if not to generic, they have to match
1927 if ((!IS_GENPTR(type) && (DCL_TYPE(optype) != DCL_TYPE(type)))) {
1928 werror(E_INCOMPAT_PTYPES);
1935 } else { // to a non pointer
1936 if (IS_PTR(optype)) { // from a pointer
1937 if (implicit) { // sneaky
1938 if (IS_INTEGRAL(type)) {
1939 werror(W_PTR2INTEGRAL_NOCAST);
1941 } else { // shouldn't do that with float, array or structure
1942 werror(E_INCOMPAT_TYPES);
1949 printFromToType (optype, type);
1952 /* if they are the same size create an assignment */
1954 /* This seems very dangerous to me, since there are several */
1955 /* optimizations (for example, gcse) that don't notice the */
1956 /* cast hidden in this assignement and may simplify an */
1957 /* iCode to use the original (uncasted) operand. */
1958 /* Unfortunately, other things break when this cast is */
1959 /* made explicit. Need to fix this someday. */
1960 /* -- EEP, 2004/01/21 */
1961 if (getSize (type) == getSize (optype) &&
1962 !IS_BITFIELD (type) &&
1964 !IS_FLOAT (optype) &&
1965 ((IS_SPEC (type) && IS_SPEC (optype)) ||
1966 (!IS_SPEC (type) && !IS_SPEC (optype))))
1968 ic = newiCode ('=', NULL, op);
1969 IC_RESULT (ic) = newiTempOperand (type, 0);
1970 SPIL_LOC (IC_RESULT (ic)) =
1971 (IS_TRUE_SYMOP (op) ? OP_SYMBOL (op) : NULL);
1972 IC_RESULT (ic)->isaddr = 0;
1976 ic = newiCode (CAST, operandFromLink (type),
1977 geniCodeRValue (op, FALSE));
1979 IC_RESULT (ic) = newiTempOperand (type, 0);
1982 /* preserve the storage class & output class */
1983 /* of the original variable */
1984 restype = getSpec (operandType (IC_RESULT (ic)));
1985 if (!IS_LITERAL(opetype) &&
1987 SPEC_SCLS (restype) = SPEC_SCLS (opetype);
1988 SPEC_OCLS (restype) = SPEC_OCLS (opetype);
1991 return IC_RESULT (ic);
1994 /*-----------------------------------------------------------------*/
1995 /* geniCodeLabel - will create a Label */
1996 /*-----------------------------------------------------------------*/
1998 geniCodeLabel (symbol * label)
2002 ic = newiCodeLabelGoto (LABEL, label);
2006 /*-----------------------------------------------------------------*/
2007 /* geniCodeGoto - will create a Goto */
2008 /*-----------------------------------------------------------------*/
2010 geniCodeGoto (symbol * label)
2014 ic = newiCodeLabelGoto (GOTO, label);
2018 /*-----------------------------------------------------------------*/
2019 /* geniCodeMultiply - gen intermediate code for multiplication */
2020 /*-----------------------------------------------------------------*/
2022 geniCodeMultiply (operand * left, operand * right, RESULT_TYPE resultType)
2029 /* if they are both literal then we know the result */
2030 if (IS_LITERAL (letype) && IS_LITERAL (retype))
2031 return operandFromValue (valMult (left->operand.valOperand,
2032 right->operand.valOperand));
2034 if (IS_LITERAL(retype)) {
2035 p2 = powof2 ((TYPE_UDWORD) floatFromVal (right->operand.valOperand));
2038 resType = usualBinaryConversions (&left, &right, resultType, '*');
2040 rtype = operandType (right);
2041 retype = getSpec (rtype);
2042 ltype = operandType (left);
2043 letype = getSpec (ltype);
2046 /* if the right is a literal & power of 2 */
2047 /* then make it a left shift */
2048 /* code generated for 1 byte * 1 byte literal = 2 bytes result is more
2049 efficient in most cases than 2 bytes result = 2 bytes << literal
2050 if port has 1 byte muldiv */
2051 if (p2 && !IS_FLOAT (letype)
2052 && !((resultType == RESULT_TYPE_INT) && (getSize (resType) != getSize (ltype))
2053 && (port->support.muldiv == 1))
2054 && strcmp (port->target, "pic14") != 0 /* don't shift for pic */
2055 && strcmp (port->target, "pic16") != 0)
2057 if ((resultType == RESULT_TYPE_INT) && (getSize (resType) != getSize (ltype)))
2059 /* LEFT_OP need same size for left and result, */
2060 left = geniCodeCast (resType, left, TRUE);
2061 ltype = operandType (left);
2063 ic = newiCode (LEFT_OP, left, operandFromLit (p2)); /* left shift */
2067 ic = newiCode ('*', left, right); /* normal multiplication */
2068 /* if the size left or right > 1 then support routine */
2069 if (getSize (ltype) > 1 || getSize (rtype) > 1)
2073 IC_RESULT (ic) = newiTempOperand (resType, 1);
2076 return IC_RESULT (ic);
2079 /*-----------------------------------------------------------------*/
2080 /* geniCodeDivision - gen intermediate code for division */
2081 /*-----------------------------------------------------------------*/
2083 geniCodeDivision (operand * left, operand * right, RESULT_TYPE resultType)
2088 sym_link *rtype = operandType (right);
2089 sym_link *retype = getSpec (rtype);
2090 sym_link *ltype = operandType (left);
2091 sym_link *letype = getSpec (ltype);
2093 resType = usualBinaryConversions (&left, &right, resultType, '/');
2095 /* if the right is a literal & power of 2
2096 and left is unsigned then make it a
2098 if (IS_LITERAL (retype) &&
2099 !IS_FLOAT (letype) &&
2100 IS_UNSIGNED(letype) &&
2101 (p2 = powof2 ((TYPE_UDWORD)
2102 floatFromVal (right->operand.valOperand)))) {
2103 ic = newiCode (RIGHT_OP, left, operandFromLit (p2)); /* right shift */
2107 ic = newiCode ('/', left, right); /* normal division */
2108 /* if the size left or right > 1 then support routine */
2109 if (getSize (ltype) > 1 || getSize (rtype) > 1)
2112 IC_RESULT (ic) = newiTempOperand (resType, 0);
2115 return IC_RESULT (ic);
2117 /*-----------------------------------------------------------------*/
2118 /* geniCodeModulus - gen intermediate code for modulus */
2119 /*-----------------------------------------------------------------*/
2121 geniCodeModulus (operand * left, operand * right, RESULT_TYPE resultType)
2127 /* if they are both literal then we know the result */
2128 if (IS_LITERAL (letype) && IS_LITERAL (retype))
2129 return operandFromValue (valMod (left->operand.valOperand,
2130 right->operand.valOperand));
2132 resType = usualBinaryConversions (&left, &right, resultType, '%');
2134 /* now they are the same size */
2135 ic = newiCode ('%', left, right);
2137 /* if the size left or right > 1 then support routine */
2138 if (getSize (ltype) > 1 || getSize (rtype) > 1)
2140 IC_RESULT (ic) = newiTempOperand (resType, 0);
2143 return IC_RESULT (ic);
2146 /*-----------------------------------------------------------------*/
2147 /* geniCodePtrPtrSubtract - subtracts pointer from pointer */
2148 /*-----------------------------------------------------------------*/
2150 geniCodePtrPtrSubtract (operand * left, operand * right)
2156 /* if they are both literals then */
2157 if (IS_LITERAL (letype) && IS_LITERAL (retype))
2159 result = operandFromValue (valMinus (left->operand.valOperand,
2160 right->operand.valOperand));
2164 ic = newiCode ('-', left, right);
2166 IC_RESULT (ic) = result = newiTempOperand (newIntLink (), 1);
2170 if (IS_VOID(ltype->next) || IS_VOID(rtype->next)) {
2174 // should we really do this? is this ANSI?
2175 return geniCodeDivision (result,
2176 operandFromLit (getSize (ltype->next)),
2180 /*-----------------------------------------------------------------*/
2181 /* geniCodeSubtract - generates code for subtraction */
2182 /*-----------------------------------------------------------------*/
2184 geniCodeSubtract (operand * left, operand * right, RESULT_TYPE resultType)
2191 /* if they both pointers then */
2192 if ((IS_PTR (ltype) || IS_ARRAY (ltype)) &&
2193 (IS_PTR (rtype) || IS_ARRAY (rtype)))
2194 return geniCodePtrPtrSubtract (left, right);
2196 /* if they are both literal then we know the result */
2197 if (IS_LITERAL (letype) && IS_LITERAL (retype)
2198 && left->isLiteral && right->isLiteral)
2199 return operandFromValue (valMinus (left->operand.valOperand,
2200 right->operand.valOperand));
2202 /* if left is an array or pointer */
2203 if (IS_PTR (ltype) || IS_ARRAY (ltype))
2205 isarray = left->isaddr;
2206 right = geniCodeMultiply (right,
2207 operandFromLit (getSize (ltype->next)),
2208 (getArraySizePtr(left) >= INTSIZE) ?
2211 resType = copyLinkChain (IS_ARRAY (ltype) ? ltype->next : ltype);
2214 { /* make them the same size */
2215 resType = usualBinaryConversions (&left, &right, resultType, '-');
2218 ic = newiCode ('-', left, right);
2220 IC_RESULT (ic) = newiTempOperand (resType, 1);
2221 IC_RESULT (ic)->isaddr = (isarray ? 1 : 0);
2223 /* if left or right is a float */
2224 if (IS_FLOAT (ltype) || IS_FLOAT (rtype))
2228 return IC_RESULT (ic);
2231 /*-----------------------------------------------------------------*/
2232 /* geniCodeAdd - generates iCode for addition */
2233 /*-----------------------------------------------------------------*/
2235 geniCodeAdd (operand * left, operand * right, RESULT_TYPE resultType, int lvl)
2244 /* if the right side is LITERAL zero */
2245 /* return the left side */
2246 if (IS_LITERAL (retype) && right->isLiteral && !floatFromVal (valFromType (rtype)))
2249 /* if left is literal zero return right */
2250 if (IS_LITERAL (letype) && left->isLiteral && !floatFromVal (valFromType (ltype)))
2253 /* if left is a pointer then size */
2254 if (IS_PTR (ltype) || IS_ARRAY(ltype))
2256 isarray = left->isaddr;
2257 // there is no need to multiply with 1
2258 if (getSize (ltype->next) != 1)
2260 size = operandFromLit (getSize (ltype->next));
2261 SPEC_USIGN (getSpec (operandType (size))) = 1;
2262 indexUnsigned = IS_UNSIGNED (getSpec (operandType (right)));
2263 right = geniCodeMultiply (right,
2265 (getArraySizePtr(left) >= INTSIZE) ?
2268 /* Even if right is a 'unsigned char',
2269 the result will be a 'signed int' due to the promotion rules.
2270 It doesn't make sense when accessing arrays, so let's fix it here: */
2272 SPEC_USIGN (getSpec (operandType (right))) = 1;
2274 resType = copyLinkChain (ltype);
2277 { // make them the same size
2278 resType = usualBinaryConversions (&left, &right, resultType, '+');
2281 /* if they are both literals then we know */
2282 if (IS_LITERAL (letype) && IS_LITERAL (retype)
2283 && left->isLiteral && right->isLiteral)
2284 return operandFromValue (valPlus (valFromType (ltype),
2285 valFromType (rtype)));
2287 ic = newiCode ('+', left, right);
2289 IC_RESULT (ic) = newiTempOperand (resType, 1);
2290 IC_RESULT (ic)->isaddr = (isarray ? 1 : 0);
2292 /* if left or right is a float then support
2294 if (IS_FLOAT (ltype) || IS_FLOAT (rtype))
2299 return IC_RESULT (ic);
2303 /*-----------------------------------------------------------------*/
2304 /* aggrToPtr - changes an aggregate to pointer to an aggregate */
2305 /*-----------------------------------------------------------------*/
2307 aggrToPtr (sym_link * type, bool force)
2312 if (IS_PTR (type) && !force)
2315 etype = getSpec (type);
2316 ptype = newLink (DECLARATOR);
2320 /* set the pointer depending on the storage class */
2321 DCL_TYPE (ptype) = PTR_TYPE (SPEC_OCLS (etype));
2325 /*-----------------------------------------------------------------*/
2326 /* geniCodeArray2Ptr - array to pointer */
2327 /*-----------------------------------------------------------------*/
2329 geniCodeArray2Ptr (operand * op)
2331 sym_link *optype = operandType (op);
2332 sym_link *opetype = getSpec (optype);
2334 /* set the pointer depending on the storage class */
2335 DCL_TYPE (optype) = PTR_TYPE (SPEC_OCLS (opetype));
2342 /*-----------------------------------------------------------------*/
2343 /* geniCodeArray - array access */
2344 /*-----------------------------------------------------------------*/
2346 geniCodeArray (operand * left, operand * right, int lvl)
2350 sym_link *ltype = operandType (left);
2355 if (IS_PTR (ltype->next) && left->isaddr)
2357 left = geniCodeRValue (left, FALSE);
2360 return geniCodeDerefPtr (geniCodeAdd (left,
2362 (getArraySizePtr(left) >= INTSIZE) ?
2368 size = operandFromLit (getSize (ltype->next));
2369 SPEC_USIGN (getSpec (operandType (size))) = 1;
2370 indexUnsigned = IS_UNSIGNED (getSpec (operandType (right)));
2371 right = geniCodeMultiply (right,
2373 (getArraySizePtr(left) >= INTSIZE) ?
2376 /* Even if right is a 'unsigned char', the result will be a 'signed int' due to the promotion rules.
2377 It doesn't make sense when accessing arrays, so let's fix it here: */
2379 SPEC_USIGN (getSpec (operandType (right))) = 1;
2380 /* we can check for limits here */
2381 /* already done in SDCCast.c
2382 if (isOperandLiteral (right) &&
2385 (operandLitValue (right) / getSize (ltype->next)) >= DCL_ELEM (ltype))
2387 werror (W_IDX_OUT_OF_BOUNDS,
2388 (int) operandLitValue (right) / getSize (ltype->next),
2393 ic = newiCode ('+', left, right);
2395 IC_RESULT (ic) = newiTempOperand (((IS_PTR (ltype) &&
2396 !IS_AGGREGATE (ltype->next) &&
2397 !IS_PTR (ltype->next))
2398 ? ltype : ltype->next), 0);
2400 IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (ltype->next));
2403 return IC_RESULT (ic);
2406 /*-----------------------------------------------------------------*/
2407 /* geniCodeStruct - generates intermediate code for structures */
2408 /*-----------------------------------------------------------------*/
2410 geniCodeStruct (operand * left, operand * right, bool islval)
2413 sym_link *type = operandType (left);
2414 sym_link *etype = getSpec (type);
2416 symbol *element = getStructElement (SPEC_STRUCT (etype),
2417 right->operand.symOperand);
2419 wassert(IS_SYMOP(right));
2421 /* add the offset */
2422 ic = newiCode ('+', left, operandFromLit (element->offset));
2424 IC_RESULT (ic) = newiTempOperand (element->type, 0);
2426 /* preserve the storage & output class of the struct */
2427 /* as well as the volatile attribute */
2428 retype = getSpec (operandType (IC_RESULT (ic)));
2429 SPEC_SCLS (retype) = SPEC_SCLS (etype);
2430 SPEC_OCLS (retype) = SPEC_OCLS (etype);
2431 SPEC_VOLATILE (retype) |= SPEC_VOLATILE (etype);
2432 SPEC_CONST (retype) |= SPEC_CONST (etype);
2434 if (IS_PTR (element->type))
2435 setOperandType (IC_RESULT (ic), aggrToPtr (operandType (IC_RESULT (ic)), TRUE));
2437 IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (element->type));
2440 return (islval ? IC_RESULT (ic) : geniCodeRValue (IC_RESULT (ic), TRUE));
2443 /*-----------------------------------------------------------------*/
2444 /* geniCodePostInc - generate int code for Post increment */
2445 /*-----------------------------------------------------------------*/
2447 geniCodePostInc (operand * op)
2451 sym_link *optype = operandType (op);
2453 operand *rv = (IS_ITEMP (op) ?
2454 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2456 sym_link *rvtype = operandType (rv);
2459 /* if this is not an address we have trouble */
2462 werror (E_LVALUE_REQUIRED, "++");
2466 rOp = newiTempOperand (rvtype, 0);
2467 OP_SYMBOL(rOp)->noSpilLoc = 1;
2470 OP_SYMBOL(rv)->noSpilLoc = 1;
2472 geniCodeAssign (rOp, rv, 0);
2474 size = (IS_PTR (rvtype) ? getSize (rvtype->next) : 1);
2475 if (IS_FLOAT (rvtype))
2476 ic = newiCode ('+', rv, operandFromValue (constFloatVal ("1.0")));
2478 ic = newiCode ('+', rv, operandFromLit (size));
2480 IC_RESULT (ic) = result = newiTempOperand (rvtype, 0);
2483 geniCodeAssign (op, result, 0);
2489 /*-----------------------------------------------------------------*/
2490 /* geniCodePreInc - generate code for preIncrement */
2491 /*-----------------------------------------------------------------*/
2493 geniCodePreInc (operand * op, bool lvalue)
2496 sym_link *optype = operandType (op);
2497 operand *rop = (IS_ITEMP (op) ?
2498 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2500 sym_link *roptype = operandType (rop);
2506 werror (E_LVALUE_REQUIRED, "++");
2511 size = (IS_PTR (roptype) ? getSize (roptype->next) : 1);
2512 if (IS_FLOAT (roptype))
2513 ic = newiCode ('+', rop, operandFromValue (constFloatVal ("1.0")));
2515 ic = newiCode ('+', rop, operandFromLit (size));
2516 IC_RESULT (ic) = result = newiTempOperand (roptype, 0);
2519 (void) geniCodeAssign (op, result, 0);
2520 if (lvalue || IS_TRUE_SYMOP (op))
2526 /*-----------------------------------------------------------------*/
2527 /* geniCodePostDec - generates code for Post decrement */
2528 /*-----------------------------------------------------------------*/
2530 geniCodePostDec (operand * op)
2534 sym_link *optype = operandType (op);
2536 operand *rv = (IS_ITEMP (op) ?
2537 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2539 sym_link *rvtype = operandType (rv);
2542 /* if this is not an address we have trouble */
2545 werror (E_LVALUE_REQUIRED, "--");
2549 rOp = newiTempOperand (rvtype, 0);
2550 OP_SYMBOL(rOp)->noSpilLoc = 1;
2553 OP_SYMBOL(rv)->noSpilLoc = 1;
2555 geniCodeAssign (rOp, rv, 0);
2557 size = (IS_PTR (rvtype) ? getSize (rvtype->next) : 1);
2558 if (IS_FLOAT (rvtype))
2559 ic = newiCode ('-', rv, operandFromValue (constFloatVal ("1.0")));
2561 ic = newiCode ('-', rv, operandFromLit (size));
2563 IC_RESULT (ic) = result = newiTempOperand (rvtype, 0);
2566 geniCodeAssign (op, result, 0);
2572 /*-----------------------------------------------------------------*/
2573 /* geniCodePreDec - generate code for pre decrement */
2574 /*-----------------------------------------------------------------*/
2576 geniCodePreDec (operand * op, bool lvalue)
2579 sym_link *optype = operandType (op);
2580 operand *rop = (IS_ITEMP (op) ?
2581 geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2583 sym_link *roptype = operandType (rop);
2589 werror (E_LVALUE_REQUIRED, "--");
2594 size = (IS_PTR (roptype) ? getSize (roptype->next) : 1);
2595 if (IS_FLOAT (roptype))
2596 ic = newiCode ('-', rop, operandFromValue (constFloatVal ("1.0")));
2598 ic = newiCode ('-', rop, operandFromLit (size));
2599 IC_RESULT (ic) = result = newiTempOperand (roptype, 0);
2602 (void) geniCodeAssign (op, result, 0);
2603 if (lvalue || IS_TRUE_SYMOP (op))
2610 /*-----------------------------------------------------------------*/
2611 /* geniCodeBitwise - gen int code for bitWise operators */
2612 /*-----------------------------------------------------------------*/
2614 geniCodeBitwise (operand * left, operand * right,
2615 int oper, sym_link * resType)
2619 left = geniCodeCast (resType, left, TRUE);
2620 right = geniCodeCast (resType, right, TRUE);
2622 ic = newiCode (oper, left, right);
2623 IC_RESULT (ic) = newiTempOperand (resType, 0);
2626 return IC_RESULT (ic);
2629 /*-----------------------------------------------------------------*/
2630 /* geniCodeAddressOf - gens icode for '&' address of operator */
2631 /*-----------------------------------------------------------------*/
2633 geniCodeAddressOf (operand * op)
2637 sym_link *optype = operandType (op);
2638 sym_link *opetype = getSpec (optype);
2640 if (IS_ITEMP (op) && op->isaddr && IS_PTR (optype))
2642 op = operandFromOperand (op);
2647 /* lvalue check already done in decorateType */
2648 /* this must be a lvalue */
2649 /* if (!op->isaddr && !IS_AGGREGATE(optype)) { */
2650 /* werror (E_LVALUE_REQUIRED,"&"); */
2654 p = newLink (DECLARATOR);
2656 /* set the pointer depending on the storage class */
2657 DCL_TYPE (p) = PTR_TYPE (SPEC_OCLS (opetype));
2659 p->next = copyLinkChain (optype);
2661 /* if already a temp */
2664 setOperandType (op, p);
2669 /* other wise make this of the type coming in */
2670 ic = newiCode (ADDRESS_OF, op, NULL);
2671 IC_RESULT (ic) = newiTempOperand (p, 1);
2672 IC_RESULT (ic)->isaddr = 0;
2674 return IC_RESULT (ic);
2676 /*-----------------------------------------------------------------*/
2677 /* setOClass - sets the output class depending on the pointer type */
2678 /*-----------------------------------------------------------------*/
2680 setOClass (sym_link * ptr, sym_link * spec)
2682 switch (DCL_TYPE (ptr))
2685 SPEC_OCLS (spec) = data;
2689 SPEC_OCLS (spec) = generic;
2693 SPEC_OCLS (spec) = xdata;
2697 SPEC_OCLS (spec) = code;
2701 SPEC_OCLS (spec) = idata;
2705 SPEC_OCLS (spec) = xstack;
2709 SPEC_OCLS (spec) = eeprom;
2718 /*-----------------------------------------------------------------*/
2719 /* geniCodeDerefPtr - dereference pointer with '*' */
2720 /*-----------------------------------------------------------------*/
2722 geniCodeDerefPtr (operand * op,int lvl)
2724 sym_link *rtype, *retype;
2725 sym_link *optype = operandType (op);
2727 // if this is an array then array access
2728 if (IS_ARRAY (optype)) {
2729 // don't worry, this will be optimized out later
2730 return geniCodeArray (op, operandFromLit (0), lvl);
2733 // just in case someone screws up
2734 wassert (IS_PTR (optype));
2736 if (IS_TRUE_SYMOP (op))
2739 op = geniCodeRValue (op, TRUE);
2742 /* now get rid of the pointer part */
2743 if (isLvaluereq(lvl) && IS_ITEMP (op))
2745 retype = getSpec (rtype = copyLinkChain (optype));
2749 retype = getSpec (rtype = copyLinkChain (optype->next));
2750 /* outputclass needs 2b updated */
2751 setOClass (optype, retype);
2754 op->isGptr = IS_GENPTR (optype);
2756 op->isaddr = (IS_PTR (rtype) ||
2757 IS_STRUCT (rtype) ||
2762 if (!isLvaluereq(lvl))
2763 op = geniCodeRValue (op, TRUE);
2765 setOperandType (op, rtype);
2770 /*-----------------------------------------------------------------*/
2771 /* geniCodeUnaryMinus - does a unary minus of the operand */
2772 /*-----------------------------------------------------------------*/
2774 geniCodeUnaryMinus (operand * op)
2777 sym_link *optype = operandType (op);
2779 if (IS_LITERAL (optype))
2780 return operandFromLit (-floatFromVal (op->operand.valOperand));
2782 ic = newiCode (UNARYMINUS, op, NULL);
2783 IC_RESULT (ic) = newiTempOperand (optype, 0);
2785 return IC_RESULT (ic);
2788 /*-----------------------------------------------------------------*/
2789 /* geniCodeLeftShift - gen i code for left shift */
2790 /*-----------------------------------------------------------------*/
2792 geniCodeLeftShift (operand * left, operand * right, RESULT_TYPE resultType)
2797 ic = newiCode (LEFT_OP, left, right);
2799 resType = usualBinaryConversions (&left, &right, resultType, LEFT_OP);
2800 IC_RESULT (ic) = newiTempOperand (resType, 0);
2802 return IC_RESULT (ic);
2805 /*-----------------------------------------------------------------*/
2806 /* geniCodeRightShift - gen i code for right shift */
2807 /*-----------------------------------------------------------------*/
2809 geniCodeRightShift (operand * left, operand * right)
2813 ic = newiCode (RIGHT_OP, left, right);
2814 IC_RESULT (ic) = newiTempOperand (operandType (left), 0);
2816 return IC_RESULT (ic);
2819 /*-----------------------------------------------------------------*/
2820 /* geniCodeLogic- logic code */
2821 /*-----------------------------------------------------------------*/
2823 geniCodeLogic (operand * left, operand * right, int op)
2827 sym_link *rtype = operandType (right);
2828 sym_link *ltype = operandType (left);
2830 /* left is integral type and right is literal then
2831 check if the literal value is within bounds */
2832 if (IS_INTEGRAL (ltype) && IS_VALOP (right) && IS_LITERAL (rtype))
2834 checkConstantRange(ltype,
2835 OP_VALUE(right), "compare operation", 1);
2838 /* if one operand is a pointer and the other is a literal generic void pointer,
2839 change the type of the literal generic void pointer to match the other pointer */
2840 if (IS_GENPTR (ltype) && IS_VOID (ltype->next) && IS_ITEMP (left)
2841 && IS_PTR (rtype) && !IS_GENPTR(rtype))
2843 /* find left's definition */
2844 ic = (iCode *) setFirstItem (iCodeChain);
2847 if (((ic->op == CAST) || (ic->op == '='))
2848 && isOperandEqual(left, IC_RESULT (ic)))
2851 ic = setNextItem (iCodeChain);
2853 /* if casting literal to generic pointer, then cast to rtype instead */
2854 if (ic && (ic->op == CAST) && isOperandLiteral(IC_RIGHT (ic)))
2856 left = operandFromValue (valCastLiteral (rtype, operandLitValue (IC_RIGHT (ic))));
2857 ltype = operandType(left);
2860 if (IS_GENPTR (rtype) && IS_VOID (rtype->next) && IS_ITEMP (right)
2861 && IS_PTR (ltype) && !IS_GENPTR(ltype))
2863 /* find right's definition */
2864 ic = (iCode *) setFirstItem (iCodeChain);
2867 if (((ic->op == CAST) || (ic->op == '='))
2868 && isOperandEqual(right, IC_RESULT (ic)))
2871 ic = setNextItem (iCodeChain);
2873 /* if casting literal to generic pointer, then cast to rtype instead */
2874 if (ic && (ic->op == CAST) && isOperandLiteral(IC_RIGHT (ic)))
2876 right = operandFromValue (valCastLiteral (ltype, operandLitValue (IC_RIGHT (ic))));
2877 rtype = operandType(right);
2881 ctype = usualBinaryConversions (&left, &right, RESULT_TYPE_NONE, 0);
2883 ic = newiCode (op, left, right);
2884 IC_RESULT (ic) = newiTempOperand (newCharLink (), 1);
2886 /* if comparing float
2887 and not a '==' || '!=' || '&&' || '||' (these
2889 if (IS_FLOAT(ctype) &&
2897 return IC_RESULT (ic);
2900 /*-----------------------------------------------------------------*/
2901 /* geniCodeUnary - for a a generic unary operation */
2902 /*-----------------------------------------------------------------*/
2904 geniCodeUnary (operand * op, int oper)
2906 iCode *ic = newiCode (oper, op, NULL);
2908 IC_RESULT (ic) = newiTempOperand (operandType (op), 0);
2910 return IC_RESULT (ic);
2913 /*-----------------------------------------------------------------*/
2914 /* geniCodeConditional - geniCode for '?' ':' operation */
2915 /*-----------------------------------------------------------------*/
2917 geniCodeConditional (ast * tree,int lvl)
2920 symbol *falseLabel = newiTempLabel (NULL);
2921 symbol *exitLabel = newiTempLabel (NULL);
2922 operand *cond = ast2iCode (tree->left,lvl+1);
2923 operand *true, *false, *result;
2925 ic = newiCodeCondition (geniCodeRValue (cond, FALSE),
2929 true = ast2iCode (tree->right->left,lvl+1);
2931 /* move the value to a new Operand */
2932 result = newiTempOperand (tree->right->ftype, 0);
2933 geniCodeAssign (result, geniCodeRValue (true, FALSE), 0);
2935 /* generate an unconditional goto */
2936 geniCodeGoto (exitLabel);
2938 /* now for the right side */
2939 geniCodeLabel (falseLabel);
2941 false = ast2iCode (tree->right->right,lvl+1);
2942 geniCodeAssign (result, geniCodeRValue (false, FALSE), 0);
2944 /* create the exit label */
2945 geniCodeLabel (exitLabel);
2950 /*-----------------------------------------------------------------*/
2951 /* geniCodeAssign - generate code for assignment */
2952 /*-----------------------------------------------------------------*/
2954 geniCodeAssign (operand * left, operand * right, int nosupdate)
2957 sym_link *ltype = operandType (left);
2958 sym_link *rtype = operandType (right);
2960 if (!left->isaddr && !IS_ITEMP (left))
2962 werror (E_LVALUE_REQUIRED, "assignment");
2966 /* left is integral type and right is literal then
2967 check if the literal value is within bounds */
2968 if (IS_INTEGRAL (ltype) && right->type == VALUE && IS_LITERAL (rtype))
2970 checkConstantRange(ltype,
2971 OP_VALUE(right), "= operation", 0);
2974 /* if the left & right type don't exactly match */
2975 /* if pointer set then make sure the check is
2976 done with the type & not the pointer */
2977 /* then cast rights type to left */
2979 /* first check the type for pointer assignement */
2980 if (left->isaddr && IS_PTR (ltype) && IS_ITEMP (left) &&
2981 compareType (ltype, rtype) <= 0)
2983 if (compareType (ltype->next, rtype) < 0)
2984 right = geniCodeCast (ltype->next, right, TRUE);
2986 else if (compareType (ltype, rtype) < 0)
2987 right = geniCodeCast (ltype, right, TRUE);
2989 /* If left is a true symbol & ! volatile
2990 create an assignment to temporary for
2991 the right & then assign this temporary
2992 to the symbol. This is SSA (static single
2993 assignment). Isn't it simple and folks have
2994 published mountains of paper on it */
2995 if (IS_TRUE_SYMOP (left) &&
2996 !isOperandVolatile (left, FALSE) &&
2997 isOperandGlobal (left))
3001 if (IS_TRUE_SYMOP (right))
3002 sym = OP_SYMBOL (right);
3003 ic = newiCode ('=', NULL, right);
3004 IC_RESULT (ic) = right = newiTempOperand (ltype, 0);
3005 SPIL_LOC (right) = sym;
3009 ic = newiCode ('=', NULL, right);
3010 IC_RESULT (ic) = left;
3013 /* if left isgptr flag is set then support
3014 routine will be required */
3018 ic->nosupdate = nosupdate;
3022 /*-----------------------------------------------------------------*/
3023 /* geniCodeDummyRead - generate code for dummy read */
3024 /*-----------------------------------------------------------------*/
3026 geniCodeDummyRead (operand * op)
3029 sym_link *type = operandType (op);
3031 if (!IS_VOLATILE(type))
3034 ic = newiCode (DUMMY_READ_VOLATILE, NULL, op);
3040 /*-----------------------------------------------------------------*/
3041 /* geniCodeSEParms - generate code for side effecting fcalls */
3042 /*-----------------------------------------------------------------*/
3044 geniCodeSEParms (ast * parms,int lvl)
3049 if (parms->type == EX_OP && parms->opval.op == PARAM)
3051 geniCodeSEParms (parms->left,lvl);
3052 geniCodeSEParms (parms->right,lvl);
3056 /* hack don't like this but too lazy to think of
3058 if (IS_ADDRESS_OF_OP (parms))
3059 parms->left->lvalue = 1;
3061 if (IS_CAST_OP (parms) &&
3062 IS_PTR (parms->ftype) &&
3063 IS_ADDRESS_OF_OP (parms->right))
3064 parms->right->left->lvalue = 1;
3066 parms->opval.oprnd =
3067 geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
3069 parms->type = EX_OPERAND;
3070 AST_ARGREG(parms) = parms->etype ? SPEC_ARGREG(parms->etype) :
3071 SPEC_ARGREG(parms->ftype);
3074 /*-----------------------------------------------------------------*/
3075 /* geniCodeParms - generates parameters */
3076 /*-----------------------------------------------------------------*/
3078 geniCodeParms (ast * parms, value *argVals, int *stack,
3079 sym_link * ftype, int lvl)
3087 if (argVals==NULL) {
3089 argVals = FUNC_ARGS (ftype);
3092 /* if this is a param node then do the left & right */
3093 if (parms->type == EX_OP && parms->opval.op == PARAM)
3095 argVals=geniCodeParms (parms->left, argVals, stack, ftype, lvl);
3096 argVals=geniCodeParms (parms->right, argVals, stack, ftype, lvl);
3100 /* get the parameter value */
3101 if (parms->type == EX_OPERAND)
3102 pval = parms->opval.oprnd;
3105 /* maybe this else should go away ?? */
3106 /* hack don't like this but too lazy to think of
3108 if (IS_ADDRESS_OF_OP (parms))
3109 parms->left->lvalue = 1;
3111 if (IS_CAST_OP (parms) &&
3112 IS_PTR (parms->ftype) &&
3113 IS_ADDRESS_OF_OP (parms->right))
3114 parms->right->left->lvalue = 1;
3116 pval = geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
3119 /* if register parm then make it a send */
3120 if ((IS_REGPARM (parms->etype) && !IFFUNC_HASVARARGS(ftype)) ||
3121 IFFUNC_ISBUILTIN(ftype))
3123 ic = newiCode (SEND, pval, NULL);
3124 ic->argreg = SPEC_ARGREG(parms->etype);
3125 ic->builtinSEND = FUNC_ISBUILTIN(ftype);
3130 /* now decide whether to push or assign */
3131 if (!(options.stackAuto || IFFUNC_ISREENT (ftype)))
3135 operand *top = operandFromSymbol (argVals->sym);
3136 /* clear useDef and other bitVectors */
3137 OP_USES(top)=OP_DEFS(top)=OP_SYMBOL(top)->clashes = NULL;
3138 geniCodeAssign (top, pval, 1);
3142 sym_link *p = operandType (pval);
3144 ic = newiCode (IPUSH, pval, NULL);
3146 /* update the stack adjustment */
3147 *stack += getSize (IS_AGGREGATE (p) ? aggrToPtr (p, FALSE) : p);
3152 argVals=argVals->next;
3156 /*-----------------------------------------------------------------*/
3157 /* geniCodeCall - generates temp code for calling */
3158 /*-----------------------------------------------------------------*/
3160 geniCodeCall (operand * left, ast * parms,int lvl)
3164 sym_link *type, *etype;
3168 if (!IS_FUNC(OP_SYMBOL(left)->type) &&
3169 !IS_CODEPTR(OP_SYMBOL(left)->type)) {
3170 werror (E_FUNCTION_EXPECTED);
3171 return operandFromValue(valueFromLit(0));
3174 /* take care of parameters with side-effecting
3175 function calls in them, this is required to take care
3176 of overlaying function parameters */
3177 geniCodeSEParms (parms,lvl);
3179 ftype = operandType (left);
3180 if (IS_CODEPTR (ftype))
3181 ftype = ftype->next;
3183 /* first the parameters */
3184 geniCodeParms (parms, NULL, &stack, ftype, lvl);
3186 /* now call : if symbol then pcall */
3187 if (IS_OP_POINTER (left) || IS_ITEMP(left)) {
3188 ic = newiCode (PCALL, left, NULL);
3190 ic = newiCode (CALL, left, NULL);
3193 type = copyLinkChain (ftype->next);
3194 etype = getSpec (type);
3195 SPEC_EXTR (etype) = 0;
3196 IC_RESULT (ic) = result = newiTempOperand (type, 1);
3200 /* stack adjustment after call */
3201 ic->parmBytes = stack;
3206 /*-----------------------------------------------------------------*/
3207 /* geniCodeReceive - generate intermediate code for "receive" */
3208 /*-----------------------------------------------------------------*/
3210 geniCodeReceive (value * args)
3212 /* for all arguments that are passed in registers */
3216 if (IS_REGPARM (args->etype))
3218 operand *opr = operandFromValue (args);
3220 symbol *sym = OP_SYMBOL (opr);
3223 /* we will use it after all optimizations
3224 and before liveRange calculation */
3225 if (!sym->addrtaken && !IS_VOLATILE (sym->etype))
3228 if ((IN_FARSPACE (SPEC_OCLS (sym->etype)) && !TARGET_IS_HC08) &&
3229 options.stackAuto == 0 &&
3230 (!(options.model == MODEL_FLAT24)) )
3235 opl = newiTempOperand (args->type, 0);
3237 sym->reqv->key = sym->key;
3238 OP_SYMBOL (sym->reqv)->key = sym->key;
3239 OP_SYMBOL (sym->reqv)->isreqv = 1;
3240 OP_SYMBOL (sym->reqv)->islocal = 0;
3241 SPIL_LOC (sym->reqv) = sym;
3245 ic = newiCode (RECEIVE, NULL, NULL);
3246 ic->argreg = SPEC_ARGREG(args->etype);
3248 currFunc->recvSize = getSize (sym->type);
3251 IC_RESULT (ic) = opr;
3259 /*-----------------------------------------------------------------*/
3260 /* geniCodeFunctionBody - create the function body */
3261 /*-----------------------------------------------------------------*/
3263 geniCodeFunctionBody (ast * tree,int lvl)
3270 /* reset the auto generation */
3276 func = ast2iCode (tree->left,lvl+1);
3277 fetype = getSpec (operandType (func));
3279 savelineno = lineno;
3280 lineno = OP_SYMBOL (func)->lineDef;
3281 /* create an entry label */
3282 geniCodeLabel (entryLabel);
3283 lineno = savelineno;
3285 /* create a proc icode */
3286 ic = newiCode (FUNCTION, func, NULL);
3287 lineno=ic->lineno = OP_SYMBOL (func)->lineDef;
3291 /* for all parameters that are passed
3292 on registers add a "receive" */
3293 geniCodeReceive (tree->values.args);
3295 /* generate code for the body */
3296 ast2iCode (tree->right,lvl+1);
3298 /* create a label for return */
3299 geniCodeLabel (returnLabel);
3301 /* now generate the end proc */
3302 ic = newiCode (ENDFUNCTION, func, NULL);
3307 /*-----------------------------------------------------------------*/
3308 /* geniCodeReturn - gen icode for 'return' statement */
3309 /*-----------------------------------------------------------------*/
3311 geniCodeReturn (operand * op)
3315 /* if the operand is present force an rvalue */
3317 op = geniCodeRValue (op, FALSE);
3319 ic = newiCode (RETURN, op, NULL);
3323 /*-----------------------------------------------------------------*/
3324 /* geniCodeIfx - generates code for extended if statement */
3325 /*-----------------------------------------------------------------*/
3327 geniCodeIfx (ast * tree,int lvl)
3330 operand *condition = ast2iCode (tree->left,lvl+1);
3333 /* if condition is null then exit */
3337 condition = geniCodeRValue (condition, FALSE);
3339 cetype = getSpec (operandType (condition));
3340 /* if the condition is a literal */
3341 if (IS_LITERAL (cetype))
3343 if (floatFromVal (condition->operand.valOperand))
3345 if (tree->trueLabel)
3346 geniCodeGoto (tree->trueLabel);
3352 if (tree->falseLabel)
3353 geniCodeGoto (tree->falseLabel);
3360 if (tree->trueLabel)
3362 ic = newiCodeCondition (condition,
3367 if (tree->falseLabel)
3368 geniCodeGoto (tree->falseLabel);
3372 ic = newiCodeCondition (condition,
3379 ast2iCode (tree->right,lvl+1);
3382 /*-----------------------------------------------------------------*/
3383 /* geniCodeJumpTable - tries to create a jump table for switch */
3384 /*-----------------------------------------------------------------*/
3386 geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
3388 int min = 0, max = 0, t, cnt = 0;
3394 int needRangeCheck = !optimize.noJTabBoundary
3395 || tree->values.switchVals.swDefault;
3397 if (!tree || !caseVals)
3400 /* the criteria for creating a jump table is */
3401 /* all integer numbers between the maximum & minimum must */
3402 /* be present , the maximum value should not exceed 255 */
3403 min = max = (int) floatFromVal (vch = caseVals);
3404 SNPRINTF (buffer, sizeof(buffer),
3406 tree->values.switchVals.swNum,
3408 addSet (&labels, newiTempLabel (buffer));
3410 /* if there is only one case value then no need */
3411 if (!(vch = vch->next))
3416 if (((t = (int) floatFromVal (vch)) - max) != 1)
3418 SNPRINTF (buffer, sizeof(buffer),
3420 tree->values.switchVals.swNum,
3422 addSet (&labels, newiTempLabel (buffer));
3428 /* if the number of case statements <= 2 then */
3429 /* it is not economical to create the jump table */
3430 /* since two compares are needed for boundary conditions */
3431 if ((needRangeCheck && cnt <= 2) || max > (255 / 3))
3434 if (tree->values.switchVals.swDefault)
3436 SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
3440 SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
3444 falseLabel = newiTempLabel (buffer);
3446 /* so we can create a jumptable */
3447 /* first we rule out the boundary conditions */
3448 /* if only optimization says so */
3451 sym_link *cetype = getSpec (operandType (cond));
3452 /* no need to check the lower bound if
3453 the condition is unsigned & minimum value is zero */
3454 if (!(min == 0 && IS_UNSIGNED (cetype)))
3456 boundary = geniCodeLogic (cond, operandFromLit (min), '<');
3457 ic = newiCodeCondition (boundary, falseLabel, NULL);
3461 /* now for upper bounds */
3462 boundary = geniCodeLogic (cond, operandFromLit (max), '>');
3463 ic = newiCodeCondition (boundary, falseLabel, NULL);
3467 /* if the min is not zero then we no make it zero */
3470 cond = geniCodeSubtract (cond, operandFromLit (min), RESULT_TYPE_CHAR);
3471 if (!IS_LITERAL(getSpec(operandType(cond))))
3472 setOperandType (cond, UCHARTYPE);
3475 /* now create the jumptable */
3476 ic = newiCode (JUMPTABLE, NULL, NULL);
3477 IC_JTCOND (ic) = cond;
3478 IC_JTLABELS (ic) = labels;
3483 /*-----------------------------------------------------------------*/
3484 /* geniCodeSwitch - changes a switch to a if statement */
3485 /*-----------------------------------------------------------------*/
3487 geniCodeSwitch (ast * tree,int lvl)
3490 operand *cond = geniCodeRValue (ast2iCode (tree->left,lvl+1), FALSE);
3491 value *caseVals = tree->values.switchVals.swVals;
3492 symbol *trueLabel, *falseLabel;
3494 /* If the condition is a literal, then just jump to the */
3495 /* appropriate case label. */
3496 if (IS_LITERAL(getSpec(operandType(cond))))
3498 int switchVal, caseVal;
3500 switchVal = (int) floatFromVal (cond->operand.valOperand);
3503 caseVal = (int) floatFromVal (caseVals);
3504 if (caseVal == switchVal)
3506 SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
3507 tree->values.switchVals.swNum, caseVal);
3508 trueLabel = newiTempLabel (buffer);
3509 geniCodeGoto (trueLabel);
3512 caseVals = caseVals->next;
3514 goto defaultOrBreak;
3517 /* if we can make this a jump table */
3518 if (geniCodeJumpTable (cond, caseVals, tree))
3519 goto jumpTable; /* no need for the comparison */
3521 /* for the cases defined do */
3525 operand *compare = geniCodeLogic (cond,
3526 operandFromValue (caseVals),
3529 SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
3530 tree->values.switchVals.swNum,
3531 (int) floatFromVal (caseVals));
3532 trueLabel = newiTempLabel (buffer);
3534 ic = newiCodeCondition (compare, trueLabel, NULL);
3536 caseVals = caseVals->next;
3541 /* if default is present then goto break else break */
3542 if (tree->values.switchVals.swDefault)
3544 SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
3548 SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
3551 falseLabel = newiTempLabel (buffer);
3552 geniCodeGoto (falseLabel);
3555 ast2iCode (tree->right,lvl+1);
3558 /*-----------------------------------------------------------------*/
3559 /* geniCodeInline - intermediate code for inline assembler */
3560 /*-----------------------------------------------------------------*/
3562 geniCodeInline (ast * tree)
3566 ic = newiCode (INLINEASM, NULL, NULL);
3567 IC_INLINE (ic) = tree->values.inlineasm;
3571 /*-----------------------------------------------------------------*/
3572 /* geniCodeArrayInit - intermediate code for array initializer */
3573 /*-----------------------------------------------------------------*/
3575 geniCodeArrayInit (ast * tree, operand *array)
3579 if (!getenv("TRY_THE_NEW_INITIALIZER")) {
3580 ic = newiCode (ARRAYINIT, array, NULL);
3581 IC_ARRAYILIST (ic) = tree->values.constlist;
3583 operand *left=newOperand(), *right=newOperand();
3584 left->type=right->type=SYMBOL;
3585 OP_SYMBOL(left)=AST_SYMBOL(tree->left);
3586 OP_SYMBOL(right)=AST_SYMBOL(tree->right);
3587 ic = newiCode (ARRAYINIT, left, right);
3592 /*-----------------------------------------------------------------*/
3593 /* geniCodeCritical - intermediate code for a critical statement */
3594 /*-----------------------------------------------------------------*/
3596 geniCodeCritical (ast *tree, int lvl)
3601 /* If op is NULL, the original interrupt state will saved on */
3602 /* the stack. Otherwise, it will be saved in op. */
3604 /* Generate a save of the current interrupt state & disabled */
3605 ic = newiCode (CRITICAL, NULL, NULL);
3606 IC_RESULT (ic) = op;
3609 /* Generate the critical code sequence */
3610 if (tree->left && tree->left->type == EX_VALUE)
3611 geniCodeDummyRead (ast2iCode (tree->left,lvl+1));
3613 ast2iCode (tree->left,lvl+1);
3615 /* Generate a restore of the original interrupt state */
3616 ic = newiCode (ENDCRITICAL, NULL, op);
3620 /*-----------------------------------------------------------------*/
3621 /* Stuff used in ast2iCode to modify geniCodeDerefPtr in some */
3622 /* particular case. Ie : assigning or dereferencing array or ptr */
3623 /*-----------------------------------------------------------------*/
3624 set * lvaluereqSet = NULL;
3625 typedef struct lvalItem
3632 /*-----------------------------------------------------------------*/
3633 /* addLvaluereq - add a flag for lvalreq for current ast level */
3634 /*-----------------------------------------------------------------*/
3635 void addLvaluereq(int lvl)
3637 lvalItem * lpItem = (lvalItem *)Safe_alloc ( sizeof (lvalItem));
3640 addSetHead(&lvaluereqSet,lpItem);
3643 /*-----------------------------------------------------------------*/
3644 /* delLvaluereq - del a flag for lvalreq for current ast level */
3645 /*-----------------------------------------------------------------*/
3649 lpItem = getSet(&lvaluereqSet);
3650 if(lpItem) Safe_free(lpItem);
3652 /*-----------------------------------------------------------------*/
3653 /* clearLvaluereq - clear lvalreq flag */
3654 /*-----------------------------------------------------------------*/
3655 void clearLvaluereq()
3658 lpItem = peekSet(lvaluereqSet);
3659 if(lpItem) lpItem->req = 0;
3661 /*-----------------------------------------------------------------*/
3662 /* getLvaluereq - get the last lvalreq level */
3663 /*-----------------------------------------------------------------*/
3664 int getLvaluereqLvl()
3667 lpItem = peekSet(lvaluereqSet);
3668 if(lpItem) return lpItem->lvl;
3671 /*-----------------------------------------------------------------*/
3672 /* isLvaluereq - is lvalreq valid for this level ? */
3673 /*-----------------------------------------------------------------*/
3674 int isLvaluereq(int lvl)
3677 lpItem = peekSet(lvaluereqSet);
3678 if(lpItem) return ((lpItem->req)&&(lvl <= (lpItem->lvl+1)));
3682 /*-----------------------------------------------------------------*/
3683 /* ast2iCode - creates an icodeList from an ast */
3684 /*-----------------------------------------------------------------*/
3686 ast2iCode (ast * tree,int lvl)
3688 operand *left = NULL;
3689 operand *right = NULL;
3693 /* set the global variables for filename & line number */
3695 filename = tree->filename;
3697 lineno = tree->lineno;
3699 block = tree->block;
3701 scopeLevel = tree->level;
3703 seqPoint = tree->seqPoint;
3705 if (tree->type == EX_VALUE)
3706 return operandFromValue (tree->opval.val);
3708 if (tree->type == EX_LINK)
3709 return operandFromLink (tree->opval.lnk);
3711 /* if we find a nullop */
3712 if (tree->type == EX_OP &&
3713 (tree->opval.op == NULLOP ||
3714 tree->opval.op == BLOCK))
3716 if (tree->left && tree->left->type == EX_VALUE)
3717 geniCodeDummyRead (ast2iCode (tree->left,lvl+1));
3719 ast2iCode (tree->left,lvl+1);
3720 if (tree->right && tree->right->type == EX_VALUE)
3721 geniCodeDummyRead (ast2iCode (tree->right,lvl+1));
3723 ast2iCode (tree->right,lvl+1);
3727 /* special cases for not evaluating */
3728 if (tree->opval.op != ':' &&
3729 tree->opval.op != '?' &&
3730 tree->opval.op != CALL &&
3731 tree->opval.op != IFX &&
3732 tree->opval.op != LABEL &&
3733 tree->opval.op != GOTO &&
3734 tree->opval.op != SWITCH &&
3735 tree->opval.op != FUNCTION &&
3736 tree->opval.op != INLINEASM &&
3737 tree->opval.op != CRITICAL)
3740 if (IS_ASSIGN_OP (tree->opval.op) ||
3741 IS_DEREF_OP (tree) ||
3742 (tree->opval.op == '&' && !tree->right) ||
3743 tree->opval.op == PTR_OP)
3746 if ((IS_ARRAY_OP (tree->left) && IS_ARRAY_OP (tree->left->left)) ||
3747 (IS_DEREF_OP (tree) && IS_ARRAY_OP (tree->left)))
3750 left = operandFromAst (tree->left,lvl);
3752 if (IS_DEREF_OP (tree) && IS_DEREF_OP (tree->left))
3753 left = geniCodeRValue (left, TRUE);
3757 left = operandFromAst (tree->left,lvl);
3759 if (tree->opval.op == INC_OP ||
3760 tree->opval.op == DEC_OP)
3763 right = operandFromAst (tree->right,lvl);
3768 right = operandFromAst (tree->right,lvl);
3772 /* now depending on the type of operand */
3773 /* this will be a biggy */
3774 switch (tree->opval.op)
3777 case '[': /* array operation */
3779 //sym_link *ltype = operandType (left);
3780 //left = geniCodeRValue (left, IS_PTR (ltype->next) ? TRUE : FALSE);
3781 left = geniCodeRValue (left, FALSE);
3782 right = geniCodeRValue (right, TRUE);
3785 return geniCodeArray (left, right,lvl);
3787 case '.': /* structure dereference */
3788 if (IS_PTR (operandType (left)))
3789 left = geniCodeRValue (left, TRUE);
3791 left = geniCodeRValue (left, FALSE);
3793 return geniCodeStruct (left, right, tree->lvalue);
3795 case PTR_OP: /* structure pointer dereference */
3798 pType = operandType (left);
3799 left = geniCodeRValue (left, TRUE);
3801 setOClass (pType, getSpec (operandType (left)));
3804 return geniCodeStruct (left, right, tree->lvalue);
3806 case INC_OP: /* increment operator */
3808 return geniCodePostInc (left);
3810 return geniCodePreInc (right, tree->lvalue);
3812 case DEC_OP: /* decrement operator */
3814 return geniCodePostDec (left);
3816 return geniCodePreDec (right, tree->lvalue);
3818 case '&': /* bitwise and or address of operator */
3820 { /* this is a bitwise operator */
3821 left = geniCodeRValue (left, FALSE);
3822 right = geniCodeRValue (right, FALSE);
3823 return geniCodeBitwise (left, right, BITWISEAND, tree->ftype);
3826 return geniCodeAddressOf (left);
3828 case '|': /* bitwise or & xor */
3830 return geniCodeBitwise (geniCodeRValue (left, FALSE),
3831 geniCodeRValue (right, FALSE),
3836 return geniCodeDivision (geniCodeRValue (left, FALSE),
3837 geniCodeRValue (right, FALSE),
3838 getResultTypeFromType (tree->ftype));
3841 return geniCodeModulus (geniCodeRValue (left, FALSE),
3842 geniCodeRValue (right, FALSE),
3843 getResultTypeFromType (tree->ftype));
3846 return geniCodeMultiply (geniCodeRValue (left, FALSE),
3847 geniCodeRValue (right, FALSE),
3848 getResultTypeFromType (tree->ftype));
3850 return geniCodeDerefPtr (geniCodeRValue (left, FALSE),lvl);
3854 return geniCodeSubtract (geniCodeRValue (left, FALSE),
3855 geniCodeRValue (right, FALSE),
3856 getResultTypeFromType (tree->ftype));
3858 return geniCodeUnaryMinus (geniCodeRValue (left, FALSE));
3862 return geniCodeAdd (geniCodeRValue (left, FALSE),
3863 geniCodeRValue (right, FALSE),
3864 getResultTypeFromType (tree->ftype),
3867 return geniCodeRValue (left, FALSE); /* unary '+' has no meaning */
3870 return geniCodeLeftShift (geniCodeRValue (left, FALSE),
3871 geniCodeRValue (right, FALSE),
3872 getResultTypeFromType (tree->ftype));
3875 return geniCodeRightShift (geniCodeRValue (left, FALSE),
3876 geniCodeRValue (right, FALSE));
3878 #if 0 // this indeed needs a second thought
3882 // let's keep this simple: get the rvalue we need
3883 op=geniCodeRValue (right, FALSE);
3884 // now cast it to whatever we want
3885 op=geniCodeCast (operandType(left), op, FALSE);
3886 // if this is going to be used as an lvalue, make it so
3892 #else // bug #604575, is it a bug ????
3893 return geniCodeCast (operandType (left),
3894 geniCodeRValue (right, FALSE), FALSE);
3901 return geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
3906 operand *op = geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
3907 setOperandType (op, UCHARTYPE);
3918 /* different compilers (even different gccs) evaluate
3919 the two calls in a different order. to get the same
3920 result on all machines we've to specify a clear sequence.
3921 return geniCodeLogic (geniCodeRValue (left, FALSE),
3922 geniCodeRValue (right, FALSE),
3926 operand *leftOp, *rightOp;
3928 rightOp = geniCodeRValue (right, FALSE);
3929 leftOp = geniCodeRValue (left , FALSE);
3931 return geniCodeLogic (leftOp, rightOp, tree->opval.op);
3934 return geniCodeConditional (tree,lvl);
3937 return operandFromLit (getSize (tree->right->ftype));
3941 sym_link *rtype = operandType (right);
3942 sym_link *ltype = operandType (left);
3943 if (IS_PTR (rtype) && IS_ITEMP (right)
3944 && right->isaddr && compareType (rtype->next, ltype) == 1)
3945 right = geniCodeRValue (right, TRUE);
3947 right = geniCodeRValue (right, FALSE);
3949 geniCodeAssign (left, right, 0);
3954 geniCodeAssign (left,
3955 geniCodeMultiply (geniCodeRValue (operandFromOperand (left),
3957 geniCodeRValue (right, FALSE), FALSE),
3958 getResultTypeFromType (tree->ftype));
3962 geniCodeAssign (left,
3963 geniCodeDivision (geniCodeRValue (operandFromOperand (left),
3965 geniCodeRValue (right, FALSE),
3966 getResultTypeFromType (tree->ftype)),
3970 geniCodeAssign (left,
3971 geniCodeModulus (geniCodeRValue (operandFromOperand (left),
3973 geniCodeRValue (right, FALSE),
3974 getResultTypeFromType (tree->ftype)),
3978 sym_link *rtype = operandType (right);
3979 sym_link *ltype = operandType (left);
3980 if (IS_PTR (rtype) && IS_ITEMP (right)
3981 && right->isaddr && compareType (rtype->next, ltype) == 1)
3982 right = geniCodeRValue (right, TRUE);
3984 right = geniCodeRValue (right, FALSE);
3987 return geniCodeAssign (left,
3988 geniCodeAdd (geniCodeRValue (operandFromOperand (left),
3991 getResultTypeFromType (tree->ftype),
3997 sym_link *rtype = operandType (right);
3998 sym_link *ltype = operandType (left);
3999 if (IS_PTR (rtype) && IS_ITEMP (right)
4000 && right->isaddr && compareType (rtype->next, ltype) == 1)
4002 right = geniCodeRValue (right, TRUE);
4006 right = geniCodeRValue (right, FALSE);
4009 geniCodeAssign (left,
4010 geniCodeSubtract (geniCodeRValue (operandFromOperand (left),
4013 getResultTypeFromType (tree->ftype)),
4018 geniCodeAssign (left,
4019 geniCodeLeftShift (geniCodeRValue (operandFromOperand (left)
4021 geniCodeRValue (right, FALSE),
4022 getResultTypeFromType (tree->ftype)),
4026 geniCodeAssign (left,
4027 geniCodeRightShift (geniCodeRValue (operandFromOperand (left)
4029 geniCodeRValue (right, FALSE)), 0);
4032 geniCodeAssign (left,
4033 geniCodeBitwise (geniCodeRValue (operandFromOperand (left),
4035 geniCodeRValue (right, FALSE),
4037 operandType (left)), 0);
4040 geniCodeAssign (left,
4041 geniCodeBitwise (geniCodeRValue (operandFromOperand (left),
4043 geniCodeRValue (right, FALSE),
4045 operandType (left)), 0);
4048 geniCodeAssign (left,
4049 geniCodeBitwise (geniCodeRValue (operandFromOperand (left)
4051 geniCodeRValue (right, FALSE),
4053 operandType (left)), 0);
4055 return geniCodeRValue (right, FALSE);
4058 return geniCodeCall (ast2iCode (tree->left,lvl+1),
4061 geniCodeLabel (ast2iCode (tree->left,lvl+1)->operand.symOperand);
4062 return ast2iCode (tree->right,lvl+1);
4065 geniCodeGoto (ast2iCode (tree->left,lvl+1)->operand.symOperand);
4066 return ast2iCode (tree->right,lvl+1);
4069 geniCodeFunctionBody (tree,lvl);
4073 geniCodeReturn (right);
4077 geniCodeIfx (tree,lvl);
4081 geniCodeSwitch (tree,lvl);
4085 geniCodeInline (tree);
4089 geniCodeArrayInit(tree, ast2iCode (tree->left,lvl+1));
4093 geniCodeCritical (tree, lvl);
4099 /*-----------------------------------------------------------------*/
4100 /* reverseICChain - gets from the list and creates a linkedlist */
4101 /*-----------------------------------------------------------------*/
4108 while ((loop = getSet (&iCodeChain)))
4120 /*-----------------------------------------------------------------*/
4121 /* iCodeFromAst - given an ast will convert it to iCode */
4122 /*-----------------------------------------------------------------*/
4124 iCodeFromAst (ast * tree)
4126 returnLabel = newiTempLabel ("_return");
4127 entryLabel = newiTempLabel ("_entry");
4129 return reverseiCChain ();
4132 static const char *opTypeToStr(OPTYPE op)
4136 case SYMBOL: return "symbol";
4137 case VALUE: return "value";
4138 case TYPE: return "type";
4140 return "undefined type";
4144 operand *validateOpType(operand *op,
4151 if (op && op->type == type)
4156 "Internal error: validateOpType failed in %s(%s) @ %s:%u:"
4157 " expected %s, got %s\n",
4158 macro, args, file, line,
4159 opTypeToStr(type), op ? opTypeToStr(op->type) : "null op");
4161 return op; // never reached, makes compiler happy.