* src/SDCCast.c (decorateType),
[fw/sdcc] / src / SDCCicode.c
1 /*-------------------------------------------------------------------------
2
3   SDCCicode.c - intermediate code generation etc.
4                 Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
5
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
9    later version.
10
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.
15
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.
19
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 -------------------------------------------------------------------------*/
24
25 #include "common.h"
26 #include "newalloc.h"
27 #include "math.h"
28
29 /*-----------------------------------------------------------------*/
30 /* global variables       */
31
32 set *iCodeChain = NULL;
33 int iTempNum = 0;
34 int iTempLblNum = 0;
35 int operandKey = 0;
36 int iCodeKey = 0;
37 char *filename;
38 int lineno;
39 int block;
40 int scopeLevel;
41 int seqPoint;
42
43 symbol *returnLabel;            /* function return label */
44 symbol *entryLabel;             /* function entry  label */
45
46 /*-----------------------------------------------------------------*/
47 /* forward definition of some functions */
48 operand *geniCodeDivision (operand *, operand *);
49 operand *geniCodeAssign (operand *, operand *, int);
50 static operand *geniCodeArray (operand *, operand *,int);
51 static operand *geniCodeArray2Ptr (operand *);
52 operand *geniCodeRValue (operand *, bool);
53 operand *geniCodeDerefPtr (operand *,int);
54 int isLvaluereq(int lvl);
55 void  setOClass (sym_link * ptr, sym_link * spec);
56 static operand *geniCodeCast (sym_link *, operand *, bool);
57
58 #define PRINTFUNC(x) void x (FILE *of, iCode *ic, char *s)
59 /* forward definition of ic print functions */
60 PRINTFUNC (picGetValueAtAddr);
61 PRINTFUNC (picSetValueAtAddr);
62 PRINTFUNC (picAddrOf);
63 PRINTFUNC (picGeneric);
64 PRINTFUNC (picGenericOne);
65 PRINTFUNC (picCast);
66 PRINTFUNC (picAssign);
67 PRINTFUNC (picLabel);
68 PRINTFUNC (picGoto);
69 PRINTFUNC (picIfx);
70 PRINTFUNC (picJumpTable);
71 PRINTFUNC (picInline);
72 PRINTFUNC (picReceive);
73 PRINTFUNC (picDummyRead);
74 PRINTFUNC (picCritical);
75 PRINTFUNC (picEndCritical);
76
77 iCodeTable codeTable[] =
78 {
79   {'!', "not", picGenericOne, NULL},
80   {'~', "~", picGenericOne, NULL},
81   {RRC, "rrc", picGenericOne, NULL},
82   {RLC, "rlc", picGenericOne, NULL},
83   {GETHBIT, "ghbit", picGenericOne, NULL},
84   {UNARYMINUS, "-", picGenericOne, NULL},
85   {IPUSH, "push", picGenericOne, NULL},
86   {IPOP, "pop", picGenericOne, NULL},
87   {CALL, "call", picGenericOne, NULL},
88   {PCALL, "pcall", picGenericOne, NULL},
89   {FUNCTION, "proc", picGenericOne, NULL},
90   {ENDFUNCTION, "eproc", picGenericOne, NULL},
91   {RETURN, "ret", picGenericOne, NULL},
92   {'+', "+", picGeneric, NULL},
93   {'-', "-", picGeneric, NULL},
94   {'*', "*", picGeneric, NULL},
95   {'/', "/", picGeneric, NULL},
96   {'%', "%", picGeneric, NULL},
97   {'>', ">", picGeneric, NULL},
98   {'<', "<", picGeneric, NULL},
99   {LE_OP, "<=", picGeneric, NULL},
100   {GE_OP, ">=", picGeneric, NULL},
101   {EQ_OP, "==", picGeneric, NULL},
102   {NE_OP, "!=", picGeneric, NULL},
103   {AND_OP, "&&", picGeneric, NULL},
104   {OR_OP, "||", picGeneric, NULL},
105   {'^', "^", picGeneric, NULL},
106   {'|', "|", picGeneric, NULL},
107   {BITWISEAND, "&", picGeneric, NULL},
108   {LEFT_OP, "<<", picGeneric, NULL},
109   {RIGHT_OP, ">>", picGeneric, NULL},
110   {GET_VALUE_AT_ADDRESS, "@", picGetValueAtAddr, NULL},
111   {ADDRESS_OF, "&", picAddrOf, NULL},
112   {CAST, "<>", picCast, NULL},
113   {'=', ":=", picAssign, NULL},
114   {LABEL, "", picLabel, NULL},
115   {GOTO, "", picGoto, NULL},
116   {JUMPTABLE, "jtab", picJumpTable, NULL},
117   {IFX, "if", picIfx, NULL},
118   {INLINEASM, "", picInline, NULL},
119   {RECEIVE, "recv", picReceive, NULL},
120   {SEND, "send", picGenericOne, NULL},
121   {ARRAYINIT, "arrayInit", picGenericOne, NULL},
122   {DUMMY_READ_VOLATILE, "dummy = (volatile)", picDummyRead, NULL},
123   {CRITICAL, "critical_start", picCritical, NULL},
124   {ENDCRITICAL, "critical_end", picEndCritical, NULL}
125 };
126
127 /*-----------------------------------------------------------------*/
128 /* checkConstantRange: check a constant against the type           */
129 /*-----------------------------------------------------------------*/
130
131
132 /*   pedantic=0: allmost anything is allowed as long as the absolute
133        value is within the bit range of the type, and -1 is treated as
134        0xf..f for unsigned types (e.g. in assign)
135      pedantic=1: -1==-1, not allowed for unsigned types (e.g. in compare)
136      pedantic>1: "char c=200" is not allowed (evaluates to -56)
137 */
138
139 void checkConstantRange(sym_link *ltype, value *val, char *msg,
140                         int pedantic) {
141   double max;
142   int warnings=0;
143   int negative=0;
144   long v;
145
146   max = pow ((double)2.0, (double)bitsForType(ltype));
147
148   if (IS_LONG(val->type)) {
149     if (IS_UNSIGNED(val->type)) {
150       v=SPEC_CVAL(val->type).v_ulong;
151     } else {
152       v=SPEC_CVAL(val->type).v_long;
153     }
154   } else {
155     if (IS_UNSIGNED(val->type)) {
156       v=SPEC_CVAL(val->type).v_uint;
157     } else {
158       v=SPEC_CVAL(val->type).v_int;
159     }
160   }
161
162
163 #if 0
164   // this could be a good idea
165   if (options.pedantic)
166     pedantic=2;
167 #endif
168
169   if (IS_FLOAT(ltype)) {
170     // anything will do
171     return;
172   }
173
174   if (!IS_UNSIGNED(val->type) && v<0) {
175     negative=1;
176     if (IS_UNSIGNED(ltype) && (pedantic>1)) {
177       warnings++;
178     }
179     v=-v;
180   }
181
182   // if very pedantic: "char c=200" is not allowed
183   if (pedantic>1 && !IS_UNSIGNED(ltype)) {
184     max = max/2 + negative;
185   }
186
187   if (v >= max) {
188     warnings++;
189   }
190
191 #if 0 // temporary disabled, leaving the warning as a reminder
192   if (warnings) {
193     SNPRINTF (message, sizeof(message), "for %s %s in %s", 
194              IS_UNSIGNED(ltype) ? "unsigned" : "signed",
195              nounName(ltype), msg);
196     werror (W_CONST_RANGE, message);
197
198     if (pedantic>1)
199       fatalError++;
200   }
201 #endif
202 }
203
204 /*-----------------------------------------------------------------*/
205 /* operandName - returns the name of the operand                   */
206 /*-----------------------------------------------------------------*/
207 int 
208 printOperand (operand * op, FILE * file)
209 {
210   sym_link *opetype;
211   int pnl = 0;
212
213   if (!op)
214     return 1;
215
216   if (!file)
217     {
218       file = stdout;
219       pnl = 1;
220     }
221   switch (op->type)
222     {
223
224     case VALUE:
225       opetype = getSpec (operandType (op));
226       if (IS_FLOAT (opetype))
227         fprintf (file, "%g {", SPEC_CVAL (opetype).v_float);
228       else
229         fprintf (file, "0x%x {", (unsigned) floatFromVal (op->operand.valOperand));
230       printTypeChain (operandType (op), file);
231       fprintf (file, "}");
232       break;
233
234     case SYMBOL:
235 #define REGA 1
236 #ifdef REGA
237       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}"  , */
238                (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name),
239                op->key,
240                OP_LIVEFROM (op), OP_LIVETO (op),
241                OP_SYMBOL (op)->stack,
242                op->isaddr, OP_SYMBOL (op)->isreqv, 
243                OP_SYMBOL (op)->remat,OP_SYMBOL(op)->noSpilLoc,
244                OP_SYMBOL(op)->ruonly,OP_SYMBOL(op)->dptr
245         );
246       {
247         fprintf (file, "{");
248         printTypeChain (operandType (op), file);
249         if (SPIL_LOC (op) && IS_ITEMP (op))
250           fprintf (file, "}{ sir@ %s", SPIL_LOC (op)->rname);
251         fprintf (file, "}");
252
253       }
254
255       /* if assigned to registers */
256       if (OP_SYMBOL (op)->nRegs)
257         {
258           if (OP_SYMBOL (op)->isspilt)
259             {
260               if (!OP_SYMBOL (op)->remat)
261                 if (OP_SYMBOL (op)->usl.spillLoc)
262                   fprintf (file, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ?
263                                        OP_SYMBOL (op)->usl.spillLoc->rname :
264                                        OP_SYMBOL (op)->usl.spillLoc->name));
265                 else
266                   fprintf (file, "[err]");
267               else
268                 fprintf (file, "[remat]");
269             }
270           else
271             {
272               int i;
273               fprintf (file, "[");
274               for (i = 0; i < OP_SYMBOL (op)->nRegs; i++)
275                 fprintf (file, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i]));
276               fprintf (file, "]");
277             }
278         }
279 #else
280       fprintf (file, "%s", (OP_SYMBOL (op)->rname[0] ?
281                             OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name));
282       /* if assigned to registers */
283       if (OP_SYMBOL (op)->nRegs && !OP_SYMBOL (op)->isspilt)
284         {
285           int i;
286           fprintf (file, "[");
287           for (i = 0; i < OP_SYMBOL (op)->nRegs; i++)
288             fprintf (file, "%s ", (OP_SYMBOL (op)->regs[i] ?
289                                    OP_SYMBOL (op)->regs[i]->name :
290                                    "err"));
291           fprintf (file, "]");
292         }
293 #endif
294       break;
295
296     case TYPE:
297       fprintf (file, "(");
298       printTypeChain (op->operand.typeOperand, file);
299       fprintf (file, ")");
300       break;
301     }
302
303   if (pnl)
304     fprintf (file, "\n");
305   return 0;
306 }
307
308
309 /*-----------------------------------------------------------------*/
310 /*                    print functions                              */
311 /*-----------------------------------------------------------------*/
312 PRINTFUNC (picGetValueAtAddr)
313 {
314   fprintf (of, "\t");
315   printOperand (IC_RESULT (ic), of);
316   fprintf (of, " = ");
317   fprintf (of, "@[");
318   printOperand (IC_LEFT (ic), of);
319   fprintf (of, "]");
320
321   fprintf (of, "\n");
322 }
323
324 PRINTFUNC (picSetValueAtAddr)
325 {
326   fprintf (of, "\t");
327   fprintf (of, "*[");
328   printOperand (IC_LEFT (ic), of);
329   fprintf (of, "] = ");
330   printOperand (IC_RIGHT (ic), of);
331   fprintf (of, "\n");
332 }
333
334 PRINTFUNC (picAddrOf)
335 {
336   fprintf (of, "\t");
337   printOperand (IC_RESULT (ic), of);
338   if (IS_ITEMP (IC_LEFT (ic)))
339     fprintf (of, " = ");
340   else
341     fprintf (of, " = &[");
342   printOperand (IC_LEFT (ic), of);
343   if (IC_RIGHT (ic))
344     {
345       if (IS_ITEMP (IC_LEFT (ic)))
346         fprintf (of, " offsetAdd ");
347       else
348         fprintf (of, " , ");
349       printOperand (IC_RIGHT (ic), of);
350     }
351   if (IS_ITEMP (IC_LEFT (ic)))
352     fprintf (of, "\n");
353   else
354     fprintf (of, "]\n");
355 }
356
357 PRINTFUNC (picJumpTable)
358 {
359   symbol *sym;
360
361   fprintf (of, "\t");
362   fprintf (of, "%s\t", s);
363   printOperand (IC_JTCOND (ic), of);
364   fprintf (of, "\n");
365   for (sym = setFirstItem (IC_JTLABELS (ic)); sym;
366        sym = setNextItem (IC_JTLABELS (ic)))
367     fprintf (of, "\t\t\t%s\n", sym->name);
368 }
369
370 PRINTFUNC (picGeneric)
371 {
372   fprintf (of, "\t");
373   printOperand (IC_RESULT (ic), of);
374   fprintf (of, " = ");
375   printOperand (IC_LEFT (ic), of);
376   fprintf (of, " %s ", s);
377   printOperand (IC_RIGHT (ic), of);
378   fprintf (of, "\n");
379 }
380
381 PRINTFUNC (picGenericOne)
382 {
383   fprintf (of, "\t");
384   if (IC_RESULT (ic))
385     {
386       printOperand (IC_RESULT (ic), of);
387       fprintf (of, " = ");
388     }
389
390   if (IC_LEFT (ic))
391     {
392       fprintf (of, "%s ", s);
393       printOperand (IC_LEFT (ic), of);
394     }
395
396   if (!IC_RESULT (ic) && !IC_LEFT (ic))
397     fprintf (of, s);
398
399   if (ic->op == SEND || ic->op == RECEIVE) {
400       fprintf(of,"{argreg = %d}",ic->argreg);
401   }
402   fprintf (of, "\n");
403 }
404
405 PRINTFUNC (picCast)
406 {
407   fprintf (of, "\t");
408   printOperand (IC_RESULT (ic), of);
409   fprintf (of, " = ");
410   printOperand (IC_LEFT (ic), of);
411   printOperand (IC_RIGHT (ic), of);
412   fprintf (of, "\n");
413 }
414
415
416 PRINTFUNC (picAssign)
417 {
418   fprintf (of, "\t");
419
420   if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic)))
421     fprintf (of, "*(");
422
423   printOperand (IC_RESULT (ic), of);
424
425   if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic)))
426     fprintf (of, ")");
427
428   fprintf (of, " %s ", s);
429   printOperand (IC_RIGHT (ic), of);
430
431   fprintf (of, "\n");
432 }
433
434 PRINTFUNC (picLabel)
435 {
436   fprintf (of, " %s($%d) :\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key);
437 }
438
439 PRINTFUNC (picGoto)
440 {
441   fprintf (of, "\t");
442   fprintf (of, " goto %s($%d)\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key);
443 }
444
445 PRINTFUNC (picIfx)
446 {
447   fprintf (of, "\t");
448   fprintf (of, "if ");
449   printOperand (IC_COND (ic), of);
450
451   if (!IC_TRUE (ic))
452     fprintf (of, " == 0 goto %s($%d)\n", IC_FALSE (ic)->name, IC_FALSE (ic)->key);
453   else
454     {
455       fprintf (of, " != 0 goto %s($%d)\n", IC_TRUE (ic)->name, IC_TRUE (ic)->key);
456       if (IC_FALSE (ic))
457         fprintf (of, "\tzzgoto %s\n", IC_FALSE (ic)->name);
458     }
459 }
460
461 PRINTFUNC (picInline)
462 {
463   fprintf (of, "%s", IC_INLINE (ic));
464 }
465
466 PRINTFUNC (picReceive)
467 {
468   printOperand (IC_RESULT (ic), of);
469   fprintf (of, " = %s ", s);
470   printOperand (IC_LEFT (ic), of);
471   fprintf (of, "\n");
472 }
473
474 PRINTFUNC (picDummyRead)
475 {
476   fprintf (of, "\t");
477   fprintf (of, "%s ", s);
478   printOperand (IC_RIGHT (ic), of);
479   fprintf (of, "\n");
480 }
481
482 PRINTFUNC (picCritical)
483 {
484   fprintf (of, "\t");
485   if (IC_RESULT (ic))
486     printOperand (IC_RESULT (ic), of);
487   else
488     fprintf (of, "(stack)");
489   fprintf (of, " = %s ", s);
490   fprintf (of, "\n");
491 }
492
493 PRINTFUNC (picEndCritical)
494 {
495   fprintf (of, "\t");
496   fprintf (of, "%s = ", s);
497   if (IC_RIGHT (ic))
498     printOperand (IC_RIGHT (ic), of);
499   else
500     fprintf (of, "(stack)");
501   fprintf (of, "\n");
502 }
503
504 /*-----------------------------------------------------------------*/
505 /* piCode - prints one iCode                                       */
506 /*-----------------------------------------------------------------*/
507 int
508 piCode (void *item, FILE * of)
509 {
510   iCode *ic = item;
511   iCodeTable *icTab;
512
513   if (!of)
514     of = stdout;
515
516   icTab = getTableEntry (ic->op);
517   fprintf (stdout, "%s(%d:%d:%d:%d:%d)\t",
518            ic->filename, ic->lineno,
519            ic->seq, ic->key, ic->depth, ic->supportRtn);
520   icTab->iCodePrint (of, ic, icTab->printName);
521   return 1;
522 }
523
524 void PICC(iCode *ic)
525 {
526         printiCChain(ic,stdout);
527 }
528 /*-----------------------------------------------------------------*/
529 /* printiCChain - prints intermediate code for humans              */
530 /*-----------------------------------------------------------------*/
531 void
532 printiCChain (iCode * icChain, FILE * of)
533 {
534   iCode *loop;
535   iCodeTable *icTab;
536
537   if (!of)
538     of = stdout;
539   for (loop = icChain; loop; loop = loop->next)
540     {
541       if ((icTab = getTableEntry (loop->op)))
542         {
543           fprintf (of, "%s(l%d:s%d:k%d:d%d:s%d)\t",
544                    loop->filename, loop->lineno,
545                    loop->seq, loop->key, loop->depth, loop->supportRtn);
546
547           icTab->iCodePrint (of, loop, icTab->printName);
548         }
549     }
550 }
551
552
553 /*-----------------------------------------------------------------*/
554 /* newOperand - allocate, init & return a new iCode                */
555 /*-----------------------------------------------------------------*/
556 operand *
557 newOperand ()
558 {
559   operand *op;
560
561   op = Safe_alloc ( sizeof (operand));
562
563   op->key = 0;
564   return op;
565 }
566
567 /*-----------------------------------------------------------------*/
568 /* newiCode - create and return a new iCode entry initialised      */
569 /*-----------------------------------------------------------------*/
570 iCode *
571 newiCode (int op, operand * left, operand * right)
572 {
573   iCode *ic;
574
575   ic = Safe_alloc ( sizeof (iCode));
576
577   ic->seqPoint = seqPoint;
578   ic->lineno = lineno;
579   ic->filename = filename;
580   ic->block = block;
581   ic->level = scopeLevel;
582   ic->op = op;
583   ic->key = iCodeKey++;
584   IC_LEFT (ic) = left;
585   IC_RIGHT (ic) = right;
586
587   return ic;
588 }
589
590 /*-----------------------------------------------------------------*/
591 /* newiCode for conditional statements                             */
592 /*-----------------------------------------------------------------*/
593 iCode *
594 newiCodeCondition (operand * condition,
595                    symbol * trueLabel,
596                    symbol * falseLabel)
597 {
598   iCode *ic;
599
600   if (IS_VOID(operandType(condition))) {
601     werror(E_VOID_VALUE_USED);
602   }
603
604   ic = newiCode (IFX, NULL, NULL);
605   IC_COND (ic) = condition;
606   IC_TRUE (ic) = trueLabel;
607   IC_FALSE (ic) = falseLabel;
608   return ic;
609 }
610
611 /*-----------------------------------------------------------------*/
612 /* newiCodeLabelGoto - unconditional goto statement| label stmnt   */
613 /*-----------------------------------------------------------------*/
614 iCode *
615 newiCodeLabelGoto (int op, symbol * label)
616 {
617   iCode *ic;
618
619   ic = newiCode (op, NULL, NULL);
620   ic->op = op;
621   ic->label = label;
622   IC_LEFT (ic) = NULL;
623   IC_RIGHT (ic) = NULL;
624   IC_RESULT (ic) = NULL;
625   return ic;
626 }
627
628 /*-----------------------------------------------------------------*/
629 /* newiTemp - allocate & return a newItemp Variable                */
630 /*-----------------------------------------------------------------*/
631 symbol *
632 newiTemp (char *s)
633 {
634   symbol *itmp;
635
636   if (s)
637   {
638       SNPRINTF (buffer, sizeof(buffer), "%s", s);
639   }
640   else
641   {
642       SNPRINTF (buffer, sizeof(buffer), "iTemp%d", iTempNum++);
643   }
644     
645   itmp = newSymbol (buffer, 1);
646   strncpyz (itmp->rname, itmp->name, SDCC_NAME_MAX);
647   itmp->isitmp = 1;
648
649   return itmp;
650 }
651
652 /*-----------------------------------------------------------------*/
653 /* newiTempLabel - creates a temp variable label                   */
654 /*-----------------------------------------------------------------*/
655 symbol *
656 newiTempLabel (char *s)
657 {
658   symbol *itmplbl;
659
660   /* check if this alredy exists */
661   if (s && (itmplbl = findSym (LabelTab, NULL, s)))
662     return itmplbl;
663
664   if (s)
665     {
666         itmplbl = newSymbol (s, 1);
667     }
668   else
669     {
670       SNPRINTF (buffer, sizeof(buffer), "iTempLbl%d", iTempLblNum++);
671       itmplbl = newSymbol (buffer, 1);
672     }
673
674   itmplbl->isitmp = 1;
675   itmplbl->islbl = 1;
676   itmplbl->key = labelKey++;
677   addSym (LabelTab, itmplbl, itmplbl->name, 0, 0, 0);
678   return itmplbl;
679 }
680
681 /*-----------------------------------------------------------------*/
682 /* newiTempPreheaderLabel - creates a new preheader label          */
683 /*-----------------------------------------------------------------*/
684 symbol *
685 newiTempPreheaderLabel ()
686 {
687   symbol *itmplbl;
688
689   SNPRINTF (buffer, sizeof(buffer), "preHeaderLbl%d", iTempLblNum++);
690   itmplbl = newSymbol (buffer, 1);
691
692   itmplbl->isitmp = 1;
693   itmplbl->islbl = 1;
694   itmplbl->key = labelKey++;
695   addSym (LabelTab, itmplbl, itmplbl->name, 0, 0, 0);
696   return itmplbl;
697 }
698
699
700 /*-----------------------------------------------------------------*/
701 /* initiCode - initialises some iCode related stuff                */
702 /*-----------------------------------------------------------------*/
703 void 
704 initiCode ()
705 {
706
707 }
708
709 /*-----------------------------------------------------------------*/
710 /* copyiCode - make a copy of the iCode given                      */
711 /*-----------------------------------------------------------------*/
712 iCode *
713 copyiCode (iCode * ic)
714 {
715   iCode *nic = newiCode (ic->op, NULL, NULL);
716
717   nic->lineno = ic->lineno;
718   nic->filename = ic->filename;
719   nic->block = ic->block;
720   nic->level = ic->level;
721   nic->parmBytes = ic->parmBytes;
722
723   /* deal with the special cases first */
724   switch (ic->op)
725     {
726     case IFX:
727       IC_COND (nic) = operandFromOperand (IC_COND (ic));
728       IC_TRUE (nic) = IC_TRUE (ic);
729       IC_FALSE (nic) = IC_FALSE (ic);
730       break;
731
732     case JUMPTABLE:
733       IC_JTCOND (nic) = operandFromOperand (IC_JTCOND (ic));
734       IC_JTLABELS (nic) = IC_JTLABELS (ic);
735       break;
736
737     case CALL:
738     case PCALL:
739       IC_RESULT (nic) = operandFromOperand (IC_RESULT (ic));
740       IC_LEFT (nic) = operandFromOperand (IC_LEFT (ic));
741       break;
742
743     case INLINEASM:
744       IC_INLINE (nic) = IC_INLINE (ic);
745       break;
746
747     case ARRAYINIT:
748       IC_ARRAYILIST(nic) = IC_ARRAYILIST(ic);
749       break;
750
751     default:
752       IC_RESULT (nic) = operandFromOperand (IC_RESULT (ic));
753       IC_LEFT (nic) = operandFromOperand (IC_LEFT (ic));
754       IC_RIGHT (nic) = operandFromOperand (IC_RIGHT (ic));
755     }
756
757   return nic;
758 }
759
760 /*-----------------------------------------------------------------*/
761 /* getTableEntry - gets the table entry for the given operator     */
762 /*-----------------------------------------------------------------*/
763 iCodeTable *
764 getTableEntry (int oper)
765 {
766   unsigned i;
767
768   for (i = 0; i < (sizeof (codeTable) / sizeof (iCodeTable)); i++)
769     if (oper == codeTable[i].icode)
770       return &codeTable[i];
771
772   return NULL;
773 }
774
775 /*-----------------------------------------------------------------*/
776 /* newiTempOperand - new intermediate temp operand                 */
777 /*-----------------------------------------------------------------*/
778 operand *
779 newiTempOperand (sym_link * type, char throwType)
780 {
781   symbol *itmp;
782   operand *op = newOperand ();
783   sym_link *etype;
784
785   op->type = SYMBOL;
786   itmp = newiTemp (NULL);
787
788   etype = getSpec (type);
789
790   if (IS_LITERAL (etype))
791     throwType = 0;
792
793   /* copy the type information */
794   if (type)
795     itmp->etype = getSpec (itmp->type = (throwType ? type :
796                                          copyLinkChain (type)));
797   if (IS_LITERAL (itmp->etype))
798     {
799       SPEC_SCLS (itmp->etype) = S_REGISTER;
800       SPEC_OCLS (itmp->etype) = reg;
801     }
802
803   op->operand.symOperand = itmp;
804   op->key = itmp->key = ++operandKey;
805   return op;
806 }
807
808 /*-----------------------------------------------------------------*/
809 /* operandType - returns the type chain for an operand             */
810 /*-----------------------------------------------------------------*/
811 sym_link *
812 operandType (operand * op)
813 {
814   /* depending on type of operand */
815   switch (op->type)
816     {
817
818     case VALUE:
819       return op->operand.valOperand->type;
820
821     case SYMBOL:
822       return op->operand.symOperand->type;
823
824     case TYPE:
825       return op->operand.typeOperand;
826     default:
827       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
828               " operand type not known ");
829       assert (0);               /* should never come here */
830       /*  Just to keep the compiler happy */
831       return (sym_link *) 0;
832     }
833 }
834
835 /*-----------------------------------------------------------------*/
836 /* isParamterToCall - will return 1 if op is a parameter to args   */
837 /*-----------------------------------------------------------------*/
838 int 
839 isParameterToCall (value * args, operand * op)
840 {
841   value *tval = args;
842
843   wassert (IS_SYMOP(op));
844     
845   while (tval)
846     {
847       if (tval->sym &&
848           isSymbolEqual (op->operand.symOperand, tval->sym))
849         return 1;
850       tval = tval->next;
851     }
852   return 0;
853 }
854
855 /*-----------------------------------------------------------------*/
856 /* isOperandGlobal   - return 1 if operand is a global variable    */
857 /*-----------------------------------------------------------------*/
858 int 
859 isOperandGlobal (operand * op)
860 {
861   if (!op)
862     return 0;
863
864   if (IS_ITEMP (op))
865     return 0;
866
867   if (IS_SYMOP(op) &&
868       (op->operand.symOperand->level == 0 ||
869        IS_STATIC (op->operand.symOperand->etype) ||
870        IS_EXTERN (op->operand.symOperand->etype))
871     )
872     return 1;
873
874   return 0;
875 }
876
877 /*-----------------------------------------------------------------*/
878 /* isOperandVolatile - return 1 if the operand is volatile         */
879 /*-----------------------------------------------------------------*/
880 int 
881 isOperandVolatile (operand * op, bool chkTemp)
882 {
883   sym_link *optype;
884   sym_link *opetype;
885
886   if (IS_ITEMP (op) && !chkTemp)
887     return 0;
888
889   opetype = getSpec (optype = operandType (op));
890     
891   if (IS_PTR (optype) && DCL_PTR_VOLATILE (optype))   
892     return 1;   
893     
894   if (IS_VOLATILE (opetype))   
895     return 1;   
896   return 0; 
897 }
898
899 /*-----------------------------------------------------------------*/
900 /* isOperandLiteral - returns 1 if an operand contains a literal   */
901 /*-----------------------------------------------------------------*/
902 int 
903 isOperandLiteral (operand * op)
904 {
905   sym_link *opetype;
906
907   if (!op)
908     return 0;
909
910   opetype = getSpec (operandType (op));
911
912   if (IS_LITERAL (opetype))
913     return 1;
914
915   return 0;
916 }
917
918 /*-----------------------------------------------------------------*/
919 /* isOperandInFarSpace - will return true if operand is in farSpace */
920 /*-----------------------------------------------------------------*/
921 bool 
922 isOperandInFarSpace (operand * op)
923 {
924   sym_link *etype;
925
926   if (!op)
927     return FALSE;
928
929   if (!IS_SYMOP (op))
930     return FALSE;
931
932   if (!IS_TRUE_SYMOP (op))
933     {
934       if (SPIL_LOC (op))
935         etype = SPIL_LOC (op)->etype;
936       else
937         return FALSE;
938     }
939   else
940     {
941       etype = getSpec (operandType (op));
942     }
943   return (IN_FARSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
944 }
945
946 /*------------------------------------------------------------------*/
947 /* isOperandInDirSpace - will return true if operand is in dirSpace */
948 /*------------------------------------------------------------------*/
949 bool 
950 isOperandInDirSpace (operand * op)
951 {
952   sym_link *etype;
953
954   if (!op)
955     return FALSE;
956
957   if (!IS_SYMOP (op))
958     return FALSE;
959
960   if (!IS_TRUE_SYMOP (op))
961     {
962       if (SPIL_LOC (op))
963         etype = SPIL_LOC (op)->etype;
964       else
965         return FALSE;
966     }
967   else
968     {
969       etype = getSpec (operandType (op));
970     }
971   return (IN_DIRSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
972 }
973
974 /*--------------------------------------------------------------------*/
975 /* isOperandInCodeSpace - will return true if operand is in codeSpace */
976 /*--------------------------------------------------------------------*/
977 bool 
978 isOperandInCodeSpace (operand * op)
979 {
980   sym_link *etype;
981
982   if (!op)
983     return FALSE;
984
985   if (!IS_SYMOP (op))
986     return FALSE;
987
988   etype = getSpec (operandType (op));
989
990   if (!IS_TRUE_SYMOP (op))
991     {
992       if (SPIL_LOC (op))
993         etype = SPIL_LOC (op)->etype;
994       else
995         return FALSE;
996     }
997   else
998     {
999       etype = getSpec (operandType (op));
1000     }
1001   return (IN_CODESPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
1002 }
1003
1004 /*-----------------------------------------------------------------*/
1005 /* isOperandOnStack - will return true if operand is on stack      */
1006 /*-----------------------------------------------------------------*/
1007 bool 
1008 isOperandOnStack (operand * op)
1009 {
1010   sym_link *etype;
1011
1012   if (!op)
1013     return FALSE;
1014
1015   if (!IS_SYMOP (op))
1016     return FALSE;
1017
1018   etype = getSpec (operandType (op));
1019   if (IN_STACK (etype) ||
1020       OP_SYMBOL(op)->onStack ||
1021       (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
1022     return TRUE;
1023
1024   return FALSE;
1025 }
1026
1027 /*-----------------------------------------------------------------*/
1028 /* isOclsExpensive - will return true if accesses to an output     */
1029 /*                   storage class are expensive                   */
1030 /*-----------------------------------------------------------------*/
1031 bool 
1032 isOclsExpensive (struct memmap *oclass)
1033 {
1034   if (port->oclsExpense)
1035     return port->oclsExpense (oclass) > 0;
1036
1037   /* In the absence of port specific guidance, assume only */
1038   /* farspace is expensive. */
1039   return IN_FARSPACE (oclass);
1040 }
1041
1042 /*-----------------------------------------------------------------*/
1043 /* operandLitValue - literal value of an operand                   */
1044 /*-----------------------------------------------------------------*/
1045 double
1046 operandLitValue (operand * op)
1047 {
1048   assert (isOperandLiteral (op));
1049
1050   return floatFromVal (op->operand.valOperand);
1051 }
1052
1053 /*-----------------------------------------------------------------*/
1054 /* getBuiltInParms - returns parameters to a builtin functions     */
1055 /*-----------------------------------------------------------------*/
1056 iCode *getBuiltinParms (iCode *ic, int *pcount, operand **parms)
1057 {
1058     sym_link *ftype;
1059
1060     *pcount = 0;
1061     /* builtin functions uses only SEND for parameters */
1062     while (ic->op != CALL) {
1063         assert(ic->op == SEND && ic->builtinSEND);
1064         ic->generated = 1;    /* mark the icode as generated */
1065         parms[*pcount] = IC_LEFT(ic);
1066         ic = ic->next;
1067         (*pcount)++;
1068     }
1069
1070     ic->generated = 1;
1071     /* make sure this is a builtin function call */
1072     assert(IS_SYMOP(IC_LEFT(ic)));
1073     ftype = operandType(IC_LEFT(ic));
1074     assert(IFFUNC_ISBUILTIN(ftype));
1075     return ic;
1076 }
1077
1078 /*-----------------------------------------------------------------*/
1079 /* operandOperation - performs operations on operands             */
1080 /*-----------------------------------------------------------------*/
1081 operand *
1082 operandOperation (operand * left, operand * right,
1083                   int op, sym_link * type)
1084 {
1085   sym_link *let , *ret=NULL;
1086   operand *retval = (operand *) 0;
1087
1088   assert (isOperandLiteral (left));
1089   let = getSpec(operandType(left));
1090   if (right) {
1091     assert (isOperandLiteral (right));
1092     ret = getSpec(operandType(right));
1093   }
1094
1095   switch (op)
1096     {
1097     case '+':
1098       retval = operandFromValue (valCastLiteral (type,
1099                                                  operandLitValue (left) +
1100                                                  operandLitValue (right)));
1101       break;
1102     case '-':
1103       retval = operandFromValue (valCastLiteral (type,
1104                                                  operandLitValue (left) -
1105                                                  operandLitValue (right)));
1106       break;
1107     case '*':
1108       /*
1109       retval = operandFromValue (valCastLiteral (type,
1110                                                  operandLitValue (left) *
1111                                                  operandLitValue (right)));
1112       This could be all we've to do, but with gcc we've to take care about
1113       overflows. Two examples:
1114       ULONG_MAX * ULONG_MAX doesn't fit into a double, some of the least
1115       significant bits are lost (52 in fraction, 63 bits would be
1116       necessary to keep full precision).
1117       If the resulting double value is greater than ULONG_MAX (resp.
1118       USHRT_MAX, ...), then 0 will be assigned to v_ulong (resp. u_uint, ...)!
1119       */
1120
1121       /* if it is not a specifier then we can assume that */
1122       /* it will be an unsigned long                      */
1123       if (IS_INT (type) ||
1124           !IS_SPEC (type))
1125         {
1126           /* long is handled here, because it can overflow with double */
1127           if (IS_LONG (type) ||
1128               !IS_SPEC (type))
1129           /* signed and unsigned mul are the same, as long as the precision
1130              of the result isn't bigger than the precision of the operands. */
1131             retval = operandFromValue (valCastLiteral (type,
1132                      (TYPE_UDWORD) operandLitValue (left) *
1133                      (TYPE_UDWORD) operandLitValue (right)));
1134           else if (IS_UNSIGNED (type)) /* unsigned int */
1135             {
1136               /* unsigned int is handled here in order to detect overflow */
1137               TYPE_UDWORD ul = (TYPE_UWORD) operandLitValue (left) *
1138                                (TYPE_UWORD) operandLitValue (right);
1139
1140               retval = operandFromValue (valCastLiteral (type, (TYPE_UWORD) ul));
1141               if (ul != (TYPE_UWORD) ul)
1142                 werror (W_INT_OVL);
1143             }
1144           else /* signed int */
1145             {
1146               /* signed int is handled here in order to detect overflow */
1147               TYPE_DWORD l = (TYPE_WORD) operandLitValue (left) *
1148                              (TYPE_WORD) operandLitValue (right);
1149
1150               retval = operandFromValue (valCastLiteral (type, (TYPE_WORD) l));
1151               if (l != (TYPE_WORD) l)
1152                 werror (W_INT_OVL);
1153             }
1154         }
1155       else
1156         /* all others go here: */
1157         retval = operandFromValue (valCastLiteral (type,
1158                                                    operandLitValue (left) *
1159                                                    operandLitValue (right)));
1160       break;
1161     case '/':
1162       if ((TYPE_UDWORD) operandLitValue (right) == 0)
1163         {
1164           werror (E_DIVIDE_BY_ZERO);
1165           retval = right;
1166
1167         }
1168       else
1169         {
1170           if (IS_UNSIGNED (type))
1171             {
1172               SPEC_USIGN (let) = 1;
1173               SPEC_USIGN (ret) = 1;
1174               retval = operandFromValue (valCastLiteral (type,
1175                                         (TYPE_UDWORD) operandLitValue (left) /
1176                                         (TYPE_UDWORD) operandLitValue (right)));
1177             }
1178           else
1179             {
1180               retval = operandFromValue (valCastLiteral (type,
1181                                                      operandLitValue (left) /
1182                                                      operandLitValue (right)));
1183             }
1184         }
1185       break;
1186     case '%':
1187       if ((TYPE_UDWORD) operandLitValue (right) == 0)
1188         {
1189           werror (E_DIVIDE_BY_ZERO);
1190           retval = right;
1191         }
1192       else
1193         {
1194           if (IS_UNSIGNED (type))
1195             retval = operandFromLit ((TYPE_UDWORD) operandLitValue (left) %
1196                                      (TYPE_UDWORD) operandLitValue (right));
1197           else
1198             retval = operandFromLit ((TYPE_DWORD) operandLitValue (left) %
1199                                      (TYPE_DWORD) operandLitValue (right));
1200         }
1201       break;
1202     case LEFT_OP:
1203       /* The number of left shifts is always unsigned. Signed doesn't make
1204          sense here. Shifting by a negative number is impossible. */
1205       retval = operandFromValue (valCastLiteral (type,
1206                                  ((TYPE_UDWORD) operandLitValue (left) <<
1207                                   (TYPE_UDWORD) operandLitValue (right))));
1208       break;
1209     case RIGHT_OP:
1210       /* The number of right shifts is always unsigned. Signed doesn't make
1211          sense here. Shifting by a negative number is impossible. */
1212       if (IS_UNSIGNED(let))
1213         /* unsigned: logic shift right */
1214         retval = operandFromLit ((TYPE_UDWORD) operandLitValue (left) >>
1215                                  (TYPE_UDWORD) operandLitValue (right));
1216       else
1217         /* signed: arithmetic shift right */
1218         retval = operandFromLit ((TYPE_DWORD ) operandLitValue (left) >>
1219                                  (TYPE_UDWORD) operandLitValue (right));
1220       break;
1221     case EQ_OP:
1222       /* this op doesn't care about signedness */
1223       {
1224         TYPE_UDWORD l, r;
1225
1226         l = (TYPE_UDWORD) operandLitValue (left);
1227         if (IS_CHAR(OP_VALUE(left)->type))
1228           l &= 0xff;
1229         else if (!IS_LONG (OP_VALUE(left)->type))
1230           l &= 0xffff;
1231         r = (TYPE_UDWORD) operandLitValue (right);
1232         if (IS_CHAR(OP_VALUE(right)->type))
1233           r &= 0xff;
1234         else if (!IS_LONG (OP_VALUE(right)->type))
1235           r &= 0xffff;
1236         retval = operandFromLit (l == r);
1237       }
1238       break;
1239     case '<':
1240       retval = operandFromLit (operandLitValue (left) <
1241                                operandLitValue (right));
1242       break;
1243     case LE_OP:
1244       retval = operandFromLit (operandLitValue (left) <=
1245                                operandLitValue (right));
1246       break;
1247     case NE_OP:
1248       retval = operandFromLit (operandLitValue (left) !=
1249                                operandLitValue (right));
1250       break;
1251     case '>':
1252       retval = operandFromLit (operandLitValue (left) >
1253                                operandLitValue (right));
1254       break;
1255     case GE_OP:
1256       retval = operandFromLit (operandLitValue (left) >=
1257                                operandLitValue (right));
1258       break;
1259     case BITWISEAND:
1260       retval = operandFromValue (valCastLiteral (type,
1261                                                  (TYPE_UDWORD)operandLitValue(left) &
1262                                                  (TYPE_UDWORD)operandLitValue(right)));
1263       break;
1264     case '|':
1265       retval = operandFromValue (valCastLiteral (type,
1266                                                  (TYPE_UDWORD)operandLitValue(left) |
1267                                                  (TYPE_UDWORD)operandLitValue(right)));
1268       break;
1269     case '^':
1270       retval = operandFromValue (valCastLiteral (type,
1271                                                  (TYPE_UDWORD)operandLitValue(left) ^
1272                                                  (TYPE_UDWORD)operandLitValue(right)));
1273       break;
1274     case AND_OP:
1275       retval = operandFromLit (operandLitValue (left) &&
1276                                operandLitValue (right));
1277       break;
1278     case OR_OP:
1279       retval = operandFromLit (operandLitValue (left) ||
1280                                operandLitValue (right));
1281       break;
1282     case RRC:
1283       {
1284         TYPE_UDWORD i = (TYPE_UDWORD) operandLitValue (left);
1285
1286         retval = operandFromLit ((i >> (getSize (operandType (left)) * 8 - 1)) |
1287                                  (i << 1));
1288       }
1289       break;
1290     case RLC:
1291       {
1292         TYPE_UDWORD i = (TYPE_UDWORD) operandLitValue (left);
1293
1294         retval = operandFromLit ((i << (getSize (operandType (left)) * 8 - 1)) |
1295                                  (i >> 1));
1296       }
1297       break;
1298
1299     case UNARYMINUS:
1300       retval = operandFromValue (valCastLiteral (type,
1301                                                  -1 * operandLitValue (left)));
1302       break;
1303
1304     case '~':
1305       retval = operandFromLit (~((TYPE_UDWORD) operandLitValue (left)));
1306       break;
1307
1308     case '!':
1309       retval = operandFromLit (!operandLitValue (left));
1310       break;
1311
1312     default:
1313       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1314               " operandOperation invalid operator ");
1315       assert (0);
1316     }
1317
1318   return retval;
1319 }
1320
1321
1322 /*-----------------------------------------------------------------*/
1323 /* isOperandEqual - compares two operand & return 1 if they r =    */
1324 /*-----------------------------------------------------------------*/
1325 int
1326 isOperandEqual (operand * left, operand * right)
1327 {
1328   /* if the pointers are equal then they are equal */
1329   if (left == right)
1330     return 1;
1331
1332   /* if either of them null then false */
1333   if (!left || !right)
1334     return 0;
1335
1336   if (left->type != right->type)
1337     return 0;
1338
1339   if (IS_SYMOP (left) && IS_SYMOP (right))
1340     return left->key == right->key;
1341
1342   /* if types are the same */
1343   switch (left->type)
1344     {
1345     case SYMBOL:
1346       return isSymbolEqual (left->operand.symOperand,
1347                             right->operand.symOperand);
1348     case VALUE:
1349       return (floatFromVal (left->operand.valOperand) ==
1350               floatFromVal (right->operand.valOperand));
1351     case TYPE:
1352       if (compareType (left->operand.typeOperand,
1353                      right->operand.typeOperand) == 1)
1354         return 1;
1355     }
1356
1357   return 0;
1358 }
1359
1360 /*-------------------------------------------------------------------*/
1361 /* isiCodeEqual - compares two iCodes are equal, returns true if yes */
1362 /*-------------------------------------------------------------------*/
1363 int 
1364 isiCodeEqual (iCode * left, iCode * right)
1365 {
1366   /* if the same pointer */
1367   if (left == right)
1368     return 1;
1369
1370   /* if either of them null */
1371   if (!left || !right)
1372     return 0;
1373
1374   /* if operand are the same */
1375   if (left->op == right->op)
1376     {
1377
1378       /* compare all the elements depending on type */
1379       if (left->op != IFX)
1380         {
1381           if (!isOperandEqual (IC_LEFT (left), IC_LEFT (right)))
1382             return 0;
1383           if (!isOperandEqual (IC_RIGHT (left), IC_RIGHT (right)))
1384             return 0;
1385
1386         }
1387       else
1388         {
1389           if (!isOperandEqual (IC_COND (left), IC_COND (right)))
1390             return 0;
1391           if (!isSymbolEqual (IC_TRUE (left), IC_TRUE (right)))
1392             return 0;
1393           if (!isSymbolEqual (IC_FALSE (left), IC_FALSE (right)))
1394             return 0;
1395         }
1396       
1397       return 1;
1398     }
1399   return 0;
1400 }
1401
1402 /*-----------------------------------------------------------------*/
1403 /* newiTempFromOp - create a temp Operand with same attributes     */
1404 /*-----------------------------------------------------------------*/
1405 operand *
1406 newiTempFromOp (operand * op)
1407 {
1408   operand *nop;
1409
1410   if (!op)
1411     return NULL;
1412
1413   if (!IS_ITEMP (op))
1414     return op;
1415
1416   nop = newiTempOperand (operandType (op), TRUE);
1417   nop->isaddr = op->isaddr;
1418   nop->isvolatile = op->isvolatile;
1419   nop->isGlobal = op->isGlobal;
1420   nop->isLiteral = op->isLiteral;
1421   nop->usesDefs = op->usesDefs;
1422   nop->isParm = op->isParm;
1423   return nop;
1424 }
1425
1426 /*-----------------------------------------------------------------*/
1427 /* operand from operand - creates an operand holder for the type   */
1428 /*-----------------------------------------------------------------*/
1429 operand *
1430 operandFromOperand (operand * op)
1431 {
1432   operand *nop;
1433
1434   if (!op)
1435     return NULL;
1436   nop = newOperand ();
1437   nop->type = op->type;
1438   nop->isaddr = op->isaddr;
1439   nop->key = op->key;
1440   nop->isvolatile = op->isvolatile;
1441   nop->isGlobal = op->isGlobal;
1442   nop->isLiteral = op->isLiteral;
1443   nop->usesDefs = op->usesDefs;
1444   nop->isParm = op->isParm;
1445   
1446   switch (nop->type)
1447     {
1448     case SYMBOL:
1449       nop->operand.symOperand = op->operand.symOperand;
1450       break;
1451     case VALUE:
1452       nop->operand.valOperand = op->operand.valOperand;
1453       break;
1454     case TYPE:
1455       nop->operand.typeOperand = op->operand.typeOperand;
1456       break;
1457     }
1458
1459   return nop;
1460 }
1461
1462 /*-----------------------------------------------------------------*/
1463 /* opFromOpWithDU - makes a copy of the operand and DU chains      */
1464 /*-----------------------------------------------------------------*/
1465 operand *
1466 opFromOpWithDU (operand * op, bitVect * defs, bitVect * uses)
1467 {
1468   operand *nop = operandFromOperand (op);
1469
1470   if (nop->type == SYMBOL)
1471     {
1472       OP_SYMBOL (nop)->defs = bitVectCopy (defs);
1473       OP_SYMBOL (nop)->uses = bitVectCopy (uses);
1474     }
1475
1476   return nop;
1477 }
1478
1479 /*-----------------------------------------------------------------*/
1480 /* operandFromSymbol - creates an operand from a symbol            */
1481 /*-----------------------------------------------------------------*/
1482 operand *
1483 operandFromSymbol (symbol * sym)
1484 {
1485   operand *op;
1486   iCode *ic;
1487   int ok = 1;
1488   /* if the symbol's type is a literal */
1489   /* then it is an enumerator type     */
1490   if (IS_LITERAL (sym->etype) && SPEC_ENUM (sym->etype))
1491     return operandFromValue (valFromType (sym->etype));
1492
1493   if (!sym->key)
1494     sym->key = ++operandKey;
1495
1496   /* if this an implicit variable, means struct/union */
1497   /* member so just return it                         */
1498   if (sym->implicit || IS_FUNC (sym->type))
1499     {
1500       op = newOperand ();
1501       op->type = SYMBOL;
1502       op->operand.symOperand = sym;
1503       op->key = sym->key;
1504       op->isvolatile = isOperandVolatile (op, TRUE);
1505       op->isGlobal = isOperandGlobal (op);
1506       return op;
1507     }
1508
1509   /* under the following conditions create a
1510      register equivalent for a local symbol */
1511   if (sym->level && sym->etype && SPEC_OCLS (sym->etype) &&
1512       (IN_FARSPACE (SPEC_OCLS (sym->etype)) &&
1513       !TARGET_IS_HC08 &&
1514       (!(options.model == MODEL_FLAT24)) ) &&
1515       options.stackAuto == 0)
1516     ok = 0;
1517
1518   if (!IS_AGGREGATE (sym->type) &&      /* not an aggregate */
1519       !IS_FUNC (sym->type) &&   /* not a function   */
1520       !sym->_isparm &&          /* not a parameter  */
1521       sym->level &&             /* is a local variable */
1522       !sym->addrtaken &&        /* whose address has not been taken */
1523       !sym->reqv &&             /* does not already have a reg equivalence */
1524       !IS_VOLATILE (sym->etype) &&      /* not declared as volatile */
1525       !IS_STATIC (sym->etype) &&        /* and not declared static  */
1526       !sym->islbl &&            /* not a label */
1527       ok &&                     /* farspace check */
1528       !IS_BITVAR (sym->etype)   /* not a bit variable */
1529     )
1530     {                                   
1531
1532       /* we will use it after all optimizations
1533          and before liveRange calculation */
1534       sym->reqv = newiTempOperand (sym->type, 0);
1535       sym->reqv->key = sym->key;
1536       OP_SYMBOL (sym->reqv)->key = sym->key;
1537       OP_SYMBOL (sym->reqv)->isreqv = 1;
1538       OP_SYMBOL (sym->reqv)->islocal = 1;
1539       OP_SYMBOL (sym->reqv)->onStack = sym->onStack;
1540       SPIL_LOC (sym->reqv) = sym;
1541     }
1542
1543   if (!IS_AGGREGATE (sym->type))
1544     {
1545       op = newOperand ();
1546       op->type = SYMBOL;
1547       op->operand.symOperand = sym;
1548       op->isaddr = 1;
1549       op->key = sym->key;
1550       op->isvolatile = isOperandVolatile (op, TRUE);
1551       op->isGlobal = isOperandGlobal (op);
1552       op->isPtr = IS_PTR (operandType (op));
1553       op->isParm = sym->_isparm;
1554       return op;
1555     }
1556
1557   /* create :-                     */
1558   /*    itemp = &[_symbol]         */
1559
1560   ic = newiCode (ADDRESS_OF, newOperand (), NULL);
1561   IC_LEFT (ic)->type = SYMBOL;
1562   IC_LEFT (ic)->operand.symOperand = sym;
1563   IC_LEFT (ic)->key = sym->key;
1564   (IC_LEFT (ic))->isvolatile = isOperandVolatile (IC_LEFT (ic), TRUE);
1565   (IC_LEFT (ic))->isGlobal = isOperandGlobal (IC_LEFT (ic));
1566   IC_LEFT (ic)->isPtr = IS_PTR (operandType (IC_LEFT (ic)));
1567
1568   /* create result */
1569   IC_RESULT (ic) = newiTempOperand (sym->type, 0);
1570   if (IS_ARRAY (sym->type))
1571     {
1572       IC_RESULT (ic) = geniCodeArray2Ptr (IC_RESULT (ic));
1573       IC_RESULT (ic)->isaddr = 0;
1574     }
1575   else
1576     IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (sym->type));
1577
1578   ADDTOCHAIN (ic);
1579
1580   return IC_RESULT (ic);
1581 }
1582
1583 /*-----------------------------------------------------------------*/
1584 /* operandFromValue - creates an operand from value                */
1585 /*-----------------------------------------------------------------*/
1586 operand *
1587 operandFromValue (value * val)
1588 {
1589   operand *op;
1590
1591   /* if this is a symbol then do the symbol thing */
1592   if (val->sym)
1593     return operandFromSymbol (val->sym);
1594
1595   /* this is not a symbol */
1596   op = newOperand ();
1597   op->type = VALUE;
1598   op->operand.valOperand = val;
1599   op->isLiteral = isOperandLiteral (op);
1600   return op;
1601 }
1602
1603 /*-----------------------------------------------------------------*/
1604 /* operandFromLink - operand from typeChain                        */
1605 /*-----------------------------------------------------------------*/
1606 operand *
1607 operandFromLink (sym_link * type)
1608 {
1609   operand *op;
1610
1611   /* operand from sym_link */
1612   if (!type)
1613     return NULL;
1614
1615   op = newOperand ();
1616   op->type = TYPE;
1617   op->operand.typeOperand = copyLinkChain (type);
1618   return op;
1619 }
1620
1621 /*-----------------------------------------------------------------*/
1622 /* operandFromLit - makes an operand from a literal value          */
1623 /*-----------------------------------------------------------------*/
1624 operand *
1625 operandFromLit (double i)
1626 {
1627   return operandFromValue (valueFromLit (i));
1628 }
1629
1630 /*-----------------------------------------------------------------*/
1631 /* operandFromAst - creates an operand from an ast                 */
1632 /*-----------------------------------------------------------------*/
1633 operand *
1634 operandFromAst (ast * tree,int lvl)
1635 {
1636
1637   if (!tree)
1638     return NULL;
1639
1640   /* depending on type do */
1641   switch (tree->type)
1642     {
1643     case EX_OP:
1644       return ast2iCode (tree,lvl+1);
1645       break;
1646
1647     case EX_VALUE:
1648       return operandFromValue (tree->opval.val);
1649       break;
1650
1651     case EX_LINK:
1652       return operandFromLink (tree->opval.lnk);
1653       break;
1654
1655     default:
1656       assert (0);
1657     }
1658   
1659   /*  Just to keep the compiler happy */
1660   return (operand *) 0;
1661 }
1662
1663 /*-----------------------------------------------------------------*/
1664 /* setOperandType - sets the operand's type to the given type      */
1665 /*-----------------------------------------------------------------*/
1666 void 
1667 setOperandType (operand * op, sym_link * type)
1668 {
1669   /* depending on the type of operand */
1670   switch (op->type)
1671     {
1672
1673     case VALUE:
1674       op->operand.valOperand->etype =
1675         getSpec (op->operand.valOperand->type =
1676                  copyLinkChain (type));
1677       return;
1678
1679     case SYMBOL:
1680       if (op->operand.symOperand->isitmp)
1681         op->operand.symOperand->etype =
1682           getSpec (op->operand.symOperand->type =
1683                    copyLinkChain (type));
1684       else
1685         werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1686                 "attempt to modify type of source");
1687       return;
1688
1689     case TYPE:
1690       op->operand.typeOperand = copyLinkChain (type);
1691       return;
1692     }
1693
1694 }
1695 /*-----------------------------------------------------------------*/
1696 /* Get size in byte of ptr need to access an array                 */
1697 /*-----------------------------------------------------------------*/
1698 int
1699 getArraySizePtr (operand * op)
1700 {
1701   sym_link *ltype = operandType(op);
1702
1703   if(IS_PTR(ltype))
1704     {
1705       int size = getSize(ltype);
1706       return(IS_GENPTR(ltype)?(size-1):size);
1707     }
1708
1709   if(IS_ARRAY(ltype))
1710     {
1711       sym_link *letype = getSpec(ltype);
1712       switch (PTR_TYPE (SPEC_OCLS (letype)))
1713         {
1714         case IPOINTER:
1715         case PPOINTER:
1716         case POINTER:
1717           return (PTRSIZE);
1718         case EEPPOINTER:
1719         case FPOINTER:
1720         case CPOINTER:
1721         case FUNCTION:
1722           return (FPTRSIZE);
1723         case GPOINTER:
1724           return (GPTRSIZE-1);
1725
1726         default:
1727           return (FPTRSIZE);
1728         }
1729     }
1730   return (FPTRSIZE);
1731 }
1732
1733 /*-----------------------------------------------------------------*/
1734 /* perform "usual unary conversions"                               */
1735 /*-----------------------------------------------------------------*/
1736 #if 0
1737 static operand *
1738 usualUnaryConversions (operand * op)
1739 {
1740   if (IS_INTEGRAL (operandType (op)))
1741     {
1742       if (getSize (operandType (op)) < (unsigned int) INTSIZE)
1743         {
1744           /* Widen to int. */
1745           return geniCodeCast (INTTYPE, op, TRUE);
1746         }
1747     }
1748   return op;
1749 }
1750 #endif
1751
1752 /*-----------------------------------------------------------------*/
1753 /* perform "usual binary conversions"                              */
1754 /*-----------------------------------------------------------------*/
1755 static sym_link *
1756 usualBinaryConversions (operand ** op1, operand ** op2,
1757                         bool promoteCharToInt, bool isMul)
1758 {
1759   sym_link *ctype;
1760   sym_link *rtype = operandType (*op2);
1761   sym_link *ltype = operandType (*op1);
1762
1763   ctype = computeType (ltype, rtype, promoteCharToInt);
1764
1765   /* special for multiplication:
1766      This if for 'mul a,b', which takes two chars and returns an int */
1767   if (   isMul
1768       /* && promoteCharToInt    superfluous, already handled by computeType() */
1769       && IS_INT  (getSpec (ctype)))
1770     {
1771       sym_link *retype = getSpec (rtype);
1772       sym_link *letype = getSpec (ltype);
1773
1774       if (   IS_CHAR (letype)
1775           && IS_CHAR (retype)
1776           && IS_UNSIGNED (letype)
1777           && IS_UNSIGNED (retype))
1778         {
1779           return ctype;
1780         }
1781     }
1782   *op1 = geniCodeCast (ctype, *op1, TRUE);
1783   *op2 = geniCodeCast (ctype, *op2, TRUE);
1784
1785   return ctype;
1786 }
1787
1788 /*-----------------------------------------------------------------*/
1789 /* geniCodeValueAtAddress - generate intermeditate code for value  */
1790 /*                          at address                             */
1791 /*-----------------------------------------------------------------*/
1792 operand *
1793 geniCodeRValue (operand * op, bool force)
1794 {
1795   iCode *ic;
1796   sym_link *type = operandType (op);
1797   sym_link *etype = getSpec (type);
1798
1799   /* if this is an array & already */
1800   /* an address then return this   */
1801   if (IS_AGGREGATE (type) ||
1802       (IS_PTR (type) && !force && !op->isaddr))
1803     return operandFromOperand (op);
1804
1805   /* if this is not an address then must be */
1806   /* rvalue already so return this one      */
1807   if (!op->isaddr)
1808     return op;
1809
1810   /* if this is not a temp symbol then */
1811   if (!IS_ITEMP (op) &&
1812       !force &&
1813       !(IN_FARSPACE (SPEC_OCLS (etype)) && !TARGET_IS_HC08))
1814     {
1815       op = operandFromOperand (op);
1816       op->isaddr = 0;
1817       return op;
1818     }
1819
1820   if (IS_SPEC (type) &&
1821       IS_TRUE_SYMOP (op) &&
1822       (!(IN_FARSPACE (SPEC_OCLS (etype)) && !TARGET_IS_HC08) ||
1823       (options.model == MODEL_FLAT24) ))
1824     {
1825       op = operandFromOperand (op);
1826       op->isaddr = 0;
1827       return op;
1828     }
1829
1830   ic = newiCode (GET_VALUE_AT_ADDRESS, op, NULL);
1831   if (IS_PTR (type) && op->isaddr && force)
1832     type = type->next;
1833
1834   type = copyLinkChain (type);
1835
1836   IC_RESULT (ic) = newiTempOperand (type, 1);
1837   IC_RESULT (ic)->isaddr = 0;
1838
1839 /*     ic->supportRtn = ((IS_GENPTR(type) | op->isGptr) & op->isaddr); */
1840
1841   ADDTOCHAIN (ic);
1842
1843   return IC_RESULT (ic);
1844 }
1845
1846 /*-----------------------------------------------------------------*/
1847 /* geniCodeCast - changes the value from one type to another       */
1848 /*-----------------------------------------------------------------*/
1849 static operand *
1850 geniCodeCast (sym_link * type, operand * op, bool implicit)
1851 {
1852   iCode *ic;
1853   sym_link *optype;
1854   sym_link *opetype = getSpec (optype = operandType (op));
1855   sym_link *restype;
1856   int errors=0;
1857
1858   /* one of them has size zero then error */
1859   if (IS_VOID (optype))
1860     {
1861       werror (E_CAST_ZERO);
1862       return op;
1863     }
1864
1865   /* if the operand is already the desired type then do nothing */
1866   if (compareType (type, optype) == 1)
1867     return op;
1868
1869   /* if this is a literal then just change the type & return */
1870   if (IS_LITERAL (opetype) && op->type == VALUE && !IS_PTR (type) && !IS_PTR (optype))
1871     {
1872       return operandFromValue (valCastLiteral (type,
1873                                                operandLitValue (op)));
1874     }
1875
1876   /* if casting to/from pointers, do some checking */
1877   if (IS_PTR(type)) { // to a pointer
1878     if (!IS_PTR(optype) && !IS_FUNC(optype) && !IS_AGGREGATE(optype)) { // from a non pointer
1879       if (IS_INTEGRAL(optype)) {
1880         // maybe this is NULL, than it's ok.
1881         if (!(IS_LITERAL(optype) && (SPEC_CVAL(optype).v_ulong ==0))) {
1882           if (port->s.gptr_size > port->s.fptr_size && IS_GENPTR(type)) {
1883             // no way to set the storage
1884             if (IS_LITERAL(optype)) {
1885               werror(E_LITERAL_GENERIC);
1886               errors++;
1887             } else {
1888               werror(E_NONPTR2_GENPTR);
1889               errors++;
1890             }
1891           } else if (implicit) {
1892             werror(W_INTEGRAL2PTR_NOCAST);
1893             errors++;
1894           }
1895         }
1896       } else {
1897         // shouldn't do that with float, array or structure unless to void
1898         if (!IS_VOID(getSpec(type)) &&
1899             !(IS_CODEPTR(type) && IS_FUNC(type->next) && IS_FUNC(optype))) {
1900           werror(E_INCOMPAT_TYPES);
1901           errors++;
1902         }
1903       }
1904     } else { // from a pointer to a pointer
1905       if (port->s.gptr_size > port->s.fptr_size /*!TARGET_IS_Z80 && !TARGET_IS_GBZ80*/) {
1906         // if not a pointer to a function
1907         if (!(IS_CODEPTR(type) && IS_FUNC(type->next) && IS_FUNC(optype))) {
1908           if (implicit) { // if not to generic, they have to match
1909             if ((!IS_GENPTR(type) && (DCL_TYPE(optype) != DCL_TYPE(type)))) {
1910               werror(E_INCOMPAT_PTYPES);
1911               errors++;
1912             }
1913           }
1914         }
1915       }
1916     }
1917   } else { // to a non pointer
1918     if (IS_PTR(optype)) { // from a pointer
1919       if (implicit) { // sneaky
1920         if (IS_INTEGRAL(type)) {
1921           werror(W_PTR2INTEGRAL_NOCAST);
1922           errors++;
1923         } else { // shouldn't do that with float, array or structure
1924           werror(E_INCOMPAT_TYPES);
1925           errors++;
1926         }
1927       }
1928     }
1929   }
1930   if (errors) {
1931     printFromToType (optype, type);
1932   }
1933
1934   /* if they are the same size create an assignment */
1935   
1936   /* This seems very dangerous to me, since there are several */
1937   /* optimizations (for example, gcse) that don't notice the  */
1938   /* cast hidden in this assignement and may simplify an      */
1939   /* iCode to use the original (uncasted) operand.            */
1940   /* Unfortunately, other things break when this cast is      */
1941   /* made explicit. Need to fix this someday.                 */
1942   /* -- EEP, 2004/01/21                                       */
1943   if (getSize (type) == getSize (optype) &&
1944       !IS_BITFIELD (type) &&
1945       !IS_FLOAT (type) &&
1946       !IS_FLOAT (optype) &&
1947       ((IS_SPEC (type) && IS_SPEC (optype)) ||
1948        (!IS_SPEC (type) && !IS_SPEC (optype))))
1949     {
1950       ic = newiCode ('=', NULL, op);
1951       IC_RESULT (ic) = newiTempOperand (type, 0);
1952       SPIL_LOC (IC_RESULT (ic)) =
1953         (IS_TRUE_SYMOP (op) ? OP_SYMBOL (op) : NULL);
1954       IC_RESULT (ic)->isaddr = 0;
1955     }
1956   else
1957     {
1958       ic = newiCode (CAST, operandFromLink (type),
1959                      geniCodeRValue (op, FALSE));
1960
1961       IC_RESULT (ic) = newiTempOperand (type, 0);
1962     }
1963
1964   /* preserve the storage class & output class */
1965   /* of the original variable                  */
1966   restype = getSpec (operandType (IC_RESULT (ic)));
1967   if (!IS_LITERAL(opetype))
1968       SPEC_SCLS (restype) = SPEC_SCLS (opetype);
1969   SPEC_OCLS (restype) = SPEC_OCLS (opetype);
1970
1971   ADDTOCHAIN (ic);
1972   return IC_RESULT (ic);
1973 }
1974
1975 /*-----------------------------------------------------------------*/
1976 /* geniCodeLabel - will create a Label                             */
1977 /*-----------------------------------------------------------------*/
1978 void
1979 geniCodeLabel (symbol * label)
1980 {
1981   iCode *ic;
1982
1983   ic = newiCodeLabelGoto (LABEL, label);
1984   ADDTOCHAIN (ic);
1985 }
1986
1987 /*-----------------------------------------------------------------*/
1988 /* geniCodeGoto  - will create a Goto                              */
1989 /*-----------------------------------------------------------------*/
1990 void
1991 geniCodeGoto (symbol * label)
1992 {
1993   iCode *ic;
1994
1995   ic = newiCodeLabelGoto (GOTO, label);
1996   ADDTOCHAIN (ic);
1997 }
1998
1999 /*-----------------------------------------------------------------*/
2000 /* geniCodeMultiply - gen intermediate code for multiplication     */
2001 /*-----------------------------------------------------------------*/
2002 operand *
2003 geniCodeMultiply (operand * left, operand * right, int resultIsInt)
2004 {
2005   iCode *ic;
2006   int p2 = 0;
2007   sym_link *resType;
2008   LRTYPE;
2009
2010   /* if they are both literal then we know the result */
2011   if (IS_LITERAL (letype) && IS_LITERAL (retype))
2012     return operandFromValue (valMult (left->operand.valOperand,
2013                                       right->operand.valOperand));
2014
2015   if (IS_LITERAL(retype)) {
2016     p2 = powof2 ((TYPE_UDWORD) floatFromVal (right->operand.valOperand));
2017   }
2018
2019   resType = usualBinaryConversions (&left, &right, resultIsInt, TRUE);
2020 #if 1
2021   rtype = operandType (right);
2022   retype = getSpec (rtype);
2023   ltype = operandType (left);
2024   letype = getSpec (ltype);
2025 #endif
2026
2027   /* if the right is a literal & power of 2 */
2028   /* then make it a left shift              */
2029   /* code generated for 1 byte * 1 byte literal = 2 bytes result is more
2030      efficient in most cases than 2 bytes result = 2 bytes << literal
2031      if port has 1 byte muldiv */
2032   if (p2 && !IS_FLOAT (letype) &&
2033       !((resultIsInt) && (getSize (resType) != getSize (ltype)) &&
2034         (port->support.muldiv == 1)))
2035     {
2036       if ((resultIsInt) && (getSize (resType) != getSize (ltype)))
2037         {
2038           /* LEFT_OP need same size for left and result, */
2039           left = geniCodeCast (resType, left, TRUE);
2040           ltype = operandType (left);
2041         }
2042       ic = newiCode (LEFT_OP, left, operandFromLit (p2)); /* left shift */
2043     }
2044   else
2045     {
2046       ic = newiCode ('*', left, right);         /* normal multiplication */
2047       /* if the size left or right > 1 then support routine */
2048       if (getSize (ltype) > 1 || getSize (rtype) > 1)
2049         ic->supportRtn = 1;
2050
2051     }
2052   IC_RESULT (ic) = newiTempOperand (resType, 1);
2053
2054   ADDTOCHAIN (ic);
2055   return IC_RESULT (ic);
2056 }
2057
2058 /*-----------------------------------------------------------------*/
2059 /* geniCodeDivision - gen intermediate code for division           */
2060 /*-----------------------------------------------------------------*/
2061 operand *
2062 geniCodeDivision (operand * left, operand * right)
2063 {
2064   iCode *ic;
2065   int p2 = 0;
2066   sym_link *resType;
2067   sym_link *rtype = operandType (right);
2068   sym_link *retype = getSpec (rtype);
2069   sym_link *ltype = operandType (left);
2070   sym_link *letype = getSpec (ltype);
2071
2072   resType = usualBinaryConversions (&left, &right,
2073               (IS_UNSIGNED (retype) && IS_UNSIGNED (letype)) ? FALSE : TRUE,
2074               FALSE);
2075
2076   /* if the right is a literal & power of 2
2077      and left is unsigned then make it a
2078      right shift */
2079   if (IS_LITERAL (retype) &&
2080       !IS_FLOAT (letype) &&
2081       IS_UNSIGNED(letype) &&
2082       (p2 = powof2 ((TYPE_UDWORD)
2083                     floatFromVal (right->operand.valOperand)))) {
2084     ic = newiCode (RIGHT_OP, left, operandFromLit (p2)); /* right shift */
2085   }
2086   else
2087     {
2088       ic = newiCode ('/', left, right);         /* normal division */
2089       /* if the size left or right > 1 then support routine */
2090       if (getSize (ltype) > 1 || getSize (rtype) > 1)
2091         ic->supportRtn = 1;
2092     }
2093   IC_RESULT (ic) = newiTempOperand (resType, 0);
2094
2095   ADDTOCHAIN (ic);
2096   return IC_RESULT (ic);
2097 }
2098 /*-----------------------------------------------------------------*/
2099 /* geniCodeModulus  - gen intermediate code for modulus            */
2100 /*-----------------------------------------------------------------*/
2101 operand *
2102 geniCodeModulus (operand * left, operand * right)
2103 {
2104   iCode *ic;
2105   sym_link *resType;
2106   LRTYPE;
2107
2108   /* if they are both literal then we know the result */
2109   if (IS_LITERAL (letype) && IS_LITERAL (retype))
2110     return operandFromValue (valMod (left->operand.valOperand,
2111                                      right->operand.valOperand));
2112
2113   resType = usualBinaryConversions (&left, &right,
2114               (IS_UNSIGNED (retype) && IS_UNSIGNED (letype)) ? FALSE : TRUE,
2115               FALSE);
2116
2117   /* now they are the same size */
2118   ic = newiCode ('%', left, right);
2119
2120   /* if the size left or right > 1 then support routine */
2121   if (getSize (ltype) > 1 || getSize (rtype) > 1)
2122     ic->supportRtn = 1;
2123   IC_RESULT (ic) = newiTempOperand (resType, 0);
2124
2125   ADDTOCHAIN (ic);
2126   return IC_RESULT (ic);
2127 }
2128
2129 /*-----------------------------------------------------------------*/
2130 /* geniCodePtrPtrSubtract - subtracts pointer from pointer         */
2131 /*-----------------------------------------------------------------*/
2132 operand *
2133 geniCodePtrPtrSubtract (operand * left, operand * right)
2134 {
2135   iCode *ic;
2136   operand *result;
2137   LRTYPE;
2138
2139   /* if they are both literals then */
2140   if (IS_LITERAL (letype) && IS_LITERAL (retype))
2141     {
2142       result = operandFromValue (valMinus (left->operand.valOperand,
2143                                            right->operand.valOperand));
2144       goto subtractExit;
2145     }
2146
2147   ic = newiCode ('-', left, right);
2148
2149   IC_RESULT (ic) = result = newiTempOperand (newIntLink (), 1);
2150   ADDTOCHAIN (ic);
2151
2152 subtractExit:
2153   if (IS_VOID(ltype->next) || IS_VOID(rtype->next)) {
2154     return result;
2155   }
2156   
2157   // should we really do this? is this ANSI?
2158   return geniCodeDivision (result,
2159                            operandFromLit (getSize (ltype->next)));
2160 }
2161
2162 /*-----------------------------------------------------------------*/
2163 /* geniCodeSubtract - generates code for subtraction               */
2164 /*-----------------------------------------------------------------*/
2165 operand *
2166 geniCodeSubtract (operand * left, operand * right)
2167 {
2168   iCode *ic;
2169   int isarray = 0;
2170   sym_link *resType;
2171   LRTYPE;
2172
2173   /* if they both pointers then */
2174   if ((IS_PTR (ltype) || IS_ARRAY (ltype)) &&
2175       (IS_PTR (rtype) || IS_ARRAY (rtype)))
2176     return geniCodePtrPtrSubtract (left, right);
2177
2178   /* if they are both literal then we know the result */
2179   if (IS_LITERAL (letype) && IS_LITERAL (retype)
2180       && left->isLiteral && right->isLiteral)
2181     return operandFromValue (valMinus (left->operand.valOperand,
2182                                        right->operand.valOperand));
2183
2184   /* if left is an array or pointer */
2185   if (IS_PTR (ltype) || IS_ARRAY (ltype))
2186     {
2187       isarray = left->isaddr;
2188       right = geniCodeMultiply (right,
2189                                 operandFromLit (getSize (ltype->next)), (getArraySizePtr(left) >= INTSIZE));
2190       resType = copyLinkChain (IS_ARRAY (ltype) ? ltype->next : ltype);
2191     }
2192   else
2193     {                           /* make them the same size */
2194       resType = usualBinaryConversions (&left, &right, FALSE, FALSE);
2195     }
2196
2197   ic = newiCode ('-', left, right);
2198
2199   IC_RESULT (ic) = newiTempOperand (resType, 1);
2200   IC_RESULT (ic)->isaddr = (isarray ? 1 : 0);
2201
2202   /* if left or right is a float */
2203   if (IS_FLOAT (ltype) || IS_FLOAT (rtype))
2204     ic->supportRtn = 1;
2205
2206   ADDTOCHAIN (ic);
2207   return IC_RESULT (ic);
2208 }
2209
2210 /*-----------------------------------------------------------------*/
2211 /* geniCodeAdd - generates iCode for addition                      */
2212 /*-----------------------------------------------------------------*/
2213 operand *
2214 geniCodeAdd (operand * left, operand * right, int lvl)
2215 {
2216   iCode *ic;
2217   sym_link *resType;
2218   operand *size;
2219   int isarray = 0;
2220   bool indexUnsigned;
2221   LRTYPE;
2222
2223   /* if the right side is LITERAL zero */
2224   /* return the left side              */
2225   if (IS_LITERAL (retype) && right->isLiteral && !floatFromVal (valFromType (rtype)))
2226     return left;
2227
2228   /* if left is literal zero return right */
2229   if (IS_LITERAL (letype) && left->isLiteral && !floatFromVal (valFromType (ltype)))
2230     return right;
2231
2232   /* if left is a pointer then size */
2233   if (IS_PTR (ltype) || IS_ARRAY(ltype))
2234     {
2235       isarray = left->isaddr;
2236       // there is no need to multiply with 1
2237       if (getSize (ltype->next) != 1)
2238         {
2239           size  = operandFromLit (getSize (ltype->next));
2240           SPEC_USIGN (getSpec (operandType (size))) = 1;
2241           indexUnsigned = IS_UNSIGNED (getSpec (operandType (right)));
2242           right = geniCodeMultiply (right, size, (getArraySizePtr(left) >= INTSIZE));
2243           /* Even if right is a 'unsigned char',
2244              the result will be a 'signed int' due to the promotion rules.
2245              It doesn't make sense when accessing arrays, so let's fix it here: */
2246           if (indexUnsigned)
2247             SPEC_USIGN (getSpec (operandType (right))) = 1;
2248         }
2249       resType = copyLinkChain (ltype);
2250     }
2251   else
2252     { // make them the same size
2253       resType = usualBinaryConversions (&left, &right, FALSE, FALSE);
2254     }
2255
2256   /* if they are both literals then we know */
2257   if (IS_LITERAL (letype) && IS_LITERAL (retype)
2258       && left->isLiteral && right->isLiteral)
2259     return operandFromValue (valPlus (valFromType (ltype),
2260                                       valFromType (rtype)));
2261
2262   ic = newiCode ('+', left, right);
2263
2264   IC_RESULT (ic) = newiTempOperand (resType, 1);
2265   IC_RESULT (ic)->isaddr = (isarray ? 1 : 0);
2266
2267   /* if left or right is a float then support
2268      routine */
2269   if (IS_FLOAT (ltype) || IS_FLOAT (rtype))
2270     ic->supportRtn = 1;
2271
2272   ADDTOCHAIN (ic);
2273
2274   return IC_RESULT (ic);
2275
2276 }
2277
2278 /*-----------------------------------------------------------------*/
2279 /* aggrToPtr - changes an aggregate to pointer to an aggregate     */
2280 /*-----------------------------------------------------------------*/
2281 sym_link *
2282 aggrToPtr (sym_link * type, bool force)
2283 {
2284   sym_link *etype;
2285   sym_link *ptype;
2286
2287   if (IS_PTR (type) && !force)
2288     return type;
2289
2290   etype = getSpec (type);
2291   ptype = newLink (DECLARATOR);
2292
2293   ptype->next = type;
2294
2295   /* set the pointer depending on the storage class */
2296   DCL_TYPE (ptype) = PTR_TYPE (SPEC_OCLS (etype));
2297   return ptype;
2298 }
2299
2300 /*-----------------------------------------------------------------*/
2301 /* geniCodeArray2Ptr - array to pointer                            */
2302 /*-----------------------------------------------------------------*/
2303 static operand *
2304 geniCodeArray2Ptr (operand * op)
2305 {
2306   sym_link *optype = operandType (op);
2307   sym_link *opetype = getSpec (optype);
2308
2309   /* set the pointer depending on the storage class */
2310   DCL_TYPE (optype) = PTR_TYPE (SPEC_OCLS (opetype));
2311
2312   op->isaddr = 0;
2313   return op;
2314 }
2315
2316
2317 /*-----------------------------------------------------------------*/
2318 /* geniCodeArray - array access                                    */
2319 /*-----------------------------------------------------------------*/
2320 static operand *
2321 geniCodeArray (operand * left, operand * right, int lvl)
2322 {
2323   iCode *ic;
2324   operand *size;
2325   sym_link *ltype = operandType (left);
2326   bool indexUnsigned;
2327
2328   if (IS_PTR (ltype))
2329     {
2330       if (IS_PTR (ltype->next) && left->isaddr)
2331         {
2332           left = geniCodeRValue (left, FALSE);
2333         }
2334
2335       return geniCodeDerefPtr (geniCodeAdd (left, right, lvl), lvl);
2336     }
2337   size = operandFromLit (getSize (ltype->next));
2338   SPEC_USIGN (getSpec (operandType (size))) = 1;
2339   indexUnsigned = IS_UNSIGNED (getSpec (operandType (right)));
2340   right = geniCodeMultiply (right, size, (getArraySizePtr(left) >= INTSIZE));
2341   /* Even if right is a 'unsigned char', the result will be a 'signed int' due to the promotion rules.
2342      It doesn't make sense when accessing arrays, so let's fix it here: */
2343   if (indexUnsigned)
2344     SPEC_USIGN (getSpec (operandType (right))) = 1;
2345   /* we can check for limits here */
2346   /* already done in SDCCast.c
2347   if (isOperandLiteral (right) &&
2348       IS_ARRAY (ltype) &&
2349       DCL_ELEM (ltype) &&
2350       (operandLitValue (right) / getSize (ltype->next)) >= DCL_ELEM (ltype))
2351     {
2352       werror (W_IDX_OUT_OF_BOUNDS,
2353               (int) operandLitValue (right) / getSize (ltype->next),
2354               DCL_ELEM (ltype));
2355     }
2356   */
2357
2358   ic = newiCode ('+', left, right);
2359
2360   IC_RESULT (ic) = newiTempOperand (((IS_PTR (ltype) &&
2361                                       !IS_AGGREGATE (ltype->next) &&
2362                                       !IS_PTR (ltype->next))
2363                                      ? ltype : ltype->next), 0);
2364
2365   IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (ltype->next));
2366   ADDTOCHAIN (ic);
2367
2368   return IC_RESULT (ic);
2369 }
2370
2371 /*-----------------------------------------------------------------*/
2372 /* geniCodeStruct - generates intermediate code for structures     */
2373 /*-----------------------------------------------------------------*/
2374 operand *
2375 geniCodeStruct (operand * left, operand * right, bool islval)
2376 {
2377   iCode *ic;
2378   sym_link *type = operandType (left);
2379   sym_link *etype = getSpec (type);
2380   sym_link *retype;
2381   symbol *element = getStructElement (SPEC_STRUCT (etype),
2382                                       right->operand.symOperand);
2383
2384   wassert(IS_SYMOP(right));
2385     
2386   /* add the offset */
2387   ic = newiCode ('+', left, operandFromLit (element->offset));
2388
2389   IC_RESULT (ic) = newiTempOperand (element->type, 0);
2390
2391   /* preserve the storage & output class of the struct */
2392   /* as well as the volatile attribute */
2393   retype = getSpec (operandType (IC_RESULT (ic)));
2394   SPEC_SCLS (retype) = SPEC_SCLS (etype);
2395   SPEC_OCLS (retype) = SPEC_OCLS (etype);
2396   SPEC_VOLATILE (retype) |= SPEC_VOLATILE (etype);
2397   SPEC_CONST (retype) |= SPEC_CONST (etype);
2398   
2399   if (IS_PTR (element->type))
2400     setOperandType (IC_RESULT (ic), aggrToPtr (operandType (IC_RESULT (ic)), TRUE));
2401   
2402   IC_RESULT (ic)->isaddr = (!IS_AGGREGATE (element->type));
2403
2404   ADDTOCHAIN (ic);
2405   return (islval ? IC_RESULT (ic) : geniCodeRValue (IC_RESULT (ic), TRUE));
2406 }
2407
2408 /*-----------------------------------------------------------------*/
2409 /* geniCodePostInc - generate int code for Post increment          */
2410 /*-----------------------------------------------------------------*/
2411 operand *
2412 geniCodePostInc (operand * op)
2413 {
2414   iCode *ic;
2415   operand *rOp;
2416   sym_link *optype = operandType (op);
2417   operand *result;
2418   operand *rv = (IS_ITEMP (op) ?
2419                  geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2420                  op);
2421   sym_link *rvtype = operandType (rv);
2422   int size = 0;
2423
2424   /* if this is not an address we have trouble */
2425   if (!op->isaddr)
2426     {
2427       werror (E_LVALUE_REQUIRED, "++");
2428       return op;
2429     }
2430
2431   rOp = newiTempOperand (rvtype, 0);
2432   OP_SYMBOL(rOp)->noSpilLoc = 1;
2433
2434   if (IS_ITEMP (rv))
2435     OP_SYMBOL(rv)->noSpilLoc = 1;
2436
2437   geniCodeAssign (rOp, rv, 0);
2438
2439   size = (IS_PTR (rvtype) ? getSize (rvtype->next) : 1);
2440   if (IS_FLOAT (rvtype))
2441     ic = newiCode ('+', rv, operandFromValue (constFloatVal ("1.0")));
2442   else
2443     ic = newiCode ('+', rv, operandFromLit (size));
2444
2445   IC_RESULT (ic) = result = newiTempOperand (rvtype, 0);
2446   ADDTOCHAIN (ic);
2447
2448   geniCodeAssign (op, result, 0);
2449
2450   return rOp;
2451
2452 }
2453
2454 /*-----------------------------------------------------------------*/
2455 /* geniCodePreInc - generate code for preIncrement                 */
2456 /*-----------------------------------------------------------------*/
2457 operand *
2458 geniCodePreInc (operand * op, bool lvalue)
2459 {
2460   iCode *ic;
2461   sym_link *optype = operandType (op);
2462   operand *rop = (IS_ITEMP (op) ?
2463                   geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2464                   op);
2465   sym_link *roptype = operandType (rop);
2466   operand *result;
2467   int size = 0;
2468
2469   if (!op->isaddr)
2470     {
2471       werror (E_LVALUE_REQUIRED, "++");
2472       return op;
2473     }
2474
2475
2476   size = (IS_PTR (roptype) ? getSize (roptype->next) : 1);
2477   if (IS_FLOAT (roptype))
2478     ic = newiCode ('+', rop, operandFromValue (constFloatVal ("1.0")));
2479   else
2480     ic = newiCode ('+', rop, operandFromLit (size));
2481   IC_RESULT (ic) = result = newiTempOperand (roptype, 0);
2482   ADDTOCHAIN (ic);
2483
2484   (void) geniCodeAssign (op, result, 0);
2485   if (lvalue || IS_TRUE_SYMOP (op))
2486     return op;
2487   else
2488     return result;
2489 }
2490
2491 /*-----------------------------------------------------------------*/
2492 /* geniCodePostDec - generates code for Post decrement             */
2493 /*-----------------------------------------------------------------*/
2494 operand *
2495 geniCodePostDec (operand * op)
2496 {
2497   iCode *ic;
2498   operand *rOp;
2499   sym_link *optype = operandType (op);
2500   operand *result;
2501   operand *rv = (IS_ITEMP (op) ?
2502                  geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2503                  op);
2504   sym_link *rvtype = operandType (rv);
2505   int size = 0;
2506
2507   /* if this is not an address we have trouble */
2508   if (!op->isaddr)
2509     {
2510       werror (E_LVALUE_REQUIRED, "--");
2511       return op;
2512     }
2513
2514   rOp = newiTempOperand (rvtype, 0);
2515   OP_SYMBOL(rOp)->noSpilLoc = 1;
2516
2517   if (IS_ITEMP (rv))
2518     OP_SYMBOL(rv)->noSpilLoc = 1;
2519
2520   geniCodeAssign (rOp, rv, 0);
2521
2522   size = (IS_PTR (rvtype) ? getSize (rvtype->next) : 1);
2523   if (IS_FLOAT (rvtype))
2524     ic = newiCode ('-', rv, operandFromValue (constFloatVal ("1.0")));
2525   else
2526     ic = newiCode ('-', rv, operandFromLit (size));
2527
2528   IC_RESULT (ic) = result = newiTempOperand (rvtype, 0);
2529   ADDTOCHAIN (ic);
2530
2531   geniCodeAssign (op, result, 0);
2532
2533   return rOp;
2534
2535 }
2536
2537 /*-----------------------------------------------------------------*/
2538 /* geniCodePreDec - generate code for pre  decrement               */
2539 /*-----------------------------------------------------------------*/
2540 operand *
2541 geniCodePreDec (operand * op, bool lvalue)
2542 {
2543   iCode *ic;
2544   sym_link *optype = operandType (op);
2545   operand *rop = (IS_ITEMP (op) ?
2546                   geniCodeRValue (op, (IS_PTR (optype) ? TRUE : FALSE)) :
2547                   op);
2548   sym_link *roptype = operandType (rop);
2549   operand *result;
2550   int size = 0;
2551
2552   if (!op->isaddr)
2553     {
2554       werror (E_LVALUE_REQUIRED, "--");
2555       return op;
2556     }
2557
2558
2559   size = (IS_PTR (roptype) ? getSize (roptype->next) : 1);
2560   if (IS_FLOAT (roptype))
2561     ic = newiCode ('-', rop, operandFromValue (constFloatVal ("1.0")));
2562   else
2563     ic = newiCode ('-', rop, operandFromLit (size));
2564   IC_RESULT (ic) = result = newiTempOperand (roptype, 0);
2565   ADDTOCHAIN (ic);
2566
2567   (void) geniCodeAssign (op, result, 0);
2568   if (lvalue || IS_TRUE_SYMOP (op))
2569     return op;
2570   else
2571     return result;
2572 }
2573
2574
2575 /*-----------------------------------------------------------------*/
2576 /* geniCodeBitwise - gen int code for bitWise  operators           */
2577 /*-----------------------------------------------------------------*/
2578 operand *
2579 geniCodeBitwise (operand * left, operand * right,
2580                  int oper, sym_link * resType)
2581 {
2582   iCode *ic;
2583
2584   left = geniCodeCast (resType, left, TRUE);
2585   right = geniCodeCast (resType, right, TRUE);
2586
2587   ic = newiCode (oper, left, right);
2588   IC_RESULT (ic) = newiTempOperand (resType, 0);
2589
2590   ADDTOCHAIN (ic);
2591   return IC_RESULT (ic);
2592 }
2593
2594 /*-----------------------------------------------------------------*/
2595 /* geniCodeAddressOf - gens icode for '&' address of operator      */
2596 /*-----------------------------------------------------------------*/
2597 operand *
2598 geniCodeAddressOf (operand * op)
2599 {
2600   iCode *ic;
2601   sym_link *p;
2602   sym_link *optype = operandType (op);
2603   sym_link *opetype = getSpec (optype);
2604
2605   if (IS_ITEMP (op) && op->isaddr && IS_PTR (optype))
2606     {
2607       op = operandFromOperand (op);
2608       op->isaddr = 0;
2609       return op;
2610     }
2611   
2612   /* lvalue check already done in decorateType */
2613   /* this must be a lvalue */
2614 /*     if (!op->isaddr && !IS_AGGREGATE(optype)) { */
2615 /*  werror (E_LVALUE_REQUIRED,"&"); */
2616 /*  return op; */
2617 /*     } */
2618
2619   p = newLink (DECLARATOR);
2620
2621   /* set the pointer depending on the storage class */
2622   DCL_TYPE (p) = PTR_TYPE (SPEC_OCLS (opetype));
2623
2624   p->next = copyLinkChain (optype);
2625
2626   /* if already a temp */
2627   if (IS_ITEMP (op))
2628     {
2629       setOperandType (op, p);
2630       op->isaddr = 0;
2631       return op;
2632     }
2633
2634   /* other wise make this of the type coming in */
2635   ic = newiCode (ADDRESS_OF, op, NULL);
2636   IC_RESULT (ic) = newiTempOperand (p, 1);
2637   IC_RESULT (ic)->isaddr = 0;
2638   ADDTOCHAIN (ic);
2639   return IC_RESULT (ic);
2640 }
2641 /*-----------------------------------------------------------------*/
2642 /* setOClass - sets the output class depending on the pointer type */
2643 /*-----------------------------------------------------------------*/
2644 void 
2645 setOClass (sym_link * ptr, sym_link * spec)
2646 {
2647   switch (DCL_TYPE (ptr))
2648     {
2649     case POINTER:
2650       SPEC_OCLS (spec) = data;
2651       break;
2652
2653     case GPOINTER:
2654       SPEC_OCLS (spec) = generic;
2655       break;
2656
2657     case FPOINTER:
2658       SPEC_OCLS (spec) = xdata;
2659       break;
2660
2661     case CPOINTER:
2662       SPEC_OCLS (spec) = code;
2663       break;
2664
2665     case IPOINTER:
2666       SPEC_OCLS (spec) = idata;
2667       break;
2668
2669     case PPOINTER:
2670       SPEC_OCLS (spec) = xstack;
2671       break;
2672
2673     case EEPPOINTER:
2674       SPEC_OCLS (spec) = eeprom;
2675       break;
2676
2677     default:
2678       break;
2679
2680     }
2681 }
2682
2683 /*-----------------------------------------------------------------*/
2684 /* geniCodeDerefPtr - dereference pointer with '*'                 */
2685 /*-----------------------------------------------------------------*/
2686 operand *
2687 geniCodeDerefPtr (operand * op,int lvl)
2688 {
2689   sym_link *rtype, *retype;
2690   sym_link *optype = operandType (op);
2691
2692   // if this is an array then array access
2693   if (IS_ARRAY (optype)) {
2694     // don't worry, this will be optimized out later
2695     return geniCodeArray (op, operandFromLit (0), lvl);
2696   }
2697
2698   // just in case someone screws up
2699   wassert (IS_PTR (optype));
2700
2701   if (IS_TRUE_SYMOP (op))
2702     {
2703       op->isaddr = 1;
2704       op = geniCodeRValue (op, TRUE);
2705     }
2706
2707   /* now get rid of the pointer part */
2708   if (isLvaluereq(lvl) && IS_ITEMP (op))
2709     {
2710       retype = getSpec (rtype = copyLinkChain (optype));
2711     }
2712   else
2713     {
2714       retype = getSpec (rtype = copyLinkChain (optype->next));
2715       /* outputclass needs 2b updated */
2716       setOClass (optype, retype);
2717     }
2718   
2719   op->isGptr = IS_GENPTR (optype);
2720
2721   op->isaddr = (IS_PTR (rtype) ||
2722                 IS_STRUCT (rtype) ||
2723                 IS_INT (rtype) ||
2724                 IS_CHAR (rtype) ||
2725                 IS_FLOAT (rtype));
2726
2727   if (!isLvaluereq(lvl))
2728     op = geniCodeRValue (op, TRUE);
2729
2730   setOperandType (op, rtype);
2731
2732   return op;
2733 }
2734
2735 /*-----------------------------------------------------------------*/
2736 /* geniCodeUnaryMinus - does a unary minus of the operand          */
2737 /*-----------------------------------------------------------------*/
2738 operand *
2739 geniCodeUnaryMinus (operand * op)
2740 {
2741   iCode *ic;
2742   sym_link *optype = operandType (op);
2743
2744   if (IS_LITERAL (optype))
2745     return operandFromLit (-floatFromVal (op->operand.valOperand));
2746
2747   ic = newiCode (UNARYMINUS, op, NULL);
2748   IC_RESULT (ic) = newiTempOperand (optype, 0);
2749   ADDTOCHAIN (ic);
2750   return IC_RESULT (ic);
2751 }
2752
2753 /*-----------------------------------------------------------------*/
2754 /* geniCodeLeftShift - gen i code for left shift                   */
2755 /*-----------------------------------------------------------------*/
2756 operand *
2757 geniCodeLeftShift (operand * left, operand * right)
2758 {
2759   iCode *ic;
2760
2761   ic = newiCode (LEFT_OP, left, right);
2762   IC_RESULT (ic) = newiTempOperand (operandType (left), 0);
2763   ADDTOCHAIN (ic);
2764   return IC_RESULT (ic);
2765 }
2766
2767 /*-----------------------------------------------------------------*/
2768 /* geniCodeRightShift - gen i code for right shift                 */
2769 /*-----------------------------------------------------------------*/
2770 operand *
2771 geniCodeRightShift (operand * left, operand * right)
2772 {
2773   iCode *ic;
2774
2775   ic = newiCode (RIGHT_OP, left, right);
2776   IC_RESULT (ic) = newiTempOperand (operandType (left), 0);
2777   ADDTOCHAIN (ic);
2778   return IC_RESULT (ic);
2779 }
2780
2781 /*-----------------------------------------------------------------*/
2782 /* geniCodeLogic- logic code                                       */
2783 /*-----------------------------------------------------------------*/
2784 operand *
2785 geniCodeLogic (operand * left, operand * right, int op)
2786 {
2787   iCode *ic;
2788   sym_link *ctype;
2789   sym_link *rtype = operandType (right);
2790   sym_link *ltype = operandType (left);
2791
2792   /* left is integral type and right is literal then
2793      check if the literal value is within bounds */
2794   if (IS_INTEGRAL (ltype) && IS_VALOP (right) && IS_LITERAL (rtype))
2795     {
2796       checkConstantRange(ltype,
2797                          OP_VALUE(right), "compare operation", 1);
2798     }
2799
2800   /* if one operand is a pointer and the other is a literal generic void pointer,
2801      change the type of the literal generic void pointer to match the other pointer */
2802   if (IS_GENPTR (ltype) && IS_VOID (ltype->next) && IS_ITEMP (left)
2803       && IS_PTR (rtype) && !IS_GENPTR(rtype))
2804     {
2805       /* find left's definition */
2806       ic = (iCode *) setFirstItem (iCodeChain);
2807       while (ic)
2808         {
2809           if (((ic->op == CAST) || (ic->op == '='))
2810               && isOperandEqual(left, IC_RESULT (ic)))
2811             break;
2812           else
2813             ic = setNextItem (iCodeChain);
2814         }
2815       /* if casting literal to generic pointer, then cast to rtype instead */
2816       if (ic && (ic->op == CAST) && isOperandLiteral(IC_RIGHT (ic))) 
2817         {
2818           left = operandFromValue (valCastLiteral (rtype, operandLitValue (IC_RIGHT (ic))));
2819           ltype = operandType(left);
2820         }
2821     }
2822   if (IS_GENPTR (rtype) && IS_VOID (rtype->next) && IS_ITEMP (right)
2823       && IS_PTR (ltype) && !IS_GENPTR(ltype))
2824     {
2825       /* find right's definition */
2826       ic = (iCode *) setFirstItem (iCodeChain);
2827       while (ic)
2828         {
2829           if (((ic->op == CAST) || (ic->op == '='))
2830               && isOperandEqual(right, IC_RESULT (ic)))
2831             break;
2832           else
2833             ic = setNextItem (iCodeChain);
2834         }
2835       /* if casting literal to generic pointer, then cast to rtype instead */
2836       if (ic && (ic->op == CAST) && isOperandLiteral(IC_RIGHT (ic))) 
2837         {
2838           right = operandFromValue (valCastLiteral (ltype, operandLitValue (IC_RIGHT (ic))));
2839           rtype = operandType(right);
2840         }
2841     }
2842
2843   ctype = usualBinaryConversions (&left, &right, FALSE, FALSE);
2844
2845   ic = newiCode (op, left, right);
2846   IC_RESULT (ic) = newiTempOperand (newCharLink (), 1);
2847
2848   /* if comparing float
2849      and not a '==' || '!=' || '&&' || '||' (these
2850      will be inlined */
2851   if (IS_FLOAT(ctype) &&
2852       op != EQ_OP &&
2853       op != NE_OP &&
2854       op != AND_OP &&
2855       op != OR_OP)
2856     ic->supportRtn = 1;
2857
2858   ADDTOCHAIN (ic);
2859   return IC_RESULT (ic);
2860 }
2861
2862 /*-----------------------------------------------------------------*/
2863 /* geniCodeUnary - for a a generic unary operation                 */
2864 /*-----------------------------------------------------------------*/
2865 operand *
2866 geniCodeUnary (operand * op, int oper)
2867 {
2868   iCode *ic = newiCode (oper, op, NULL);
2869
2870   IC_RESULT (ic) = newiTempOperand (operandType (op), 0);
2871   ADDTOCHAIN (ic);
2872   return IC_RESULT (ic);
2873 }
2874
2875 /*-----------------------------------------------------------------*/
2876 /* geniCodeConditional - geniCode for '?' ':' operation            */
2877 /*-----------------------------------------------------------------*/
2878 operand *
2879 geniCodeConditional (ast * tree,int lvl)
2880 {
2881   iCode *ic;
2882   symbol *falseLabel = newiTempLabel (NULL);
2883   symbol *exitLabel = newiTempLabel (NULL);
2884   operand *cond = ast2iCode (tree->left,lvl+1);
2885   operand *true, *false, *result;
2886
2887   ic = newiCodeCondition (geniCodeRValue (cond, FALSE),
2888                           NULL, falseLabel);
2889   ADDTOCHAIN (ic);
2890
2891   true = ast2iCode (tree->right->left,lvl+1);
2892
2893   /* move the value to a new Operand */
2894   result = newiTempOperand (tree->right->ftype, 0);
2895   geniCodeAssign (result, geniCodeRValue (true, FALSE), 0);
2896
2897   /* generate an unconditional goto */
2898   geniCodeGoto (exitLabel);
2899
2900   /* now for the right side */
2901   geniCodeLabel (falseLabel);
2902
2903   false = ast2iCode (tree->right->right,lvl+1);
2904   geniCodeAssign (result, geniCodeRValue (false, FALSE), 0);
2905
2906   /* create the exit label */
2907   geniCodeLabel (exitLabel);
2908
2909   return result;
2910 }
2911
2912 /*-----------------------------------------------------------------*/
2913 /* geniCodeAssign - generate code for assignment                   */
2914 /*-----------------------------------------------------------------*/
2915 operand *
2916 geniCodeAssign (operand * left, operand * right, int nosupdate)
2917 {
2918   iCode *ic;
2919   sym_link *ltype = operandType (left);
2920   sym_link *rtype = operandType (right);
2921
2922   if (!left->isaddr && !IS_ITEMP (left))
2923     {
2924       werror (E_LVALUE_REQUIRED, "assignment");
2925       return left;
2926     }
2927
2928   /* left is integral type and right is literal then
2929      check if the literal value is within bounds */
2930   if (IS_INTEGRAL (ltype) && right->type == VALUE && IS_LITERAL (rtype))
2931     {
2932       checkConstantRange(ltype, 
2933                          OP_VALUE(right), "= operation", 0);
2934     }
2935
2936   /* if the left & right type don't exactly match */
2937   /* if pointer set then make sure the check is
2938      done with the type & not the pointer */
2939   /* then cast rights type to left */
2940
2941   /* first check the type for pointer assignement */
2942   if (left->isaddr && IS_PTR (ltype) && IS_ITEMP (left) &&
2943       compareType (ltype, rtype) <= 0)
2944     {
2945       if (compareType (ltype->next, rtype) < 0)
2946         right = geniCodeCast (ltype->next, right, TRUE);
2947     }
2948   else if (compareType (ltype, rtype) < 0)
2949     right = geniCodeCast (ltype, right, TRUE);
2950
2951   /* If left is a true symbol & ! volatile
2952      create an assignment to temporary for
2953      the right & then assign this temporary
2954      to the symbol. This is SSA (static single
2955      assignment). Isn't it simple and folks have
2956      published mountains of paper on it */
2957   if (IS_TRUE_SYMOP (left) &&
2958       !isOperandVolatile (left, FALSE) &&
2959       isOperandGlobal (left))
2960     {
2961       symbol *sym = NULL;
2962
2963       if (IS_TRUE_SYMOP (right))
2964         sym = OP_SYMBOL (right);
2965       ic = newiCode ('=', NULL, right);
2966       IC_RESULT (ic) = right = newiTempOperand (ltype, 0);
2967       SPIL_LOC (right) = sym;
2968       ADDTOCHAIN (ic);
2969     }
2970
2971   ic = newiCode ('=', NULL, right);
2972   IC_RESULT (ic) = left;
2973   ADDTOCHAIN (ic);
2974
2975   /* if left isgptr flag is set then support
2976      routine will be required */
2977   if (left->isGptr)
2978     ic->supportRtn = 1;
2979
2980   ic->nosupdate = nosupdate;
2981   return left;
2982 }
2983
2984 /*-----------------------------------------------------------------*/
2985 /* geniCodeDummyRead - generate code for dummy read                */
2986 /*-----------------------------------------------------------------*/
2987 static void
2988 geniCodeDummyRead (operand * op)
2989 {
2990   iCode *ic;
2991   sym_link *type = operandType (op);
2992
2993   if (!IS_VOLATILE(type))
2994     return;
2995     
2996   ic = newiCode (DUMMY_READ_VOLATILE, NULL, op);
2997   ADDTOCHAIN (ic);
2998
2999   ic->nosupdate = 1;
3000 }
3001
3002 /*-----------------------------------------------------------------*/
3003 /* geniCodeSEParms - generate code for side effecting fcalls       */
3004 /*-----------------------------------------------------------------*/
3005 static void 
3006 geniCodeSEParms (ast * parms,int lvl)
3007 {
3008   if (!parms)
3009     return;
3010
3011   if (parms->type == EX_OP && parms->opval.op == PARAM)
3012     {
3013       geniCodeSEParms (parms->left,lvl);
3014       geniCodeSEParms (parms->right,lvl);
3015       return;
3016     }
3017
3018   /* hack don't like this but too lazy to think of
3019      something better */
3020   if (IS_ADDRESS_OF_OP (parms))
3021     parms->left->lvalue = 1;
3022
3023   if (IS_CAST_OP (parms) &&
3024       IS_PTR (parms->ftype) &&
3025       IS_ADDRESS_OF_OP (parms->right))
3026     parms->right->left->lvalue = 1;
3027
3028   parms->opval.oprnd = 
3029     geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
3030                 
3031   parms->type = EX_OPERAND;
3032   AST_ARGREG(parms) = parms->etype ? SPEC_ARGREG(parms->etype) :
3033                 SPEC_ARGREG(parms->ftype);
3034 }
3035
3036 /*-----------------------------------------------------------------*/
3037 /* geniCodeParms - generates parameters                            */
3038 /*-----------------------------------------------------------------*/
3039 value *
3040 geniCodeParms (ast * parms, value *argVals, int *stack, 
3041                sym_link * ftype, int lvl)
3042 {
3043   iCode *ic;
3044   operand *pval;
3045
3046   if (!parms)
3047     return argVals;
3048
3049   if (argVals==NULL) {
3050     // first argument
3051     argVals = FUNC_ARGS (ftype);
3052   }
3053
3054   /* if this is a param node then do the left & right */
3055   if (parms->type == EX_OP && parms->opval.op == PARAM)
3056     {
3057       argVals=geniCodeParms (parms->left, argVals, stack, ftype, lvl);
3058       argVals=geniCodeParms (parms->right, argVals, stack, ftype, lvl);
3059       return argVals;
3060     }
3061
3062   /* get the parameter value */
3063   if (parms->type == EX_OPERAND)
3064     pval = parms->opval.oprnd;
3065   else
3066     {
3067       /* maybe this else should go away ?? */
3068       /* hack don't like this but too lazy to think of
3069          something better */
3070       if (IS_ADDRESS_OF_OP (parms))
3071         parms->left->lvalue = 1;
3072
3073       if (IS_CAST_OP (parms) &&
3074           IS_PTR (parms->ftype) &&
3075           IS_ADDRESS_OF_OP (parms->right))
3076         parms->right->left->lvalue = 1;
3077
3078       pval = geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
3079     }
3080
3081   /* if register parm then make it a send */
3082   if ((IS_REGPARM (parms->etype) && !IFFUNC_HASVARARGS(ftype)) ||
3083       IFFUNC_ISBUILTIN(ftype))
3084     {
3085       ic = newiCode (SEND, pval, NULL);
3086       ic->argreg = SPEC_ARGREG(parms->etype);
3087       ic->builtinSEND = FUNC_ISBUILTIN(ftype);
3088       ADDTOCHAIN (ic);
3089     }
3090   else
3091     {
3092       /* now decide whether to push or assign */
3093       if (!(options.stackAuto || IFFUNC_ISREENT (ftype)))
3094         {
3095
3096           /* assign */
3097           operand *top = operandFromSymbol (argVals->sym);
3098           /* clear useDef and other bitVectors */
3099           OP_USES(top)=OP_DEFS(top)=OP_SYMBOL(top)->clashes = NULL;
3100           geniCodeAssign (top, pval, 1);
3101         }
3102       else
3103         {
3104           sym_link *p = operandType (pval);
3105           /* push */
3106           ic = newiCode (IPUSH, pval, NULL);
3107           ic->parmPush = 1;
3108           /* update the stack adjustment */
3109           *stack += getSize (IS_AGGREGATE (p) ? aggrToPtr (p, FALSE) : p);
3110           ADDTOCHAIN (ic);
3111         }
3112     }
3113
3114   argVals=argVals->next;
3115   return argVals;
3116 }
3117
3118 /*-----------------------------------------------------------------*/
3119 /* geniCodeCall - generates temp code for calling                  */
3120 /*-----------------------------------------------------------------*/
3121 operand *
3122 geniCodeCall (operand * left, ast * parms,int lvl)
3123 {
3124   iCode *ic;
3125   operand *result;
3126   sym_link *type, *etype;
3127   sym_link *ftype;
3128   int stack = 0;
3129
3130   if (!IS_FUNC(OP_SYMBOL(left)->type) && 
3131       !IS_CODEPTR(OP_SYMBOL(left)->type)) {
3132     werror (E_FUNCTION_EXPECTED);
3133     return operandFromValue(valueFromLit(0));
3134   }
3135
3136   /* take care of parameters with side-effecting
3137      function calls in them, this is required to take care
3138      of overlaying function parameters */
3139   geniCodeSEParms (parms,lvl);
3140
3141   ftype = operandType (left);
3142   if (IS_CODEPTR (ftype))
3143     ftype = ftype->next;
3144     
3145   /* first the parameters */
3146   geniCodeParms (parms, NULL, &stack, ftype, lvl);
3147
3148   /* now call : if symbol then pcall */
3149   if (IS_OP_POINTER (left) || IS_ITEMP(left)) {
3150     ic = newiCode (PCALL, left, NULL);
3151   } else {
3152     ic = newiCode (CALL, left, NULL);
3153   }
3154
3155   type = copyLinkChain (ftype->next);
3156   etype = getSpec (type);
3157   SPEC_EXTR (etype) = 0;
3158   IC_RESULT (ic) = result = newiTempOperand (type, 1);
3159
3160   ADDTOCHAIN (ic);
3161
3162   /* stack adjustment after call */
3163   ic->parmBytes = stack;
3164
3165   return result;
3166 }
3167
3168 /*-----------------------------------------------------------------*/
3169 /* geniCodeReceive - generate intermediate code for "receive"      */
3170 /*-----------------------------------------------------------------*/
3171 static void 
3172 geniCodeReceive (value * args)
3173 {
3174   /* for all arguments that are passed in registers */
3175   while (args)
3176     {
3177       int first = 1;
3178       if (IS_REGPARM (args->etype))
3179         {
3180           operand *opr = operandFromValue (args);
3181           operand *opl;
3182           symbol *sym = OP_SYMBOL (opr);
3183           iCode *ic;
3184
3185           /* we will use it after all optimizations
3186              and before liveRange calculation */
3187           if (!sym->addrtaken && !IS_VOLATILE (sym->etype))
3188             {
3189
3190               if ((IN_FARSPACE (SPEC_OCLS (sym->etype)) && !TARGET_IS_HC08) &&
3191                   options.stackAuto == 0 &&
3192                   (!(options.model == MODEL_FLAT24)) )
3193                 {
3194                 }
3195               else
3196                 {
3197                   opl = newiTempOperand (args->type, 0);
3198                   sym->reqv = opl;
3199                   sym->reqv->key = sym->key;
3200                   OP_SYMBOL (sym->reqv)->key = sym->key;
3201                   OP_SYMBOL (sym->reqv)->isreqv = 1;
3202                   OP_SYMBOL (sym->reqv)->islocal = 0;
3203                   SPIL_LOC (sym->reqv) = sym;
3204                 }
3205             }
3206
3207           ic = newiCode (RECEIVE, NULL, NULL);    
3208           ic->argreg = SPEC_ARGREG(args->etype);
3209           if (first) {
3210               currFunc->recvSize = getSize (sym->type);
3211               first = 0;
3212           }
3213           IC_RESULT (ic) = opr;
3214           ADDTOCHAIN (ic);
3215         }
3216
3217       args = args->next;
3218     }
3219 }
3220
3221 /*-----------------------------------------------------------------*/
3222 /* geniCodeFunctionBody - create the function body                 */
3223 /*-----------------------------------------------------------------*/
3224 void 
3225 geniCodeFunctionBody (ast * tree,int lvl)
3226 {
3227   iCode *ic;
3228   operand *func;
3229   sym_link *fetype;
3230   int savelineno;
3231
3232   /* reset the auto generation */
3233   /* numbers */
3234   iTempNum = 0;
3235   iTempLblNum = 0;
3236   operandKey = 0;
3237   iCodeKey = 0;
3238   func = ast2iCode (tree->left,lvl+1);
3239   fetype = getSpec (operandType (func));
3240
3241   savelineno = lineno;
3242   lineno = OP_SYMBOL (func)->lineDef;
3243   /* create an entry label */
3244   geniCodeLabel (entryLabel);
3245   lineno = savelineno;
3246
3247   /* create a proc icode */
3248   ic = newiCode (FUNCTION, func, NULL);
3249   lineno=ic->lineno = OP_SYMBOL (func)->lineDef;
3250
3251   ADDTOCHAIN (ic);
3252
3253   /* for all parameters that are passed
3254      on registers add a "receive" */
3255   geniCodeReceive (tree->values.args);
3256
3257   /* generate code for the body */
3258   ast2iCode (tree->right,lvl+1);
3259
3260   /* create a label for return */
3261   geniCodeLabel (returnLabel);
3262
3263   /* now generate the end proc */
3264   ic = newiCode (ENDFUNCTION, func, NULL);
3265   ADDTOCHAIN (ic);
3266   return;
3267 }
3268
3269 /*-----------------------------------------------------------------*/
3270 /* geniCodeReturn - gen icode for 'return' statement               */
3271 /*-----------------------------------------------------------------*/
3272 void 
3273 geniCodeReturn (operand * op)
3274 {
3275   iCode *ic;
3276
3277   /* if the operand is present force an rvalue */
3278   if (op)
3279     op = geniCodeRValue (op, FALSE);
3280
3281   ic = newiCode (RETURN, op, NULL);
3282   ADDTOCHAIN (ic);
3283 }
3284
3285 /*-----------------------------------------------------------------*/
3286 /* geniCodeIfx - generates code for extended if statement          */
3287 /*-----------------------------------------------------------------*/
3288 void 
3289 geniCodeIfx (ast * tree,int lvl)
3290 {
3291   iCode *ic;
3292   operand *condition = ast2iCode (tree->left,lvl+1);
3293   sym_link *cetype;
3294
3295   /* if condition is null then exit */
3296   if (!condition)
3297     goto exit;
3298   else
3299     condition = geniCodeRValue (condition, FALSE);
3300
3301   cetype = getSpec (operandType (condition));
3302   /* if the condition is a literal */
3303   if (IS_LITERAL (cetype))
3304     {
3305       if (floatFromVal (condition->operand.valOperand))
3306         {
3307           if (tree->trueLabel)
3308             geniCodeGoto (tree->trueLabel);
3309           else
3310             assert (0);
3311         }
3312       else
3313         {
3314           if (tree->falseLabel)
3315             geniCodeGoto (tree->falseLabel);
3316           else
3317             assert (0);
3318         }
3319       goto exit;
3320     }
3321
3322   if (tree->trueLabel)
3323     {
3324       ic = newiCodeCondition (condition,
3325                               tree->trueLabel,
3326                               NULL);
3327       ADDTOCHAIN (ic);
3328
3329       if (tree->falseLabel)
3330         geniCodeGoto (tree->falseLabel);
3331     }
3332   else
3333     {
3334       ic = newiCodeCondition (condition,
3335                               NULL,
3336                               tree->falseLabel);
3337       ADDTOCHAIN (ic);
3338     }
3339
3340 exit:
3341   ast2iCode (tree->right,lvl+1);
3342 }
3343
3344 /*-----------------------------------------------------------------*/
3345 /* geniCodeJumpTable - tries to create a jump table for switch     */
3346 /*-----------------------------------------------------------------*/
3347 int 
3348 geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
3349 {
3350   int min = 0, max = 0, t, cnt = 0;
3351   value *vch;
3352   iCode *ic;
3353   operand *boundary;
3354   symbol *falseLabel;
3355   set *labels = NULL;
3356   int needRangeCheck = !optimize.noJTabBoundary
3357                        || tree->values.switchVals.swDefault;
3358
3359   if (!tree || !caseVals)
3360     return 0;
3361
3362   /* the criteria for creating a jump table is */
3363   /* all integer numbers between the maximum & minimum must */
3364   /* be present , the maximum value should not exceed 255 */
3365   min = max = (int) floatFromVal (vch = caseVals);
3366   SNPRINTF (buffer, sizeof(buffer), 
3367             "_case_%d_%d",
3368            tree->values.switchVals.swNum,
3369            min);
3370   addSet (&labels, newiTempLabel (buffer));
3371
3372   /* if there is only one case value then no need */
3373   if (!(vch = vch->next))
3374     return 0;
3375
3376   while (vch)
3377     {
3378       if (((t = (int) floatFromVal (vch)) - max) != 1)
3379         return 0;
3380       SNPRINTF (buffer, sizeof(buffer), 
3381                 "_case_%d_%d",
3382                tree->values.switchVals.swNum,
3383                t);
3384       addSet (&labels, newiTempLabel (buffer));
3385       max = t;
3386       cnt++;
3387       vch = vch->next;
3388     }
3389
3390   /* if the number of case statements <= 2 then */
3391   /* it is not economical to create the jump table */
3392   /* since two compares are needed for boundary conditions */
3393   if ((needRangeCheck && cnt <= 2) || max > (255 / 3))
3394     return 0;
3395
3396   if (tree->values.switchVals.swDefault)
3397     {
3398         SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
3399     }
3400   else
3401     {
3402         SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
3403     }
3404     
3405
3406   falseLabel = newiTempLabel (buffer);
3407
3408   /* so we can create a jumptable */
3409   /* first we rule out the boundary conditions */
3410   /* if only optimization says so */
3411   if (needRangeCheck)
3412     {
3413       sym_link *cetype = getSpec (operandType (cond));
3414       /* no need to check the lower bound if
3415          the condition is unsigned & minimum value is zero */
3416       if (!(min == 0 && IS_UNSIGNED (cetype)))
3417         {
3418           boundary = geniCodeLogic (cond, operandFromLit (min), '<');
3419           ic = newiCodeCondition (boundary, falseLabel, NULL);
3420           ADDTOCHAIN (ic);
3421         }
3422
3423       /* now for upper bounds */
3424       boundary = geniCodeLogic (cond, operandFromLit (max), '>');
3425       ic = newiCodeCondition (boundary, falseLabel, NULL);
3426       ADDTOCHAIN (ic);
3427     }
3428
3429   /* if the min is not zero then we no make it zero */
3430   if (min)
3431     {
3432       cond = geniCodeSubtract (cond, operandFromLit (min));
3433       if (!IS_LITERAL(getSpec(operandType(cond))))
3434         setOperandType (cond, UCHARTYPE);
3435     }
3436
3437   /* now create the jumptable */
3438   ic = newiCode (JUMPTABLE, NULL, NULL);
3439   IC_JTCOND (ic) = cond;
3440   IC_JTLABELS (ic) = labels;
3441   ADDTOCHAIN (ic);
3442   return 1;
3443 }
3444
3445 /*-----------------------------------------------------------------*/
3446 /* geniCodeSwitch - changes a switch to a if statement             */
3447 /*-----------------------------------------------------------------*/
3448 void
3449 geniCodeSwitch (ast * tree,int lvl)
3450 {
3451   iCode *ic;
3452   operand *cond = geniCodeRValue (ast2iCode (tree->left,lvl+1), FALSE);
3453   value *caseVals = tree->values.switchVals.swVals;
3454   symbol *trueLabel, *falseLabel;
3455       
3456   /* If the condition is a literal, then just jump to the */
3457   /* appropriate case label. */
3458   if (IS_LITERAL(getSpec(operandType(cond))))
3459     {
3460       int switchVal, caseVal;
3461       
3462       switchVal = (int) floatFromVal (cond->operand.valOperand);
3463       while (caseVals)
3464         {
3465           caseVal = (int) floatFromVal (caseVals);
3466           if (caseVal == switchVal)
3467             {
3468               SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
3469                         tree->values.switchVals.swNum, caseVal);
3470               trueLabel = newiTempLabel (buffer);
3471               geniCodeGoto (trueLabel);
3472               goto jumpTable;
3473             }
3474           caseVals = caseVals->next;
3475         }
3476       goto defaultOrBreak;
3477     }
3478
3479   /* if we can make this a jump table */
3480   if (geniCodeJumpTable (cond, caseVals, tree))
3481     goto jumpTable;             /* no need for the comparison */
3482
3483   /* for the cases defined do */
3484   while (caseVals)
3485     {
3486
3487       operand *compare = geniCodeLogic (cond,
3488                                         operandFromValue (caseVals),
3489                                         EQ_OP);
3490
3491       SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
3492                tree->values.switchVals.swNum,
3493                (int) floatFromVal (caseVals));
3494       trueLabel = newiTempLabel (buffer);
3495
3496       ic = newiCodeCondition (compare, trueLabel, NULL);
3497       ADDTOCHAIN (ic);
3498       caseVals = caseVals->next;
3499     }
3500
3501
3502 defaultOrBreak:
3503   /* if default is present then goto break else break */
3504   if (tree->values.switchVals.swDefault)
3505     {
3506         SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
3507     }
3508   else
3509     {
3510         SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
3511     }
3512
3513   falseLabel = newiTempLabel (buffer);
3514   geniCodeGoto (falseLabel);
3515
3516 jumpTable:
3517   ast2iCode (tree->right,lvl+1);
3518 }
3519
3520 /*-----------------------------------------------------------------*/
3521 /* geniCodeInline - intermediate code for inline assembler         */
3522 /*-----------------------------------------------------------------*/
3523 static void 
3524 geniCodeInline (ast * tree)
3525 {
3526   iCode *ic;
3527
3528   ic = newiCode (INLINEASM, NULL, NULL);
3529   IC_INLINE (ic) = tree->values.inlineasm;
3530   ADDTOCHAIN (ic);
3531 }
3532
3533 /*-----------------------------------------------------------------*/
3534 /* geniCodeArrayInit - intermediate code for array initializer     */
3535 /*-----------------------------------------------------------------*/
3536 static void
3537 geniCodeArrayInit (ast * tree, operand *array)
3538 {
3539   iCode *ic;
3540
3541   if (!getenv("TRY_THE_NEW_INITIALIZER")) {
3542     ic = newiCode (ARRAYINIT, array, NULL);
3543     IC_ARRAYILIST (ic) = tree->values.constlist;
3544   } else {
3545     operand *left=newOperand(), *right=newOperand();
3546     left->type=right->type=SYMBOL;
3547     OP_SYMBOL(left)=AST_SYMBOL(tree->left);
3548     OP_SYMBOL(right)=AST_SYMBOL(tree->right);
3549     ic = newiCode (ARRAYINIT, left, right);
3550   }
3551   ADDTOCHAIN (ic);
3552 }
3553         
3554 /*-----------------------------------------------------------------*/
3555 /* geniCodeCritical - intermediate code for a critical statement   */
3556 /*-----------------------------------------------------------------*/
3557 static void 
3558 geniCodeCritical (ast *tree, int lvl)
3559 {
3560   iCode *ic;
3561   operand *op = NULL;
3562
3563   /* If op is NULL, the original interrupt state will saved on */
3564   /* the stack. Otherwise, it will be saved in op. */
3565   
3566   /* Generate a save of the current interrupt state & disabled */
3567   ic = newiCode (CRITICAL, NULL, NULL);
3568   IC_RESULT (ic) = op;
3569   ADDTOCHAIN (ic);
3570   
3571   /* Generate the critical code sequence */
3572   if (tree->left && tree->left->type == EX_VALUE)
3573     geniCodeDummyRead (ast2iCode (tree->left,lvl+1));
3574   else
3575     ast2iCode (tree->left,lvl+1);
3576   
3577   /* Generate a restore of the original interrupt state */
3578   ic = newiCode (ENDCRITICAL, NULL, op);
3579   ADDTOCHAIN (ic);
3580 }
3581
3582 /*-----------------------------------------------------------------*/
3583 /* Stuff used in ast2iCode to modify geniCodeDerefPtr in some      */
3584 /* particular case. Ie : assigning or dereferencing array or ptr   */
3585 /*-----------------------------------------------------------------*/
3586 set * lvaluereqSet = NULL;
3587 typedef struct lvalItem
3588   {
3589     int req;
3590     int lvl;
3591   }
3592 lvalItem;
3593
3594 /*-----------------------------------------------------------------*/
3595 /* addLvaluereq - add a flag for lvalreq for current ast level     */
3596 /*-----------------------------------------------------------------*/
3597 void addLvaluereq(int lvl)
3598 {
3599   lvalItem * lpItem = (lvalItem *)Safe_alloc ( sizeof (lvalItem));
3600   lpItem->req=1;
3601   lpItem->lvl=lvl;
3602   addSetHead(&lvaluereqSet,lpItem);
3603
3604 }
3605 /*-----------------------------------------------------------------*/
3606 /* delLvaluereq - del a flag for lvalreq for current ast level     */
3607 /*-----------------------------------------------------------------*/
3608 void delLvaluereq()
3609 {
3610   lvalItem * lpItem;
3611   lpItem = getSet(&lvaluereqSet);
3612   if(lpItem) Safe_free(lpItem);
3613 }
3614 /*-----------------------------------------------------------------*/
3615 /* clearLvaluereq - clear lvalreq flag                             */
3616 /*-----------------------------------------------------------------*/
3617 void clearLvaluereq()
3618 {
3619   lvalItem * lpItem;
3620   lpItem = peekSet(lvaluereqSet);
3621   if(lpItem) lpItem->req = 0;
3622 }
3623 /*-----------------------------------------------------------------*/
3624 /* getLvaluereq - get the last lvalreq level                       */
3625 /*-----------------------------------------------------------------*/
3626 int getLvaluereqLvl()
3627 {
3628   lvalItem * lpItem;
3629   lpItem = peekSet(lvaluereqSet);
3630   if(lpItem) return lpItem->lvl;
3631   return 0;
3632 }
3633 /*-----------------------------------------------------------------*/
3634 /* isLvaluereq - is lvalreq valid for this level ?                 */
3635 /*-----------------------------------------------------------------*/
3636 int isLvaluereq(int lvl)
3637 {
3638   lvalItem * lpItem;
3639   lpItem = peekSet(lvaluereqSet);
3640   if(lpItem) return ((lpItem->req)&&(lvl <= (lpItem->lvl+1)));
3641   return 0;
3642 }
3643
3644 /*-----------------------------------------------------------------*/
3645 /* ast2iCode - creates an icodeList from an ast                    */
3646 /*-----------------------------------------------------------------*/
3647 operand *
3648 ast2iCode (ast * tree,int lvl)
3649 {
3650   operand *left = NULL;
3651   operand *right = NULL;
3652   if (!tree)
3653     return NULL;
3654
3655   /* set the global variables for filename & line number */
3656   if (tree->filename)
3657     filename = tree->filename;
3658   if (tree->lineno)
3659     lineno = tree->lineno;
3660   if (tree->block)
3661     block = tree->block;
3662   if (tree->level)
3663     scopeLevel = tree->level;
3664   if (tree->seqPoint)
3665     seqPoint = tree->seqPoint;
3666
3667   if (tree->type == EX_VALUE)
3668     return operandFromValue (tree->opval.val);
3669
3670   if (tree->type == EX_LINK)
3671     return operandFromLink (tree->opval.lnk);
3672
3673   /* if we find a nullop */
3674   if (tree->type == EX_OP &&
3675      (tree->opval.op == NULLOP ||
3676      tree->opval.op == BLOCK))
3677     {
3678       if (tree->left && tree->left->type == EX_VALUE)
3679         geniCodeDummyRead (ast2iCode (tree->left,lvl+1));
3680       else
3681         ast2iCode (tree->left,lvl+1);
3682       if (tree->right && tree->right->type == EX_VALUE)
3683         geniCodeDummyRead (ast2iCode (tree->right,lvl+1));
3684       else
3685         ast2iCode (tree->right,lvl+1);
3686       return NULL;
3687     }
3688
3689   /* special cases for not evaluating */
3690   if (tree->opval.op != ':' &&
3691       tree->opval.op != '?' &&
3692       tree->opval.op != CALL &&
3693       tree->opval.op != IFX &&
3694       tree->opval.op != LABEL &&
3695       tree->opval.op != GOTO &&
3696       tree->opval.op != SWITCH &&
3697       tree->opval.op != FUNCTION &&
3698       tree->opval.op != INLINEASM &&
3699       tree->opval.op != CRITICAL)
3700     {
3701
3702         if (IS_ASSIGN_OP (tree->opval.op) ||
3703            IS_DEREF_OP (tree) ||
3704            (tree->opval.op == '&' && !tree->right) ||
3705            tree->opval.op == PTR_OP)
3706           {
3707             addLvaluereq(lvl);
3708             if ((IS_ARRAY_OP (tree->left) && IS_ARRAY_OP (tree->left->left)) ||
3709                (IS_DEREF_OP (tree) && IS_ARRAY_OP (tree->left)))
3710               clearLvaluereq();
3711
3712             left = operandFromAst (tree->left,lvl);
3713             delLvaluereq();
3714             if (IS_DEREF_OP (tree) && IS_DEREF_OP (tree->left))
3715               left = geniCodeRValue (left, TRUE);
3716           }
3717         else
3718           {
3719             left = operandFromAst (tree->left,lvl);
3720           }
3721         if (tree->opval.op == INC_OP ||
3722             tree->opval.op == DEC_OP)
3723           {
3724             addLvaluereq(lvl);
3725             right = operandFromAst (tree->right,lvl);
3726             delLvaluereq();
3727           }
3728         else
3729           {
3730             right = operandFromAst (tree->right,lvl);
3731           }
3732       }
3733
3734   /* now depending on the type of operand */
3735   /* this will be a biggy                 */
3736   switch (tree->opval.op)
3737     {
3738
3739     case '[':                   /* array operation */
3740       {
3741         //sym_link *ltype = operandType (left);
3742         //left = geniCodeRValue (left, IS_PTR (ltype->next) ? TRUE : FALSE);
3743         left = geniCodeRValue (left, FALSE);
3744         right = geniCodeRValue (right, TRUE);
3745       }
3746
3747       return geniCodeArray (left, right,lvl);
3748
3749     case '.':                   /* structure dereference */
3750       if (IS_PTR (operandType (left)))
3751         left = geniCodeRValue (left, TRUE);
3752       else
3753         left = geniCodeRValue (left, FALSE);
3754
3755       return geniCodeStruct (left, right, tree->lvalue);
3756
3757     case PTR_OP:                /* structure pointer dereference */
3758       {
3759         sym_link *pType;
3760         pType = operandType (left);
3761         left = geniCodeRValue (left, TRUE);
3762
3763         setOClass (pType, getSpec (operandType (left)));
3764       }
3765
3766       return geniCodeStruct (left, right, tree->lvalue);
3767
3768     case INC_OP:                /* increment operator */
3769       if (left)
3770         return geniCodePostInc (left);
3771       else
3772         return geniCodePreInc (right, tree->lvalue);
3773
3774     case DEC_OP:                /* decrement operator */
3775       if (left)
3776         return geniCodePostDec (left);
3777       else
3778         return geniCodePreDec (right, tree->lvalue);
3779
3780     case '&':                   /* bitwise and or address of operator */
3781       if (right)
3782         {                       /* this is a bitwise operator   */
3783           left = geniCodeRValue (left, FALSE);
3784           right = geniCodeRValue (right, FALSE);
3785           return geniCodeBitwise (left, right, BITWISEAND, tree->ftype);
3786         }
3787       else
3788         return geniCodeAddressOf (left);
3789
3790     case '|':                   /* bitwise or & xor */
3791     case '^':
3792       return geniCodeBitwise (geniCodeRValue (left, FALSE),
3793                               geniCodeRValue (right, FALSE),
3794                               tree->opval.op,
3795                               tree->ftype);
3796
3797     case '/':
3798       return geniCodeDivision (geniCodeRValue (left, FALSE),
3799                                geniCodeRValue (right, FALSE));
3800
3801     case '%':
3802       return geniCodeModulus (geniCodeRValue (left, FALSE),
3803                               geniCodeRValue (right, FALSE));
3804     case '*':
3805       if (right)
3806         return geniCodeMultiply (geniCodeRValue (left, FALSE),
3807                                  geniCodeRValue (right, FALSE),IS_INT(tree->ftype));
3808       else
3809         return geniCodeDerefPtr (geniCodeRValue (left, FALSE),lvl);
3810
3811     case '-':
3812       if (right)
3813         return geniCodeSubtract (geniCodeRValue (left, FALSE),
3814                                  geniCodeRValue (right, FALSE));
3815       else
3816         return geniCodeUnaryMinus (geniCodeRValue (left, FALSE));
3817
3818     case '+':
3819       if (right)
3820         return geniCodeAdd (geniCodeRValue (left, FALSE),
3821                             geniCodeRValue (right, FALSE),lvl);
3822       else
3823         return geniCodeRValue (left, FALSE);    /* unary '+' has no meaning */
3824
3825     case LEFT_OP:
3826       return geniCodeLeftShift (geniCodeRValue (left, FALSE),
3827                                 geniCodeRValue (right, FALSE));
3828
3829     case RIGHT_OP:
3830       return geniCodeRightShift (geniCodeRValue (left, FALSE),
3831                                  geniCodeRValue (right, FALSE));
3832     case CAST:
3833 #if 0 // this indeed needs a second thought
3834       {
3835         operand *op;
3836
3837         // let's keep this simple: get the rvalue we need
3838         op=geniCodeRValue (right, FALSE);
3839         // now cast it to whatever we want
3840         op=geniCodeCast (operandType(left), op, FALSE);
3841         // if this is going to be used as an lvalue, make it so
3842         if (tree->lvalue) {
3843           op->isaddr=1;
3844         }
3845         return op;
3846       }
3847 #else // bug #604575, is it a bug ????
3848       return geniCodeCast (operandType (left),
3849                            geniCodeRValue (right, FALSE), FALSE);
3850 #endif
3851
3852     case '~':
3853     case RRC:
3854     case RLC:
3855     case SWAP:
3856       return geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
3857
3858     case '!':
3859     case GETHBIT:
3860       {
3861         operand *op = geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
3862         setOperandType (op, UCHARTYPE);
3863         return op;
3864       }
3865     case '>':
3866     case '<':
3867     case LE_OP:
3868     case GE_OP:
3869     case EQ_OP:
3870     case NE_OP:
3871     case AND_OP:
3872     case OR_OP:
3873       /* different compilers (even different gccs) evaluate
3874          the two calls in a different order. to get the same
3875          result on all machines we've to specify a clear sequence.
3876       return geniCodeLogic (geniCodeRValue (left, FALSE),
3877                             geniCodeRValue (right, FALSE),
3878                             tree->opval.op);
3879       */
3880       {
3881         operand *leftOp, *rightOp;
3882
3883         rightOp = geniCodeRValue (right, FALSE);
3884         leftOp  = geniCodeRValue (left , FALSE);
3885
3886         return geniCodeLogic (leftOp, rightOp, tree->opval.op);
3887       }
3888     case '?':
3889       return geniCodeConditional (tree,lvl);
3890
3891     case SIZEOF:
3892       return operandFromLit (getSize (tree->right->ftype));
3893
3894     case '=':
3895       {
3896         sym_link *rtype = operandType (right);
3897         sym_link *ltype = operandType (left);
3898         if (IS_PTR (rtype) && IS_ITEMP (right)
3899             && right->isaddr && compareType (rtype->next, ltype) == 1)
3900           right = geniCodeRValue (right, TRUE);
3901         else
3902           right = geniCodeRValue (right, FALSE);
3903
3904         geniCodeAssign (left, right, 0);
3905         return right;
3906       }
3907     case MUL_ASSIGN:
3908       return
3909         geniCodeAssign (left,
3910                 geniCodeMultiply (geniCodeRValue (operandFromOperand (left),
3911                                                   FALSE),
3912                                   geniCodeRValue (right, FALSE),FALSE), 0);
3913
3914     case DIV_ASSIGN:
3915       return
3916         geniCodeAssign (left,
3917                 geniCodeDivision (geniCodeRValue (operandFromOperand (left),
3918                                                   FALSE),
3919                                   geniCodeRValue (right, FALSE)), 0);
3920     case MOD_ASSIGN:
3921       return
3922         geniCodeAssign (left,
3923                  geniCodeModulus (geniCodeRValue (operandFromOperand (left),
3924                                                   FALSE),
3925                                   geniCodeRValue (right, FALSE)), 0);
3926     case ADD_ASSIGN:
3927       {
3928         sym_link *rtype = operandType (right);
3929         sym_link *ltype = operandType (left);
3930         if (IS_PTR (rtype) && IS_ITEMP (right)
3931             && right->isaddr && compareType (rtype->next, ltype) == 1)
3932           right = geniCodeRValue (right, TRUE);
3933         else
3934           right = geniCodeRValue (right, FALSE);
3935
3936
3937         return geniCodeAssign (left,
3938                      geniCodeAdd (geniCodeRValue (operandFromOperand (left),
3939                                                   FALSE),
3940                                   right,lvl), 0);
3941       }
3942     case SUB_ASSIGN:
3943       {
3944         sym_link *rtype = operandType (right);
3945         sym_link *ltype = operandType (left);
3946         if (IS_PTR (rtype) && IS_ITEMP (right)
3947             && right->isaddr && compareType (rtype->next, ltype) == 1)
3948           {
3949             right = geniCodeRValue (right, TRUE);
3950           }
3951         else
3952           {
3953             right = geniCodeRValue (right, FALSE);
3954           }
3955         return
3956           geniCodeAssign (left,
3957                 geniCodeSubtract (geniCodeRValue (operandFromOperand (left),
3958                                                   FALSE),
3959                                   right), 0);
3960       }
3961     case LEFT_ASSIGN:
3962       return
3963         geniCodeAssign (left,
3964                 geniCodeLeftShift (geniCodeRValue (operandFromOperand (left)
3965                                                    ,FALSE),
3966                                    geniCodeRValue (right, FALSE)), 0);
3967     case RIGHT_ASSIGN:
3968       return
3969         geniCodeAssign (left,
3970                geniCodeRightShift (geniCodeRValue (operandFromOperand (left)
3971                                                    ,FALSE),
3972                                    geniCodeRValue (right, FALSE)), 0);
3973     case AND_ASSIGN:
3974       return
3975         geniCodeAssign (left,
3976                  geniCodeBitwise (geniCodeRValue (operandFromOperand (left),
3977                                                   FALSE),
3978                                   geniCodeRValue (right, FALSE),
3979                                   BITWISEAND,
3980                                   operandType (left)), 0);
3981     case XOR_ASSIGN:
3982       return
3983         geniCodeAssign (left,
3984                  geniCodeBitwise (geniCodeRValue (operandFromOperand (left),
3985                                                   FALSE),
3986                                   geniCodeRValue (right, FALSE),
3987                                   '^',
3988                                   operandType (left)), 0);
3989     case OR_ASSIGN:
3990       return
3991         geniCodeAssign (left,
3992                   geniCodeBitwise (geniCodeRValue (operandFromOperand (left)
3993                                                    ,FALSE),
3994                                    geniCodeRValue (right, FALSE),
3995                                    '|',
3996                                    operandType (left)), 0);
3997     case ',':
3998       return geniCodeRValue (right, FALSE);
3999
4000     case CALL:
4001       return geniCodeCall (ast2iCode (tree->left,lvl+1),
4002                            tree->right,lvl);
4003     case LABEL:
4004       geniCodeLabel (ast2iCode (tree->left,lvl+1)->operand.symOperand);
4005       return ast2iCode (tree->right,lvl+1);
4006
4007     case GOTO:
4008       geniCodeGoto (ast2iCode (tree->left,lvl+1)->operand.symOperand);
4009       return ast2iCode (tree->right,lvl+1);
4010
4011     case FUNCTION:
4012       geniCodeFunctionBody (tree,lvl);
4013       return NULL;
4014
4015     case RETURN:
4016       geniCodeReturn (right);
4017       return NULL;
4018
4019     case IFX:
4020       geniCodeIfx (tree,lvl);
4021       return NULL;
4022
4023     case SWITCH:
4024       geniCodeSwitch (tree,lvl);
4025       return NULL;
4026
4027     case INLINEASM:
4028       geniCodeInline (tree);
4029       return NULL;
4030         
4031     case ARRAYINIT:
4032         geniCodeArrayInit(tree, ast2iCode (tree->left,lvl+1));
4033         return NULL;
4034     
4035     case CRITICAL:
4036         geniCodeCritical (tree, lvl);
4037     }
4038
4039   return NULL;
4040 }
4041
4042 /*-----------------------------------------------------------------*/
4043 /* reverseICChain - gets from the list and creates a linkedlist    */
4044 /*-----------------------------------------------------------------*/
4045 iCode *
4046 reverseiCChain ()
4047 {
4048   iCode *loop = NULL;
4049   iCode *prev = NULL;
4050
4051   while ((loop = getSet (&iCodeChain)))
4052     {
4053       loop->next = prev;
4054       if (prev)
4055         prev->prev = loop;
4056       prev = loop;
4057     }
4058
4059   return prev;
4060 }
4061
4062
4063 /*-----------------------------------------------------------------*/
4064 /* iCodeFromAst - given an ast will convert it to iCode            */
4065 /*-----------------------------------------------------------------*/
4066 iCode *
4067 iCodeFromAst (ast * tree)
4068 {
4069   returnLabel = newiTempLabel ("_return");
4070   entryLabel = newiTempLabel ("_entry");
4071   ast2iCode (tree,0);
4072   return reverseiCChain ();
4073 }
4074
4075 static const char *opTypeToStr(OPTYPE op)
4076 {
4077     switch(op)
4078     {
4079       case SYMBOL: return "symbol";
4080       case VALUE: return "value";
4081       case TYPE: return "type";
4082     }
4083     return "undefined type";    
4084 }
4085
4086
4087 operand *validateOpType(operand         *op, 
4088                         const char      *macro,
4089                         const char      *args,
4090                         OPTYPE          type,
4091                         const char      *file, 
4092                         unsigned        line)
4093 {    
4094     if (op && op->type == type)
4095     {
4096         return op;
4097     }
4098     fprintf(stderr, 
4099             "Internal error: validateOpType failed in %s(%s) @ %s:%u:"
4100             " expected %s, got %s\n",
4101             macro, args, file, line, 
4102             opTypeToStr(type), op ? opTypeToStr(op->type) : "null op");
4103     exit(-1);
4104     return op; // never reached, makes compiler happy.
4105 }