expr.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. /*
  2. * GTools C compiler
  3. * =================
  4. * source file :
  5. * expressions
  6. *
  7. * Copyright 2001-2004 Paul Froissart.
  8. * Credits to Christoph van Wuellen and Matthew Brandt.
  9. * All commercial rights reserved.
  10. *
  11. * This compiler may be redistributed as long there is no
  12. * commercial interest. The compiler must not be redistributed
  13. * without its full sources. This notice must stay intact.
  14. */
  15. #define DECLARE
  16. #include "define.h"
  17. _FILE(__FILE__)
  18. #include "c.h"
  19. #include "expr.h"
  20. #include "gen.h"
  21. #include "cglbdec.h"
  22. #ifndef NOFLOAT
  23. #include "ffplib.h"
  24. #endif
  25. #ifdef PC
  26. #ifdef SHORT_INT
  27. #undef int
  28. #endif
  29. #include <ctype.h>
  30. #ifdef SHORT_INT
  31. #define int short
  32. #endif
  33. #else
  34. #include "define.h"
  35. #endif
  36. long inittype();
  37. unsigned char sizeof_flag CGLOB;
  38. unsigned char id_are_zero CGLOB;
  39. #ifdef MID_DECL_IN_EXPR
  40. struct enode *md_expr CGLOB;
  41. struct typ *md_type CGLOB;
  42. #endif
  43. /*
  44. * expression evaluation
  45. *
  46. * this set of routines builds a parse tree for an expression. no code is
  47. * generated for the expressions during the build, this is the job of the
  48. * codegen module. for most purposes expression() is the routine to call. it
  49. * will allow all of the C operators. for the case where the comma operator
  50. * is not valid (function parameters for instance) call exprnc().
  51. *
  52. * each of the routines returns a pointer to a describing type structure. each
  53. * routine also takes one parameter which is a pointer to an expression node
  54. * by reference (address of pointer). the completed expression is returned in
  55. * this pointer. all routines return either a pointer to a valid type or NULL
  56. * if the hierarchy of the next operator is too low or the next symbol is not
  57. * part of an expression.
  58. */
  59. static TYP *primary();
  60. static TYP *unary();
  61. static TYP *multops();
  62. static TYP *addops();
  63. #ifndef isscalar
  64. static int isscalar();
  65. #endif
  66. TYP *force_cast_op();
  67. int not_lvalue(struct enode *node) {
  68. while (node->nodetype==en_cast) node=node->v.p[0];
  69. return node->nodetype-en_ref;
  70. }
  71. struct enode *mk_node(enum(e_node) nt, struct enode *v1, struct enode *v2) {
  72. /*
  73. * build an expression node with a node type of nt and values v1 and v2.
  74. */
  75. struct enode *ep;
  76. ep = (struct enode *) xalloc((int) sizeof(struct enode), ENODE+MK_NODE);
  77. ep->nodetype = nt;
  78. ep->etype = bt_void;
  79. ep->esize = -1;
  80. ep->v.p[0] = v1;
  81. ep->v.p[1] = v2;
  82. return ep;
  83. }
  84. struct enode *mk_icon(long i) {
  85. /*
  86. * build an expression node forming an integer constant
  87. */
  88. struct enode *ep;
  89. /* if (i==0x4E0003)
  90. __HALT;*/
  91. /* if (global_flag && !temp_mem)
  92. printf("gfdiog");*/
  93. // ep = (struct enode *) xalloc((int) sizeof(struct enode), ENODE+MK_ICON);
  94. ep = (struct enode *) xalloc((int) sizeof(struct xcon), ENODE+MK_ICON);
  95. ep->nodetype = en_icon;
  96. ep->etype = bt_void;
  97. #ifdef NO_CALLOC
  98. ep->esize = 0;
  99. #endif
  100. ep->v.i = i;
  101. return ep;
  102. }
  103. TYP *deref(struct enode **node, TYP *tp) {
  104. /*
  105. * build the proper dereference operation for a node using the type pointer
  106. * tp.
  107. */
  108. switch (tp->type) {
  109. case bt_void:
  110. case bt_char:
  111. case bt_uchar:
  112. case bt_short:
  113. case bt_ushort:
  114. case bt_long:
  115. case bt_pointer:
  116. case bt_ulong:
  117. case bt_struct:
  118. case bt_union:
  119. case bt_float:
  120. #ifdef DOUBLE
  121. case bt_double:
  122. #endif
  123. *node = mk_node(en_ref, *node, NIL_ENODE);
  124. (*node)->etype = tp->type;
  125. (*node)->esize = tp->size;
  126. break;
  127. case bt_bitfield:
  128. *node = mk_node(en_fieldref, *node, NIL_ENODE);
  129. (*node)->bit_width = tp->bit_width;
  130. (*node)->bit_offset = tp->bit_offset;
  131. /*
  132. * maybe it should be 'unsigned'
  133. */
  134. (*node)->etype = tp_int.type;
  135. (*node)->esize = tp_int.size;
  136. tp = (TYP *)&tp_int;
  137. break;
  138. default:
  139. error(ERR_DEREF);
  140. break;
  141. }
  142. return tp;
  143. }
  144. TYP *cond_deref(struct enode **node, TYP *tp) {
  145. TYP *tp1;
  146. /*
  147. * dereference the node if val_flag is zero. If val_flag is non_zero and
  148. * tp->type is bt_pointer (array reference), set the size field to the
  149. * pointer size (if this code is not executed on behalf of a sizeof
  150. * operator)
  151. */
  152. /* infunc("chk_curword")
  153. bkpt();*/
  154. if (tp->val_flag == 0)
  155. return deref(node, tp);
  156. if (tp->type == bt_pointer && sizeof_flag == 0) {
  157. tp1 = tp->btp;
  158. tp = mk_type(bt_pointer, 4);
  159. tp->btp = tp1;
  160. }
  161. return tp;
  162. }
  163. TYP *nameref(struct enode **node) {
  164. /*
  165. * nameref will build an expression tree that references an identifier. if
  166. * the identifier is not in the global or local symbol table then a
  167. * look-ahead to the next character is done and if it indicates a function
  168. * call the identifier is coerced to an external function name. non-value
  169. * references generate an additional level of indirection.
  170. */
  171. struct sym *sp;
  172. TYP *tp;
  173. sp = lastsp;
  174. if (sp == 0) {
  175. getcache(id);
  176. if (cached_sym == openpa && !cached_flag) {
  177. uwarn("function '%s' not defined; assuming extern returning int", lastid);
  178. ++global_flag;
  179. sp = (struct sym *) xalloc((int) sizeof(struct sym), _SYM+NAMEREF);
  180. sp->tp = (TYP *)&tp_func;
  181. sp->name = strsave(lastid);
  182. sp->storage_class = sc_external;
  183. append(&sp, &gsyms);
  184. --global_flag;
  185. tp = (TYP *)&tp_func;
  186. *node = mk_node(en_nacon, NIL_ENODE, NIL_ENODE);
  187. (*node)->v.ensp = sp->name;
  188. #ifdef AS
  189. (*node)->v.enlab = splbl(sp);
  190. #endif
  191. sp->used = 1;
  192. (*node)->etype = bt_pointer;
  193. (*node)->esize = 4;
  194. if (asm_zflag) (*node)->etype = bt_ulong;
  195. } else {
  196. if (lastid[0]=='_' && lastid[1]=='R' && !strncmp(lastid+3,"M_CALL_",7)) {
  197. ++global_flag;
  198. sp = (struct sym *) xalloc((int) sizeof(struct sym), _SYM+NAMEREF);
  199. sp->tp = (TYP *)&tp_void_ptr;
  200. sp->name = strsave(lastid);
  201. sp->storage_class = sc_external;
  202. append(&sp, &gsyms);
  203. --global_flag;
  204. goto ok_sp;
  205. } else {
  206. if (!asm_xflag) {
  207. tp = 0;
  208. uerr(ERR_UNDEFINED,lastid);
  209. } else {
  210. tp = (TYP *)&tp_ulong;
  211. *node = mk_node(en_nacon, NIL_ENODE, NIL_ENODE);
  212. (*node)->v.ensp = strsave(lastid);
  213. #ifdef AS
  214. (*node)->v.enlab = label(lastid);
  215. #endif
  216. (*node)->etype = bt_ulong;
  217. (*node)->esize = 4;
  218. }
  219. }
  220. }
  221. } else {
  222. ok_sp:
  223. if ((tp = sp->tp) == 0) {
  224. uerr(ERR_UNDEFINED,lastid);
  225. return 0; /* guard against untyped entries */
  226. }
  227. switch (sp->storage_class) {
  228. case sc_static:
  229. *node = mk_node(en_labcon, NIL_ENODE, NIL_ENODE);
  230. #ifdef AS
  231. (*node)->v.enlab = splbl(sp);
  232. #else
  233. (*node)->v.enlab = sp->value.i;
  234. #endif
  235. (*node)->etype = bt_pointer;
  236. (*node)->esize = 4;
  237. break;
  238. case sc_global:
  239. case sc_external:
  240. *node = mk_node(en_nacon, NIL_ENODE, NIL_ENODE);
  241. (*node)->v.ensp = sp->name;
  242. #ifdef AS
  243. (*node)->v.enlab = splbl(sp);
  244. #endif
  245. sp->used = 1;
  246. (*node)->etype = bt_pointer;
  247. (*node)->esize = 4;
  248. break;
  249. case sc_const:
  250. *node = mk_icon((long) sp->value.i);
  251. (*node)->etype = tp_econst.type;
  252. (*node)->esize = tp_econst.size;
  253. break;
  254. default: /* auto and any errors */
  255. if (sp->storage_class != sc_auto)
  256. error(ERR_ILLCLASS);
  257. *node = mk_node(en_autocon, NIL_ENODE, NIL_ENODE);
  258. (*node)->v.i = sp->value.i;
  259. (*node)->etype = bt_pointer;
  260. (*node)->esize = 4;
  261. /* infunc("chk_curword")
  262. if (sp->value.i==(long)(char)0xCF)
  263. bkpt();*/
  264. /* infunc("chk_curword")
  265. if (*(long *)node==0x7b4bf8)
  266. bkpt();*/
  267. break;
  268. }
  269. if (asm_zflag) {
  270. if ((*node)->etype == bt_pointer)
  271. (*node)->etype = bt_ulong;
  272. if (tp->type == bt_pointer || tp->type == bt_struct || tp->type == bt_union)
  273. tp = (TYP *)&tp_ulong;
  274. } else tp = cond_deref(node, tp);
  275. }
  276. getsym();
  277. return tp;
  278. }
  279. #ifndef NO_VARARG_FUNC
  280. extern char variable_arg_name[];
  281. #define is_variable_arg(sp) ((sp)->name==variable_arg_name)
  282. #define mk_variable_arg(sp) ((sp)->name=variable_arg_name)
  283. #endif
  284. struct enode *parmlist(SYM *f) {
  285. /*
  286. * parmlist will build a list of parameter expressions in a function call and
  287. * return a pointer to the last expression parsed. since parameters are
  288. * generally pushed from right to left we get just what we asked for...
  289. */
  290. struct enode *ep1, *ep2;
  291. TYP *tp;
  292. ep1 = 0;
  293. if (lastst != closepa)
  294. for (;;) {
  295. tp = exprnc(&ep2); /* evaluate a parameter */
  296. if (tp == 0)
  297. error(ERR_EXPREXPECT);
  298. #ifdef INTEL_386
  299. /* trap struct assigns */
  300. if (isaggregate(tp))
  301. uses_structassign=1;
  302. #endif
  303. /*
  304. * do the default promotions
  305. */
  306. if (f) {
  307. if (tp->const_flag && f->tp->type==bt_pointer && !f->tp->btp->const_flag)
  308. uwarn("read-only arg might be modified by function");
  309. tp = cast_op(&ep2, tp, f->tp);
  310. }
  311. #ifndef NOFLOAT
  312. #ifdef DOUBLE
  313. if (tp->type == bt_float)
  314. tp = cast_op(&ep2, tp, (TYP *)&tp_double);
  315. #endif
  316. #endif
  317. if (short_option) {
  318. if (tp->type == bt_char || tp->type == bt_uchar)
  319. (void) cast_op(&ep2, tp, (TYP *)&tp_short);
  320. } else {
  321. if (tp->type == bt_uchar || tp->type == bt_char ||
  322. tp->type == bt_short || tp->type == bt_ushort)
  323. (void) cast_op(&ep2, tp, (TYP *)&tp_long);
  324. }
  325. ep1 = mk_node(en_void, ep2, ep1);
  326. if (lastst != comma)
  327. break;
  328. getsym();
  329. if (f && !(f=f->next)) error(ERR_TOOMPARAMS);
  330. #ifndef NO_VARARG_FUNC
  331. if (f && is_variable_arg(f)) f=NULL; // switch to K&R mode for variable args...
  332. #endif
  333. }
  334. if (f && f->next && !is_variable_arg(f->next)) error(ERR_TOOFPARAMS);
  335. return ep1;
  336. }
  337. int castbegin(enum(e_sym) st) {
  338. /*
  339. * return 1 if st in set of [ kw_char, kw_short, kw_long, kw_float,
  340. * kw_double, kw_struct, kw_union ] CVW change: or kw_void CVW change: or an
  341. * type identifier
  342. */
  343. if (st == kw_char || st == kw_short || st == kw_int ||
  344. st == kw_long || st == kw_float || st == kw_double ||
  345. st == kw_struct || st == kw_union || st == kw_unsigned ||
  346. st == kw_void || st == kw_enum || st == kw_typeof ||
  347. st == kw_signed || st == kw_const || st == kw_volatile)
  348. return 1;
  349. if (st == id && lastsp != 0 &&
  350. lastsp->storage_class == sc_typedef)
  351. return 1;
  352. return 0;
  353. }
  354. static TYP *primary(struct enode **node) {
  355. /*
  356. * primary will parse a primary expression and set the node pointer returning
  357. * the type of the expression parsed. primary expressions are any of:
  358. * id
  359. * constant
  360. * string
  361. * ( expression )
  362. * primary[ expression ]
  363. #ifndef OLD_PRIOR
  364. * primary++
  365. * primary--
  366. #endif
  367. * primary.id
  368. * primary->id
  369. * primary( parameter list )
  370. * -- or just a semicolon, yields empty expression --
  371. *
  372. */
  373. struct enode *pnode, *qnode, *rnode;
  374. struct sym *sp;
  375. TYP *tptr;
  376. TYP *tp1,*tp2;
  377. switch (lastst) {
  378. case id:
  379. if (id_are_zero) {
  380. pnode = mk_icon(0l);
  381. tptr = (TYP *)&tp_long; // dans #if, le type par défaut est 'long'
  382. pnode->etype = tp_long.type;
  383. pnode->esize = tp_long.size;
  384. getsym();
  385. break;
  386. }
  387. tptr = nameref(&pnode);
  388. if (tptr == 0)
  389. break;
  390. /*
  391. * function names alone are pointers to functions.
  392. * If followed by '(', the reference is stripped off
  393. * later.
  394. */
  395. if (tptr->type == bt_func) {
  396. tp1 = mk_type(bt_pointer, 4);
  397. tp1->btp = tptr;
  398. tptr = tp1;
  399. }
  400. break;
  401. case iconst:
  402. case uconst:
  403. case lconst:
  404. case ulconst:
  405. pnode = mk_icon(0l);
  406. pnode->v.i = ival;
  407. if (lastst == uconst) {
  408. tptr = (TYP *)&tp_uint;
  409. pnode->etype = tp_uint.type;
  410. pnode->esize = tp_uint.size;
  411. } else if (lastst == lconst) {
  412. tptr = (TYP *)&tp_long;
  413. pnode->etype = bt_long;
  414. pnode->esize = 4;
  415. } else if (lastst == ulconst) {
  416. tptr = (TYP *)&tp_ulong;
  417. pnode->etype = bt_ulong;
  418. pnode->esize = 4;
  419. } else {
  420. tptr = (TYP *)&tp_int;
  421. pnode->etype = tp_int.type;
  422. pnode->esize = tp_int.size;
  423. }
  424. getsym();
  425. break;
  426. #ifndef NOFLOAT
  427. case rconst:
  428. tptr = (TYP *)&tp_double;
  429. pnode = mk_node(en_fcon, NIL_ENODE, NIL_ENODE);
  430. pnode->v.f = rval;
  431. pnode->etype = tp_double.type;
  432. pnode->esize = tp_double.size;
  433. getsym();
  434. break;
  435. #endif
  436. case sconst:
  437. if (sizeof_flag) {
  438. tptr = mk_type(bt_pointer, 0);
  439. tptr->size = lstrlen + 1;
  440. tptr->btp = (TYP *)&tp_char;
  441. tptr->val_flag = 1;
  442. } else
  443. tptr = (TYP *)&tp_string;
  444. pnode = mk_node(en_labcon, NIL_ENODE, NIL_ENODE);
  445. if (sizeof_flag == 0)
  446. pnode->v.enlab = stringlit(laststr, lstrlen);
  447. pnode->etype = bt_pointer;
  448. pnode->esize = 4;
  449. getsym();
  450. break;
  451. case kw_softcast:
  452. getsym();
  453. if (!castbegin(lastst)) {
  454. error(ERR_SYNTAX);
  455. tptr = 0; // just to avoid a compiler warning =)
  456. } else {
  457. struct typ *local_head = head, *local_tail=tail;
  458. decl((HTABLE *)NULL); /* do cast declaration */
  459. decl1();
  460. tptr = head;
  461. needpunc(closepa);
  462. if (!(tp1 = unary(&pnode))) {
  463. error(ERR_IDEXPECT);
  464. tptr = 0;
  465. } else
  466. /* do the cast */
  467. tptr = cast_op(&pnode, tp1, tptr);
  468. head = local_head;
  469. tail = local_tail;
  470. }
  471. break;
  472. case openpa:
  473. getsym();
  474. if (!castbegin(lastst)) {
  475. if (lastst==begin) { /* compound expression */
  476. struct snode *snp;
  477. struct enode *old_init_node = init_node;
  478. int old_middle_decl = middle_decl;
  479. getsym();
  480. init_node = 0;
  481. middle_decl = 0;
  482. snp = compound(0);
  483. init_node = old_init_node;
  484. middle_decl = old_middle_decl;
  485. tptr = lastexpr_tp;
  486. pnode = mk_node(en_compound, (struct enode *)snp, NIL_ENODE);
  487. pnode->etype=lastexpr_type;
  488. pnode->esize=lastexpr_size;
  489. } else tptr = expression(&pnode);
  490. needpunc(closepa);
  491. } else { /* cast operator or middle declaration */
  492. struct typ *local_head = head, *local_tail=tail;
  493. #ifdef MID_DECL_IN_EXPR
  494. struct enode *old_md_expr = md_expr;
  495. struct typ *old_md_type = md_type;
  496. md_expr = 0;
  497. middle_decl++;
  498. dodecl(sc_auto); /* if it's a real cast, same as decl(NULL);decl1(); */
  499. middle_decl--;
  500. #else
  501. decl((HTABLE *)NULL); /* do cast declaration */
  502. decl1();
  503. #endif
  504. tptr = head;
  505. needpunc(closepa);
  506. #ifdef MID_DECL_IN_EXPR
  507. if ((pnode = md_expr))
  508. tptr = md_type; /* we can't use head since parsing md_expr might change it */
  509. md_expr = old_md_expr;
  510. md_type = old_md_type;
  511. if (pnode)
  512. break;
  513. #endif
  514. if (lastst==begin) { /* cast constructor */
  515. int lab=nxtlabel();
  516. TYP *tp=tptr;
  517. nl();
  518. dseg();
  519. put_align(alignment(tptr));
  520. put_label(lab);
  521. inittype(tp,&tp);
  522. pnode = mk_node(en_labcon, NIL_ENODE, NIL_ENODE);
  523. pnode->v.enlab=lab;
  524. pnode->etype=bt_pointer;
  525. pnode->esize=4;
  526. tptr=cond_deref(&pnode,tp);
  527. } else if ((tp1 = unary(&pnode)) == 0) {
  528. error(ERR_IDEXPECT);
  529. tptr = 0;
  530. } else {
  531. /* do the cast */
  532. tptr = force_cast_op(&pnode, tp1, tptr);
  533. }
  534. head = local_head;
  535. tail = local_tail;
  536. }
  537. break;
  538. default:
  539. tptr=0;
  540. break;
  541. }
  542. if (tptr == 0)
  543. return 0;
  544. for (;;) {
  545. switch (lastst) {
  546. case openbr: /* build a subscript reference */
  547. getsym();
  548. /*
  549. * a[b] is defined as *(a+b), such exactly one of (a,b) must be a pointer
  550. * and one of (a,b) must be an integer expression
  551. */
  552. if (tptr->type == bt_pointer) {
  553. tp2 = expression(&rnode);
  554. tp1 = tptr;
  555. } else {
  556. tp2 = tptr;
  557. rnode = pnode;
  558. tp1 = expression(&pnode);
  559. tptr = tp1;
  560. }
  561. /*
  562. * now, pnode and tp1 describe the pointer,
  563. * rnode and tp2 describe the integral value
  564. */
  565. if (tptr->type != bt_pointer)
  566. error (ERR_NOPOINTER);
  567. else
  568. tptr = tptr->btp;
  569. if (tptr->size==1) { /* doing this : 1) saves RAM; 2) allows file_ESI[filesize] */
  570. cast_op(&rnode, tp2, (TYP *)&tp_long); /* (even with fast_array on) */
  571. cast_op(&rnode, (TYP *)&tp_long, tp1);
  572. qnode=rnode;
  573. } else {
  574. qnode = mk_icon((long) tptr->size);
  575. #ifdef NO_SECURE_POINTERS
  576. qnode->etype = bt_short;
  577. qnode->esize = 2;
  578. #else
  579. qnode->etype = bt_long;
  580. qnode->esize = 4;
  581. #endif
  582. /*
  583. * qnode is the size of the referenced object
  584. */
  585. #ifdef NO_SECURE_POINTERS
  586. cast_op(&rnode, tp2, (TYP *)&tp_short);
  587. #else
  588. cast_op(&rnode, tp2, (TYP *)&tp_long);
  589. #endif
  590. /*
  591. * we could check the type of the expression here...
  592. */
  593. #ifdef NO_SECURE_POINTERS
  594. qnode = mk_node(en_mul, qnode, rnode);
  595. qnode->etype = bt_short;
  596. qnode->esize = 2;
  597. cast_op(&qnode, (TYP *)&tp_short, (TYP *)&tp_long);
  598. cast_op(&qnode, (TYP *)&tp_long, tp1);
  599. #else
  600. qnode = mk_node(en_mul, qnode, rnode);
  601. qnode->etype = bt_long;
  602. qnode->esize = 4;
  603. cast_op(&qnode, (TYP *)&tp_long, tp1);
  604. #endif
  605. }
  606. pnode = mk_node(en_add, pnode, qnode);
  607. // pnode = mk_node(en_add, qnode, pnode);
  608. pnode->etype = bt_pointer;
  609. pnode->esize = 4;
  610. tptr = cond_deref(&pnode, tptr);
  611. needpunc(closebr);
  612. break;
  613. case pointsto:
  614. if (tptr->type != bt_pointer)
  615. error(ERR_NOPOINTER);
  616. else
  617. tptr = tptr->btp;
  618. /*
  619. * tptr->type should be bt_struct or bt_union tptr->val_flag
  620. * should be 0 the ref node will be stripped off in a minute
  621. */
  622. tptr = cond_deref(&pnode, tptr);
  623. /*
  624. * fall through to dot operation
  625. */
  626. case dot:
  627. getsym(); /* past -> or . */
  628. if (lastst != id)
  629. error(ERR_IDEXPECT);
  630. else {
  631. if (tptr->type!=bt_struct && tptr->type!=bt_union)
  632. uerrc("not a structure/union");
  633. {
  634. TABLE *tab=&tptr->lst;
  635. long offs=0;
  636. search_again:
  637. sp = search(lastid, lastcrc, (HTABLE *)tab);
  638. if (sp == 0) {
  639. sp = search("__unnamed__", -1, (HTABLE *)tab);
  640. if (!sp || (sp->tp->type!=bt_union && sp->tp->type!=bt_struct))
  641. uerr(ERR_NOMEMBER,lastid);
  642. else {
  643. offs += sp->value.i;
  644. tab = &sp->tp->lst;
  645. goto search_again;
  646. }
  647. } else {
  648. /* strip off the en_ref node on top */
  649. if (lvalue(pnode))
  650. pnode = pnode->v.p[0];
  651. else {
  652. pnode = mk_node(en_deref, pnode, NIL_ENODE);
  653. pnode->etype = bt_pointer;
  654. pnode->esize = 4;
  655. }
  656. tptr = sp->tp;
  657. qnode = mk_icon((long) (offs+sp->value.i));
  658. qnode->etype = bt_long;
  659. qnode->esize = 4;
  660. pnode = mk_node(en_add, pnode, qnode);
  661. pnode->etype = bt_pointer;
  662. pnode->esize = 4;
  663. tptr = cond_deref(&pnode, tptr);
  664. }
  665. }
  666. getsym(); /* past id */
  667. }
  668. break;
  669. case openpa: /* function reference */
  670. #ifdef ASM
  671. #ifndef OLD_AMODE_INPUT
  672. if (asm_zflag) goto fini;
  673. #endif
  674. #endif
  675. getsym();
  676. /*
  677. * the '*' may be ommitted with pointers to functions
  678. * we have included another indirection (see above, case id:)
  679. */
  680. if (tptr->type == bt_pointer)
  681. tptr = tptr->btp;
  682. if (tptr->type != bt_func)
  683. error(ERR_NOFUNC);
  684. /*
  685. * This hack lets us remember that this function itself calls
  686. * other functions.
  687. * The code generator might use this information to generate
  688. * safer register-pop-off code.
  689. */
  690. is_leaf_function = 0;
  691. pnode = mk_node(en_fcall, pnode, parmlist(tptr->lst.head));
  692. #ifdef REGPARM
  693. pnode->rp_dn = tptr->rp_dn;
  694. pnode->rp_an = tptr->rp_an;
  695. #endif
  696. tptr = tptr->btp;
  697. pnode->etype = tptr->type;
  698. pnode->esize = tptr->size;
  699. needpunc(closepa);
  700. break;
  701. case autoinc:
  702. case autodec:
  703. if (g_lvalue(pnode)) {
  704. qnode = mk_icon((tptr->type==bt_pointer?(long)tptr->btp->size:1L));
  705. pnode = mk_node(lastst==autoinc?en_ainc:en_adec, pnode, qnode);
  706. pnode->etype = tptr->type;
  707. pnode->esize = tptr->size;
  708. } else
  709. error(ERR_LVALUE);
  710. getsym();
  711. break;
  712. default:
  713. goto fini;
  714. }
  715. }
  716. fini:
  717. *node = pnode;
  718. return tptr;
  719. }
  720. static TYP *unary(struct enode **node) {
  721. /*
  722. * unary evaluates unary expressions and returns the type of the expression
  723. * evaluated. unary expressions are any of:
  724. *
  725. * primary
  726. #ifdef OLD_PRIOR
  727. * primary++
  728. * primary--
  729. #endif
  730. * !unary
  731. * ~unary
  732. * ++unary
  733. * --unary
  734. * -unary
  735. * *unary
  736. * &unary
  737. * (typecast)unary
  738. * sizeof(typecast)
  739. * sizeof unary
  740. *
  741. */
  742. TYP *tp, *tp1;
  743. struct enode *ep1, *ep2;
  744. int flag;
  745. // long i;
  746. flag = 0;
  747. switch (lastst) {
  748. case autodec:
  749. flag = 1;
  750. /* fall through to common increment */
  751. case autoinc:
  752. getsym();
  753. tp = unary(&ep1);
  754. if (tp == 0) {
  755. error(ERR_IDEXPECT);
  756. return 0;
  757. }
  758. if (g_lvalue(ep1)) {
  759. if (tp->type == bt_pointer)
  760. ep2 = mk_icon((long) tp->btp->size);
  761. else {
  762. ep2 = mk_icon(1l);
  763. if (!integral(tp))
  764. error(ERR_INTEGER);
  765. }
  766. ep2->etype = bt_long;
  767. ep2->esize = 4;
  768. ep1 = mk_node(flag ? en_assub : en_asadd, ep1, ep2);
  769. ep1->etype = tp->type;
  770. ep1->esize = tp->size;
  771. } else
  772. error(ERR_LVALUE);
  773. break;
  774. case minus:
  775. getsym();
  776. tp = unary(&ep1);
  777. if (tp == 0) {
  778. error(ERR_IDEXPECT);
  779. return 0;
  780. }
  781. ep1 = mk_node(en_uminus, ep1, NIL_ENODE);
  782. ep1->etype = tp->type;
  783. ep1->esize = tp->size;
  784. break;
  785. case not:
  786. getsym();
  787. tp = unary(&ep1);
  788. if (tp == 0) {
  789. error(ERR_IDEXPECT);
  790. return 0;
  791. }
  792. if (!bt_comparable(tp->type))
  793. uerrc("cannot test expression");
  794. ep1 = mk_node(en_not, ep1, NIL_ENODE);
  795. tp = (TYP *)&tp_int;
  796. ep1->etype = tp_int.type;
  797. ep1->esize = tp_int.size;
  798. break;
  799. case compl:
  800. getsym();
  801. tp = unary(&ep1);
  802. if (tp == 0) {
  803. error(ERR_IDEXPECT);
  804. return 0;
  805. }
  806. ep1 = mk_node(en_compl, ep1, NIL_ENODE);
  807. ep1->etype = tp->type;
  808. ep1->esize = tp->size;
  809. if (!integral(tp))
  810. error(ERR_INTEGER);
  811. break;
  812. case star:
  813. getsym();
  814. tp = unary(&ep1);
  815. if (tp == 0) {
  816. error(ERR_IDEXPECT);
  817. return 0;
  818. }
  819. if (tp->type!=bt_pointer)
  820. error(ERR_DEREF);
  821. else
  822. tp = tp->btp;
  823. tp = cond_deref(&ep1, tp);
  824. break;
  825. case and:
  826. getsym();
  827. /*if (lineid==0x216)
  828. bkpt();*/
  829. tp = unary(&ep1);
  830. if (tp == 0) {
  831. error(ERR_IDEXPECT);
  832. return 0;
  833. }
  834. if (lvalue(ep1)) { /* TODO: adapt this to g_lvalue */
  835. /* if (ep1->nodetype==en_cast */
  836. ep1 = ep1->v.p[0];
  837. tp1 = mk_type(bt_pointer, 4);
  838. tp1->st_flag = 0;
  839. tp1->btp = tp;
  840. tp = tp1;
  841. } else if (tp->type == bt_pointer && tp->btp->type == bt_func) {
  842. uwarn("'&' operator ignored");
  843. } else
  844. error(ERR_LVALUE);
  845. break;
  846. case kw_c: {
  847. int zf=asm_zflag;
  848. asm_zflag=0;
  849. getsym();
  850. needpunc(openpa);
  851. tp=expression(&ep1);
  852. asm_zflag=zf;
  853. needpunc(closepa);
  854. } break;
  855. case kw_defined:
  856. skipspace();
  857. { int parenth=0;
  858. if (lastch=='(') getch(),parenth=1;
  859. skipspace();
  860. if ((lastch>='A'&&lastch<='Z') || (lastch>='a'&&lastch<='z')
  861. || lastch=='_' || lastch=='$') {
  862. getidstr();
  863. ep1=mk_icon((long)!!search(lastid,lastcrc,&defsyms));
  864. tp = (TYP *)&tp_int;
  865. ep1->etype = tp_int.type;
  866. ep1->esize = tp_int.size;
  867. getsym();
  868. } else {
  869. error(ERR_IDEXPECT);
  870. tp = 0; // just to avoid a compiler warning =)
  871. }
  872. if (parenth)
  873. needpunc(closepa);
  874. }
  875. break;
  876. case kwb_constant_p:
  877. getsym();
  878. needpunc(openpa);
  879. if (!expression(&ep1)) error(ERR_EXPREXPECT);
  880. needpunc(closepa);
  881. opt0(&ep1);
  882. ep1=mk_icon((long)(ep1->nodetype==en_icon || ep1->nodetype==en_fcon));
  883. tp = (TYP *)&tp_int;
  884. ep1->etype = tp_int.type;
  885. ep1->esize = tp_int.size;
  886. break;
  887. case kw_alloca:
  888. getsym();
  889. needpunc(openpa);
  890. if (!(tp=expression(&ep1))) error(ERR_EXPREXPECT);
  891. needpunc(closepa);
  892. uses_link = 1;
  893. cast_op(&ep1,tp,(TYP *)&tp_short);
  894. ep1 = mk_node(en_alloca,ep1,NIL_ENODE);
  895. tp = (TYP *)&tp_void_ptr;
  896. ep1->etype = tp_void_ptr.type;
  897. ep1->esize = tp_void_ptr.size;
  898. break;
  899. case kw_sizeof:
  900. getsym();
  901. if (lastst == openpa) {
  902. flag = 1;
  903. getsym();
  904. }
  905. if (flag && castbegin(lastst)) {
  906. /*
  907. * save head and tail, since we may be called from inside decl
  908. * imagine: int x[sizeof(...)];
  909. */
  910. tp = head;
  911. tp1 = tail;
  912. decl((HTABLE *) 0);
  913. decl1();
  914. if (head != 0) {
  915. ep1 = mk_icon((long) head->size);
  916. /*
  917. * Guard against the size of not-yet-declared struct/unions
  918. */
  919. if (head->size == 0) {
  920. uwarn("sizeof(item) = 0");
  921. }
  922. } else
  923. ep1 = mk_icon(1l);
  924. head = tp;
  925. tail = tp1;
  926. } else {
  927. /*
  928. * This is a mess.
  929. * Normally, we treat array names just as pointers, but with sizeof,
  930. * we want to get the real array size.
  931. * sizeof_flag != 0 tells cond_deref not to convert array names to pointers
  932. */
  933. sizeof_flag++;
  934. tp = unary(&ep1);
  935. sizeof_flag--;
  936. if (tp == 0) {
  937. error(ERR_SYNTAX);
  938. ep1 = mk_icon(1l);
  939. } else
  940. ep1 = mk_icon((long) tp->size);
  941. }
  942. /* if (short_option && ep1->v.i > 65535)
  943. do_warning("'sizeof' value greater than 65535\n");*/
  944. tp = (TYP *)&tp_uint;
  945. ep1->etype = tp_uint.type;
  946. ep1->esize = tp_uint.size;
  947. if (flag)
  948. needpunc(closepa);
  949. break;
  950. default:
  951. tp = primary(&ep1);
  952. break;
  953. }
  954. *node = ep1;
  955. return tp;
  956. }
  957. #ifndef NO_TYPE_STR
  958. int type_str_pos CGLOB;
  959. typedef char type_str_content_type[32];
  960. type_str_content_type type_str_content[4];
  961. char *type_str(TYP *tp) {
  962. char *p,*q;
  963. int n;
  964. switch (tp->type) {
  965. case bt_char:
  966. return "char";
  967. case bt_uchar:
  968. return "unsigned char";
  969. case bt_short:
  970. return "short";
  971. case bt_ushort:
  972. return "unsigned short";
  973. case bt_long:
  974. return "long";
  975. case bt_ulong:
  976. return "unsigned long";
  977. case bt_float:
  978. return "float";
  979. #ifdef DOUBLE
  980. case bt_double:
  981. return "double";
  982. #endif
  983. case bt_void:
  984. return "void";
  985. case bt_struct:
  986. #ifdef LISTING
  987. if (tp->sname) return tp->sname;
  988. #endif
  989. return "<struct>";
  990. case bt_union:
  991. return "<union>";
  992. case bt_func:
  993. return "<func>";
  994. case bt_bitfield:
  995. return "<bitfield>";
  996. case bt_pointer:
  997. n=type_str_pos;
  998. q=p=type_str(tp->btp);
  999. if (n==type_str_pos) {
  1000. type_str_pos++;
  1001. strcpy(type_str_content[n],p);
  1002. q=p=type_str_content[n];
  1003. while (*p++);
  1004. p[-1]=' '; *p++=0;
  1005. } else while (*p++);
  1006. if (!tp->val_flag) p[-1]='*';
  1007. else p[-1]='[',*p++=']';
  1008. *p=0;
  1009. return q;
  1010. }
  1011. return "<unknown>";
  1012. }
  1013. #endif
  1014. TYP *forcefit(struct enode **node1, TYP *tp1, struct enode **node2, TYP *tp2) {
  1015. /*
  1016. * forcefit will coerce the nodes passed into compatible types and return the
  1017. * type of the resulting expression.
  1018. */
  1019. /* cast short and char to int */
  1020. if (short_option) {
  1021. if (tp1->type == bt_char || tp1->type == bt_uchar)
  1022. tp1 = cast_op(node1, tp1, (TYP *)&tp_short);
  1023. if (tp2->type == bt_char || tp2->type == bt_uchar)
  1024. tp2 = cast_op(node2, tp2, (TYP *)&tp_short);
  1025. } else {
  1026. if (tp1->type == bt_char || tp1->type == bt_uchar ||
  1027. tp1->type == bt_short || tp1->type == bt_ushort)
  1028. tp1 = cast_op(node1, tp1, (TYP *)&tp_long);
  1029. if (tp2->type == bt_char || tp2->type == bt_uchar ||
  1030. tp2->type == bt_short || tp2->type == bt_ushort)
  1031. tp2 = cast_op(node2, tp2, (TYP *)&tp_long);
  1032. }
  1033. #ifndef NOFLOAT
  1034. /* cast float to double */
  1035. #ifdef DOUBLE
  1036. if (tp1->type == bt_float)
  1037. tp1 = cast_op(node1, tp1, (TYP *)&tp_double);
  1038. if (tp2->type == bt_float)
  1039. tp2 = cast_op(node2, tp2, (TYP *)&tp_double);
  1040. #endif
  1041. if (tp1->type == bt_double && isscalar(tp2))
  1042. tp2 = cast_op(node2, tp2, (TYP *)&tp_double);
  1043. else if (tp2->type == bt_double && isscalar(tp1))
  1044. tp1 = cast_op(node1, tp1, (TYP *)&tp_double);
  1045. #endif
  1046. if (tp1->type == bt_ulong && isscalar(tp2))
  1047. tp2 = cast_op(node2, tp2, tp1);
  1048. else if (tp2->type == bt_ulong && isscalar(tp1))
  1049. tp1 = cast_op(node1, tp1, tp2);
  1050. if (tp1->type == bt_long && isscalar(tp2))
  1051. tp2 = cast_op(node2, tp2, tp1);
  1052. else if (tp2->type == bt_long && isscalar(tp2))
  1053. tp1 = cast_op(node1, tp1, tp2);
  1054. if (tp1->type == bt_ushort && isscalar(tp2))
  1055. tp2 = cast_op(node2, tp2, tp1);
  1056. else if (tp2->type == bt_ushort && isscalar(tp2))
  1057. tp1 = cast_op(node1, tp1, tp2);
  1058. if (isscalar(tp1) && isscalar(tp2))
  1059. return (tp1);
  1060. /* pointers may be combined with integer constant 0 */
  1061. if (tp1->type == bt_pointer && (*node2)->nodetype == en_icon &&
  1062. (*node2)->v.i == 0)
  1063. return tp1;
  1064. if (tp2->type == bt_pointer && (*node1)->nodetype == en_icon &&
  1065. (*node2)->v.i == 0)
  1066. return tp2;
  1067. if (tp1->type == bt_pointer && tp2->type == bt_pointer)
  1068. return tp1;
  1069. /* report mismatch error */
  1070. uerr(ERR_MISMATCH,type_str(tp1),type_str(tp2));
  1071. return tp1;
  1072. }
  1073. TYP *forceft2(struct enode **node1, TYP *tp1, struct enode **node2, TYP *tp2) {
  1074. /*
  1075. * ,,forcefit'' for comparisons:
  1076. * When comparing two char's, it is not necessary to cast
  1077. * both of them to long in advance
  1078. *
  1079. * Perhaps not strictly K&R, but more efficient.
  1080. * If you don't like it, use forcefit in ALL cases
  1081. */
  1082. /* short cut: */
  1083. if (tp1->type == tp2->type)
  1084. return tp1;
  1085. /* comparison with integer constant */
  1086. if ((*node1)->nodetype == en_icon) {
  1087. struct enode **node = node1;
  1088. TYP *tp = tp1;
  1089. node1 = node2;
  1090. tp1 = tp2;
  1091. node2 = node;
  1092. tp2 = tp;
  1093. }
  1094. opt4(node2);
  1095. if ((*node2)->nodetype == en_icon) {
  1096. long val = (*node2)->v.i;
  1097. enum(e_bt) typ1 = tp1->type;
  1098. if ((typ1 == bt_char && -128 <= val && val <= 127) ||
  1099. (typ1 == bt_uchar && 0 <= val && val <= 255) ||
  1100. (typ1 == bt_short && -32768 <= val && val <= 32767) ||
  1101. (typ1 == bt_ushort && 0 <= val && val <= 65535) ||
  1102. (typ1 == bt_pointer && val == 0))
  1103. return cast_op(node2, tp2, tp1);
  1104. }
  1105. switch (tp1->type) {
  1106. /* Type of first operand */
  1107. case bt_char:
  1108. case bt_uchar:
  1109. switch (tp2->type) {
  1110. case bt_char:
  1111. case bt_uchar:
  1112. (void) cast_op(node1, tp1, (TYP *)&tp_short);
  1113. return cast_op(node2, tp2, (TYP *)&tp_short);
  1114. case bt_short:
  1115. case bt_long:
  1116. case bt_ushort:
  1117. case bt_ulong:
  1118. case bt_float:
  1119. #ifdef DOUBLE
  1120. case bt_double:
  1121. #endif
  1122. return cast_op(node1, tp1, tp2);
  1123. }
  1124. break;
  1125. case bt_short:
  1126. case bt_ushort:
  1127. switch (tp2->type) {
  1128. case bt_char:
  1129. case bt_uchar:
  1130. return cast_op(node2, tp2, tp1);
  1131. case bt_ushort:
  1132. if (short_option)
  1133. return cast_op (node1, tp1, (TYP *)&tp_ushort);
  1134. else {
  1135. (void) cast_op (node1, tp1, (TYP *)&tp_long);
  1136. return cast_op (node2, tp2, (TYP *)&tp_long);
  1137. }
  1138. case bt_short:
  1139. if (short_option)
  1140. return cast_op(node2, tp2, (TYP *)&tp_ushort);
  1141. else {
  1142. (void) cast_op (node1, tp1, (TYP *)&tp_long);
  1143. return cast_op (node2, tp2, (TYP *)&tp_long);
  1144. }
  1145. case bt_long:
  1146. case bt_ulong:
  1147. case bt_float:
  1148. #ifdef DOUBLE
  1149. case bt_double:
  1150. #endif
  1151. return cast_op(node1, tp1, tp2);
  1152. }
  1153. break;
  1154. case bt_long:
  1155. case bt_ulong:
  1156. switch (tp2->type) {
  1157. case bt_char:
  1158. case bt_uchar:
  1159. case bt_short:
  1160. case bt_ushort:
  1161. return cast_op(node2, tp2, tp1);
  1162. case bt_long:
  1163. return cast_op(node2, tp2, tp1);
  1164. case bt_ulong:
  1165. return cast_op(node1, tp1, tp2);
  1166. case bt_float:
  1167. #ifdef DOUBLE
  1168. case bt_double:
  1169. #endif
  1170. return cast_op(node1, tp1, tp2);
  1171. }
  1172. break;
  1173. case bt_float:
  1174. #ifdef DOUBLE
  1175. case bt_double:
  1176. #endif
  1177. switch (tp2->type) {
  1178. case bt_char:
  1179. case bt_uchar:
  1180. case bt_short:
  1181. case bt_ushort:
  1182. case bt_long:
  1183. case bt_ulong:
  1184. case bt_float:
  1185. return cast_op(node2, tp2, tp1);
  1186. #ifdef DOUBLE
  1187. case bt_double:
  1188. return cast_op(node1, tp1, tp2);
  1189. #endif
  1190. }
  1191. break;
  1192. /*
  1193. * pointers are equivalent to function names
  1194. */
  1195. case bt_pointer:
  1196. if (tp2->type == bt_func)
  1197. return cast_op(node2, tp2, tp1);
  1198. break;
  1199. case bt_func:
  1200. if (tp2->type == bt_pointer)
  1201. return cast_op(node1, tp1, tp2);
  1202. break;
  1203. }
  1204. uerr(ERR_MISMATCH,type_str(tp1),type_str(tp2));
  1205. return 0;
  1206. }
  1207. #ifndef isscalar
  1208. static int isscalar(TYP *tp) {
  1209. /*
  1210. * this function returns true when the type of the argument is a scalar type
  1211. * (enum included)
  1212. */
  1213. /* return tp->type == bt_char ||
  1214. tp->type == bt_uchar ||
  1215. tp->type == bt_ushort ||
  1216. tp->type == bt_short ||
  1217. tp->type == bt_long ||
  1218. tp->type == bt_ulong ||
  1219. tp->type == bt_float ||
  1220. tp->type == bt_double; */
  1221. return bt_scalar(tp->type);
  1222. }
  1223. #endif
  1224. static TYP *multops(struct enode **node) {
  1225. /*
  1226. * multops parses the multiply priority operators. the syntax of this group
  1227. * is:
  1228. *
  1229. * unary multop * unary multop / unary multop % unary
  1230. */
  1231. struct enode *ep1, *ep2;
  1232. TYP *tp1, *tp2;
  1233. enum(e_sym) oper;
  1234. tp1 = unary(&ep1);
  1235. if (tp1 == 0)
  1236. return 0;
  1237. while (lastst == star || lastst == divide || lastst == modop) {
  1238. oper = lastst;
  1239. getsym(); /* move on to next unary op */
  1240. /* if (lineid==77)
  1241. bkpt();*/
  1242. tp2 = unary(&ep2);
  1243. if (tp2 == 0) {
  1244. error(ERR_IDEXPECT);
  1245. *node = ep1;
  1246. return tp1;
  1247. }
  1248. #ifdef INTEL_386
  1249. tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1250. #endif
  1251. switch (oper) {
  1252. case star:
  1253. /*{extern SYM *func_sp;
  1254. if (!strcmp(func_sp->name,"bonus"))
  1255. printf("gjio");}*/
  1256. /* if (lineid==1189)
  1257. bkpt();*/
  1258. tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1259. #ifndef NOFLOAT
  1260. //#warning STUDY ME!!!
  1261. // uwarn("!!! study this !!!");
  1262. // if (tp1->type==bt_float || tp2->type==bt_float)
  1263. // tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1264. // else
  1265. #endif
  1266. #ifdef MC68000
  1267. // {
  1268. // tp1 = cast_op(&ep1, tp1, (TYP *)&tp_long);
  1269. // tp2 = cast_op(&ep2, tp2, (TYP *)&tp_long);
  1270. // }
  1271. #endif
  1272. ep1 = mk_node(en_mul, ep1, ep2);
  1273. #ifndef NOFLOAT
  1274. if (tp1->type!=bt_float)
  1275. #endif
  1276. #ifdef MC68000
  1277. // tp1 = (TYP *)&tp_long;
  1278. #endif
  1279. /* if (bt_integral(tp1->type)) {
  1280. if (bt_uns(tp1->type)) tp1=(TYP *)&tp_ulong;
  1281. else tp1=(TYP *)&tp_long;
  1282. }*/
  1283. break;
  1284. case divide:
  1285. case modop:
  1286. #ifndef NOFLOAT
  1287. if (tp1->type==bt_float || tp2->type==bt_float)
  1288. tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1289. else
  1290. #endif
  1291. #ifdef MC68000
  1292. {
  1293. #ifdef OLD_STUPID_DIVIDE /* !!! n'importe quoi !!! */
  1294. tp1 = cast_op(&ep1, tp1, (TYP *)&tp_long);
  1295. if (tp2->type==bt_long || tp2->type==bt_ulong) {
  1296. ep1 = mk_node(oper==divide?en_div:en_mod, ep1, ep2);
  1297. tp1 = (TYP *)&tp_long;
  1298. break;
  1299. }
  1300. tp2 = cast_op(&ep2, tp2, (TYP *)&tp_short);
  1301. #else
  1302. tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1303. #endif
  1304. }
  1305. #endif
  1306. ep1 = mk_node(oper==divide?en_div:en_mod, ep1, ep2);
  1307. #ifdef OLD_STUPID_DIVIDE /* !!! n'importe quoi !!! */
  1308. #ifndef NOFLOAT
  1309. if (tp1->type!=bt_float)
  1310. #endif
  1311. #ifdef MC68000
  1312. tp1 = (TYP *)&tp_short;
  1313. #endif
  1314. #endif
  1315. break;
  1316. }
  1317. ep1->etype = tp1->type;
  1318. ep1->esize = tp1->size;
  1319. }
  1320. *node = ep1;
  1321. return tp1;
  1322. }
  1323. static TYP *addops(struct enode **node) {
  1324. /*
  1325. * addops handles the addition and subtraction operators.
  1326. */
  1327. struct enode *ep1, *ep2, *ep3;
  1328. TYP *tp1, *tp2;
  1329. int oper;
  1330. tp1 = multops(&ep1);
  1331. if (tp1 == 0)
  1332. return 0;
  1333. while (lastst == plus || lastst == minus) {
  1334. /* if (asm_zflag)
  1335. printf("gfi");*/
  1336. /* if (lineid==852)
  1337. bkpt();*/
  1338. oper = (lastst - minus);
  1339. getsym();
  1340. tp2 = multops(&ep2);
  1341. if (tp2 == 0) {
  1342. error(ERR_IDEXPECT);
  1343. *node = ep1;
  1344. return tp1;
  1345. }
  1346. if (tp1->type == bt_pointer && tp2->type == bt_pointer
  1347. && tp1->btp->size == tp2->btp->size && (!oper)) {
  1348. /* pointer subtraction */
  1349. ep1 = mk_node(en_sub, ep1, ep2);
  1350. ep1->etype = bt_long;
  1351. ep1->esize = 4;
  1352. /* divide the result by the size */
  1353. ep2 = mk_icon((long) tp1->btp->size);
  1354. ep2->etype = bt_long;
  1355. ep2->esize = 4;
  1356. ep1 = mk_node(en_div, ep1, ep2);
  1357. ep1->etype = bt_long;
  1358. ep1->esize = 4;
  1359. //#ifdef POINTER_DIFF_IS_SHORT
  1360. /*
  1361. * cast the result to ,,int''. K&R says that pointer subtraction
  1362. * yields an int result so I do it although it is not sensible on
  1363. * an 68000 with 32-bit pointers and 16-bit ints. In my opinion,
  1364. * it should remain ,,long''.
  1365. */
  1366. // if (short_option) do_warning("pointer difference casted to 16-bit 'int'\n");
  1367. if (tp1->btp->size!=1) /* if size is 1 maybe we want it bijective... */
  1368. tp1 = cast_op(&ep1, (TYP *)&tp_long, (TYP *)&tp_int);
  1369. else tp1 = (TYP *)&tp_long;
  1370. //#endif
  1371. *node = ep1;
  1372. continue;
  1373. }
  1374. if (tp2->type == bt_pointer && oper) {
  1375. TYP *tpi;
  1376. /* integer + pointer */
  1377. tpi=tp1;
  1378. tp1=tp2;
  1379. tp2=tpi;
  1380. ep3=ep1;
  1381. ep1=ep2;
  1382. ep2=ep3;
  1383. }
  1384. if (tp1->type == bt_pointer) {
  1385. /* pointer +/- integer */
  1386. unsigned int s = tp1->btp->size;// int p;
  1387. if (!integral(tp2))
  1388. error(ERR_INTEGER);
  1389. /*if ((p=pwrof2(s))>=0) {
  1390. if (tp2->type == bt_char || tp2->type == bt_uchar)
  1391. tp2 = cast_op(&ep2, tp2, (TYP *)&tp_short);
  1392. if (p) {
  1393. ep3 = mk_icon((long)p);
  1394. ep3->etype = bt_short;
  1395. ep3->esize = 2;
  1396. ep2 = mk_node(en_lsh, ep3, ep2);
  1397. ep2->etype = bt_short;
  1398. ep2->esize = 2;
  1399. }
  1400. ep1 = mk_node(oper ? en_add : en_sub, ep1, ep2);
  1401. ep1->etype = bt_pointer;
  1402. ep1->esize = 4;
  1403. continue;
  1404. } else {*/
  1405. cast_op(&ep2, tp2, (TYP *)&tp_long); // g_xmul will restore all this to short if
  1406. ep3 = mk_icon(s); // necessary :)
  1407. ep3->etype = bt_long;
  1408. ep3->esize = 4;
  1409. ep2 = mk_node(en_mul, ep3, ep2);
  1410. ep2->etype = bt_long;
  1411. ep2->esize = 4;
  1412. // cast_op(&ep2, (TYP *)&tp_short, (TYP *)&tp_long);
  1413. ep1 = mk_node(oper ? en_add : en_sub, ep1, ep2);
  1414. ep1->etype = bt_pointer;
  1415. ep1->esize = 4;
  1416. continue;
  1417. /*}*/
  1418. }
  1419. tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1420. ep1 = mk_node(oper ? en_add : en_sub, ep1, ep2);
  1421. ep1->etype = tp1->type;
  1422. ep1->esize = tp1->size;
  1423. }
  1424. *node = ep1;
  1425. return tp1;
  1426. }
  1427. TYP *shiftop(struct enode **node) {
  1428. /*
  1429. * shiftop handles the shift operators << and >>.
  1430. */
  1431. struct enode *ep1, *ep2;
  1432. TYP *tp1, *tp2;
  1433. int oper;
  1434. tp1 = addops(&ep1);
  1435. if (tp1 == 0)
  1436. return 0;
  1437. while (lastst == lshift || lastst == rshift) {
  1438. oper = (lastst == lshift);
  1439. getsym();
  1440. tp2 = addops(&ep2);
  1441. if (tp2 == 0)
  1442. error(ERR_IDEXPECT);
  1443. else {
  1444. #ifndef MINIMAL_SIZES
  1445. tp1 = forcefit(&ep1, tp1, &ep2, tp2);
  1446. #else
  1447. /*
  1448. tp1 = forcefit(&ep1, tp1, &ep2, tp2); -> this is wrong: C89 specifies the type depends only on the left op.
  1449. cast_op(&ep2, tp2, (TYP *)&tp_char);
  1450. */
  1451. if (tp1->type == bt_char || tp1->type == bt_uchar)
  1452. tp1 = cast_op(&ep1, tp1, (TYP *)&tp_short);
  1453. cast_op(&ep2, tp2, (TYP *)&tp_char);
  1454. #endif
  1455. ep1 = mk_node(oper ? en_lsh : en_rsh, ep1, ep2);
  1456. ep1->etype = tp1->type;
  1457. ep1->esize = tp1->size;
  1458. if (!integral(tp1))
  1459. error(ERR_INTEGER);
  1460. }
  1461. }
  1462. *node = ep1;
  1463. return tp1;
  1464. }
  1465. TYP *relation(struct enode **node) {
  1466. /*
  1467. * relation handles the relational operators < <= > and >=.
  1468. */
  1469. struct enode *ep1, *ep2;
  1470. TYP *tp1, *tp2;
  1471. enum(e_node) nt;
  1472. tp1 = shiftop(&ep1);
  1473. if (tp1 == 0)
  1474. return 0;
  1475. for (;;) {
  1476. switch (lastst) {
  1477. case lt:
  1478. nt = en_lt;
  1479. break;
  1480. case gt:
  1481. nt = en_gt;
  1482. break;
  1483. case leq:
  1484. nt = en_le;
  1485. break;
  1486. case geq:
  1487. nt = en_ge;
  1488. break;
  1489. default:
  1490. goto fini;
  1491. }
  1492. getsym();
  1493. tp2 = shiftop(&ep2);
  1494. if (tp2 == 0)
  1495. error(ERR_EXPREXPECT);
  1496. else {
  1497. tp1 = forceft2(&ep1, tp1, &ep2, tp2);
  1498. if (!iscomparable(tp1))
  1499. uerr(ERR_ILLTYPE);
  1500. ep1 = mk_node(nt, ep1, ep2);
  1501. tp1 = (TYP *)&tp_int;
  1502. ep1->etype = tp_int.type;
  1503. ep1->esize = tp_int.size;
  1504. if (lastst>=lt && lastst<=geq) {
  1505. if (flags & X_COMP_STRING) {
  1506. uwarn("not implemented\n");
  1507. } else uwarn("() suggested to clarify priority");
  1508. }
  1509. }
  1510. }
  1511. fini:*node = ep1;
  1512. return tp1;
  1513. }
  1514. TYP *equalops(struct enode **node) {
  1515. /*
  1516. * equalops handles the equality and inequality operators.
  1517. */
  1518. struct enode *ep1, *ep2;
  1519. TYP *tp1, *tp2;
  1520. int oper;
  1521. tp1 = relation(&ep1);
  1522. if (tp1 == 0)
  1523. return 0;
  1524. while (lastst == eq || lastst == neq) {
  1525. oper = (lastst == eq);
  1526. getsym();
  1527. tp2 = relation(&ep2);
  1528. if (tp2 == 0)
  1529. error(ERR_IDEXPECT);
  1530. else {
  1531. tp1 = forceft2(&ep1, tp1, &ep2, tp2);
  1532. ep1 = mk_node(oper ? en_eq : en_ne, ep1, ep2);
  1533. tp1 = (TYP *)&tp_int;
  1534. ep1->etype = tp_int.type;
  1535. ep1->esize = tp_int.size;
  1536. }
  1537. }
  1538. *node = ep1;
  1539. return tp1;
  1540. }
  1541. TYP *binop(struct enode **node, TYP *(*xfunc)(), enum(e_node) nt, enum (e_sym) sy) {
  1542. /*
  1543. * binop is a common routine to handle all of the legwork and error checking
  1544. * for bitand, bitor, bitxor
  1545. */
  1546. struct enode *ep1, *ep2;
  1547. TYP *tp1, *tp2;
  1548. tp1 = (*xfunc) (&ep1);
  1549. if (tp1 == 0)
  1550. return 0;
  1551. while (lastst == sy) {
  1552. getsym();
  1553. tp2 = (*xfunc) (&ep2);
  1554. if (tp2 == 0)
  1555. error(ERR_IDEXPECT);
  1556. else {
  1557. tp1 = forceft2(&ep1, tp1, &ep2, tp2);
  1558. ep1 = mk_node(nt, ep1, ep2);
  1559. ep1->etype = tp1->type;
  1560. ep1->esize = tp1->size;
  1561. if (!integral(tp1))
  1562. error(ERR_INTEGER);
  1563. }
  1564. }
  1565. *node = ep1;
  1566. return tp1;
  1567. }
  1568. TYP *binlog(struct enode **node, TYP *(*xfunc)(), enum(e_node) nt, enum(e_sym) sy) {
  1569. /*
  1570. * binlog is a common routine to handle all of the legwork and error checking
  1571. * for logical and, or
  1572. */
  1573. struct enode *ep1, *ep2;
  1574. TYP *tp1, *tp2;
  1575. tp1 = (*xfunc) (&ep1);
  1576. if (tp1 == 0)
  1577. return 0;
  1578. while (lastst == sy) {
  1579. getsym();
  1580. tp2 = (*xfunc) (&ep2);
  1581. if (tp2 == 0)
  1582. error(ERR_IDEXPECT);
  1583. else {
  1584. ep1 = mk_node(nt, ep1, ep2);
  1585. tp1 = (TYP *)&tp_int;
  1586. ep1->etype = tp_int.type;
  1587. ep1->esize = tp_int.size;
  1588. }
  1589. }
  1590. *node = ep1;
  1591. return tp1;
  1592. }
  1593. TYP *bitand(struct enode **node) {
  1594. /*
  1595. * the bitwise and operator...
  1596. */
  1597. return binop(node, equalops, en_and, and);
  1598. }
  1599. TYP *bitxor(struct enode **node) {
  1600. return binop(node, bitand, en_xor, uparrow);
  1601. }
  1602. TYP *bitor(struct enode **node) {
  1603. return binop(node, bitxor, en_or, or);
  1604. }
  1605. TYP *andop(struct enode **node) {
  1606. return binlog(node, bitor, en_land, land);
  1607. }
  1608. TYP *orop(struct enode **node) {
  1609. return binlog(node, andop, en_lor, lor);
  1610. }
  1611. TYP *conditional(struct enode **node) {
  1612. /*
  1613. * this routine processes the hook operator.
  1614. */
  1615. TYP *tp1, *tp2, *tp3;
  1616. struct enode *ep1, *ep2, *ep3;
  1617. tp1 = orop(&ep1); /* get condition */
  1618. if (tp1 == 0)
  1619. return 0;
  1620. if (lastst == hook) {
  1621. getsym();
  1622. if (lastst==colon) {
  1623. ep2=NULL; tp2=tp1;
  1624. } else if ((tp2 = expression(&ep2)) == 0) {
  1625. error(ERR_IDEXPECT);
  1626. return 0;
  1627. }
  1628. needpunc(colon);
  1629. if ((tp3 = exprnc(&ep3)) == 0) {
  1630. error(ERR_IDEXPECT);
  1631. return 0;
  1632. }
  1633. /*
  1634. * If either type is void and the other is not, cast the other one to void.
  1635. * I dare not doing this in forceft2
  1636. * Strict ANSI does not allow that only one part of the sentence is void,
  1637. * that is what gcc -pedantic tells me.
  1638. * But since such conditionals occur even in GNU Software (look at obstack.h),
  1639. * I allow such constructs here.
  1640. */
  1641. if (tp2->type == bt_void && tp3->type != bt_void)
  1642. tp3 = cast_op(&ep3, tp3, tp2);
  1643. else if (tp3->type == bt_void && tp2->type != bt_void && ep2!=NULL)
  1644. tp2 = cast_op(&ep2, tp2, tp3);
  1645. if (ep2==NULL)
  1646. tp1 = forceft2(&ep1, tp1, &ep3, tp3);
  1647. else tp1 = forceft2(&ep2, tp2, &ep3, tp3);
  1648. if (tp1 == 0)
  1649. return 0;
  1650. ep2 = mk_node(en_void, ep2, ep3);
  1651. ep1 = mk_node(en_cond, ep1, ep2);
  1652. ep1->etype = tp1->type;
  1653. ep1->esize = tp1->size;
  1654. }
  1655. *node = ep1;
  1656. return tp1;
  1657. }
  1658. TYP *asnop(struct enode **node) {
  1659. /*
  1660. * asnop handles the assignment operators.
  1661. */
  1662. struct enode *ep1, *ep2, *ep3;
  1663. TYP *tp1, *tp2, *tp0;
  1664. enum(e_node) op;
  1665. tp0 = NULL;
  1666. tp1 = conditional(&ep1);
  1667. if (tp1 == 0)
  1668. return 0;
  1669. for (;;) {
  1670. switch (lastst) {
  1671. case assign:
  1672. op = en_assign;
  1673. ascomm: getsym();
  1674. tp2 = asnop(&ep2);
  1675. ascomm2:
  1676. if (tp2 == 0)
  1677. break;
  1678. if (ep1->nodetype != en_ref && ep1->nodetype != en_fieldref) {
  1679. if (ep1->nodetype==en_cast) { /* CAUTION : works well only with en_assign */
  1680. enum(e_node) nt;
  1681. if ((nt=ep1->v.p[0]->nodetype) != en_ref && nt != en_fieldref)
  1682. error(ERR_LVALUE);
  1683. tp2 = cast_op(&ep2, tp2, tp1);
  1684. tp1 = (TYP *)ep1->v.p[1];
  1685. /* } else if (ep1->nodetype==en_cond) {
  1686. struct enode *epvoid=ep1->v.p[1];
  1687. tp1 = cast_op(&epvoid->v.p[0], tp2, tp1);
  1688. tp1 = cast_op(&epvoid->v.p[1], tp2, tp1);*/
  1689. } else error(ERR_LVALUE);
  1690. }
  1691. if (tp1->const_flag)
  1692. uwarn("assignment of read-only lvalue");
  1693. if (tp0 != 0) tp1 = force_cast_op(&ep2, tp2, tp1);
  1694. else tp1 = cast_op(&ep2, tp2, tp1);
  1695. if (tp1 == 0)
  1696. break;
  1697. if (op!=en_assign && !iscomparable(tp1))
  1698. uerr(ERR_ILLTYPE);
  1699. ep1 = mk_node(op, ep1, ep2);
  1700. ep1->etype = tp1->type;
  1701. ep1->esize = tp1->size;
  1702. if (tp0 != 0) tp1 = cast_op(&ep1, tp1, tp0);
  1703. #ifdef INTEL_386
  1704. /* trap struct assigns */
  1705. if (isaggregate(tp1))
  1706. uses_structassign=1;
  1707. #endif
  1708. break;
  1709. case asplus:
  1710. op = en_asadd;
  1711. ascomm3:
  1712. getsym();
  1713. tp2 = asnop(&ep2);
  1714. if (tp2 == 0)
  1715. break;
  1716. if (tp1->type == bt_pointer && integral(tp2)) {
  1717. #ifdef NO_SECURE_POINTERS
  1718. if (tp1->btp->size!=1) {
  1719. cast_op(&ep2, tp2, (TYP *)&tp_short);
  1720. ep3 = mk_icon((long) tp1->btp->size);
  1721. ep3->etype = bt_short;
  1722. ep3->esize = 2;
  1723. ep2 = mk_node(en_mul, ep2, ep3);
  1724. ep2->etype = bt_short;
  1725. ep2->esize = 2;
  1726. tp2 = cast_op(&ep2, (TYP *)&tp_short, (TYP *)&tp_long);
  1727. } else
  1728. tp2 = cast_op(&ep2, tp2, (TYP *)&tp_long);
  1729. tp2 = cast_op(&ep2, (TYP *)&tp_long, tp1);
  1730. #else
  1731. cast_op(&ep2, tp2, (TYP *)&tp_long);
  1732. ep3 = mk_icon((long) tp1->btp->size);
  1733. ep3->etype = bt_long;
  1734. ep3->esize = 4;
  1735. ep2 = mk_node(en_mul, ep2, ep3);
  1736. ep2->etype = bt_long;
  1737. ep2->esize = 4;
  1738. tp2 = cast_op(&ep2, (TYP *)&tp_long, tp1);
  1739. #endif
  1740. }
  1741. goto ascomm2;
  1742. case asminus:
  1743. op = en_assub;
  1744. goto ascomm3;
  1745. case astimes:
  1746. op = en_asmul;
  1747. goto ascomm;
  1748. case asdivide:
  1749. op = en_asdiv;
  1750. goto ascomm;
  1751. case asmodop:
  1752. op = en_asmod;
  1753. goto ascomm;
  1754. case aslshift:
  1755. op = en_aslsh;
  1756. goto ascomm;
  1757. case asrshift:
  1758. op = en_asrsh;
  1759. goto ascomm;
  1760. case asand:
  1761. op = en_asand;
  1762. goto ascomm;
  1763. case asor:
  1764. op = en_asor;
  1765. goto ascomm;
  1766. case asuparrow:
  1767. op = en_asxor;
  1768. goto ascomm;
  1769. default:
  1770. goto asexit;
  1771. }
  1772. }
  1773. asexit:
  1774. *node = ep1;
  1775. return tp1;
  1776. }
  1777. TYP *exprnc(struct enode **node) {
  1778. /*
  1779. * evaluate an expression where the comma operator is not legal.
  1780. */
  1781. TYP *tp;
  1782. tp = asnop(node);
  1783. if (tp == 0)
  1784. *node = 0;
  1785. return tp;
  1786. }
  1787. TYP *commaop(struct enode **node) {
  1788. /*
  1789. * evaluate the comma operator. comma operators are kept as void nodes.
  1790. */
  1791. TYP *tp1;
  1792. struct enode *ep1, *ep2;
  1793. tp1 = asnop(&ep1);
  1794. if (tp1 == 0)
  1795. return 0;
  1796. if (lastst == comma) {
  1797. getsym();
  1798. tp1 = commaop(&ep2);
  1799. if (tp1 == 0) {
  1800. error(ERR_IDEXPECT);
  1801. goto coexit;
  1802. }
  1803. ep1 = mk_node(en_void, ep1, ep2);
  1804. ep1->esize = ep2->esize;
  1805. ep1->etype = ep2->etype;
  1806. }
  1807. coexit:*node = ep1;
  1808. return tp1;
  1809. }
  1810. TYP *expression(struct enode **node) {
  1811. /*
  1812. * evaluate an expression where all operators are legal.
  1813. */
  1814. TYP *tp;
  1815. tp = commaop(node);
  1816. if (tp == 0)
  1817. *node = 0;
  1818. return tp;
  1819. }
  1820. int cast_ok(TYP *tp1, TYP *tp2, int need_physically_compatible) {
  1821. /*
  1822. * This is used to tell whether an implicit cast will generate a warning
  1823. *
  1824. * If need_physically_compatible is 0, then we do not make a difference between
  1825. * short[123], short[124], short[0] and short* (otherwise we do because they're
  1826. * not stored in memory the same way).
  1827. */
  1828. if (tp1 == 0 || tp2 == 0)
  1829. return 0;
  1830. if (tp1 == tp2 || tp2->type == bt_void)
  1831. return 1;
  1832. if (bt_integral(tp1->type))
  1833. return (tp1->type^tp2->type)<=1;
  1834. if (tp1->type != tp2->type)
  1835. return 0;
  1836. /* from now on, tp1->type==tp2->type */
  1837. if (tp1->type == bt_pointer) {
  1838. if (need_physically_compatible)
  1839. if (tp1->val_flag!=tp2->val_flag ||
  1840. (tp1->val_flag && tp1->size!=tp2->size))
  1841. return 0;
  1842. return tp1->btp->type == bt_void || cast_ok(tp1->btp, tp2->btp, 1);
  1843. }
  1844. if (tp1->type == bt_func) {
  1845. SYM *sp=tp1->lst.head,*sp2=tp2->lst.head;
  1846. if (!sp || !sp2) // one is only a prototype : OK
  1847. return eq_type(tp1->btp, tp2->btp);
  1848. while (sp && sp2) {
  1849. if (!eq_type(sp->tp,sp2->tp)) return 0;
  1850. sp=sp->next; sp2=sp2->next;
  1851. }
  1852. if (sp || sp2) return 0; // != # of args
  1853. return eq_type(tp1->btp, tp2->btp);
  1854. }
  1855. if (tp1->type == bt_struct || tp1->type == bt_union)
  1856. return (tp1->lst.head == tp2->lst.head);
  1857. return 1;
  1858. }
  1859. TYP *cast_op(struct enode **ep, TYP *tp1, TYP *tp2) {
  1860. struct enode *ep2;
  1861. if (tp1 == 0 || tp2 == 0 || (tp1->type == bt_void && tp2->type != bt_void)) {
  1862. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1863. return 0;
  1864. }
  1865. if (tp1->type == tp2->type) {
  1866. if (!cast_ok(tp1, tp2, 0))
  1867. uwarn("conversion between incompatible %stypes",
  1868. (tp1->type == bt_pointer) ? "pointer " : "");
  1869. if (tp1->type == bt_struct || tp1->type == bt_union) {
  1870. if (tp1->size != tp2->size)
  1871. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1872. }
  1873. #ifdef NO_VERYTRIVIAL_CASTS
  1874. return tp2;
  1875. #endif
  1876. }
  1877. opt0(ep); /* to make a constant really a constant */
  1878. if ((*ep)->nodetype == en_icon) {
  1879. if (integral(tp2) || tp2->type == bt_pointer || tp2->type == bt_void) {
  1880. long j = (*ep)->v.i;
  1881. long d;
  1882. (*ep)->etype = tp2->type;
  1883. (*ep)->esize = tp2->size;
  1884. /*
  1885. * The cast may affect the value of (*ep)->v.i
  1886. */
  1887. (*ep)->v.i = strip_icon(j, tp2->type);
  1888. d = j ^ (*ep)->v.i;
  1889. if (d && d!=0xFFFF0000 && d!=0xFFFFFF00)
  1890. uwarn("cast changed integer constant 0x%08lx to 0x%08lx", j, (*ep)->v.i);
  1891. return tp2;
  1892. #ifndef NOFLOAT
  1893. } else if (tp2->type == bt_float || tp2->type == bt_double) {
  1894. #ifdef XCON_DOESNT_SUPPORT_FLOATS
  1895. #error Fix me now :)
  1896. #endif
  1897. (*ep)->nodetype = en_fcon;
  1898. (*ep)->etype = tp2->type;
  1899. #ifdef PC
  1900. (*ep)->v.f = (double) (*ep)->v.i;
  1901. #else
  1902. #ifdef BCDFLT
  1903. (*ep)->v.f = ffpltof((*ep)->v.i);
  1904. #else
  1905. (*ep)->v.f = (float) (*ep)->v.i;
  1906. #endif
  1907. #endif
  1908. (*ep)->esize =
  1909. #if defined(DOUBLE) || defined(INTEL_386)
  1910. tp2->type == bt_float ? tp_float.size : tp_double.size;
  1911. #else
  1912. float_size;
  1913. #endif
  1914. return tp2;
  1915. #endif
  1916. } else {
  1917. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1918. return 0;
  1919. }
  1920. }
  1921. /* if (tp2->type != bt_void && tp1->size > tp2->size)
  1922. uwarn("cast to a narrower type loses accuracy");*/
  1923. if (tp1->type == bt_pointer && tp2->type != bt_pointer)
  1924. uwarn("cast from pointer to integer is dangerous");
  1925. if (tp2->type == bt_pointer && tp1->size < 4)
  1926. uwarn("cast from short to pointer is dangerous");
  1927. if ((tp1->type == bt_func && tp2->type != bt_func)
  1928. || (tp2->type == bt_func && tp1->type != bt_func))
  1929. uwarn("implicit cast from '%s' to '%s'",type_str(tp1),type_str(tp2));
  1930. if ((bt_uncastable(tp1->type) || bt_uncastable(tp2->type)) && tp1->type!=tp2->type)
  1931. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1932. #ifdef NO_TRIVIAL_CASTS
  1933. if (tp2->size==tp1->size && (tp2->type==tp1->type
  1934. || ((tp2->type^tp1->type)==1 && bt_integral(tp1->type))
  1935. || (tp2->type==bt_pointer && (tp1->type==bt_long || tp1->type==bt_ulong))
  1936. || (tp1->type==bt_pointer && (tp2->type==bt_long || tp2->type==bt_ulong)))) {
  1937. if (needs_trivial_casts((*ep)->nodetype))
  1938. goto trivial_cast_do;
  1939. (*ep)->etype = tp2->type;
  1940. return tp2;
  1941. }
  1942. trivial_cast_do:
  1943. #endif
  1944. ep2 = mk_node(en_cast, *ep, NIL_ENODE);
  1945. ep2->etype = tp2->type;
  1946. ep2->esize = tp2->size;
  1947. *ep = ep2;
  1948. return tp2;
  1949. }
  1950. TYP *force_cast_op(struct enode **ep, TYP *tp1, TYP *tp2) {
  1951. struct enode *ep2;
  1952. if (tp1 == 0 || tp2 == 0 || (tp1->type == bt_void && tp2->type != bt_void)) {
  1953. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1954. return 0;
  1955. }
  1956. if (tp1->type == tp2->type) {
  1957. if (tp1->type == bt_struct || tp1->type == bt_union) {
  1958. if (tp1->size != tp2->size)
  1959. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1960. }
  1961. // return tp2; so that an en_cast enode is generated & the old TYP is saved
  1962. #ifdef NO_VERYTRIVIAL_CASTS
  1963. return tp2;
  1964. #endif
  1965. }
  1966. opt0(ep); /* to make a constant really a constant */
  1967. if ((*ep)->nodetype == en_icon) {
  1968. if (integral(tp2) || tp2->type == bt_pointer || tp2->type == bt_void) {
  1969. long j = (*ep)->v.i;
  1970. (*ep)->etype = tp2->type;
  1971. (*ep)->esize = tp2->size;
  1972. /*
  1973. * The cast may affect the value of (*ep)->v.i
  1974. */
  1975. (*ep)->v.i = strip_icon(j, tp2->type);
  1976. return tp2;
  1977. #ifndef NOFLOAT
  1978. } else if (tp2->type == bt_float || tp2->type == bt_double) {
  1979. #ifdef XCON_DOESNT_SUPPORT_FLOATS
  1980. #error Fix me now :)
  1981. #endif
  1982. (*ep)->nodetype = en_fcon;
  1983. (*ep)->etype = tp2->type;
  1984. #ifdef PC
  1985. (*ep)->v.f = (double) (*ep)->v.i;
  1986. #else
  1987. (*ep)->v.f = ffpltof((*ep)->v.i);
  1988. #endif
  1989. (*ep)->esize =
  1990. #if defined(DOUBLE) || defined(INTEL_386)
  1991. tp2->type == bt_float ? tp_float.size : tp_double.size;
  1992. #else
  1993. float_size;
  1994. #endif
  1995. return tp2;
  1996. #endif
  1997. } else {
  1998. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  1999. return 0;
  2000. }
  2001. }
  2002. if ((bt_uncastable(tp1->type) || bt_uncastable(tp2->type)) && tp1->type!=tp2->type)
  2003. uerr(ERR_CAST,type_str(tp1),type_str(tp2));
  2004. /* if (tp2->type == bt_pointer && tp1->size < 4)
  2005. uwarn("cast from short to pointer is dangerous");*/
  2006. #ifdef NO_TRIVIAL_CASTS
  2007. if (tp2->size==tp1->size && (tp2->type==tp1->type
  2008. || ((tp2->type^tp1->type)==1 && bt_integral(tp1->type))
  2009. || (tp2->type==bt_pointer && (tp1->type==bt_long || tp1->type==bt_ulong))
  2010. || (tp1->type==bt_pointer && (tp2->type==bt_long || tp2->type==bt_ulong)))) {
  2011. if (needs_trivial_casts((*ep)->nodetype))
  2012. goto trivial_cast_do;
  2013. (*ep)->etype = tp2->type;
  2014. return tp2;
  2015. }
  2016. trivial_cast_do:
  2017. #endif
  2018. ep2 = mk_node(en_cast, *ep, (struct enode *)tp1); // the latter is for (int)x=value;
  2019. ep2->etype = tp2->type;
  2020. ep2->esize = tp2->size;
  2021. *ep = ep2;
  2022. return tp2;
  2023. }
  2024. #ifndef isscalar
  2025. int integral(TYP *tp) {
  2026. /* returns true it tp is an integral type */
  2027. return bt_integral(tp->type);
  2028. }
  2029. #endif
  2030. long strip_icon(long i, enum(e_bt) type) {
  2031. /*
  2032. * This function handles the adjustment of integer constants upon
  2033. * casts. It forces the constant into the range acceptable for
  2034. * the given type.
  2035. * This code assumes somehow that longs are 32 bit on the
  2036. * machine that runs the compiler, but how do you get this
  2037. * machine independent?
  2038. */
  2039. switch (type) {
  2040. case bt_uchar: /* 0 .. 255 */
  2041. #ifdef PC
  2042. i &= 0xff;
  2043. #else
  2044. i = (long)((unsigned char)i);
  2045. #endif
  2046. break;
  2047. case bt_char: /* -128 .. 127 */
  2048. #ifdef PC
  2049. i &= 0xff;
  2050. if (i >= 128)
  2051. i |= 0xffffff00;
  2052. #else
  2053. i = (long)((char)i);
  2054. #endif
  2055. break;
  2056. case bt_ushort: /* 0 .. 65535 */
  2057. #ifdef PC
  2058. i &= 0xffff;
  2059. #else
  2060. i = (long)((unsigned short)i);
  2061. #endif
  2062. break;
  2063. case bt_short: /* -32768 .. 32767 */
  2064. #ifdef PC
  2065. i &= 0xffff;
  2066. if (i >= 32768)
  2067. i |= 0xffff0000;
  2068. #else
  2069. i = (long)((short)i);
  2070. #endif
  2071. break;
  2072. }
  2073. return i;
  2074. }
  2075. // vim:ts=4:sw=4