expressc.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  1. /* ------------------------------------------------------------------------- */
  2. /* "expressc" : The expression code generator */
  3. /* */
  4. /* Part of Inform 6.33 */
  5. /* copyright (c) Graham Nelson 1993 - 2014 */
  6. /* */
  7. /* ------------------------------------------------------------------------- */
  8. #include "header.h"
  9. int vivc_flag; /* TRUE if the last code-generated
  10. expression produced a "value in void
  11. context" error: used to help the syntax
  12. analyser recover from unknown-keyword
  13. errors, since unknown keywords are
  14. treated as yet-to-be-defined constants
  15. and thus as values in void context */
  16. /* These data structures are global, because they're too useful to be
  17. static. */
  18. assembly_operand stack_pointer, temp_var1, temp_var2, temp_var3,
  19. temp_var4, zero_operand, one_operand, two_operand, three_operand,
  20. four_operand, valueless_operand;
  21. static void make_operands(void)
  22. {
  23. if (!glulx_mode) {
  24. stack_pointer.type = VARIABLE_OT;
  25. stack_pointer.value = 0;
  26. stack_pointer.marker = 0;
  27. temp_var1.type = VARIABLE_OT;
  28. temp_var1.value = 255;
  29. temp_var1.marker = 0;
  30. temp_var2.type = VARIABLE_OT;
  31. temp_var2.value = 254;
  32. temp_var2.marker = 0;
  33. temp_var3.type = VARIABLE_OT;
  34. temp_var3.value = 253;
  35. temp_var3.marker = 0;
  36. temp_var4.type = VARIABLE_OT;
  37. temp_var4.value = 252;
  38. temp_var4.marker = 0;
  39. zero_operand.type = SHORT_CONSTANT_OT;
  40. zero_operand.value = 0;
  41. zero_operand.marker = 0;
  42. one_operand.type = SHORT_CONSTANT_OT;
  43. one_operand.value = 1;
  44. one_operand.marker = 0;
  45. two_operand.type = SHORT_CONSTANT_OT;
  46. two_operand.value = 2;
  47. two_operand.marker = 0;
  48. three_operand.type = SHORT_CONSTANT_OT;
  49. three_operand.value = 3;
  50. three_operand.marker = 0;
  51. four_operand.type = SHORT_CONSTANT_OT;
  52. four_operand.value = 4;
  53. four_operand.marker = 0;
  54. valueless_operand.type = OMITTED_OT;
  55. valueless_operand.value = 0;
  56. valueless_operand.marker = 0;
  57. }
  58. else {
  59. stack_pointer.type = LOCALVAR_OT;
  60. stack_pointer.value = 0;
  61. stack_pointer.marker = 0;
  62. temp_var1.type = GLOBALVAR_OT;
  63. temp_var1.value = MAX_LOCAL_VARIABLES+0;
  64. temp_var1.marker = 0;
  65. temp_var2.type = GLOBALVAR_OT;
  66. temp_var2.value = MAX_LOCAL_VARIABLES+1;
  67. temp_var2.marker = 0;
  68. temp_var3.type = GLOBALVAR_OT;
  69. temp_var3.value = MAX_LOCAL_VARIABLES+2;
  70. temp_var3.marker = 0;
  71. temp_var4.type = GLOBALVAR_OT;
  72. temp_var4.value = MAX_LOCAL_VARIABLES+3;
  73. temp_var4.marker = 0;
  74. zero_operand.type = ZEROCONSTANT_OT;
  75. zero_operand.value = 0;
  76. zero_operand.marker = 0;
  77. one_operand.type = BYTECONSTANT_OT;
  78. one_operand.value = 1;
  79. one_operand.marker = 0;
  80. two_operand.type = BYTECONSTANT_OT;
  81. two_operand.value = 2;
  82. two_operand.marker = 0;
  83. three_operand.type = BYTECONSTANT_OT;
  84. three_operand.value = 3;
  85. three_operand.marker = 0;
  86. four_operand.type = BYTECONSTANT_OT;
  87. four_operand.value = 4;
  88. four_operand.marker = 0;
  89. valueless_operand.type = OMITTED_OT;
  90. valueless_operand.value = 0;
  91. valueless_operand.marker = 0;
  92. }
  93. }
  94. /* ------------------------------------------------------------------------- */
  95. /* The table of conditionals. (Only used in Glulx) */
  96. #define ZERO_CC (500)
  97. #define EQUAL_CC (502)
  98. #define LT_CC (504)
  99. #define GT_CC (506)
  100. #define HAS_CC (508)
  101. #define IN_CC (510)
  102. #define OFCLASS_CC (512)
  103. #define PROVIDES_CC (514)
  104. #define FIRST_CC (500)
  105. #define LAST_CC (515)
  106. typedef struct condclass_s {
  107. int32 posform; /* Opcode for the conditional in its positive form. */
  108. int32 negform; /* Opcode for the conditional in its negated form. */
  109. } condclass;
  110. condclass condclasses[] = {
  111. { jz_gc, jnz_gc },
  112. { jeq_gc, jne_gc },
  113. { jlt_gc, jge_gc },
  114. { jgt_gc, jle_gc },
  115. { -1, -1 },
  116. { -1, -1 },
  117. { -1, -1 },
  118. { -1, -1 }
  119. };
  120. /* ------------------------------------------------------------------------- */
  121. /* The table of operators.
  122. The ordering in this table is not significant except that it must match
  123. the #define's in "header.h" */
  124. operator operators[NUM_OPERATORS] =
  125. {
  126. /* ------------------------ */
  127. /* Level 0: , */
  128. /* ------------------------ */
  129. { 0, SEP_TT, COMMA_SEP, IN_U, L_A, 0, -1, -1, 0, 0, "comma" },
  130. /* ------------------------ */
  131. /* Level 1: = */
  132. /* ------------------------ */
  133. { 1, SEP_TT, SETEQUALS_SEP, IN_U, R_A, 1, -1, -1, 1, 0,
  134. "assignment operator '='" },
  135. /* ------------------------ */
  136. /* Level 2: ~~ && || */
  137. /* ------------------------ */
  138. { 2, SEP_TT, LOGAND_SEP, IN_U, L_A, 0, -1, -1, 0, LOGOR_OP,
  139. "logical conjunction '&&'" },
  140. { 2, SEP_TT, LOGOR_SEP, IN_U, L_A, 0, -1, -1, 0, LOGAND_OP,
  141. "logical disjunction '||'" },
  142. { 2, SEP_TT, LOGNOT_SEP, PRE_U, R_A, 0, -1, -1, 0, LOGNOT_OP,
  143. "logical negation '~~'" },
  144. /* ------------------------ */
  145. /* Level 3: == ~= */
  146. /* > >= < <= */
  147. /* has hasnt */
  148. /* in notin */
  149. /* provides */
  150. /* ofclass */
  151. /* ------------------------ */
  152. { 3, -1, -1, -1, 0, 0, 400 + jz_zc, ZERO_CC+0, 0, NONZERO_OP,
  153. "expression used as condition then negated" },
  154. { 3, -1, -1, -1, 0, 0, 800 + jz_zc, ZERO_CC+1, 0, ZERO_OP,
  155. "expression used as condition" },
  156. { 3, SEP_TT, CONDEQUALS_SEP, IN_U, 0, 0, 400 + je_zc, EQUAL_CC+0, 0, NOTEQUAL_OP,
  157. "'==' condition" },
  158. { 3, SEP_TT, NOTEQUAL_SEP, IN_U, 0, 0, 800 + je_zc, EQUAL_CC+1, 0, CONDEQUALS_OP,
  159. "'~=' condition" },
  160. { 3, SEP_TT, GE_SEP, IN_U, 0, 0, 800 + jl_zc, LT_CC+1, 0, LESS_OP,
  161. "'>=' condition" },
  162. { 3, SEP_TT, GREATER_SEP, IN_U, 0, 0, 400 + jg_zc, GT_CC+0, 0, LE_OP,
  163. "'>' condition" },
  164. { 3, SEP_TT, LE_SEP, IN_U, 0, 0, 800 + jg_zc, GT_CC+1, 0, GREATER_OP,
  165. "'<=' condition" },
  166. { 3, SEP_TT, LESS_SEP, IN_U, 0, 0, 400 + jl_zc, LT_CC+0, 0, GE_OP,
  167. "'<' condition" },
  168. { 3, CND_TT, HAS_COND, IN_U, 0, 0, 400 + test_attr_zc, HAS_CC+0, 0, HASNT_OP,
  169. "'has' condition" },
  170. { 3, CND_TT, HASNT_COND, IN_U, 0, 0, 800 + test_attr_zc, HAS_CC+1, 0, HAS_OP,
  171. "'hasnt' condition" },
  172. { 3, CND_TT, IN_COND, IN_U, 0, 0, 400 + jin_zc, IN_CC+0, 0, NOTIN_OP,
  173. "'in' condition" },
  174. { 3, CND_TT, NOTIN_COND, IN_U, 0, 0, 800 + jin_zc, IN_CC+1, 0, IN_OP,
  175. "'notin' condition" },
  176. { 3, CND_TT, OFCLASS_COND, IN_U, 0, 0, 600, OFCLASS_CC+0, 0, NOTOFCLASS_OP,
  177. "'ofclass' condition" },
  178. { 3, CND_TT, PROVIDES_COND, IN_U, 0, 0, 601, PROVIDES_CC+0, 0, NOTPROVIDES_OP,
  179. "'provides' condition" },
  180. { 3, -1, -1, -1, 0, 0, 1000, OFCLASS_CC+1, 0, OFCLASS_OP,
  181. "negated 'ofclass' condition" },
  182. { 3, -1, -1, -1, 0, 0, 1001, PROVIDES_CC+1, 0, PROVIDES_OP,
  183. "negated 'provides' condition" },
  184. /* ------------------------ */
  185. /* Level 4: or */
  186. /* ------------------------ */
  187. { 4, CND_TT, OR_COND, IN_U, L_A, 0, -1, -1, 0, 0, "'or'" },
  188. /* ------------------------ */
  189. /* Level 5: + binary - */
  190. /* ------------------------ */
  191. { 5, SEP_TT, PLUS_SEP, IN_U, L_A, 0, add_zc, add_gc, 0, 0, "'+'" },
  192. { 5, SEP_TT, MINUS_SEP, IN_U, L_A, 0, sub_zc, sub_gc, 0, 0, "'-'" },
  193. /* ------------------------ */
  194. /* Level 6: * / % */
  195. /* & | ~ */
  196. /* ------------------------ */
  197. { 6, SEP_TT, TIMES_SEP, IN_U, L_A, 0, mul_zc, mul_gc, 0, 0, "'*'" },
  198. { 6, SEP_TT, DIVIDE_SEP, IN_U, L_A, 0, div_zc, div_gc, 0, 0, "'/'" },
  199. { 6, SEP_TT, REMAINDER_SEP, IN_U, L_A, 0, mod_zc, mod_gc, 0, 0,
  200. "remainder after division '%'" },
  201. { 6, SEP_TT, ARTAND_SEP, IN_U, L_A, 0, and_zc, bitand_gc, 0, 0,
  202. "bitwise AND '&'" },
  203. { 6, SEP_TT, ARTOR_SEP, IN_U, L_A, 0, or_zc, bitor_gc, 0, 0,
  204. "bitwise OR '|'" },
  205. { 6, SEP_TT, ARTNOT_SEP, PRE_U, R_A, 0, -1, bitnot_gc, 0, 0,
  206. "bitwise NOT '~'" },
  207. /* ------------------------ */
  208. /* Level 7: -> --> */
  209. /* ------------------------ */
  210. { 7, SEP_TT, ARROW_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  211. "byte array operator '->'" },
  212. { 7, SEP_TT, DARROW_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  213. "word array operator '-->'" },
  214. /* ------------------------ */
  215. /* Level 8: unary - */
  216. /* ------------------------ */
  217. { 8, SEP_TT, UNARY_MINUS_SEP, PRE_U, R_A, 0, -1, neg_gc, 0, 0,
  218. "unary minus" },
  219. /* ------------------------ */
  220. /* Level 9: ++ -- */
  221. /* (prefix or postfix) */
  222. /* ------------------------ */
  223. { 9, SEP_TT, INC_SEP, PRE_U, R_A, 2, -1, -1, 1, 0,
  224. "pre-increment operator '++'" },
  225. { 9, SEP_TT, POST_INC_SEP, POST_U, R_A, 3, -1, -1, 1, 0,
  226. "post-increment operator '++'" },
  227. { 9, SEP_TT, DEC_SEP, PRE_U, R_A, 4, -1, -1, 1, 0,
  228. "pre-decrement operator '--'" },
  229. { 9, SEP_TT, POST_DEC_SEP, POST_U, R_A, 5, -1, -1, 1, 0,
  230. "post-decrement operator '--'" },
  231. /* ------------------------ */
  232. /* Level 10: .& .# */
  233. /* ..& ..# */
  234. /* ------------------------ */
  235. {10, SEP_TT, PROPADD_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  236. "property address operator '.&'" },
  237. {10, SEP_TT, PROPNUM_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  238. "property length operator '.#'" },
  239. {10, SEP_TT, MPROPADD_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  240. "individual property address operator '..&'" },
  241. {10, SEP_TT, MPROPNUM_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  242. "individual property length operator '..#'" },
  243. /* ------------------------ */
  244. /* Level 11: function ( */
  245. /* ------------------------ */
  246. {11, SEP_TT, OPENB_SEP, IN_U, L_A, 0, -1, -1, 1, 0,
  247. "function call" },
  248. /* ------------------------ */
  249. /* Level 12: . .. */
  250. /* ------------------------ */
  251. {12, SEP_TT, MESSAGE_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  252. "individual property selector '..'" },
  253. {12, SEP_TT, PROPERTY_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  254. "property selector '.'" },
  255. /* ------------------------ */
  256. /* Level 13: :: */
  257. /* ------------------------ */
  258. {13, SEP_TT, SUPERCLASS_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
  259. "superclass operator '::'" },
  260. /* ------------------------ */
  261. /* Miscellaneous operators */
  262. /* generated at lvalue */
  263. /* checking time */
  264. /* ------------------------ */
  265. { 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -> = */
  266. "byte array entry assignment" },
  267. { 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* --> = */
  268. "word array entry assignment" },
  269. { 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* .. = */
  270. "individual property assignment" },
  271. { 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* . = */
  272. "common property assignment" },
  273. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ -> */
  274. "byte array entry preincrement" },
  275. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ --> */
  276. "word array entry preincrement" },
  277. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ .. */
  278. "individual property preincrement" },
  279. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ . */
  280. "common property preincrement" },
  281. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- -> */
  282. "byte array entry predecrement" },
  283. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- --> */
  284. "word array entry predecrement" },
  285. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- .. */
  286. "individual property predecrement" },
  287. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- . */
  288. "common property predecrement" },
  289. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -> ++ */
  290. "byte array entry postincrement" },
  291. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* --> ++ */
  292. "word array entry postincrement" },
  293. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* .. ++ */
  294. "individual property postincrement" },
  295. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* . ++ */
  296. "common property postincrement" },
  297. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -> -- */
  298. "byte array entry postdecrement" },
  299. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* --> -- */
  300. "word array entry postdecrement" },
  301. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* .. -- */
  302. "individual property postdecrement" },
  303. { 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* . -- */
  304. "common property postdecrement" },
  305. {11, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* x.y(args) */
  306. "call to common property" },
  307. {11, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* x..y(args) */
  308. "call to individual property" },
  309. /* ------------------------ */
  310. /* And one Glulx-only op */
  311. /* which just pushes its */
  312. /* argument on the stack, */
  313. /* unchanged. */
  314. /* ------------------------ */
  315. {14, -1, -1, -1, -1, 0, -1, -1, 1, 0,
  316. "push on stack" }
  317. };
  318. /* --- Condition annotater ------------------------------------------------- */
  319. static void annotate_for_conditions(int n, int a, int b)
  320. { int i, opnum = ET[n].operator_number;
  321. ET[n].label_after = -1;
  322. ET[n].to_expression = FALSE;
  323. ET[n].true_label = a;
  324. ET[n].false_label = b;
  325. if (ET[n].down == -1) return;
  326. if ((operators[opnum].precedence == 2)
  327. || (operators[opnum].precedence == 3))
  328. { if ((a == -1) && (b == -1))
  329. { if (opnum == LOGAND_OP)
  330. { b = next_label++;
  331. ET[n].false_label = b;
  332. ET[n].to_expression = TRUE;
  333. }
  334. else
  335. { a = next_label++;
  336. ET[n].true_label = a;
  337. ET[n].to_expression = TRUE;
  338. }
  339. }
  340. }
  341. switch(opnum)
  342. { case LOGAND_OP:
  343. if (b == -1)
  344. { b = next_label++;
  345. ET[n].false_label = b;
  346. ET[n].label_after = b;
  347. }
  348. annotate_for_conditions(ET[n].down, -1, b);
  349. if (b == ET[n].label_after)
  350. annotate_for_conditions(ET[ET[n].down].right, a, -1);
  351. else annotate_for_conditions(ET[ET[n].down].right, a, b);
  352. return;
  353. case LOGOR_OP:
  354. if (a == -1)
  355. { a = next_label++;
  356. ET[n].true_label = a;
  357. ET[n].label_after = a;
  358. }
  359. annotate_for_conditions(ET[n].down, a, -1);
  360. if (a == ET[n].label_after)
  361. annotate_for_conditions(ET[ET[n].down].right, -1, b);
  362. else annotate_for_conditions(ET[ET[n].down].right, a, b);
  363. return;
  364. }
  365. i = ET[n].down;
  366. while (i != -1)
  367. { annotate_for_conditions(i, -1, -1); i = ET[i].right; }
  368. }
  369. /* --- Code generator ------------------------------------------------------ */
  370. static void value_in_void_context_z(assembly_operand AO)
  371. { char *t;
  372. ASSERT_ZCODE();
  373. switch(AO.type)
  374. { case LONG_CONSTANT_OT:
  375. case SHORT_CONSTANT_OT:
  376. t = "<constant>";
  377. if (AO.marker == SYMBOL_MV)
  378. t = (char *) (symbs[AO.value]);
  379. break;
  380. case VARIABLE_OT:
  381. t = variable_name(AO.value);
  382. break;
  383. default:
  384. compiler_error("Unable to print value in void context");
  385. t = "<expression>";
  386. break;
  387. }
  388. vivc_flag = TRUE;
  389. if (strcmp(t, "print_paddr") == 0)
  390. obsolete_warning("ignoring 'print_paddr': use 'print (string)' instead");
  391. else
  392. if (strcmp(t, "print_addr") == 0)
  393. obsolete_warning("ignoring 'print_addr': use 'print (address)' instead");
  394. else
  395. if (strcmp(t, "print_char") == 0)
  396. obsolete_warning("ignoring 'print_char': use 'print (char)' instead");
  397. else
  398. ebf_error("expression with side-effects", t);
  399. }
  400. static void write_result_z(assembly_operand to, assembly_operand from)
  401. { if (to.value == from.value) return;
  402. if (to.value == 0) assemblez_1(push_zc, from);
  403. else assemblez_store(to, from);
  404. }
  405. static void pop_zm_stack(void)
  406. { assembly_operand st;
  407. if (version_number < 5) assemblez_0(pop_zc);
  408. else
  409. { st.marker = 0; st.type = VARIABLE_OT; st.value = 0;
  410. assemblez_1_branch(jz_zc, st, -2, TRUE);
  411. }
  412. }
  413. static void access_memory_z(int oc, assembly_operand AO1, assembly_operand AO2,
  414. assembly_operand AO3)
  415. { int vr;
  416. assembly_operand zero_ao, max_ao, size_ao, en_ao, type_ao, an_ao,
  417. index_ao;
  418. int x, y, byte_flag, read_flag, from_module;
  419. if (AO1.marker == ARRAY_MV)
  420. {
  421. if ((oc == loadb_zc) || (oc == storeb_zc)) byte_flag=TRUE;
  422. else byte_flag = FALSE;
  423. if ((oc == loadb_zc) || (oc == loadw_zc)) read_flag=TRUE;
  424. else read_flag = FALSE;
  425. zero_ao.type = SHORT_CONSTANT_OT;
  426. zero_ao.value = 0; zero_ao.marker = 0;
  427. size_ao = zero_ao; size_ao.value = -1;
  428. for (x=0; x<no_arrays; x++)
  429. { if (AO1.value == svals[array_symbols[x]])
  430. { size_ao.value = array_sizes[x]; y=x;
  431. }
  432. }
  433. if (size_ao.value==-1)
  434. from_module=TRUE;
  435. else {
  436. from_module=FALSE;
  437. type_ao = zero_ao; type_ao.value = array_types[y];
  438. if ((!is_systemfile()))
  439. { if (byte_flag)
  440. {
  441. if ((array_types[y] == WORD_ARRAY)
  442. || (array_types[y] == TABLE_ARRAY))
  443. warning("Using '->' to access a --> or table array");
  444. }
  445. else
  446. {
  447. if ((array_types[y] == BYTE_ARRAY)
  448. || (array_types[y] == STRING_ARRAY))
  449. warning("Using '-->' to access a -> or string array");
  450. }
  451. }
  452. }
  453. }
  454. if ((!runtime_error_checking_switch) || (veneer_mode))
  455. { if ((oc == loadb_zc) || (oc == loadw_zc))
  456. assemblez_2_to(oc, AO1, AO2, AO3);
  457. else
  458. assemblez_3(oc, AO1, AO2, AO3);
  459. return;
  460. }
  461. /* If we recognise AO1 as arising textually from a declared
  462. array, we can check bounds explicitly. */
  463. if ((AO1.marker == ARRAY_MV) && (!from_module))
  464. {
  465. int passed_label = next_label++, failed_label = next_label++,
  466. final_label = next_label++;
  467. /* Calculate the largest permitted array entry + 1
  468. Here "size_ao.value" = largest permitted entry of its own kind */
  469. max_ao = size_ao;
  470. if (byte_flag
  471. && ((array_types[y] == WORD_ARRAY)
  472. || (array_types[y] == TABLE_ARRAY)))
  473. { max_ao.value = size_ao.value*2 + 1;
  474. type_ao.value += 8;
  475. }
  476. if ((!byte_flag)
  477. && ((array_types[y] == BYTE_ARRAY)
  478. || (array_types[y] == STRING_ARRAY)
  479. || (array_types[y] == BUFFER_ARRAY)))
  480. { if ((size_ao.value % 2) == 0)
  481. max_ao.value = size_ao.value/2 - 1;
  482. else max_ao.value = (size_ao.value-1)/2;
  483. type_ao.value += 16;
  484. }
  485. max_ao.value++;
  486. if (size_ao.value >= 256) size_ao.type = LONG_CONSTANT_OT;
  487. if (max_ao.value >= 256) max_ao.type = LONG_CONSTANT_OT;
  488. /* Can't write to the size entry in a string or table */
  489. if (((array_types[y] == STRING_ARRAY)
  490. || (array_types[y] == TABLE_ARRAY))
  491. && (!read_flag))
  492. { if ((array_types[y] == TABLE_ARRAY) && byte_flag)
  493. zero_ao.value = 2;
  494. else zero_ao.value = 1;
  495. }
  496. en_ao = zero_ao; en_ao.value = ABOUNDS_RTE;
  497. switch(oc) { case loadb_zc: en_ao.value = ABOUNDS_RTE; break;
  498. case loadw_zc: en_ao.value = ABOUNDS_RTE+1; break;
  499. case storeb_zc: en_ao.value = ABOUNDS_RTE+2; break;
  500. case storew_zc: en_ao.value = ABOUNDS_RTE+3; break; }
  501. index_ao = AO2;
  502. if ((AO2.type == VARIABLE_OT)&&(AO2.value == 0))
  503. { assemblez_store(temp_var2, AO2);
  504. assemblez_store(AO2, temp_var2);
  505. index_ao = temp_var2;
  506. }
  507. assemblez_2_branch(jl_zc, index_ao, zero_ao, failed_label, TRUE);
  508. assemblez_2_branch(jl_zc, index_ao, max_ao, passed_label, TRUE);
  509. assemble_label_no(failed_label);
  510. an_ao = zero_ao; an_ao.value = y;
  511. assemblez_6(call_vn2_zc, veneer_routine(RT__Err_VR), en_ao,
  512. index_ao, size_ao, type_ao, an_ao);
  513. /* We have to clear any of AO1, AO2, AO3 off the stack if
  514. present, so that we can achieve the same effect on the stack
  515. that executing the opcode would have had */
  516. if ((AO1.type == VARIABLE_OT) && (AO1.value == 0)) pop_zm_stack();
  517. if ((AO2.type == VARIABLE_OT) && (AO2.value == 0)) pop_zm_stack();
  518. if ((AO3.type == VARIABLE_OT) && (AO3.value == 0))
  519. { if ((oc == loadb_zc) || (oc == loadw_zc))
  520. { assemblez_store(AO3, zero_ao);
  521. }
  522. else pop_zm_stack();
  523. }
  524. assemblez_jump(final_label);
  525. assemble_label_no(passed_label);
  526. if ((oc == loadb_zc) || (oc == loadw_zc))
  527. assemblez_2_to(oc, AO1, AO2, AO3);
  528. else
  529. assemblez_3(oc, AO1, AO2, AO3);
  530. assemble_label_no(final_label);
  531. return;
  532. }
  533. /* Otherwise, compile a call to the veneer which verifies that
  534. the proposed read/write is within dynamic Z-machine memory. */
  535. switch(oc) { case loadb_zc: vr = RT__ChLDB_VR; break;
  536. case loadw_zc: vr = RT__ChLDW_VR; break;
  537. case storeb_zc: vr = RT__ChSTB_VR; break;
  538. case storew_zc: vr = RT__ChSTW_VR; break; }
  539. if ((oc == loadb_zc) || (oc == loadw_zc))
  540. assemblez_3_to(call_vs_zc, veneer_routine(vr), AO1, AO2, AO3);
  541. else
  542. assemblez_4(call_vn_zc, veneer_routine(vr), AO1, AO2, AO3);
  543. }
  544. static assembly_operand check_nonzero_at_runtime_z(assembly_operand AO1,
  545. int error_label, int rte_number)
  546. { assembly_operand AO2, AO3;
  547. int check_sp = FALSE, passed_label, failed_label, last_label;
  548. if (veneer_mode) return AO1;
  549. /* Assemble to code to check that the operand AO1 is ofclass Object:
  550. if it is, execution should continue and the stack should be
  551. unchanged. Otherwise, call the veneer's run-time-error routine
  552. with the given error number, and then: if the label isn't -1,
  553. switch execution to this label, with the value popped from
  554. the stack if it was on the stack in the first place;
  555. if the label is -1, either replace the top of the stack with
  556. the constant 2, or return the operand (short constant) 2.
  557. The point of 2 is that object 2 is the class-object Object
  558. and therefore has no parent, child or sibling, so that the
  559. built-in tree functions will safely return 0 on this object. */
  560. /* Sometimes we can already see that the object number is valid. */
  561. if (((AO1.type == LONG_CONSTANT_OT) || (AO1.type == SHORT_CONSTANT_OT))
  562. && (AO1.marker == 0) && (AO1.value >= 1) && (AO1.value < no_objects))
  563. return AO1;
  564. passed_label = next_label++;
  565. failed_label = next_label++;
  566. AO2.type = LONG_CONSTANT_OT;
  567. AO2.value = actual_largest_object_SC;
  568. AO2.marker = INCON_MV;
  569. AO3.value = 5; AO3.type = SHORT_CONSTANT_OT; AO3.marker = 0;
  570. if ((rte_number == IN_RTE) || (rte_number == HAS_RTE)
  571. || (rte_number == PROPERTY_RTE) || (rte_number == PROP_NUM_RTE)
  572. || (rte_number == PROP_ADD_RTE))
  573. { /* Allow classes */
  574. AO3.value = 1;
  575. if ((AO1.type == VARIABLE_OT) && (AO1.value == 0))
  576. { /* That is, if AO1 is the stack pointer */
  577. check_sp = TRUE;
  578. assemblez_store(temp_var2, AO1);
  579. assemblez_store(AO1, temp_var2);
  580. assemblez_2_branch(jg_zc, AO3, temp_var2, failed_label, TRUE);
  581. assemblez_2_branch(jg_zc, temp_var2, AO2, passed_label, FALSE);
  582. }
  583. else
  584. { assemblez_2_branch(jg_zc, AO3, AO1, failed_label, TRUE);
  585. assemblez_2_branch(jg_zc, AO1, AO2, passed_label, FALSE);
  586. }
  587. }
  588. else
  589. { if ((AO1.type == VARIABLE_OT) && (AO1.value == 0))
  590. { /* That is, if AO1 is the stack pointer */
  591. check_sp = TRUE;
  592. assemblez_store(temp_var2, AO1);
  593. assemblez_store(AO1, temp_var2);
  594. assemblez_2_branch(jg_zc, AO3, temp_var2, failed_label, TRUE);
  595. assemblez_2_branch(jg_zc, temp_var2, AO2, failed_label, TRUE);
  596. AO3.value = 1;
  597. assemblez_2_branch(jin_zc, temp_var2, AO3, passed_label, FALSE);
  598. }
  599. else
  600. { assemblez_2_branch(jg_zc, AO3, AO1, failed_label, TRUE);
  601. assemblez_2_branch(jg_zc, AO1, AO2, failed_label, TRUE);
  602. AO3.value = 1;
  603. assemblez_2_branch(jin_zc, AO1, AO3, passed_label, FALSE);
  604. }
  605. }
  606. assemble_label_no(failed_label);
  607. AO2.type = SHORT_CONSTANT_OT; AO2.value = rte_number; AO2.marker = 0;
  608. if (version_number >= 5)
  609. assemblez_3(call_vn_zc, veneer_routine(RT__Err_VR), AO2, AO1);
  610. else
  611. assemblez_3_to(call_zc, veneer_routine(RT__Err_VR), AO2, AO1, temp_var2);
  612. if (error_label != -1)
  613. { /* Jump to the error label */
  614. if (error_label == -3) assemblez_0(rfalse_zc);
  615. else if (error_label == -4) assemblez_0(rtrue_zc);
  616. else assemblez_jump(error_label);
  617. }
  618. else
  619. { if (check_sp)
  620. { /* Push the short constant 2 */
  621. AO2.type = SHORT_CONSTANT_OT; AO2.value = 2; AO2.marker = 0;
  622. assemblez_store(AO1, AO2);
  623. }
  624. else
  625. { /* Store either short constant 2 or the operand's value in
  626. the temporary variable */
  627. AO2.type = SHORT_CONSTANT_OT; AO2.value = 2; AO2.marker = 0;
  628. AO3 = temp_var2; assemblez_store(AO3, AO2);
  629. last_label = next_label++;
  630. assemblez_jump(last_label);
  631. assemble_label_no(passed_label);
  632. assemblez_store(AO3, AO1);
  633. assemble_label_no(last_label);
  634. return AO3;
  635. }
  636. }
  637. assemble_label_no(passed_label);
  638. return AO1;
  639. }
  640. static void compile_conditional_z(int oc,
  641. assembly_operand AO1, assembly_operand AO2, int label, int flag)
  642. { assembly_operand AO3; int the_zc, error_label = label,
  643. va_flag = FALSE, va_label;
  644. ASSERT_ZCODE();
  645. if (oc<200)
  646. { if ((runtime_error_checking_switch) && (oc == jin_zc))
  647. { if (flag) error_label = next_label++;
  648. AO1 = check_nonzero_at_runtime(AO1, error_label, IN_RTE);
  649. }
  650. if ((runtime_error_checking_switch) && (oc == test_attr_zc))
  651. { if (flag) error_label = next_label++;
  652. AO1 = check_nonzero_at_runtime(AO1, error_label, HAS_RTE);
  653. switch(AO2.type)
  654. { case SHORT_CONSTANT_OT:
  655. case LONG_CONSTANT_OT:
  656. if (AO2.marker == 0)
  657. { if ((AO2.value < 0) || (AO2.value > 47))
  658. error("'has'/'hasnt' applied to illegal attribute number");
  659. break;
  660. }
  661. case VARIABLE_OT:
  662. { int pa_label = next_label++, fa_label = next_label++;
  663. assembly_operand en_ao, zero_ao, max_ao;
  664. assemblez_store(temp_var1, AO1);
  665. if ((AO1.type == VARIABLE_OT)&&(AO1.value == 0))
  666. assemblez_store(AO1, temp_var1);
  667. assemblez_store(temp_var2, AO2);
  668. if ((AO2.type == VARIABLE_OT)&&(AO2.value == 0))
  669. assemblez_store(AO2, temp_var2);
  670. zero_ao.type = SHORT_CONSTANT_OT; zero_ao.marker = 0;
  671. zero_ao.value = 0; max_ao = zero_ao; max_ao.value = 48;
  672. assemblez_2_branch(jl_zc,temp_var2,zero_ao,fa_label,TRUE);
  673. assemblez_2_branch(jl_zc,temp_var2,max_ao,pa_label,TRUE);
  674. assemble_label_no(fa_label);
  675. en_ao = zero_ao; en_ao.value = 19;
  676. assemblez_4(call_vn_zc, veneer_routine(RT__Err_VR),
  677. en_ao, temp_var1, temp_var2);
  678. va_flag = TRUE; va_label = next_label++;
  679. assemblez_jump(va_label);
  680. assemble_label_no(pa_label);
  681. }
  682. }
  683. }
  684. assemblez_2_branch(oc, AO1, AO2, label, flag);
  685. if (error_label != label) assemble_label_no(error_label);
  686. if (va_flag) assemble_label_no(va_label);
  687. return;
  688. }
  689. AO3.type = VARIABLE_OT; AO3.value = 0; AO3.marker = 0;
  690. the_zc = (version_number == 3)?call_zc:call_vs_zc;
  691. if (oc == 201)
  692. assemblez_3_to(the_zc, veneer_routine(OP__Pr_VR), AO1, AO2, AO3);
  693. else
  694. assemblez_3_to(the_zc, veneer_routine(OC__Cl_VR), AO1, AO2, AO3);
  695. assemblez_1_branch(jz_zc, AO3, label, !flag);
  696. }
  697. static void value_in_void_context_g(assembly_operand AO)
  698. { char *t;
  699. ASSERT_GLULX();
  700. switch(AO.type)
  701. { case CONSTANT_OT:
  702. case HALFCONSTANT_OT:
  703. case BYTECONSTANT_OT:
  704. case ZEROCONSTANT_OT:
  705. t = "<constant>";
  706. if (AO.marker == SYMBOL_MV)
  707. t = (char *) (symbs[AO.value]);
  708. break;
  709. case GLOBALVAR_OT:
  710. case LOCALVAR_OT:
  711. t = variable_name(AO.value);
  712. break;
  713. default:
  714. compiler_error("Unable to print value in void context");
  715. t = "<expression>";
  716. break;
  717. }
  718. vivc_flag = TRUE;
  719. ebf_error("expression with side-effects", t);
  720. }
  721. static void write_result_g(assembly_operand to, assembly_operand from)
  722. { if (to.value == from.value && to.type == from.type) return;
  723. assembleg_store(to, from);
  724. }
  725. static void access_memory_g(int oc, assembly_operand AO1, assembly_operand AO2,
  726. assembly_operand AO3)
  727. { int vr;
  728. int data_len, read_flag;
  729. assembly_operand zero_ao, max_ao, size_ao, en_ao, type_ao, an_ao,
  730. index_ao, five_ao;
  731. int passed_label, failed_label, final_label, x, y;
  732. if ((oc == aloadb_gc) || (oc == astoreb_gc)) data_len = 1;
  733. else if ((oc == aloads_gc) || (oc == astores_gc)) data_len = 2;
  734. else data_len = 4;
  735. if ((oc == aloadb_gc) || (oc == aloads_gc) || (oc == aload_gc))
  736. read_flag = TRUE;
  737. else
  738. read_flag = FALSE;
  739. if (AO1.marker == ARRAY_MV)
  740. {
  741. zero_ao.value = 0; zero_ao.marker = 0;
  742. size_ao = zero_ao; size_ao.value = -1;
  743. for (x=0; x<no_arrays; x++)
  744. { if (AO1.value == svals[array_symbols[x]])
  745. { size_ao.value = array_sizes[x]; y=x;
  746. }
  747. }
  748. if (size_ao.value==-1) compiler_error("Array size can't be found");
  749. type_ao = zero_ao; type_ao.value = array_types[y];
  750. if ((!is_systemfile()))
  751. { if (data_len == 1)
  752. {
  753. if ((array_types[y] == WORD_ARRAY)
  754. || (array_types[y] == TABLE_ARRAY))
  755. warning("Using '->' to access a --> or table array");
  756. }
  757. else
  758. {
  759. if ((array_types[y] == BYTE_ARRAY)
  760. || (array_types[y] == STRING_ARRAY))
  761. warning("Using '-->' to access a -> or string array");
  762. }
  763. }
  764. }
  765. if ((!runtime_error_checking_switch) || (veneer_mode))
  766. {
  767. assembleg_3(oc, AO1, AO2, AO3);
  768. return;
  769. }
  770. /* If we recognise AO1 as arising textually from a declared
  771. array, we can check bounds explicitly. */
  772. if (AO1.marker == ARRAY_MV)
  773. {
  774. /* Calculate the largest permitted array entry + 1
  775. Here "size_ao.value" = largest permitted entry of its own kind */
  776. max_ao = size_ao;
  777. if (data_len == 1
  778. && ((array_types[y] == WORD_ARRAY)
  779. || (array_types[y] == TABLE_ARRAY)))
  780. { max_ao.value = size_ao.value*4 + 3;
  781. type_ao.value += 8;
  782. }
  783. if (data_len == 4
  784. && ((array_types[y] == BYTE_ARRAY)
  785. || (array_types[y] == STRING_ARRAY)
  786. || (array_types[y] == BUFFER_ARRAY)))
  787. { max_ao.value = (size_ao.value-3)/4;
  788. type_ao.value += 16;
  789. }
  790. max_ao.value++;
  791. /* Can't write to the size entry in a string or table */
  792. if (((array_types[y] == STRING_ARRAY)
  793. || (array_types[y] == TABLE_ARRAY))
  794. && (!read_flag))
  795. { if ((array_types[y] == TABLE_ARRAY) && data_len == 1)
  796. zero_ao.value = 4;
  797. else zero_ao.value = 1;
  798. }
  799. en_ao = zero_ao; en_ao.value = ABOUNDS_RTE;
  800. switch(oc) { case aloadb_gc: en_ao.value = ABOUNDS_RTE; break;
  801. case aload_gc: en_ao.value = ABOUNDS_RTE+1; break;
  802. case astoreb_gc: en_ao.value = ABOUNDS_RTE+2; break;
  803. case astore_gc: en_ao.value = ABOUNDS_RTE+3; break; }
  804. set_constant_ot(&zero_ao);
  805. set_constant_ot(&size_ao);
  806. set_constant_ot(&max_ao);
  807. set_constant_ot(&type_ao);
  808. set_constant_ot(&en_ao);
  809. /* If we recognize A02 as a constant, we can do the test right
  810. now. */
  811. if (is_constant_ot(AO2.type) && AO2.marker == 0) {
  812. if (AO2.value < zero_ao.value || AO2.value >= max_ao.value) {
  813. error("Array reference is out-of-bounds");
  814. }
  815. assembleg_3(oc, AO1, AO2, AO3);
  816. return;
  817. }
  818. passed_label = next_label++;
  819. failed_label = next_label++;
  820. final_label = next_label++;
  821. index_ao = AO2;
  822. if ((AO2.type == LOCALVAR_OT)&&(AO2.value == 0))
  823. { assembleg_store(temp_var2, AO2); /* ### could peek */
  824. assembleg_store(AO2, temp_var2);
  825. index_ao = temp_var2;
  826. }
  827. assembleg_2_branch(jlt_gc, index_ao, zero_ao, failed_label);
  828. assembleg_2_branch(jlt_gc, index_ao, max_ao, passed_label);
  829. assemble_label_no(failed_label);
  830. an_ao = zero_ao; an_ao.value = y;
  831. set_constant_ot(&an_ao);
  832. five_ao = zero_ao; five_ao.value = 5;
  833. set_constant_ot(&five_ao);
  834. /* Call the error veneer routine. */
  835. assembleg_store(stack_pointer, an_ao);
  836. assembleg_store(stack_pointer, type_ao);
  837. assembleg_store(stack_pointer, size_ao);
  838. assembleg_store(stack_pointer, index_ao);
  839. assembleg_store(stack_pointer, en_ao);
  840. assembleg_3(call_gc, veneer_routine(RT__Err_VR),
  841. five_ao, zero_operand);
  842. /* We have to clear any of AO1, AO2, AO3 off the stack if
  843. present, so that we can achieve the same effect on the stack
  844. that executing the opcode would have had */
  845. if ((AO1.type == LOCALVAR_OT) && (AO1.value == 0))
  846. assembleg_2(copy_gc, stack_pointer, zero_operand);
  847. if ((AO2.type == LOCALVAR_OT) && (AO2.value == 0))
  848. assembleg_2(copy_gc, stack_pointer, zero_operand);
  849. if ((AO3.type == LOCALVAR_OT) && (AO3.value == 0))
  850. { if ((oc == aloadb_gc) || (oc == aload_gc))
  851. { assembleg_store(AO3, zero_ao);
  852. }
  853. else assembleg_2(copy_gc, stack_pointer, zero_operand);
  854. }
  855. assembleg_jump(final_label);
  856. assemble_label_no(passed_label);
  857. assembleg_3(oc, AO1, AO2, AO3);
  858. assemble_label_no(final_label);
  859. return;
  860. }
  861. /* Otherwise, compile a call to the veneer which verifies that
  862. the proposed read/write is within dynamic Z-machine memory. */
  863. switch(oc) {
  864. case aloadb_gc: vr = RT__ChLDB_VR; break;
  865. case aload_gc: vr = RT__ChLDW_VR; break;
  866. case astoreb_gc: vr = RT__ChSTB_VR; break;
  867. case astore_gc: vr = RT__ChSTW_VR; break;
  868. }
  869. if ((oc == aloadb_gc) || (oc == aload_gc))
  870. assembleg_call_2(veneer_routine(vr), AO1, AO2, AO3);
  871. else
  872. assembleg_call_3(veneer_routine(vr), AO1, AO2, AO3, zero_operand);
  873. }
  874. static assembly_operand check_nonzero_at_runtime_g(assembly_operand AO1,
  875. int error_label, int rte_number)
  876. {
  877. assembly_operand AO, AO2, AO3;
  878. int ln;
  879. int check_sp = FALSE, passed_label, failed_label, last_label;
  880. if (veneer_mode)
  881. return AO1;
  882. /* Assemble to code to check that the operand AO1 is ofclass Object:
  883. if it is, execution should continue and the stack should be
  884. unchanged. Otherwise, call the veneer's run-time-error routine
  885. with the given error number, and then: if the label isn't -1,
  886. switch execution to this label, with the value popped from
  887. the stack if it was on the stack in the first place;
  888. if the label is -1, either replace the top of the stack with
  889. the constant symbol (class-object) Object.
  890. The Object has no parent, child or sibling, so that the
  891. built-in tree functions will safely return 0 on this object. */
  892. /* Sometimes we can already see that the object number is valid. */
  893. if (AO1.marker == OBJECT_MV &&
  894. ((AO1.value >= 1) && (AO1.value <= no_objects))) {
  895. return AO1;
  896. }
  897. passed_label = next_label++;
  898. failed_label = next_label++;
  899. if ((AO1.type == LOCALVAR_OT) && (AO1.value == 0) && (AO1.marker == 0)) {
  900. /* That is, if AO1 is the stack pointer */
  901. check_sp = TRUE;
  902. assembleg_store(temp_var2, stack_pointer);
  903. assembleg_store(stack_pointer, temp_var2);
  904. AO = temp_var2;
  905. }
  906. else {
  907. AO = AO1;
  908. }
  909. if ((rte_number == IN_RTE) || (rte_number == HAS_RTE)
  910. || (rte_number == PROPERTY_RTE) || (rte_number == PROP_NUM_RTE)
  911. || (rte_number == PROP_ADD_RTE)) {
  912. /* Allow classes */
  913. /* Test if zero... */
  914. assembleg_1_branch(jz_gc, AO, failed_label);
  915. /* Test if first byte is 0x70... */
  916. assembleg_3(aloadb_gc, AO, zero_operand, stack_pointer);
  917. AO3.marker = 0;
  918. AO3.value = 0x70; /* type byte -- object */
  919. set_constant_ot(&AO3);
  920. assembleg_2_branch(jeq_gc, stack_pointer, AO3, passed_label);
  921. }
  922. else {
  923. /* Test if zero... */
  924. assembleg_1_branch(jz_gc, AO, failed_label);
  925. /* Test if first byte is 0x70... */
  926. assembleg_3(aloadb_gc, AO, zero_operand, stack_pointer);
  927. AO3.marker = 0;
  928. AO3.value = 0x70; /* type byte -- object */
  929. set_constant_ot(&AO3);
  930. assembleg_2_branch(jne_gc, stack_pointer, AO3, failed_label);
  931. /* Test if inside the "Class" object... */
  932. AO3.type = BYTECONSTANT_OT;
  933. AO3.value = GOBJFIELD_PARENT();
  934. AO3.marker = 0;
  935. assembleg_3(aload_gc, AO, AO3, stack_pointer);
  936. ln = symbol_index("Class", -1);
  937. AO3.value = svals[ln];
  938. AO3.marker = OBJECT_MV;
  939. AO3.type = CONSTANT_OT;
  940. assembleg_2_branch(jne_gc, stack_pointer, AO3, passed_label);
  941. }
  942. assemble_label_no(failed_label);
  943. AO2.marker = 0;
  944. AO2.value = rte_number;
  945. set_constant_ot(&AO2);
  946. assembleg_call_2(veneer_routine(RT__Err_VR), AO2, AO1, zero_operand);
  947. if (error_label != -1) {
  948. /* Jump to the error label */
  949. if (error_label == -3) assembleg_1(return_gc, zero_operand);
  950. else if (error_label == -4) assembleg_1(return_gc, one_operand);
  951. else assembleg_jump(error_label);
  952. }
  953. else {
  954. /* Build the symbol for "Object" */
  955. ln = symbol_index("Object", -1);
  956. AO2.value = svals[ln];
  957. AO2.marker = OBJECT_MV;
  958. AO2.type = CONSTANT_OT;
  959. if (check_sp) {
  960. /* Push "Object" */
  961. assembleg_store(AO1, AO2);
  962. }
  963. else {
  964. /* Store either "Object" or the operand's value in the temporary
  965. variable. */
  966. assembleg_store(temp_var2, AO2);
  967. last_label = next_label++;
  968. assembleg_jump(last_label);
  969. assemble_label_no(passed_label);
  970. assembleg_store(temp_var2, AO1);
  971. assemble_label_no(last_label);
  972. return temp_var2;
  973. }
  974. }
  975. assemble_label_no(passed_label);
  976. return AO1;
  977. }
  978. static void compile_conditional_g(condclass *cc,
  979. assembly_operand AO1, assembly_operand AO2, int label, int flag)
  980. { assembly_operand AO4;
  981. int the_zc, error_label = label,
  982. va_flag = FALSE, va_label;
  983. ASSERT_GLULX();
  984. the_zc = (flag ? cc->posform : cc->negform);
  985. if (the_zc == -1) {
  986. switch ((cc-condclasses)*2 + 500) {
  987. case HAS_CC:
  988. if (runtime_error_checking_switch) {
  989. if (flag)
  990. error_label = next_label++;
  991. AO1 = check_nonzero_at_runtime(AO1, error_label, HAS_RTE);
  992. if (is_constant_ot(AO2.type) && AO2.marker == 0) {
  993. if ((AO2.value < 0) || (AO2.value >= NUM_ATTR_BYTES*8)) {
  994. error("'has'/'hasnt' applied to illegal attribute number");
  995. }
  996. }
  997. else {
  998. int pa_label = next_label++, fa_label = next_label++;
  999. assembly_operand en_ao, max_ao;
  1000. if ((AO1.type == LOCALVAR_OT) && (AO1.value == 0)) {
  1001. if ((AO2.type == LOCALVAR_OT) && (AO2.value == 0)) {
  1002. assembleg_2(stkpeek_gc, zero_operand, temp_var1);
  1003. assembleg_2(stkpeek_gc, one_operand, temp_var2);
  1004. }
  1005. else {
  1006. assembleg_2(stkpeek_gc, zero_operand, temp_var1);
  1007. assembleg_store(temp_var2, AO2);
  1008. }
  1009. }
  1010. else {
  1011. assembleg_store(temp_var1, AO1);
  1012. if ((AO2.type == LOCALVAR_OT) && (AO2.value == 0)) {
  1013. assembleg_2(stkpeek_gc, zero_operand, temp_var2);
  1014. }
  1015. else {
  1016. assembleg_store(temp_var2, AO2);
  1017. }
  1018. }
  1019. max_ao.marker = 0;
  1020. max_ao.value = NUM_ATTR_BYTES*8;
  1021. set_constant_ot(&max_ao);
  1022. assembleg_2_branch(jlt_gc, temp_var2, zero_operand, fa_label);
  1023. assembleg_2_branch(jlt_gc, temp_var2, max_ao, pa_label);
  1024. assemble_label_no(fa_label);
  1025. en_ao.marker = 0;
  1026. en_ao.value = 19; /* INVALIDATTR_RTE */
  1027. set_constant_ot(&en_ao);
  1028. assembleg_store(stack_pointer, temp_var2);
  1029. assembleg_store(stack_pointer, temp_var1);
  1030. assembleg_store(stack_pointer, en_ao);
  1031. assembleg_3(call_gc, veneer_routine(RT__Err_VR),
  1032. three_operand, zero_operand);
  1033. va_flag = TRUE;
  1034. va_label = next_label++;
  1035. assembleg_jump(va_label);
  1036. assemble_label_no(pa_label);
  1037. }
  1038. }
  1039. if (is_constant_ot(AO2.type) && AO2.marker == 0) {
  1040. AO2.value += 8;
  1041. set_constant_ot(&AO2);
  1042. }
  1043. else {
  1044. AO4.value = 8;
  1045. AO4.marker = 0;
  1046. AO4.type = BYTECONSTANT_OT;
  1047. if ((AO1.type == LOCALVAR_OT) && (AO1.value == 0)) {
  1048. if ((AO2.type == LOCALVAR_OT) && (AO2.value == 0))
  1049. assembleg_0(stkswap_gc);
  1050. assembleg_3(add_gc, AO2, AO4, stack_pointer);
  1051. assembleg_0(stkswap_gc);
  1052. }
  1053. else {
  1054. assembleg_3(add_gc, AO2, AO4, stack_pointer);
  1055. }
  1056. AO2 = stack_pointer;
  1057. }
  1058. assembleg_3(aloadbit_gc, AO1, AO2, stack_pointer);
  1059. the_zc = (flag ? jnz_gc : jz_gc);
  1060. AO1 = stack_pointer;
  1061. break;
  1062. case IN_CC:
  1063. if (runtime_error_checking_switch) {
  1064. if (flag)
  1065. error_label = next_label++;
  1066. AO1 = check_nonzero_at_runtime(AO1, error_label, IN_RTE);
  1067. }
  1068. AO4.value = GOBJFIELD_PARENT();
  1069. AO4.marker = 0;
  1070. AO4.type = BYTECONSTANT_OT;
  1071. assembleg_3(aload_gc, AO1, AO4, stack_pointer);
  1072. AO1 = stack_pointer;
  1073. the_zc = (flag ? jeq_gc : jne_gc);
  1074. break;
  1075. case OFCLASS_CC:
  1076. assembleg_call_2(veneer_routine(OC__Cl_VR), AO1, AO2, stack_pointer);
  1077. the_zc = (flag ? jnz_gc : jz_gc);
  1078. AO1 = stack_pointer;
  1079. break;
  1080. case PROVIDES_CC:
  1081. assembleg_call_2(veneer_routine(OP__Pr_VR), AO1, AO2, stack_pointer);
  1082. the_zc = (flag ? jnz_gc : jz_gc);
  1083. AO1 = stack_pointer;
  1084. break;
  1085. default:
  1086. error("condition not yet supported in Glulx");
  1087. return;
  1088. }
  1089. }
  1090. if (the_zc == jnz_gc || the_zc == jz_gc)
  1091. assembleg_1_branch(the_zc, AO1, label);
  1092. else
  1093. assembleg_2_branch(the_zc, AO1, AO2, label);
  1094. if (error_label != label) assemble_label_no(error_label);
  1095. if (va_flag) assemble_label_no(va_label);
  1096. }
  1097. static void value_in_void_context(assembly_operand AO)
  1098. {
  1099. if (!glulx_mode)
  1100. value_in_void_context_z(AO);
  1101. else
  1102. value_in_void_context_g(AO);
  1103. }
  1104. extern assembly_operand check_nonzero_at_runtime(assembly_operand AO1,
  1105. int error_label, int rte_number)
  1106. {
  1107. if (!glulx_mode)
  1108. return check_nonzero_at_runtime_z(AO1, error_label, rte_number);
  1109. else
  1110. return check_nonzero_at_runtime_g(AO1, error_label, rte_number);
  1111. }
  1112. static void generate_code_from(int n, int void_flag)
  1113. {
  1114. /* When void, this must not leave anything on the stack. */
  1115. int i, j, below, above, opnum, arity; assembly_operand Result;
  1116. below = ET[n].down; above = ET[n].up;
  1117. if (below == -1)
  1118. { if ((void_flag) && (ET[n].value.type != OMITTED_OT))
  1119. value_in_void_context(ET[n].value);
  1120. return;
  1121. }
  1122. opnum = ET[n].operator_number;
  1123. if (opnum == COMMA_OP)
  1124. { generate_code_from(below, TRUE);
  1125. generate_code_from(ET[below].right, void_flag);
  1126. ET[n].value = ET[ET[below].right].value;
  1127. goto OperatorGenerated;
  1128. }
  1129. if ((opnum == LOGAND_OP) || (opnum == LOGOR_OP))
  1130. { generate_code_from(below, FALSE);
  1131. generate_code_from(ET[below].right, FALSE);
  1132. goto OperatorGenerated;
  1133. }
  1134. if (opnum == -1)
  1135. {
  1136. /* Signifies a SETEQUALS_OP which has already been done */
  1137. ET[n].down = -1; return;
  1138. }
  1139. /* Note that (except in the cases of comma and logical and/or) it
  1140. is essential to code generate the operands right to left, because
  1141. of the peculiar way the Z-machine's stack works:
  1142. @sub sp sp -> a;
  1143. (for instance) pulls to the first operand, then the second. So
  1144. @mul a 2 -> sp;
  1145. @add b 7 -> sp;
  1146. @sub sp sp -> a;
  1147. calculates (b+7)-(a*2), not the other way around (as would be more
  1148. usual in stack machines evaluating expressions written in reverse
  1149. Polish notation). (Basically this is because the Z-machine was
  1150. designed to implement a LISP-like language naturally expressed
  1151. in forward Polish notation: (PLUS 3 4), for instance.) */
  1152. /* And the Glulx machine follows the Z-machine in this respect. */
  1153. i=below; arity = 0;
  1154. while (i != -1)
  1155. { i = ET[i].right; arity++;
  1156. }
  1157. for (j=arity;j>0;j--)
  1158. { int k = 1;
  1159. i = below;
  1160. while (k<j)
  1161. { k++; i = ET[i].right;
  1162. }
  1163. generate_code_from(i, FALSE);
  1164. }
  1165. /* Check this again, because code generation lower down may have
  1166. stubbed it into -1 */
  1167. if (ET[n].operator_number == -1)
  1168. { ET[n].down = -1; return;
  1169. }
  1170. if (!glulx_mode) {
  1171. if (operators[opnum].opcode_number_z >= 400)
  1172. {
  1173. /* Conditional terms such as '==': */
  1174. int a = ET[n].true_label, b = ET[n].false_label,
  1175. branch_away, branch_other,
  1176. make_jump_away = FALSE, make_branch_label = FALSE;
  1177. int oc = operators[opnum].opcode_number_z-400, flag = TRUE;
  1178. if (oc >= 400) { oc = oc - 400; flag = FALSE; }
  1179. if ((oc == je_zc) && (arity == 2))
  1180. { i = ET[ET[n].down].right;
  1181. if ((ET[i].value.value == zero_operand.value)
  1182. && (ET[i].value.type == zero_operand.type))
  1183. oc = jz_zc;
  1184. }
  1185. /* If the condition has truth state flag, branch to
  1186. label a, and if not, to label b. Possibly one of a, b
  1187. equals -1, meaning "continue from this instruction".
  1188. branch_away is the label which is a branch away (the one
  1189. which isn't immediately after) and flag is the truth
  1190. state to branch there.
  1191. Note that when multiple instructions are needed (because
  1192. of the use of the 'or' operator) the branch_other label
  1193. is created if need be.
  1194. */
  1195. /* Reduce to the case where the branch_away label does exist: */
  1196. if (a == -1) { a = b; b = -1; flag = !flag; }
  1197. branch_away = a; branch_other = b;
  1198. if (branch_other != -1) make_jump_away = TRUE;
  1199. if ((((oc != je_zc)&&(arity > 2)) || (arity > 4)) && (flag == FALSE))
  1200. {
  1201. /* In this case, we have an 'or' situation where multiple
  1202. instructions are needed and where the overall condition
  1203. is negated. That is, we have, e.g.
  1204. if not (A cond B or C or D) then branch_away
  1205. which we transform into
  1206. if (A cond B) then branch_other
  1207. if (A cond C) then branch_other
  1208. if not (A cond D) then branch_away
  1209. .branch_other */
  1210. if (branch_other == -1)
  1211. { branch_other = next_label++; make_branch_label = TRUE;
  1212. }
  1213. }
  1214. if (oc == jz_zc)
  1215. assemblez_1_branch(jz_zc, ET[below].value, branch_away, flag);
  1216. else
  1217. { assembly_operand left_operand;
  1218. if (arity == 2)
  1219. compile_conditional_z(oc, ET[below].value,
  1220. ET[ET[below].right].value, branch_away, flag);
  1221. else
  1222. { /* The case of a condition using "or".
  1223. First: if the condition tests the stack pointer,
  1224. and it can't always be done in a single test, move
  1225. the value off the stack and into temporary variable
  1226. storage. */
  1227. if (((ET[below].value.type == VARIABLE_OT)
  1228. && (ET[below].value.value == 0))
  1229. && ((oc != je_zc) || (arity>4)) )
  1230. { left_operand.type = VARIABLE_OT;
  1231. left_operand.value = 255;
  1232. left_operand.marker = 0;
  1233. assemblez_store(left_operand, ET[below].value);
  1234. }
  1235. else left_operand = ET[below].value;
  1236. i = ET[below].right; arity--;
  1237. /* "left_operand" now holds the quantity to be tested;
  1238. "i" holds the right operand reached so far;
  1239. "arity" the number of right operands. */
  1240. while (i != -1)
  1241. { if ((oc == je_zc) && (arity>1))
  1242. {
  1243. /* je_zc is an especially good case since the
  1244. Z-machine implements "or" for up to three
  1245. right operands automatically, though it's an
  1246. especially bad case to generate code for! */
  1247. if (arity == 2)
  1248. { assemblez_3_branch(je_zc,
  1249. left_operand, ET[i].value,
  1250. ET[ET[i].right].value, branch_away, flag);
  1251. i = ET[i].right; arity--;
  1252. }
  1253. else
  1254. { if ((arity == 3) || flag)
  1255. assemblez_4_branch(je_zc, left_operand,
  1256. ET[i].value,
  1257. ET[ET[i].right].value,
  1258. ET[ET[ET[i].right].right].value,
  1259. branch_away, flag);
  1260. else
  1261. assemblez_4_branch(je_zc, left_operand,
  1262. ET[i].value,
  1263. ET[ET[i].right].value,
  1264. ET[ET[ET[i].right].right].value,
  1265. branch_other, !flag);
  1266. i = ET[ET[i].right].right; arity -= 2;
  1267. }
  1268. }
  1269. else
  1270. { /* Otherwise we can compare the left_operand with
  1271. only one right operand at the time. There are
  1272. two cases: it's the last right operand, or it
  1273. isn't. */
  1274. if ((arity == 1) || flag)
  1275. compile_conditional_z(oc, left_operand,
  1276. ET[i].value, branch_away, flag);
  1277. else
  1278. compile_conditional_z(oc, left_operand,
  1279. ET[i].value, branch_other, !flag);
  1280. }
  1281. i = ET[i].right; arity--;
  1282. }
  1283. }
  1284. }
  1285. /* NB: These two conditions cannot both occur, fortunately! */
  1286. if (make_branch_label) assemble_label_no(branch_other);
  1287. if (make_jump_away) assemblez_jump(branch_other);
  1288. goto OperatorGenerated;
  1289. }
  1290. }
  1291. else {
  1292. if (operators[opnum].opcode_number_g >= FIRST_CC
  1293. && operators[opnum].opcode_number_g <= LAST_CC) {
  1294. /* Conditional terms such as '==': */
  1295. int a = ET[n].true_label, b = ET[n].false_label;
  1296. int branch_away, branch_other, flag,
  1297. make_jump_away = FALSE, make_branch_label = FALSE;
  1298. int ccode = operators[opnum].opcode_number_g;
  1299. condclass *cc = &condclasses[(ccode-FIRST_CC) / 2];
  1300. flag = (ccode & 1) ? 0 : 1;
  1301. /* If the comparison is "equal to (constant) 0", change it
  1302. to the simple "zero" test. Unfortunately, this doesn't
  1303. work for the commutative form "(constant) 0 is equal to".
  1304. At least I don't think it does. */
  1305. if ((cc == &condclasses[1]) && (arity == 2)) {
  1306. i = ET[ET[n].down].right;
  1307. if ((ET[i].value.value == 0)
  1308. && (ET[i].value.marker == 0)
  1309. && is_constant_ot(ET[i].value.type)) {
  1310. cc = &condclasses[0];
  1311. }
  1312. }
  1313. /* If the condition has truth state flag, branch to
  1314. label a, and if not, to label b. Possibly one of a, b
  1315. equals -1, meaning "continue from this instruction".
  1316. branch_away is the label which is a branch away (the one
  1317. which isn't immediately after) and flag is the truth
  1318. state to branch there.
  1319. Note that when multiple instructions are needed (because
  1320. of the use of the 'or' operator) the branch_other label
  1321. is created if need be.
  1322. */
  1323. /* Reduce to the case where the branch_away label does exist: */
  1324. if (a == -1) { a = b; b = -1; flag = !flag; }
  1325. branch_away = a; branch_other = b;
  1326. if (branch_other != -1) make_jump_away = TRUE;
  1327. if ((arity > 2) && (flag == FALSE)) {
  1328. /* In this case, we have an 'or' situation where multiple
  1329. instructions are needed and where the overall condition
  1330. is negated. That is, we have, e.g.
  1331. if not (A cond B or C or D) then branch_away
  1332. which we transform into
  1333. if (A cond B) then branch_other
  1334. if (A cond C) then branch_other
  1335. if not (A cond D) then branch_away
  1336. .branch_other */
  1337. if (branch_other == -1) {
  1338. branch_other = next_label++; make_branch_label = TRUE;
  1339. }
  1340. }
  1341. if (cc == &condclasses[0]) {
  1342. assembleg_1_branch((flag ? cc->posform : cc->negform),
  1343. ET[below].value, branch_away);
  1344. }
  1345. else {
  1346. if (arity == 2) {
  1347. compile_conditional_g(cc, ET[below].value,
  1348. ET[ET[below].right].value, branch_away, flag);
  1349. }
  1350. else {
  1351. /* The case of a condition using "or".
  1352. First: if the condition tests the stack pointer,
  1353. and it can't always be done in a single test, move
  1354. the value off the stack and into temporary variable
  1355. storage. */
  1356. assembly_operand left_operand;
  1357. if (((ET[below].value.type == LOCALVAR_OT)
  1358. && (ET[below].value.value == 0))) {
  1359. assembleg_store(temp_var1, ET[below].value);
  1360. left_operand = temp_var1;
  1361. }
  1362. else {
  1363. left_operand = ET[below].value;
  1364. }
  1365. i = ET[below].right;
  1366. arity--;
  1367. /* "left_operand" now holds the quantity to be tested;
  1368. "i" holds the right operand reached so far;
  1369. "arity" the number of right operands. */
  1370. while (i != -1) {
  1371. /* We can compare the left_operand with
  1372. only one right operand at the time. There are
  1373. two cases: it's the last right operand, or it
  1374. isn't. */
  1375. if ((arity == 1) || flag)
  1376. compile_conditional_g(cc, left_operand,
  1377. ET[i].value, branch_away, flag);
  1378. else
  1379. compile_conditional_g(cc, left_operand,
  1380. ET[i].value, branch_other, !flag);
  1381. i = ET[i].right;
  1382. arity--;
  1383. }
  1384. }
  1385. }
  1386. /* NB: These two conditions cannot both occur, fortunately! */
  1387. if (make_branch_label) assemble_label_no(branch_other);
  1388. if (make_jump_away) assembleg_jump(branch_other);
  1389. goto OperatorGenerated;
  1390. }
  1391. }
  1392. /* The operator is now definitely one which produces a value */
  1393. if (void_flag && (!(operators[opnum].side_effect)))
  1394. error_named("Evaluating this has no effect:",
  1395. operators[opnum].description);
  1396. /* Where shall we put the resulting value? (In Glulx, this could
  1397. be smarter, and peg the result into ZEROCONSTANT.) */
  1398. if (void_flag) Result = temp_var1; /* Throw it away */
  1399. else
  1400. { if ((above != -1) && (ET[above].operator_number == SETEQUALS_OP))
  1401. {
  1402. /* If the node above is "set variable equal to", then
  1403. make that variable the place to put the result, and
  1404. delete the SETEQUALS_OP node since its effect has already
  1405. been accomplished. */
  1406. ET[above].operator_number = -1;
  1407. Result = ET[ET[above].down].value;
  1408. ET[above].value = Result;
  1409. }
  1410. else Result = stack_pointer; /* Otherwise, put it on the stack */
  1411. }
  1412. if (!glulx_mode) {
  1413. if (operators[opnum].opcode_number_z != -1)
  1414. {
  1415. /* Operators directly translatable into Z-code opcodes: infix ops
  1416. take two operands whereas pre/postfix operators take only one */
  1417. if (operators[opnum].usage == IN_U)
  1418. { int o_n = operators[opnum].opcode_number_z;
  1419. if (runtime_error_checking_switch && (!veneer_mode)
  1420. && ((o_n == div_zc) || (o_n == mod_zc)))
  1421. { assembly_operand by_ao, error_ao; int ln;
  1422. by_ao = ET[ET[below].right].value;
  1423. if ((by_ao.value != 0) && (by_ao.marker == 0)
  1424. && ((by_ao.type == SHORT_CONSTANT_OT)
  1425. || (by_ao.type == LONG_CONSTANT_OT)))
  1426. assemblez_2_to(o_n, ET[below].value,
  1427. by_ao, Result);
  1428. else
  1429. {
  1430. assemblez_store(temp_var1, ET[below].value);
  1431. assemblez_store(temp_var2, by_ao);
  1432. ln = next_label++;
  1433. assemblez_1_branch(jz_zc, temp_var2, ln, FALSE);
  1434. error_ao.type = SHORT_CONSTANT_OT; error_ao.marker = 0;
  1435. error_ao.value = DBYZERO_RTE;
  1436. assemblez_2(call_vn_zc, veneer_routine(RT__Err_VR),
  1437. error_ao);
  1438. assemblez_inc(temp_var2);
  1439. assemble_label_no(ln);
  1440. assemblez_2_to(o_n, temp_var1, temp_var2, Result);
  1441. }
  1442. }
  1443. else {
  1444. assemblez_2_to(o_n, ET[below].value,
  1445. ET[ET[below].right].value, Result);
  1446. }
  1447. }
  1448. else
  1449. assemblez_1_to(operators[opnum].opcode_number_z, ET[below].value,
  1450. Result);
  1451. }
  1452. else
  1453. switch(opnum)
  1454. { case ARROW_OP:
  1455. access_memory_z(loadb_zc, ET[below].value,
  1456. ET[ET[below].right].value, Result);
  1457. break;
  1458. case DARROW_OP:
  1459. access_memory_z(loadw_zc, ET[below].value,
  1460. ET[ET[below].right].value, Result);
  1461. break;
  1462. case UNARY_MINUS_OP:
  1463. assemblez_2_to(sub_zc, zero_operand, ET[below].value, Result);
  1464. break;
  1465. case ARTNOT_OP:
  1466. assemblez_1_to(not_zc, ET[below].value, Result);
  1467. break;
  1468. case PROP_ADD_OP:
  1469. { assembly_operand AO = ET[below].value;
  1470. if (runtime_error_checking_switch && (!veneer_mode))
  1471. AO = check_nonzero_at_runtime(AO, -1, PROP_ADD_RTE);
  1472. assemblez_2_to(get_prop_addr_zc, AO,
  1473. ET[ET[below].right].value, temp_var1);
  1474. if (!void_flag) write_result_z(Result, temp_var1);
  1475. }
  1476. break;
  1477. case PROP_NUM_OP:
  1478. { assembly_operand AO = ET[below].value;
  1479. if (runtime_error_checking_switch && (!veneer_mode))
  1480. AO = check_nonzero_at_runtime(AO, -1, PROP_NUM_RTE);
  1481. assemblez_2_to(get_prop_addr_zc, AO,
  1482. ET[ET[below].right].value, temp_var1);
  1483. assemblez_1_branch(jz_zc, temp_var1, next_label++, TRUE);
  1484. assemblez_1_to(get_prop_len_zc, temp_var1, temp_var1);
  1485. assemble_label_no(next_label-1);
  1486. if (!void_flag) write_result_z(Result, temp_var1);
  1487. }
  1488. break;
  1489. case PROPERTY_OP:
  1490. { assembly_operand AO = ET[below].value;
  1491. if (runtime_error_checking_switch && (!veneer_mode))
  1492. assemblez_3_to(call_vs_zc, veneer_routine(RT__ChPR_VR),
  1493. AO, ET[ET[below].right].value, temp_var1);
  1494. else
  1495. assemblez_2_to(get_prop_zc, AO,
  1496. ET[ET[below].right].value, temp_var1);
  1497. if (!void_flag) write_result_z(Result, temp_var1);
  1498. }
  1499. break;
  1500. case MESSAGE_OP:
  1501. j=1; AI.operand[0] = veneer_routine(RV__Pr_VR);
  1502. goto GenFunctionCallZ;
  1503. case MPROP_ADD_OP:
  1504. j=1; AI.operand[0] = veneer_routine(RA__Pr_VR);
  1505. goto GenFunctionCallZ;
  1506. case MPROP_NUM_OP:
  1507. j=1; AI.operand[0] = veneer_routine(RL__Pr_VR);
  1508. goto GenFunctionCallZ;
  1509. case MESSAGE_SETEQUALS_OP:
  1510. j=1; AI.operand[0] = veneer_routine(WV__Pr_VR);
  1511. goto GenFunctionCallZ;
  1512. case MESSAGE_INC_OP:
  1513. j=1; AI.operand[0] = veneer_routine(IB__Pr_VR);
  1514. goto GenFunctionCallZ;
  1515. case MESSAGE_DEC_OP:
  1516. j=1; AI.operand[0] = veneer_routine(DB__Pr_VR);
  1517. goto GenFunctionCallZ;
  1518. case MESSAGE_POST_INC_OP:
  1519. j=1; AI.operand[0] = veneer_routine(IA__Pr_VR);
  1520. goto GenFunctionCallZ;
  1521. case MESSAGE_POST_DEC_OP:
  1522. j=1; AI.operand[0] = veneer_routine(DA__Pr_VR);
  1523. goto GenFunctionCallZ;
  1524. case SUPERCLASS_OP:
  1525. j=1; AI.operand[0] = veneer_routine(RA__Sc_VR);
  1526. goto GenFunctionCallZ;
  1527. case PROP_CALL_OP:
  1528. j=1; AI.operand[0] = veneer_routine(CA__Pr_VR);
  1529. goto GenFunctionCallZ;
  1530. case MESSAGE_CALL_OP:
  1531. j=1; AI.operand[0] = veneer_routine(CA__Pr_VR);
  1532. goto GenFunctionCallZ;
  1533. case FCALL_OP:
  1534. j = 0;
  1535. if ((ET[below].value.type == VARIABLE_OT)
  1536. && (ET[below].value.value >= 256))
  1537. { int sf_number = ET[below].value.value - 256;
  1538. i = ET[below].right;
  1539. if (i == -1)
  1540. { error("Argument to system function missing");
  1541. AI.operand[0] = one_operand;
  1542. AI.operand_count = 1;
  1543. }
  1544. else
  1545. { j=0;
  1546. while (i != -1) { j++; i = ET[i].right; }
  1547. if (((sf_number != INDIRECT_SYSF) &&
  1548. (sf_number != RANDOM_SYSF) && (j > 1))
  1549. || ((sf_number == INDIRECT_SYSF) && (j>7)))
  1550. { j=1;
  1551. error("System function given with too many arguments");
  1552. }
  1553. if (sf_number != RANDOM_SYSF)
  1554. { int jcount;
  1555. i = ET[below].right;
  1556. for (jcount = 0; jcount < j; jcount++)
  1557. { AI.operand[jcount] = ET[i].value;
  1558. i = ET[i].right;
  1559. }
  1560. AI.operand_count = j;
  1561. }
  1562. }
  1563. AI.store_variable_number = Result.value;
  1564. AI.branch_label_number = -1;
  1565. switch(sf_number)
  1566. { case RANDOM_SYSF:
  1567. if (j>1)
  1568. { assembly_operand AO, AO2; int arg_c, arg_et;
  1569. AO.value = j; AO.marker = 0;
  1570. AO.type = SHORT_CONSTANT_OT;
  1571. AO2.type = LONG_CONSTANT_OT;
  1572. AO2.value = begin_word_array();
  1573. AO2.marker = ARRAY_MV;
  1574. for (arg_c=0, arg_et = ET[below].right;arg_c<j;
  1575. arg_c++, arg_et = ET[arg_et].right)
  1576. { if (ET[arg_et].value.type == VARIABLE_OT)
  1577. error("Only constants can be used as possible 'random' results");
  1578. array_entry(arg_c, ET[arg_et].value);
  1579. }
  1580. finish_array(arg_c);
  1581. assemblez_1_to(random_zc, AO, temp_var1);
  1582. assemblez_dec(temp_var1);
  1583. assemblez_2_to(loadw_zc, AO2, temp_var1, Result);
  1584. }
  1585. else
  1586. assemblez_1_to(random_zc,
  1587. ET[ET[below].right].value, Result);
  1588. break;
  1589. case PARENT_SYSF:
  1590. { assembly_operand AO;
  1591. AO = ET[ET[below].right].value;
  1592. if (runtime_error_checking_switch)
  1593. AO = check_nonzero_at_runtime(AO, -1,
  1594. PARENT_RTE);
  1595. assemblez_1_to(get_parent_zc, AO, Result);
  1596. }
  1597. break;
  1598. case ELDEST_SYSF:
  1599. case CHILD_SYSF:
  1600. { assembly_operand AO;
  1601. AO = ET[ET[below].right].value;
  1602. if (runtime_error_checking_switch)
  1603. AO = check_nonzero_at_runtime(AO, -1,
  1604. (sf_number==CHILD_SYSF)?CHILD_RTE:ELDEST_RTE);
  1605. assemblez_objcode(get_child_zc,
  1606. AO, Result, -2, TRUE);
  1607. }
  1608. break;
  1609. case YOUNGER_SYSF:
  1610. case SIBLING_SYSF:
  1611. { assembly_operand AO;
  1612. AO = ET[ET[below].right].value;
  1613. if (runtime_error_checking_switch)
  1614. AO = check_nonzero_at_runtime(AO, -1,
  1615. (sf_number==SIBLING_SYSF)
  1616. ?SIBLING_RTE:YOUNGER_RTE);
  1617. assemblez_objcode(get_sibling_zc,
  1618. AO, Result, -2, TRUE);
  1619. }
  1620. break;
  1621. case INDIRECT_SYSF:
  1622. j=0; i = ET[below].right;
  1623. goto IndirectFunctionCallZ;
  1624. case CHILDREN_SYSF:
  1625. { assembly_operand AO;
  1626. AO = ET[ET[below].right].value;
  1627. if (runtime_error_checking_switch)
  1628. AO = check_nonzero_at_runtime(AO, -1,
  1629. CHILDREN_RTE);
  1630. assemblez_store(temp_var1, zero_operand);
  1631. assemblez_objcode(get_child_zc,
  1632. AO, stack_pointer, next_label+1, FALSE);
  1633. assemble_label_no(next_label);
  1634. assemblez_inc(temp_var1);
  1635. assemblez_objcode(get_sibling_zc,
  1636. stack_pointer, stack_pointer,
  1637. next_label, TRUE);
  1638. assemble_label_no(next_label+1);
  1639. assemblez_store(temp_var2, stack_pointer);
  1640. if (!void_flag) write_result_z(Result, temp_var1);
  1641. next_label += 2;
  1642. }
  1643. break;
  1644. case YOUNGEST_SYSF:
  1645. { assembly_operand AO;
  1646. AO = ET[ET[below].right].value;
  1647. if (runtime_error_checking_switch)
  1648. AO = check_nonzero_at_runtime(AO, -1,
  1649. YOUNGEST_RTE);
  1650. assemblez_objcode(get_child_zc,
  1651. AO, temp_var1, next_label+1, FALSE);
  1652. assemblez_1(push_zc, temp_var1);
  1653. assemble_label_no(next_label);
  1654. assemblez_store(temp_var1, stack_pointer);
  1655. assemblez_objcode(get_sibling_zc,
  1656. temp_var1, stack_pointer, next_label, TRUE);
  1657. assemble_label_no(next_label+1);
  1658. if (!void_flag) write_result_z(Result, temp_var1);
  1659. next_label += 2;
  1660. }
  1661. break;
  1662. case ELDER_SYSF:
  1663. assemblez_store(temp_var1, ET[ET[below].right].value);
  1664. if (runtime_error_checking_switch)
  1665. check_nonzero_at_runtime(temp_var1, -1,
  1666. ELDER_RTE);
  1667. assemblez_1_to(get_parent_zc, temp_var1, temp_var3);
  1668. assemblez_1_branch(jz_zc, temp_var3,next_label+1,TRUE);
  1669. assemblez_store(temp_var2, temp_var3);
  1670. assemblez_store(temp_var3, zero_operand);
  1671. assemblez_objcode(get_child_zc,
  1672. temp_var2, temp_var2, next_label, TRUE);
  1673. assemble_label_no(next_label++);
  1674. assemblez_2_branch(je_zc, temp_var1, temp_var2,
  1675. next_label, TRUE);
  1676. assemblez_store(temp_var3, temp_var2);
  1677. assemblez_objcode(get_sibling_zc,
  1678. temp_var2, temp_var2, next_label - 1, TRUE);
  1679. assemble_label_no(next_label++);
  1680. if (!void_flag) write_result_z(Result, temp_var3);
  1681. break;
  1682. case METACLASS_SYSF:
  1683. assemblez_2_to((version_number==3)?call_zc:call_vs_zc,
  1684. veneer_routine(Metaclass_VR),
  1685. ET[ET[below].right].value, Result);
  1686. break;
  1687. case GLK_SYSF:
  1688. error("The glk() system function does not exist in Z-code");
  1689. break;
  1690. }
  1691. break;
  1692. }
  1693. GenFunctionCallZ:
  1694. i = below;
  1695. IndirectFunctionCallZ:
  1696. while ((i != -1) && (j<8))
  1697. { AI.operand[j++] = ET[i].value;
  1698. i = ET[i].right;
  1699. }
  1700. if ((j > 4) && (version_number == 3))
  1701. { error("A function may be called with at most 3 arguments");
  1702. j = 4;
  1703. }
  1704. if ((j==8) && (i != -1))
  1705. { error("A function may be called with at most 7 arguments");
  1706. }
  1707. AI.operand_count = j;
  1708. if ((void_flag) && (version_number >= 5))
  1709. { AI.store_variable_number = -1;
  1710. switch(j)
  1711. { case 1: AI.internal_number = call_1n_zc; break;
  1712. case 2: AI.internal_number = call_2n_zc; break;
  1713. case 3: case 4: AI.internal_number = call_vn_zc; break;
  1714. case 5: case 6: case 7: case 8:
  1715. AI.internal_number = call_vn2_zc; break;
  1716. }
  1717. }
  1718. else
  1719. { AI.store_variable_number = Result.value;
  1720. if (version_number == 3)
  1721. AI.internal_number = call_zc;
  1722. else
  1723. switch(j)
  1724. { case 1: AI.internal_number = call_1s_zc; break;
  1725. case 2: AI.internal_number = call_2s_zc; break;
  1726. case 3: case 4: AI.internal_number = call_vs_zc; break;
  1727. case 5: case 6: case 7: case 8:
  1728. AI.internal_number = call_vs2_zc; break;
  1729. }
  1730. }
  1731. AI.branch_label_number = -1;
  1732. assemblez_instruction(&AI);
  1733. break;
  1734. case SETEQUALS_OP:
  1735. assemblez_store(ET[below].value,
  1736. ET[ET[below].right].value);
  1737. if (!void_flag) write_result_z(Result, ET[below].value);
  1738. break;
  1739. case PROPERTY_SETEQUALS_OP:
  1740. if (!void_flag)
  1741. { if (runtime_error_checking_switch)
  1742. assemblez_4_to(call_zc, veneer_routine(RT__ChPS_VR),
  1743. ET[below].value, ET[ET[below].right].value,
  1744. ET[ET[ET[below].right].right].value, Result);
  1745. else
  1746. { assemblez_store(temp_var1,
  1747. ET[ET[ET[below].right].right].value);
  1748. assemblez_3(put_prop_zc, ET[below].value,
  1749. ET[ET[below].right].value,
  1750. temp_var1);
  1751. write_result_z(Result, temp_var1);
  1752. }
  1753. }
  1754. else
  1755. { if (runtime_error_checking_switch && (!veneer_mode))
  1756. assemblez_4(call_vn_zc, veneer_routine(RT__ChPS_VR),
  1757. ET[below].value, ET[ET[below].right].value,
  1758. ET[ET[ET[below].right].right].value);
  1759. else assemblez_3(put_prop_zc, ET[below].value,
  1760. ET[ET[below].right].value,
  1761. ET[ET[ET[below].right].right].value);
  1762. }
  1763. break;
  1764. case ARROW_SETEQUALS_OP:
  1765. if (!void_flag)
  1766. { assemblez_store(temp_var1,
  1767. ET[ET[ET[below].right].right].value);
  1768. access_memory_z(storeb_zc, ET[below].value,
  1769. ET[ET[below].right].value,
  1770. temp_var1);
  1771. write_result_z(Result, temp_var1);
  1772. }
  1773. else access_memory_z(storeb_zc, ET[below].value,
  1774. ET[ET[below].right].value,
  1775. ET[ET[ET[below].right].right].value);
  1776. break;
  1777. case DARROW_SETEQUALS_OP:
  1778. if (!void_flag)
  1779. { assemblez_store(temp_var1,
  1780. ET[ET[ET[below].right].right].value);
  1781. access_memory_z(storew_zc, ET[below].value,
  1782. ET[ET[below].right].value,
  1783. temp_var1);
  1784. write_result_z(Result, temp_var1);
  1785. }
  1786. else
  1787. access_memory_z(storew_zc, ET[below].value,
  1788. ET[ET[below].right].value,
  1789. ET[ET[ET[below].right].right].value);
  1790. break;
  1791. case INC_OP:
  1792. assemblez_inc(ET[below].value);
  1793. if (!void_flag) write_result_z(Result, ET[below].value);
  1794. break;
  1795. case DEC_OP:
  1796. assemblez_dec(ET[below].value);
  1797. if (!void_flag) write_result_z(Result, ET[below].value);
  1798. break;
  1799. case POST_INC_OP:
  1800. if (!void_flag) write_result_z(Result, ET[below].value);
  1801. assemblez_inc(ET[below].value);
  1802. break;
  1803. case POST_DEC_OP:
  1804. if (!void_flag) write_result_z(Result, ET[below].value);
  1805. assemblez_dec(ET[below].value);
  1806. break;
  1807. case ARROW_INC_OP:
  1808. assemblez_store(temp_var1, ET[below].value);
  1809. assemblez_store(temp_var2, ET[ET[below].right].value);
  1810. access_memory_z(loadb_zc, temp_var1, temp_var2, temp_var3);
  1811. assemblez_inc(temp_var3);
  1812. access_memory_z(storeb_zc, temp_var1, temp_var2, temp_var3);
  1813. if (!void_flag) write_result_z(Result, temp_var3);
  1814. break;
  1815. case ARROW_DEC_OP:
  1816. assemblez_store(temp_var1, ET[below].value);
  1817. assemblez_store(temp_var2, ET[ET[below].right].value);
  1818. access_memory_z(loadb_zc, temp_var1, temp_var2, temp_var3);
  1819. assemblez_dec(temp_var3);
  1820. access_memory_z(storeb_zc, temp_var1, temp_var2, temp_var3);
  1821. if (!void_flag) write_result_z(Result, temp_var3);
  1822. break;
  1823. case ARROW_POST_INC_OP:
  1824. assemblez_store(temp_var1, ET[below].value);
  1825. assemblez_store(temp_var2, ET[ET[below].right].value);
  1826. access_memory_z(loadb_zc, temp_var1, temp_var2, temp_var3);
  1827. if (!void_flag) write_result_z(Result, temp_var3);
  1828. assemblez_inc(temp_var3);
  1829. access_memory_z(storeb_zc, temp_var1, temp_var2, temp_var3);
  1830. break;
  1831. case ARROW_POST_DEC_OP:
  1832. assemblez_store(temp_var1, ET[below].value);
  1833. assemblez_store(temp_var2, ET[ET[below].right].value);
  1834. access_memory_z(loadb_zc, temp_var1, temp_var2, temp_var3);
  1835. if (!void_flag) write_result_z(Result, temp_var3);
  1836. assemblez_dec(temp_var3);
  1837. access_memory_z(storeb_zc, temp_var1, temp_var2, temp_var3);
  1838. break;
  1839. case DARROW_INC_OP:
  1840. assemblez_store(temp_var1, ET[below].value);
  1841. assemblez_store(temp_var2, ET[ET[below].right].value);
  1842. access_memory_z(loadw_zc, temp_var1, temp_var2, temp_var3);
  1843. assemblez_inc(temp_var3);
  1844. access_memory_z(storew_zc, temp_var1, temp_var2, temp_var3);
  1845. if (!void_flag) write_result_z(Result, temp_var3);
  1846. break;
  1847. case DARROW_DEC_OP:
  1848. assemblez_store(temp_var1, ET[below].value);
  1849. assemblez_store(temp_var2, ET[ET[below].right].value);
  1850. access_memory_z(loadw_zc, temp_var1, temp_var2, temp_var3);
  1851. assemblez_dec(temp_var3);
  1852. access_memory_z(storew_zc, temp_var1, temp_var2, temp_var3);
  1853. if (!void_flag) write_result_z(Result, temp_var3);
  1854. break;
  1855. case DARROW_POST_INC_OP:
  1856. assemblez_store(temp_var1, ET[below].value);
  1857. assemblez_store(temp_var2, ET[ET[below].right].value);
  1858. access_memory_z(loadw_zc, temp_var1, temp_var2, temp_var3);
  1859. if (!void_flag) write_result_z(Result, temp_var3);
  1860. assemblez_inc(temp_var3);
  1861. access_memory_z(storew_zc, temp_var1, temp_var2, temp_var3);
  1862. break;
  1863. case DARROW_POST_DEC_OP:
  1864. assemblez_store(temp_var1, ET[below].value);
  1865. assemblez_store(temp_var2, ET[ET[below].right].value);
  1866. access_memory_z(loadw_zc, temp_var1, temp_var2, temp_var3);
  1867. if (!void_flag) write_result_z(Result, temp_var3);
  1868. assemblez_dec(temp_var3);
  1869. access_memory_z(storew_zc, temp_var1, temp_var2, temp_var3);
  1870. break;
  1871. case PROPERTY_INC_OP:
  1872. assemblez_store(temp_var1, ET[below].value);
  1873. assemblez_store(temp_var2, ET[ET[below].right].value);
  1874. assemblez_2_to(get_prop_zc, temp_var1, temp_var2, temp_var3);
  1875. assemblez_inc(temp_var3);
  1876. if (runtime_error_checking_switch && (!veneer_mode))
  1877. assemblez_4(call_vn_zc, veneer_routine(RT__ChPS_VR),
  1878. temp_var1, temp_var2, temp_var3);
  1879. else assemblez_3(put_prop_zc, temp_var1, temp_var2, temp_var3);
  1880. if (!void_flag) write_result_z(Result, temp_var3);
  1881. break;
  1882. case PROPERTY_DEC_OP:
  1883. assemblez_store(temp_var1, ET[below].value);
  1884. assemblez_store(temp_var2, ET[ET[below].right].value);
  1885. assemblez_2_to(get_prop_zc, temp_var1, temp_var2, temp_var3);
  1886. assemblez_dec(temp_var3);
  1887. if (runtime_error_checking_switch && (!veneer_mode))
  1888. assemblez_4(call_vn_zc, veneer_routine(RT__ChPS_VR),
  1889. temp_var1, temp_var2, temp_var3);
  1890. else assemblez_3(put_prop_zc, temp_var1, temp_var2, temp_var3);
  1891. if (!void_flag) write_result_z(Result, temp_var3);
  1892. break;
  1893. case PROPERTY_POST_INC_OP:
  1894. assemblez_store(temp_var1, ET[below].value);
  1895. assemblez_store(temp_var2, ET[ET[below].right].value);
  1896. assemblez_2_to(get_prop_zc, temp_var1, temp_var2, temp_var3);
  1897. if (!void_flag) write_result_z(Result, temp_var3);
  1898. assemblez_inc(temp_var3);
  1899. if (runtime_error_checking_switch && (!veneer_mode))
  1900. assemblez_4(call_vn_zc, veneer_routine(RT__ChPS_VR),
  1901. temp_var1, temp_var2, temp_var3);
  1902. else assemblez_3(put_prop_zc, temp_var1, temp_var2, temp_var3);
  1903. break;
  1904. case PROPERTY_POST_DEC_OP:
  1905. assemblez_store(temp_var1, ET[below].value);
  1906. assemblez_store(temp_var2, ET[ET[below].right].value);
  1907. assemblez_2_to(get_prop_zc, temp_var1, temp_var2, temp_var3);
  1908. if (!void_flag) write_result_z(Result, temp_var3);
  1909. assemblez_dec(temp_var3);
  1910. if (runtime_error_checking_switch && (!veneer_mode))
  1911. assemblez_4(call_vn_zc, veneer_routine(RT__ChPS_VR),
  1912. temp_var1, temp_var2, temp_var3);
  1913. else assemblez_3(put_prop_zc, temp_var1, temp_var2, temp_var3);
  1914. break;
  1915. default:
  1916. printf("** Trouble op = %d i.e. '%s' **\n",
  1917. opnum, operators[opnum].description);
  1918. compiler_error("Expr code gen: Can't generate yet");
  1919. }
  1920. }
  1921. else {
  1922. assembly_operand AO, AO2;
  1923. if (operators[opnum].opcode_number_g != -1)
  1924. {
  1925. /* Operators directly translatable into opcodes: infix ops
  1926. take two operands whereas pre/postfix operators take only one */
  1927. if (operators[opnum].usage == IN_U)
  1928. { int o_n = operators[opnum].opcode_number_g;
  1929. if (runtime_error_checking_switch && (!veneer_mode)
  1930. && ((o_n == div_gc) || (o_n == mod_gc)))
  1931. { assembly_operand by_ao, error_ao; int ln;
  1932. by_ao = ET[ET[below].right].value;
  1933. if ((by_ao.value != 0) && (by_ao.marker == 0)
  1934. && is_constant_ot(by_ao.type))
  1935. assembleg_3(o_n, ET[below].value,
  1936. by_ao, Result);
  1937. else
  1938. { assembleg_store(temp_var1, ET[below].value);
  1939. assembleg_store(temp_var2, by_ao);
  1940. ln = next_label++;
  1941. assembleg_1_branch(jnz_gc, temp_var2, ln);
  1942. error_ao.marker = 0;
  1943. error_ao.value = DBYZERO_RTE;
  1944. set_constant_ot(&error_ao);
  1945. assembleg_call_1(veneer_routine(RT__Err_VR),
  1946. error_ao, zero_operand);
  1947. assembleg_store(temp_var2, one_operand);
  1948. assemble_label_no(ln);
  1949. assembleg_3(o_n, temp_var1, temp_var2, Result);
  1950. }
  1951. }
  1952. else
  1953. assembleg_3(o_n, ET[below].value,
  1954. ET[ET[below].right].value, Result);
  1955. }
  1956. else
  1957. assembleg_2(operators[opnum].opcode_number_g, ET[below].value,
  1958. Result);
  1959. }
  1960. else
  1961. switch(opnum)
  1962. {
  1963. case PUSH_OP:
  1964. if (ET[below].value.type == Result.type
  1965. && ET[below].value.value == Result.value
  1966. && ET[below].value.marker == Result.marker)
  1967. break;
  1968. assembleg_2(copy_gc, ET[below].value, Result);
  1969. break;
  1970. case UNARY_MINUS_OP:
  1971. assembleg_2(neg_gc, ET[below].value, Result);
  1972. break;
  1973. case ARTNOT_OP:
  1974. assembleg_2(bitnot_gc, ET[below].value, Result);
  1975. break;
  1976. case ARROW_OP:
  1977. access_memory_g(aloadb_gc, ET[below].value,
  1978. ET[ET[below].right].value, Result);
  1979. break;
  1980. case DARROW_OP:
  1981. access_memory_g(aload_gc, ET[below].value,
  1982. ET[ET[below].right].value, Result);
  1983. break;
  1984. case SETEQUALS_OP:
  1985. assembleg_store(ET[below].value,
  1986. ET[ET[below].right].value);
  1987. if (!void_flag) write_result_g(Result, ET[below].value);
  1988. break;
  1989. case ARROW_SETEQUALS_OP:
  1990. if (!void_flag)
  1991. { assembleg_store(temp_var1,
  1992. ET[ET[ET[below].right].right].value);
  1993. access_memory_g(astoreb_gc, ET[below].value,
  1994. ET[ET[below].right].value,
  1995. temp_var1);
  1996. write_result_g(Result, temp_var1);
  1997. }
  1998. else access_memory_g(astoreb_gc, ET[below].value,
  1999. ET[ET[below].right].value,
  2000. ET[ET[ET[below].right].right].value);
  2001. break;
  2002. case DARROW_SETEQUALS_OP:
  2003. if (!void_flag)
  2004. { assembleg_store(temp_var1,
  2005. ET[ET[ET[below].right].right].value);
  2006. access_memory_g(astore_gc, ET[below].value,
  2007. ET[ET[below].right].value,
  2008. temp_var1);
  2009. write_result_g(Result, temp_var1);
  2010. }
  2011. else
  2012. access_memory_g(astore_gc, ET[below].value,
  2013. ET[ET[below].right].value,
  2014. ET[ET[ET[below].right].right].value);
  2015. break;
  2016. case INC_OP:
  2017. assembleg_inc(ET[below].value);
  2018. if (!void_flag) write_result_g(Result, ET[below].value);
  2019. break;
  2020. case DEC_OP:
  2021. assembleg_dec(ET[below].value);
  2022. if (!void_flag) write_result_g(Result, ET[below].value);
  2023. break;
  2024. case POST_INC_OP:
  2025. if (!void_flag) write_result_g(Result, ET[below].value);
  2026. assembleg_inc(ET[below].value);
  2027. break;
  2028. case POST_DEC_OP:
  2029. if (!void_flag) write_result_g(Result, ET[below].value);
  2030. assembleg_dec(ET[below].value);
  2031. break;
  2032. case ARROW_INC_OP:
  2033. assembleg_store(temp_var1, ET[below].value);
  2034. assembleg_store(temp_var2, ET[ET[below].right].value);
  2035. access_memory_g(aloadb_gc, temp_var1, temp_var2, temp_var3);
  2036. assembleg_inc(temp_var3);
  2037. access_memory_g(astoreb_gc, temp_var1, temp_var2, temp_var3);
  2038. if (!void_flag) write_result_g(Result, temp_var3);
  2039. break;
  2040. case ARROW_DEC_OP:
  2041. assembleg_store(temp_var1, ET[below].value);
  2042. assembleg_store(temp_var2, ET[ET[below].right].value);
  2043. access_memory_g(aloadb_gc, temp_var1, temp_var2, temp_var3);
  2044. assembleg_dec(temp_var3);
  2045. access_memory_g(astoreb_gc, temp_var1, temp_var2, temp_var3);
  2046. if (!void_flag) write_result_g(Result, temp_var3);
  2047. break;
  2048. case ARROW_POST_INC_OP:
  2049. assembleg_store(temp_var1, ET[below].value);
  2050. assembleg_store(temp_var2, ET[ET[below].right].value);
  2051. access_memory_g(aloadb_gc, temp_var1, temp_var2, temp_var3);
  2052. if (!void_flag) write_result_g(Result, temp_var3);
  2053. assembleg_inc(temp_var3);
  2054. access_memory_g(astoreb_gc, temp_var1, temp_var2, temp_var3);
  2055. break;
  2056. case ARROW_POST_DEC_OP:
  2057. assembleg_store(temp_var1, ET[below].value);
  2058. assembleg_store(temp_var2, ET[ET[below].right].value);
  2059. access_memory_g(aloadb_gc, temp_var1, temp_var2, temp_var3);
  2060. if (!void_flag) write_result_g(Result, temp_var3);
  2061. assembleg_dec(temp_var3);
  2062. access_memory_g(astoreb_gc, temp_var1, temp_var2, temp_var3);
  2063. break;
  2064. case DARROW_INC_OP:
  2065. assembleg_store(temp_var1, ET[below].value);
  2066. assembleg_store(temp_var2, ET[ET[below].right].value);
  2067. access_memory_g(aload_gc, temp_var1, temp_var2, temp_var3);
  2068. assembleg_inc(temp_var3);
  2069. access_memory_g(astore_gc, temp_var1, temp_var2, temp_var3);
  2070. if (!void_flag) write_result_g(Result, temp_var3);
  2071. break;
  2072. case DARROW_DEC_OP:
  2073. assembleg_store(temp_var1, ET[below].value);
  2074. assembleg_store(temp_var2, ET[ET[below].right].value);
  2075. access_memory_g(aload_gc, temp_var1, temp_var2, temp_var3);
  2076. assembleg_dec(temp_var3);
  2077. access_memory_g(astore_gc, temp_var1, temp_var2, temp_var3);
  2078. if (!void_flag) write_result_g(Result, temp_var3);
  2079. break;
  2080. case DARROW_POST_INC_OP:
  2081. assembleg_store(temp_var1, ET[below].value);
  2082. assembleg_store(temp_var2, ET[ET[below].right].value);
  2083. access_memory_g(aload_gc, temp_var1, temp_var2, temp_var3);
  2084. if (!void_flag) write_result_g(Result, temp_var3);
  2085. assembleg_inc(temp_var3);
  2086. access_memory_g(astore_gc, temp_var1, temp_var2, temp_var3);
  2087. break;
  2088. case DARROW_POST_DEC_OP:
  2089. assembleg_store(temp_var1, ET[below].value);
  2090. assembleg_store(temp_var2, ET[ET[below].right].value);
  2091. access_memory_g(aload_gc, temp_var1, temp_var2, temp_var3);
  2092. if (!void_flag) write_result_g(Result, temp_var3);
  2093. assembleg_dec(temp_var3);
  2094. access_memory_g(astore_gc, temp_var1, temp_var2, temp_var3);
  2095. break;
  2096. case PROPERTY_OP:
  2097. case MESSAGE_OP:
  2098. AO = veneer_routine(RV__Pr_VR);
  2099. goto TwoArgFunctionCall;
  2100. case MPROP_ADD_OP:
  2101. case PROP_ADD_OP:
  2102. AO = veneer_routine(RA__Pr_VR);
  2103. goto TwoArgFunctionCall;
  2104. case MPROP_NUM_OP:
  2105. case PROP_NUM_OP:
  2106. AO = veneer_routine(RL__Pr_VR);
  2107. goto TwoArgFunctionCall;
  2108. case PROP_CALL_OP:
  2109. case MESSAGE_CALL_OP:
  2110. AO2 = veneer_routine(CA__Pr_VR);
  2111. i = below;
  2112. goto DoFunctionCall;
  2113. case MESSAGE_INC_OP:
  2114. case PROPERTY_INC_OP:
  2115. AO = veneer_routine(IB__Pr_VR);
  2116. goto TwoArgFunctionCall;
  2117. case MESSAGE_DEC_OP:
  2118. case PROPERTY_DEC_OP:
  2119. AO = veneer_routine(DB__Pr_VR);
  2120. goto TwoArgFunctionCall;
  2121. case MESSAGE_POST_INC_OP:
  2122. case PROPERTY_POST_INC_OP:
  2123. AO = veneer_routine(IA__Pr_VR);
  2124. goto TwoArgFunctionCall;
  2125. case MESSAGE_POST_DEC_OP:
  2126. case PROPERTY_POST_DEC_OP:
  2127. AO = veneer_routine(DA__Pr_VR);
  2128. goto TwoArgFunctionCall;
  2129. case SUPERCLASS_OP:
  2130. AO = veneer_routine(RA__Sc_VR);
  2131. goto TwoArgFunctionCall;
  2132. TwoArgFunctionCall:
  2133. {
  2134. assembly_operand AO2 = ET[below].value;
  2135. assembly_operand AO3 = ET[ET[below].right].value;
  2136. if (void_flag)
  2137. assembleg_call_2(AO, AO2, AO3, zero_operand);
  2138. else
  2139. assembleg_call_2(AO, AO2, AO3, Result);
  2140. }
  2141. break;
  2142. case PROPERTY_SETEQUALS_OP:
  2143. case MESSAGE_SETEQUALS_OP:
  2144. if (runtime_error_checking_switch && (!veneer_mode))
  2145. AO = veneer_routine(RT__ChPS_VR);
  2146. else
  2147. AO = veneer_routine(WV__Pr_VR);
  2148. {
  2149. assembly_operand AO2 = ET[below].value;
  2150. assembly_operand AO3 = ET[ET[below].right].value;
  2151. assembly_operand AO4 = ET[ET[ET[below].right].right].value;
  2152. if (AO4.type == LOCALVAR_OT && AO4.value == 0) {
  2153. /* Rightmost is on the stack; reduce to previous case. */
  2154. if (AO2.type == LOCALVAR_OT && AO2.value == 0) {
  2155. if (AO3.type == LOCALVAR_OT && AO3.value == 0) {
  2156. /* both already on stack. */
  2157. }
  2158. else {
  2159. assembleg_store(stack_pointer, AO3);
  2160. assembleg_0(stkswap_gc);
  2161. }
  2162. }
  2163. else {
  2164. if (AO3.type == LOCALVAR_OT && AO3.value == 0) {
  2165. assembleg_store(stack_pointer, AO2);
  2166. }
  2167. else {
  2168. assembleg_store(stack_pointer, AO3);
  2169. assembleg_store(stack_pointer, AO2);
  2170. }
  2171. }
  2172. }
  2173. else {
  2174. /* We have to get the rightmost on the stack, below the
  2175. others. */
  2176. if (AO3.type == LOCALVAR_OT && AO3.value == 0) {
  2177. if (AO2.type == LOCALVAR_OT && AO2.value == 0) {
  2178. assembleg_store(stack_pointer, AO4);
  2179. assembleg_2(stkroll_gc, three_operand, one_operand);
  2180. }
  2181. else {
  2182. assembleg_store(stack_pointer, AO4);
  2183. assembleg_0(stkswap_gc);
  2184. assembleg_store(stack_pointer, AO2);
  2185. }
  2186. }
  2187. else {
  2188. if (AO2.type == LOCALVAR_OT && AO2.value == 0) {
  2189. assembleg_store(stack_pointer, AO4);
  2190. assembleg_store(stack_pointer, AO3);
  2191. assembleg_2(stkroll_gc, three_operand, two_operand);
  2192. }
  2193. else {
  2194. assembleg_store(stack_pointer, AO4);
  2195. assembleg_store(stack_pointer, AO3);
  2196. assembleg_store(stack_pointer, AO2);
  2197. }
  2198. }
  2199. }
  2200. if (void_flag)
  2201. assembleg_3(call_gc, AO, three_operand, zero_operand);
  2202. else
  2203. assembleg_3(call_gc, AO, three_operand, Result);
  2204. }
  2205. break;
  2206. case FCALL_OP:
  2207. j = 0;
  2208. if (ET[below].value.type == SYSFUN_OT)
  2209. { int sf_number = ET[below].value.value;
  2210. i = ET[below].right;
  2211. if (i == -1)
  2212. { error("Argument to system function missing");
  2213. AI.operand[0] = one_operand;
  2214. AI.operand_count = 1;
  2215. }
  2216. else
  2217. { j=0;
  2218. while (i != -1) { j++; i = ET[i].right; }
  2219. if (((sf_number != INDIRECT_SYSF) &&
  2220. (sf_number != GLK_SYSF) &&
  2221. (sf_number != RANDOM_SYSF) && (j > 1)))
  2222. { j=1;
  2223. error("System function given with too many arguments");
  2224. }
  2225. if (sf_number != RANDOM_SYSF)
  2226. { int jcount;
  2227. i = ET[below].right;
  2228. for (jcount = 0; jcount < j; jcount++)
  2229. { AI.operand[jcount] = ET[i].value;
  2230. i = ET[i].right;
  2231. }
  2232. AI.operand_count = j;
  2233. }
  2234. }
  2235. switch(sf_number)
  2236. {
  2237. case RANDOM_SYSF:
  2238. if (j>1)
  2239. { assembly_operand AO, AO2;
  2240. int arg_c, arg_et;
  2241. AO.value = j;
  2242. AO.marker = 0;
  2243. set_constant_ot(&AO);
  2244. AO2.type = CONSTANT_OT;
  2245. AO2.value = begin_word_array();
  2246. AO2.marker = ARRAY_MV;
  2247. for (arg_c=0, arg_et = ET[below].right;arg_c<j;
  2248. arg_c++, arg_et = ET[arg_et].right)
  2249. { if (ET[arg_et].value.type == LOCALVAR_OT
  2250. || ET[arg_et].value.type == GLOBALVAR_OT)
  2251. error("Only constants can be used as possible 'random' results");
  2252. array_entry(arg_c, ET[arg_et].value);
  2253. }
  2254. finish_array(arg_c);
  2255. assembleg_2(random_gc, AO, stack_pointer);
  2256. assembleg_3(aload_gc, AO2, stack_pointer, Result);
  2257. }
  2258. else {
  2259. assembleg_2(random_gc,
  2260. ET[ET[below].right].value, stack_pointer);
  2261. assembleg_3(add_gc, stack_pointer, one_operand,
  2262. Result);
  2263. }
  2264. break;
  2265. case PARENT_SYSF:
  2266. { assembly_operand AO;
  2267. AO = ET[ET[below].right].value;
  2268. if (runtime_error_checking_switch)
  2269. AO = check_nonzero_at_runtime(AO, -1,
  2270. PARENT_RTE);
  2271. AO2.type = BYTECONSTANT_OT;
  2272. AO2.value = GOBJFIELD_PARENT();
  2273. AO2.marker = 0;
  2274. assembleg_3(aload_gc, AO, AO2, Result);
  2275. }
  2276. break;
  2277. case ELDEST_SYSF:
  2278. case CHILD_SYSF:
  2279. { assembly_operand AO;
  2280. AO = ET[ET[below].right].value;
  2281. if (runtime_error_checking_switch)
  2282. AO = check_nonzero_at_runtime(AO, -1,
  2283. (sf_number==CHILD_SYSF)?CHILD_RTE:ELDEST_RTE);
  2284. AO2.type = BYTECONSTANT_OT;
  2285. AO2.value = GOBJFIELD_CHILD();
  2286. AO2.marker = 0;
  2287. assembleg_3(aload_gc, AO, AO2, Result);
  2288. }
  2289. break;
  2290. case YOUNGER_SYSF:
  2291. case SIBLING_SYSF:
  2292. { assembly_operand AO;
  2293. AO = ET[ET[below].right].value;
  2294. if (runtime_error_checking_switch)
  2295. AO = check_nonzero_at_runtime(AO, -1,
  2296. (sf_number==SIBLING_SYSF)
  2297. ?SIBLING_RTE:YOUNGER_RTE);
  2298. AO2.type = BYTECONSTANT_OT;
  2299. AO2.value = GOBJFIELD_SIBLING();
  2300. AO2.marker = 0;
  2301. assembleg_3(aload_gc, AO, AO2, Result);
  2302. }
  2303. break;
  2304. case CHILDREN_SYSF:
  2305. { assembly_operand AO;
  2306. AO = ET[ET[below].right].value;
  2307. if (runtime_error_checking_switch)
  2308. AO = check_nonzero_at_runtime(AO, -1,
  2309. CHILDREN_RTE);
  2310. AO2.type = BYTECONSTANT_OT;
  2311. AO2.value = GOBJFIELD_CHILD();
  2312. AO2.marker = 0;
  2313. assembleg_store(temp_var1, zero_operand);
  2314. assembleg_3(aload_gc, AO, AO2, temp_var2);
  2315. AO2.value = GOBJFIELD_SIBLING();
  2316. assemble_label_no(next_label);
  2317. assembleg_1_branch(jz_gc, temp_var2, next_label+1);
  2318. assembleg_3(add_gc, temp_var1, one_operand,
  2319. temp_var1);
  2320. assembleg_3(aload_gc, temp_var2, AO2, temp_var2);
  2321. assembleg_0_branch(jump_gc, next_label);
  2322. assemble_label_no(next_label+1);
  2323. next_label += 2;
  2324. if (!void_flag)
  2325. write_result_g(Result, temp_var1);
  2326. }
  2327. break;
  2328. case INDIRECT_SYSF:
  2329. i = ET[below].right;
  2330. goto IndirectFunctionCallG;
  2331. case GLK_SYSF:
  2332. AO2 = veneer_routine(Glk__Wrap_VR);
  2333. i = ET[below].right;
  2334. goto DoFunctionCall;
  2335. case METACLASS_SYSF:
  2336. assembleg_call_1(veneer_routine(Metaclass_VR),
  2337. ET[ET[below].right].value, Result);
  2338. break;
  2339. case YOUNGEST_SYSF:
  2340. AO = ET[ET[below].right].value;
  2341. if (runtime_error_checking_switch)
  2342. AO = check_nonzero_at_runtime(AO, -1,
  2343. YOUNGEST_RTE);
  2344. AO2.marker = 0;
  2345. AO2.value = GOBJFIELD_CHILD();
  2346. AO2.type = BYTECONSTANT_OT;
  2347. assembleg_3(aload_gc, AO, AO2, temp_var1);
  2348. AO2.value = GOBJFIELD_SIBLING();
  2349. assembleg_1_branch(jz_gc, temp_var1, next_label+1);
  2350. assemble_label_no(next_label);
  2351. assembleg_3(aload_gc, temp_var1, AO2, temp_var2);
  2352. assembleg_1_branch(jz_gc, temp_var2, next_label+1);
  2353. assembleg_store(temp_var1, temp_var2);
  2354. assembleg_0_branch(jump_gc, next_label);
  2355. assemble_label_no(next_label+1);
  2356. if (!void_flag)
  2357. write_result_g(Result, temp_var1);
  2358. next_label += 2;
  2359. break;
  2360. case ELDER_SYSF:
  2361. AO = ET[ET[below].right].value;
  2362. if (runtime_error_checking_switch)
  2363. AO = check_nonzero_at_runtime(AO, -1,
  2364. YOUNGEST_RTE);
  2365. assembleg_store(temp_var3, AO);
  2366. AO2.marker = 0;
  2367. AO2.value = GOBJFIELD_PARENT();
  2368. AO2.type = BYTECONSTANT_OT;
  2369. assembleg_3(aload_gc, temp_var3, AO2, temp_var1);
  2370. assembleg_1_branch(jz_gc, temp_var1, next_label+2);
  2371. AO2.value = GOBJFIELD_CHILD();
  2372. assembleg_3(aload_gc, temp_var1, AO2, temp_var1);
  2373. assembleg_1_branch(jz_gc, temp_var1, next_label+2);
  2374. assembleg_2_branch(jeq_gc, temp_var3, temp_var1,
  2375. next_label+1);
  2376. assemble_label_no(next_label);
  2377. AO2.value = GOBJFIELD_SIBLING();
  2378. assembleg_3(aload_gc, temp_var1, AO2, temp_var2);
  2379. assembleg_2_branch(jeq_gc, temp_var3, temp_var2,
  2380. next_label+2);
  2381. assembleg_store(temp_var1, temp_var2);
  2382. assembleg_0_branch(jump_gc, next_label);
  2383. assemble_label_no(next_label+1);
  2384. assembleg_store(temp_var1, zero_operand);
  2385. assemble_label_no(next_label+2);
  2386. if (!void_flag)
  2387. write_result_g(Result, temp_var1);
  2388. next_label += 3;
  2389. break;
  2390. default:
  2391. error("*** system function not implemented ***");
  2392. break;
  2393. }
  2394. break;
  2395. }
  2396. i = below;
  2397. IndirectFunctionCallG:
  2398. /* Get the function address. */
  2399. AO2 = ET[i].value;
  2400. i = ET[i].right;
  2401. DoFunctionCall:
  2402. {
  2403. /* If all the function arguments are in local/global
  2404. variables, we have to push them all on the stack.
  2405. If all of them are on the stack, we have to do nothing.
  2406. If some are and some aren't, we have a hopeless mess,
  2407. and we should throw a compiler error.
  2408. */
  2409. int onstack = 0;
  2410. int offstack = 0;
  2411. /* begin part of patch G03701 */
  2412. int nargs = 0;
  2413. j = i;
  2414. while (j != -1) {
  2415. nargs++;
  2416. j = ET[j].right;
  2417. }
  2418. if (nargs==0) {
  2419. assembleg_2(callf_gc, AO2, void_flag ? zero_operand : Result);
  2420. } else if (nargs==1) {
  2421. assembleg_call_1(AO2, ET[i].value, void_flag ? zero_operand : Result);
  2422. } else if (nargs==2) {
  2423. assembly_operand o1 = ET[i].value;
  2424. assembly_operand o2 = ET[ET[i].right].value;
  2425. assembleg_call_2(AO2, o1, o2, void_flag ? zero_operand : Result);
  2426. } else if (nargs==3) {
  2427. assembly_operand o1 = ET[i].value;
  2428. assembly_operand o2 = ET[ET[i].right].value;
  2429. assembly_operand o3 = ET[ET[ET[i].right].right].value;
  2430. assembleg_call_3(AO2, o1, o2, o3, void_flag ? zero_operand : Result);
  2431. } else {
  2432. j = 0;
  2433. while (i != -1) {
  2434. if (ET[i].value.type == LOCALVAR_OT
  2435. && ET[i].value.value == 0) {
  2436. onstack++;
  2437. }
  2438. else {
  2439. assembleg_store(stack_pointer, ET[i].value);
  2440. offstack++;
  2441. }
  2442. i = ET[i].right;
  2443. j++;
  2444. }
  2445. if (onstack && offstack)
  2446. error("*** Function call cannot be generated with mixed arguments ***");
  2447. if (offstack > 1)
  2448. error("*** Function call cannot be generated with more than one nonstack argument ***");
  2449. AO.value = j;
  2450. AO.marker = 0;
  2451. set_constant_ot(&AO);
  2452. if (void_flag)
  2453. assembleg_3(call_gc, AO2, AO, zero_operand);
  2454. else
  2455. assembleg_3(call_gc, AO2, AO, Result);
  2456. } /* else nargs>=4 */
  2457. } /* DoFunctionCall: */
  2458. break;
  2459. default:
  2460. printf("** Trouble op = %d i.e. '%s' **\n",
  2461. opnum, operators[opnum].description);
  2462. compiler_error("Expr code gen: Can't generate yet");
  2463. }
  2464. }
  2465. ET[n].value = Result;
  2466. OperatorGenerated:
  2467. if (!glulx_mode) {
  2468. if (ET[n].to_expression)
  2469. { if (ET[n].true_label != -1)
  2470. { assemblez_1(push_zc, zero_operand);
  2471. assemblez_jump(next_label++);
  2472. assemble_label_no(ET[n].true_label);
  2473. assemblez_1(push_zc, one_operand);
  2474. assemble_label_no(next_label-1);
  2475. }
  2476. else
  2477. { assemblez_1(push_zc, one_operand);
  2478. assemblez_jump(next_label++);
  2479. assemble_label_no(ET[n].false_label);
  2480. assemblez_1(push_zc, zero_operand);
  2481. assemble_label_no(next_label-1);
  2482. }
  2483. ET[n].value = stack_pointer;
  2484. }
  2485. else
  2486. if (ET[n].label_after != -1)
  2487. assemble_label_no(ET[n].label_after);
  2488. }
  2489. else {
  2490. if (ET[n].to_expression)
  2491. { if (ET[n].true_label != -1)
  2492. { assembleg_store(stack_pointer, zero_operand);
  2493. assembleg_jump(next_label++);
  2494. assemble_label_no(ET[n].true_label);
  2495. assembleg_store(stack_pointer, one_operand);
  2496. assemble_label_no(next_label-1);
  2497. }
  2498. else
  2499. { assembleg_store(stack_pointer, one_operand);
  2500. assembleg_jump(next_label++);
  2501. assemble_label_no(ET[n].false_label);
  2502. assembleg_store(stack_pointer, zero_operand);
  2503. assemble_label_no(next_label-1);
  2504. }
  2505. ET[n].value = stack_pointer;
  2506. }
  2507. else
  2508. if (ET[n].label_after != -1)
  2509. assemble_label_no(ET[n].label_after);
  2510. }
  2511. ET[n].down = -1;
  2512. }
  2513. assembly_operand code_generate(assembly_operand AO, int context, int label)
  2514. {
  2515. /* Used in three contexts: VOID_CONTEXT, CONDITION_CONTEXT and
  2516. QUANTITY_CONTEXT.
  2517. If CONDITION_CONTEXT, then compile code branching to label number
  2518. "label" if the condition is false: there's no return value.
  2519. (Except that if label is -3 or -4 (internal codes for rfalse and
  2520. rtrue rather than branch) then this is for branching when the
  2521. condition is true. This is used for optimising code generation
  2522. for "if" statements.)
  2523. Otherwise return the assembly operand containing the result
  2524. (probably the stack pointer variable but not necessarily:
  2525. e.g. is would be short constant 2 from the expression "j++, 2") */
  2526. vivc_flag = FALSE;
  2527. if (AO.type != EXPRESSION_OT)
  2528. { switch(context)
  2529. { case VOID_CONTEXT:
  2530. value_in_void_context(AO);
  2531. AO.type = OMITTED_OT;
  2532. AO.value = 0;
  2533. break;
  2534. case CONDITION_CONTEXT:
  2535. if (!glulx_mode) {
  2536. if (label < -2) assemblez_1_branch(jz_zc, AO, label, FALSE);
  2537. else assemblez_1_branch(jz_zc, AO, label, TRUE);
  2538. }
  2539. else {
  2540. if (label < -2)
  2541. assembleg_1_branch(jnz_gc, AO, label);
  2542. else
  2543. assembleg_1_branch(jz_gc, AO, label);
  2544. }
  2545. AO.type = OMITTED_OT;
  2546. AO.value = 0;
  2547. break;
  2548. }
  2549. return AO;
  2550. }
  2551. if (expr_trace_level >= 2)
  2552. { printf("Raw parse tree:\n"); show_tree(AO, FALSE);
  2553. }
  2554. if (context == CONDITION_CONTEXT)
  2555. { if (label < -2) annotate_for_conditions(AO.value, label, -1);
  2556. else annotate_for_conditions(AO.value, -1, label);
  2557. }
  2558. else annotate_for_conditions(AO.value, -1, -1);
  2559. if (expr_trace_level >= 1)
  2560. { printf("Code generation for expression in ");
  2561. switch(context)
  2562. { case VOID_CONTEXT: printf("void"); break;
  2563. case CONDITION_CONTEXT: printf("condition"); break;
  2564. case QUANTITY_CONTEXT: printf("quantity"); break;
  2565. case ASSEMBLY_CONTEXT: printf("assembly"); break;
  2566. case ARRAY_CONTEXT: printf("array initialisation"); break;
  2567. default: printf("* ILLEGAL *"); break;
  2568. }
  2569. printf(" context with annotated tree:\n");
  2570. show_tree(AO, TRUE);
  2571. }
  2572. generate_code_from(AO.value, (context==VOID_CONTEXT));
  2573. return ET[AO.value].value;
  2574. }
  2575. /* ========================================================================= */
  2576. /* Data structure management routines */
  2577. /* ------------------------------------------------------------------------- */
  2578. extern void init_expressc_vars(void)
  2579. { make_operands();
  2580. }
  2581. extern void expressc_begin_pass(void)
  2582. {
  2583. }
  2584. extern void expressc_allocate_arrays(void)
  2585. {
  2586. }
  2587. extern void expressc_free_arrays(void)
  2588. {
  2589. }
  2590. /* ========================================================================= */