asm.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173
  1. /* ------------------------------------------------------------------------- */
  2. /* "asm" : The Inform assembler */
  3. /* */
  4. /* Part of Inform 6.33 */
  5. /* copyright (c) Graham Nelson 1993 - 2014 */
  6. /* */
  7. /* ------------------------------------------------------------------------- */
  8. #include "header.h"
  9. uchar *zcode_holding_area; /* Area holding code yet to be transferred
  10. to either zcode_area or temp file no 1 */
  11. uchar *zcode_markers; /* Bytes holding marker values for this
  12. code */
  13. static int zcode_ha_size; /* Number of bytes in holding area */
  14. memory_block zcode_area; /* Block to hold assembled code (if
  15. temporary files are not being used) */
  16. int32 zmachine_pc; /* PC position of assembly (byte offset
  17. from start of Z-code area) */
  18. int32 no_instructions; /* Number of instructions assembled */
  19. int execution_never_reaches_here, /* TRUE if the current PC value in the
  20. code area cannot be reached: e.g. if
  21. the previous instruction was a "quit"
  22. opcode and no label is set to here */
  23. next_label, /* Used to count the labels created all
  24. over Inform in current routine, from 0 */
  25. next_sequence_point; /* Likewise, for sequence points */
  26. int no_sequence_points; /* Kept for statistics purposes only */
  27. static int label_moved_error_already_given;
  28. /* When one label has moved, all subsequent
  29. ones probably have too, and this flag
  30. suppresses the runaway chain of error
  31. messages which would otherwise result */
  32. int sequence_point_follows; /* Will the next instruction assembled */
  33. /* be at a sequence point in the routine? */
  34. int uses_unicode_features; /* Makes use of Glulx Unicode (3.0)
  35. features? */
  36. int uses_memheap_features; /* Makes use of Glulx mem/heap (3.1)
  37. features? */
  38. int uses_acceleration_features; /* Makes use of Glulx acceleration (3.1.1)
  39. features? */
  40. int uses_float_features; /* Makes use of Glulx floating-point (3.1.2)
  41. features? */
  42. debug_location statement_debug_location;
  43. /* Location of current statement */
  44. int32 *variable_tokens; /* The allocated size is
  45. (MAX_LOCAL_VARIABLES +
  46. MAX_GLOBAL_VARIABLES). The entries
  47. MAX_LOCAL_VARIABLES and up give the
  48. symbol table index for the names of
  49. the global variables */
  50. int *variable_usage; /* TRUE if referred to, FALSE otherwise */
  51. assembly_instruction AI; /* A structure used to hold the full
  52. specification of a single Z-code
  53. instruction: effectively this is the
  54. input to the routine
  55. assemble_instruction() */
  56. static char opcode_syntax_string[128]; /* Text buffer holding the correct
  57. syntax for an opcode: used to produce
  58. helpful assembler error messages */
  59. static int routine_symbol; /* The symbol index of the routine currently
  60. being compiled */
  61. static char *routine_name; /* The name of the routine currently being
  62. compiled */
  63. static int routine_locals; /* The number of local variables used by
  64. the routine currently being compiled */
  65. static int32 routine_start_pc;
  66. int32 *named_routine_symbols;
  67. static void transfer_routine_z(void);
  68. static void transfer_routine_g(void);
  69. /* ------------------------------------------------------------------------- */
  70. /* Label data */
  71. /* ------------------------------------------------------------------------- */
  72. static int first_label, last_label;
  73. static int32 *label_offsets; /* Double-linked list of label offsets */
  74. static int *label_next, /* (i.e. zmachine_pc values) in PC order */
  75. *label_prev;
  76. static int32 *label_symbols; /* Symbol numbers if defined in source */
  77. static int *sequence_point_labels;
  78. /* Label numbers for each */
  79. static debug_location *sequence_point_locations;
  80. /* Source code references for each */
  81. /* (used for making debugging file) */
  82. static void set_label_offset(int label, int32 offset)
  83. {
  84. if (label >= MAX_LABELS) memoryerror("MAX_LABELS", MAX_LABELS);
  85. label_offsets[label] = offset;
  86. if (last_label == -1)
  87. { label_prev[label] = -1;
  88. first_label = label;
  89. }
  90. else
  91. { label_prev[label] = last_label;
  92. label_next[last_label] = label;
  93. }
  94. last_label = label;
  95. label_next[label] = -1;
  96. label_symbols[label] = -1;
  97. }
  98. /* ------------------------------------------------------------------------- */
  99. /* Useful tool for building operands */
  100. /* ------------------------------------------------------------------------- */
  101. extern void set_constant_ot(assembly_operand *AO)
  102. {
  103. if (!glulx_mode) {
  104. if (AO->value >= 0 && AO->value <= 255)
  105. AO->type = SHORT_CONSTANT_OT;
  106. else
  107. AO->type = LONG_CONSTANT_OT;
  108. }
  109. else {
  110. if (AO->value == 0)
  111. AO->type = ZEROCONSTANT_OT;
  112. else if (AO->value >= -0x80 && AO->value < 0x80)
  113. AO->type = BYTECONSTANT_OT;
  114. else if (AO->value >= -0x8000 && AO->value < 0x8000)
  115. AO->type = HALFCONSTANT_OT;
  116. else
  117. AO->type = CONSTANT_OT;
  118. }
  119. }
  120. extern int is_constant_ot(int otval)
  121. {
  122. if (!glulx_mode) {
  123. return ((otval == LONG_CONSTANT_OT)
  124. || (otval == SHORT_CONSTANT_OT));
  125. }
  126. else {
  127. return ((otval == CONSTANT_OT)
  128. || (otval == HALFCONSTANT_OT)
  129. || (otval == BYTECONSTANT_OT)
  130. || (otval == ZEROCONSTANT_OT));
  131. }
  132. }
  133. extern int is_variable_ot(int otval)
  134. {
  135. if (!glulx_mode) {
  136. return (otval == VARIABLE_OT);
  137. }
  138. else {
  139. return ((otval == LOCALVAR_OT)
  140. || (otval == GLOBALVAR_OT));
  141. }
  142. }
  143. /* ------------------------------------------------------------------------- */
  144. /* Used in printing assembly traces */
  145. /* ------------------------------------------------------------------------- */
  146. extern char *variable_name(int32 i)
  147. {
  148. if (i==0) return("sp");
  149. if (i<MAX_LOCAL_VARIABLES) return local_variable_texts[i-1];
  150. if (!glulx_mode) {
  151. if (i==255) return("TEMP1");
  152. if (i==254) return("TEMP2");
  153. if (i==253) return("TEMP3");
  154. if (i==252) return("TEMP4");
  155. if (i==251) return("self");
  156. if (i==250) return("sender");
  157. if (i==249) return("sw__var");
  158. if (i >= 256 && i < 286)
  159. { if (i - 256 < NUMBER_SYSTEM_FUNCTIONS) return system_functions.keywords[i - 256];
  160. return "<unnamed system function>";
  161. }
  162. }
  163. else {
  164. switch (i - MAX_LOCAL_VARIABLES) {
  165. case 0: return "temp_global";
  166. case 1: return "temp__global2";
  167. case 2: return "temp__global3";
  168. case 3: return "temp__global4";
  169. case 4: return "self";
  170. case 5: return "sender";
  171. case 6: return "sw__var";
  172. case 7: return "sys__glob0";
  173. case 8: return "sys__glob1";
  174. case 9: return "sys__glob2";
  175. case 10: return "sys_statusline_flag";
  176. }
  177. }
  178. return ((char *) symbs[variable_tokens[i]]);
  179. }
  180. static void print_operand_z(assembly_operand o)
  181. { switch(o.type)
  182. { case EXPRESSION_OT: printf("expr_"); break;
  183. case LONG_CONSTANT_OT: printf("long_"); break;
  184. case SHORT_CONSTANT_OT: printf("short_"); break;
  185. case VARIABLE_OT:
  186. if (o.value==0) { printf("sp"); return; }
  187. printf("%s", variable_name(o.value)); return;
  188. case OMITTED_OT: printf("<no value>"); return;
  189. }
  190. printf("%d", o.value);
  191. }
  192. static void print_operand_g(assembly_operand o)
  193. {
  194. switch (o.type) {
  195. case EXPRESSION_OT: printf("expr_"); break;
  196. case CONSTANT_OT: printf("long_"); break;
  197. case HALFCONSTANT_OT: printf("short_"); break;
  198. case BYTECONSTANT_OT: printf("byte_"); break;
  199. case ZEROCONSTANT_OT: printf("zero_"); return;
  200. case DEREFERENCE_OT: printf("*"); break;
  201. case GLOBALVAR_OT:
  202. printf("%s (global_%d)", variable_name(o.value), o.value);
  203. return;
  204. case LOCALVAR_OT:
  205. if (o.value == 0)
  206. printf("stackptr");
  207. else
  208. printf("%s (local_%d)", variable_name(o.value), o.value-1);
  209. return;
  210. case SYSFUN_OT:
  211. if (o.value >= 0 && o.value < NUMBER_SYSTEM_FUNCTIONS)
  212. printf("%s", system_functions.keywords[o.value]);
  213. else
  214. printf("<unnamed system function>");
  215. return;
  216. case OMITTED_OT: printf("<no value>"); return;
  217. default: printf("???_"); break;
  218. }
  219. printf("%d", o.value);
  220. }
  221. extern void print_operand(assembly_operand o)
  222. {
  223. if (!glulx_mode)
  224. print_operand_z(o);
  225. else
  226. print_operand_g(o);
  227. }
  228. /* ------------------------------------------------------------------------- */
  229. /* Writing bytes to the code area */
  230. /* ------------------------------------------------------------------------- */
  231. static void byteout(int32 i, int mv)
  232. { if (zcode_ha_size >= MAX_ZCODE_SIZE)
  233. memoryerror("MAX_ZCODE_SIZE",MAX_ZCODE_SIZE);
  234. zcode_markers[zcode_ha_size] = (uchar) mv;
  235. zcode_holding_area[zcode_ha_size++] = (uchar) i;
  236. zmachine_pc++;
  237. }
  238. /* ------------------------------------------------------------------------- */
  239. /* A database of the 115 canonical Infocom opcodes in Versions 3 to 6 */
  240. /* And of the however-many-there-are Glulx opcode */
  241. /* ------------------------------------------------------------------------- */
  242. typedef struct opcodez
  243. { uchar *name; /* Lower case standard name */
  244. int version1; /* Valid from this version number... */
  245. int version2; /* ...until this one (or forever if this is 0) */
  246. int extension; /* In later versions, see this line in extension table:
  247. if -1, the opcode is illegal in later versions */
  248. int code; /* Opcode number within its operand-number block */
  249. int flags; /* Flags (see below) */
  250. int op_rules; /* Any unusual operand rule applying (see below) */
  251. int flags2_set; /* If not zero, set this bit in Flags 2 in the header
  252. of any game using the opcode */
  253. int no; /* Number of operands (see below) */
  254. } opcodez;
  255. typedef struct opcodeg
  256. { uchar *name; /* Lower case standard name */
  257. int32 code; /* Opcode number */
  258. int flags; /* Flags (see below) */
  259. int op_rules; /* Any unusual operand rule applying (see below) */
  260. int no; /* Number of operands */
  261. } opcodeg;
  262. /* Flags which can be set */
  263. #define St 1 /* Store */
  264. #define Br 2 /* Branch */
  265. #define Rf 4 /* "Return flag": execution never continues after this
  266. opcode (e.g., is a return or unconditional jump) */
  267. #define St2 8 /* Store2 (second-to-last operand is store (Glulx)) */
  268. /* Codes for any unusual operand assembly rules */
  269. /* Z-code: */
  270. #define VARIAB 1 /* First operand expected to be a variable name and
  271. assembled to a short constant: the variable number */
  272. #define TEXT 2 /* One text operand, to be Z-encoded into the program */
  273. #define LABEL 3 /* One operand, a label, given as long constant offset */
  274. #define CALL 4 /* First operand is name of a routine, to be assembled
  275. as long constant (the routine's packed address):
  276. as if the name were prefixed by #r$ */
  277. /* Glulx: (bit flags for Glulx VM features) */
  278. #define GOP_Unicode 1 /* uses_unicode_features */
  279. #define GOP_MemHeap 2 /* uses_memheap_features */
  280. #define GOP_Acceleration 4 /* uses_acceleration_features */
  281. #define GOP_Float 8 /* uses_float_features */
  282. /* Codes for the number of operands */
  283. #define TWO 1 /* 2 (with certain types of operand, compiled as VAR) */
  284. #define VAR 2 /* 0 to 4 */
  285. #define VAR_LONG 3 /* 0 to 8 */
  286. #define ONE 4 /* 1 */
  287. #define ZERO 5 /* 0 */
  288. #define EXT 6 /* Extended opcode set VAR: 0 to 4 */
  289. #define EXT_LONG 7 /* Extended: 0 to 8 (not used by the canonical opcodes) */
  290. static opcodez opcodes_table_z[] =
  291. {
  292. /* Opcodes introduced in Version 3 */
  293. /* 0 */ { (uchar *) "je", 3, 0, -1, 0x01, Br, 0, 0, TWO },
  294. /* 1 */ { (uchar *) "jl", 3, 0, -1, 0x02, Br, 0, 0, TWO },
  295. /* 2 */ { (uchar *) "jg", 3, 0, -1, 0x03, Br, 0, 0, TWO },
  296. /* 3 */ { (uchar *) "dec_chk", 3, 0, -1, 0x04, Br, VARIAB, 0, TWO },
  297. /* 4 */ { (uchar *) "inc_chk", 3, 0, -1, 0x05, Br, VARIAB, 0, TWO },
  298. /* 5 */ { (uchar *) "jin", 3, 0, -1, 0x06, Br, 0, 0, TWO },
  299. /* 6 */ { (uchar *) "test", 3, 0, -1, 0x07, Br, 0, 0, TWO },
  300. /* 7 */ { (uchar *) "or", 3, 0, -1, 0x08, St, 0, 0, TWO },
  301. /* 8 */ { (uchar *) "and", 3, 0, -1, 0x09, St, 0, 0, TWO },
  302. /* 9 */ { (uchar *) "test_attr", 3, 0, -1, 0x0A, Br, 0, 0, TWO },
  303. /* 10 */ {(uchar *) "set_attr", 3, 0, -1, 0x0B, 0, 0, 0, TWO },
  304. /* 11 */ {(uchar *) "clear_attr", 3, 0, -1, 0x0C, 0, 0, 0, TWO },
  305. /* 12 */ {(uchar *) "store", 3, 0, -1, 0x0D, 0, VARIAB, 0, TWO },
  306. /* 13 */ {(uchar *) "insert_obj", 3, 0, -1, 0x0E, 0, 0, 0, TWO },
  307. /* 14 */ {(uchar *) "loadw", 3, 0, -1, 0x0F, St, 0, 0, TWO },
  308. /* 15 */ {(uchar *) "loadb", 3, 0, -1, 0x10, St, 0, 0, TWO },
  309. /* 16 */ {(uchar *) "get_prop", 3, 0, -1, 0x11, St, 0, 0, TWO },
  310. /* 17 */ {(uchar *) "get_prop_addr", 3, 0, -1, 0x12, St, 0, 0, TWO },
  311. /* 18 */ {(uchar *) "get_next_prop", 3, 0, -1, 0x13, St, 0, 0, TWO },
  312. /* 19 */ {(uchar *) "add", 3, 0, -1, 0x14, St, 0, 0, TWO },
  313. /* 20 */ {(uchar *) "sub", 3, 0, -1, 0x15, St, 0, 0, TWO },
  314. /* 21 */ {(uchar *) "mul", 3, 0, -1, 0x16, St, 0, 0, TWO },
  315. /* 22 */ {(uchar *) "div", 3, 0, -1, 0x17, St, 0, 0, TWO },
  316. /* 23 */ {(uchar *) "mod", 3, 0, -1, 0x18, St, 0, 0, TWO },
  317. /* 24 */ {(uchar *) "call", 3, 0, -1, 0x20, St, CALL, 0, VAR },
  318. /* 25 */ {(uchar *) "storew", 3, 0, -1, 0x21, 0, 0, 0, VAR },
  319. /* 26 */ {(uchar *) "storeb", 3, 0, -1, 0x22, 0, 0, 0, VAR },
  320. /* 27 */ {(uchar *) "put_prop", 3, 0, -1, 0x23, 0, 0, 0, VAR },
  321. /* This is the version of "read" called "sread" internally: */
  322. /* 28 */ {(uchar *) "read", 3, 0, -1, 0x24, 0, 0, 0, VAR },
  323. /* 29 */ {(uchar *) "print_char", 3, 0, -1, 0x25, 0, 0, 0, VAR },
  324. /* 30 */ {(uchar *) "print_num", 3, 0, -1, 0x26, 0, 0, 0, VAR },
  325. /* 31 */ {(uchar *) "random", 3, 0, -1, 0x27, St, 0, 0, VAR },
  326. /* 32 */ {(uchar *) "push", 3, 0, -1, 0x28, 0, 0, 0, VAR },
  327. /* 33 */ {(uchar *) "pull", 3, 5, 6, 0x29, 0, VARIAB, 0, VAR },
  328. /* 34 */ {(uchar *) "split_window", 3, 0, -1, 0x2A, 0, 0, 0, VAR },
  329. /* 35 */ {(uchar *) "set_window", 3, 0, -1, 0x2B, 0, 0, 0, VAR },
  330. /* 36 */ {(uchar *) "output_stream", 3, 0, -1, 0x33, 0, 0, 0, VAR },
  331. /* 37 */ {(uchar *) "input_stream", 3, 0, -1, 0x34, 0, 0, 0, VAR },
  332. /* 38 */ {(uchar *) "sound_effect", 3, 0, -1, 0x35, 0, 0, 7, VAR },
  333. /* 39 */ {(uchar *) "jz", 3, 0, -1, 0x00, Br, 0, 0, ONE },
  334. /* 40 */ {(uchar *) "get_sibling", 3, 0, -1, 0x01, St+Br, 0, 0, ONE },
  335. /* 41 */ {(uchar *) "get_child", 3, 0, -1, 0x02, St+Br, 0, 0, ONE },
  336. /* 42 */ {(uchar *) "get_parent", 3, 0, -1, 0x03, St, 0, 0, ONE },
  337. /* 43 */ {(uchar *) "get_prop_len", 3, 0, -1, 0x04, St, 0, 0, ONE },
  338. /* 44 */ {(uchar *) "inc", 3, 0, -1, 0x05, 0, VARIAB, 0, ONE },
  339. /* 45 */ {(uchar *) "dec", 3, 0, -1, 0x06, 0, VARIAB, 0, ONE },
  340. /* 46 */ {(uchar *) "print_addr", 3, 0, -1, 0x07, 0, 0, 0, ONE },
  341. /* 47 */ {(uchar *) "remove_obj", 3, 0, -1, 0x09, 0, 0, 0, ONE },
  342. /* 48 */ {(uchar *) "print_obj", 3, 0, -1, 0x0A, 0, 0, 0, ONE },
  343. /* 49 */ {(uchar *) "ret", 3, 0, -1, 0x0B, Rf, 0, 0, ONE },
  344. /* 50 */ {(uchar *) "jump", 3, 0, -1, 0x0C, Rf, LABEL, 0, ONE },
  345. /* 51 */ {(uchar *) "print_paddr", 3, 0, -1, 0x0D, 0, 0, 0, ONE },
  346. /* 52 */ {(uchar *) "load", 3, 0, -1, 0x0E, St, VARIAB, 0, ONE },
  347. /* 53 */ {(uchar *) "not", 3, 3, 0, 0x0F, St, 0, 0, ONE },
  348. /* 54 */ {(uchar *) "rtrue", 3, 0, -1, 0x00, Rf, 0, 0,ZERO },
  349. /* 55 */ {(uchar *) "rfalse", 3, 0, -1, 0x01, Rf, 0, 0,ZERO },
  350. /* 56 */ {(uchar *) "print", 3, 0, -1, 0x02, 0, TEXT, 0,ZERO },
  351. /* 57 */ {(uchar *) "print_ret", 3, 0, -1, 0x03, Rf, TEXT, 0,ZERO },
  352. /* 58 */ {(uchar *) "nop", 3, 0, -1, 0x04, 0, 0, 0,ZERO },
  353. /* 59 */ {(uchar *) "save", 3, 3, 1, 0x05, Br, 0, 0,ZERO },
  354. /* 60 */ {(uchar *) "restore", 3, 3, 2, 0x06, Br, 0, 0,ZERO },
  355. /* 61 */ {(uchar *) "restart", 3, 0, -1, 0x07, 0, 0, 0,ZERO },
  356. /* 62 */ {(uchar *) "ret_popped", 3, 0, -1, 0x08, Rf, 0, 0,ZERO },
  357. /* 63 */ {(uchar *) "pop", 3, 4, -1, 0x09, 0, 0, 0,ZERO },
  358. /* 64 */ {(uchar *) "quit", 3, 0, -1, 0x0A, Rf, 0, 0,ZERO },
  359. /* 65 */ {(uchar *) "new_line", 3, 0, -1, 0x0B, 0, 0, 0,ZERO },
  360. /* 66 */ {(uchar *) "show_status", 3, 3, -1, 0x0C, 0, 0, 0,ZERO },
  361. /* 67 */ {(uchar *) "verify", 3, 0, -1, 0x0D, Br, 0, 0,ZERO },
  362. /* Opcodes introduced in Version 4 */
  363. /* 68 */ {(uchar *) "call_2s", 4, 0, -1, 0x19, St, CALL, 0, TWO },
  364. /* 69 */ {(uchar *) "call_vs", 4, 0, -1, 0x20, St, CALL, 0, VAR },
  365. /* This is the version of "read" called "aread" internally: */
  366. /* 70 */ {(uchar *) "read", 4, 0, -1, 0x24, St, 0, 0, VAR },
  367. /* 71 */ {(uchar *) "call_vs2", 4, 0, -1, 0x2C, St, CALL, 0,
  368. VAR_LONG },
  369. /* 72 */ {(uchar *) "erase_window", 4, 0, -1, 0x2D, 0, 0, 0, VAR },
  370. /* 73 */ {(uchar *) "erase_line", 4, 0, -1, 0x2E, 0, 0, 0, VAR },
  371. /* 74 */ {(uchar *) "set_cursor", 4, 0, -1, 0x2F, 0, 0, 0, VAR },
  372. /* 75 */ {(uchar *) "get_cursor", 4, 0, -1, 0x30, 0, 0, 0, VAR },
  373. /* 76 */ {(uchar *) "set_text_style", 4, 0, -1, 0x31, 0, 0, 0, VAR },
  374. /* 77 */ {(uchar *) "buffer_mode", 4, 0, -1, 0x32, 0, 0, 0, VAR },
  375. /* 78 */ {(uchar *) "read_char", 4, 0, -1, 0x36, St, 0, 0, VAR },
  376. /* 79 */ {(uchar *) "scan_table", 4, 0, -1, 0x37, St+Br, 0, 0, VAR },
  377. /* 80 */ {(uchar *) "call_1s", 4, 0, -1, 0x08, St, CALL, 0, ONE },
  378. /* Opcodes introduced in Version 5 */
  379. /* 81 */ {(uchar *) "call_2n", 5, 0, -1, 0x1a, 0, CALL, 0, TWO },
  380. /* 82 */ {(uchar *) "set_colour", 5, 0, -1, 0x1b, 0, 0, 6, TWO },
  381. /* 83 */ {(uchar *) "throw", 5, 0, -1, 0x1c, 0, 0, 0, TWO },
  382. /* 84 */ {(uchar *) "call_vn", 5, 0, -1, 0x39, 0, CALL, 0, VAR },
  383. /* 85 */ {(uchar *) "call_vn2", 5, 0, -1, 0x3a, 0, CALL, 0,
  384. VAR_LONG },
  385. /* 86 */ {(uchar *) "tokenise", 5, 0, -1, 0x3b, 0, 0, 0, VAR },
  386. /* 87 */ {(uchar *) "encode_text", 5, 0, -1, 0x3c, 0, 0, 0, VAR },
  387. /* 88 */ {(uchar *) "copy_table", 5, 0, -1, 0x3d, 0, 0, 0, VAR },
  388. /* 89 */ {(uchar *) "print_table", 5, 0, -1, 0x3e, 0, 0, 0, VAR },
  389. /* 90 */ {(uchar *) "check_arg_count", 5, 0, -1, 0x3f, Br, 0, 0, VAR },
  390. /* 91 */ {(uchar *) "call_1n", 5, 0, -1, 0x0F, 0, CALL, 0, ONE },
  391. /* 92 */ {(uchar *) "catch", 5, 0, -1, 0x09, St, 0, 0, ZERO },
  392. /* 93 */ {(uchar *) "piracy", 5, 0, -1, 0x0F, Br, 0, 0, ZERO },
  393. /* 94 */ {(uchar *) "log_shift", 5, 0, -1, 0x02, St, 0, 0, EXT },
  394. /* 95 */ {(uchar *) "art_shift", 5, 0, -1, 0x03, St, 0, 0, EXT },
  395. /* 96 */ {(uchar *) "set_font", 5, 0, -1, 0x04, St, 0, 0, EXT },
  396. /* 97 */ {(uchar *) "save_undo", 5, 0, -1, 0x09, St, 0, 4, EXT },
  397. /* 98 */ {(uchar *) "restore_undo", 5, 0, -1, 0x0A, St, 0, 4, EXT },
  398. /* Opcodes introduced in Version 6 */
  399. /* 99 */ { (uchar *) "draw_picture", 6, 6, -1, 0x05, 0, 0, 3, EXT },
  400. /* 100 */ { (uchar *) "picture_data", 6, 6, -1, 0x06, Br, 0, 3, EXT },
  401. /* 101 */ { (uchar *) "erase_picture", 6, 6, -1, 0x07, 0, 0, 3, EXT },
  402. /* 102 */ { (uchar *) "set_margins", 6, 6, -1, 0x08, 0, 0, 0, EXT },
  403. /* 103 */ { (uchar *) "move_window", 6, 6, -1, 0x10, 0, 0, 0, EXT },
  404. /* 104 */ { (uchar *) "window_size", 6, 6, -1, 0x11, 0, 0, 0, EXT },
  405. /* 105 */ { (uchar *) "window_style", 6, 6, -1, 0x12, 0, 0, 0, EXT },
  406. /* 106 */ { (uchar *) "get_wind_prop", 6, 6, -1, 0x13, St, 0, 0, EXT },
  407. /* 107 */ { (uchar *) "scroll_window", 6, 6, -1, 0x14, 0, 0, 0, EXT },
  408. /* 108 */ { (uchar *) "pop_stack", 6, 6, -1, 0x15, 0, 0, 0, EXT },
  409. /* 109 */ { (uchar *) "read_mouse", 6, 6, -1, 0x16, 0, 0, 5, EXT },
  410. /* 110 */ { (uchar *) "mouse_window", 6, 6, -1, 0x17, 0, 0, 5, EXT },
  411. /* 111 */ { (uchar *) "push_stack", 6, 6, -1, 0x18, Br, 0, 0, EXT },
  412. /* 112 */ { (uchar *) "put_wind_prop", 6, 6, -1, 0x19, 0, 0, 0, EXT },
  413. /* 113 */ { (uchar *) "print_form", 6, 6, -1, 0x1a, 0, 0, 0, EXT },
  414. /* 114 */ { (uchar *) "make_menu", 6, 6, -1, 0x1b, Br, 0, 8, EXT },
  415. /* 115 */ { (uchar *) "picture_table", 6, 6, -1, 0x1c, 0, 0, 3, EXT },
  416. /* Opcodes introduced in Z-Machine Specification Standard 1.0 */
  417. /* 116 */ { (uchar *) "print_unicode", 5, 0, -1, 0x0b, 0, 0, 0, EXT },
  418. /* 117 */ { (uchar *) "check_unicode", 5, 0, -1, 0x0c, St, 0, 0, EXT }
  419. };
  420. /* Subsequent forms for opcodes whose meaning changes with version */
  421. static opcodez extension_table_z[] =
  422. {
  423. /* 0 */ { (uchar *) "not", 4, 4, 3, 0x0F, St, 0, 0, ONE },
  424. /* 1 */ { (uchar *) "save", 4, 4, 4, 0x05, St, 0, 0,ZERO },
  425. /* 2 */ { (uchar *) "restore", 4, 4, 5, 0x06, St, 0, 0,ZERO },
  426. /* 3 */ { (uchar *) "not", 5, 0, -1, 0x38, St, 0, 0, VAR },
  427. /* 4 */ { (uchar *) "save", 5, 0, -1, 0x00, St, 0, 0, EXT },
  428. /* 5 */ { (uchar *) "restore", 5, 0, -1, 0x01, St, 0, 0, EXT },
  429. /* 6 */ { (uchar *) "pull", 6, 6, -1, 0x29, St, 0, 0, VAR }
  430. };
  431. static opcodez invalid_opcode_z =
  432. { (uchar *) "invalid", 0, 0, -1, 0xff, 0, 0, 0, ZERO};
  433. static opcodez custom_opcode_z;
  434. /* Note that this table assumes that all opcodes have at most two
  435. branch-label or store operands, and that if they exist, they are the
  436. last operands. Glulx does not actually guarantee this. But it is
  437. true for all opcodes in the current Glulx spec, so we will assume
  438. it for now.
  439. Also note that Inform can only compile branches to constant offsets,
  440. even though the Glulx machine can handle stack or memory-loaded
  441. operands in a branch instruction.
  442. */
  443. static opcodeg opcodes_table_g[] = {
  444. { (uchar *) "nop", 0x00, 0, 0, 0 },
  445. { (uchar *) "add", 0x10, St, 0, 3 },
  446. { (uchar *) "sub", 0x11, St, 0, 3 },
  447. { (uchar *) "mul", 0x12, St, 0, 3 },
  448. { (uchar *) "div", 0x13, St, 0, 3 },
  449. { (uchar *) "mod", 0x14, St, 0, 3 },
  450. { (uchar *) "neg", 0x15, St, 0, 2 },
  451. { (uchar *) "bitand", 0x18, St, 0, 3 },
  452. { (uchar *) "bitor", 0x19, St, 0, 3 },
  453. { (uchar *) "bitxor", 0x1A, St, 0, 3 },
  454. { (uchar *) "bitnot", 0x1B, St, 0, 2 },
  455. { (uchar *) "shiftl", 0x1C, St, 0, 3 },
  456. { (uchar *) "sshiftr", 0x1D, St, 0, 3 },
  457. { (uchar *) "ushiftr", 0x1E, St, 0, 3 },
  458. { (uchar *) "jump", 0x20, Br|Rf, 0, 1 },
  459. { (uchar *) "jz", 0x22, Br, 0, 2 },
  460. { (uchar *) "jnz", 0x23, Br, 0, 2 },
  461. { (uchar *) "jeq", 0x24, Br, 0, 3 },
  462. { (uchar *) "jne", 0x25, Br, 0, 3 },
  463. { (uchar *) "jlt", 0x26, Br, 0, 3 },
  464. { (uchar *) "jge", 0x27, Br, 0, 3 },
  465. { (uchar *) "jgt", 0x28, Br, 0, 3 },
  466. { (uchar *) "jle", 0x29, Br, 0, 3 },
  467. { (uchar *) "jltu", 0x2A, Br, 0, 3 },
  468. { (uchar *) "jgeu", 0x2B, Br, 0, 3 },
  469. { (uchar *) "jgtu", 0x2C, Br, 0, 3 },
  470. { (uchar *) "jleu", 0x2D, Br, 0, 3 },
  471. { (uchar *) "call", 0x30, St, 0, 3 },
  472. { (uchar *) "return", 0x31, Rf, 0, 1 },
  473. { (uchar *) "catch", 0x32, Br|St, 0, 2 },
  474. { (uchar *) "throw", 0x33, Rf, 0, 2 },
  475. { (uchar *) "tailcall", 0x34, Rf, 0, 2 },
  476. { (uchar *) "copy", 0x40, St, 0, 2 },
  477. { (uchar *) "copys", 0x41, St, 0, 2 },
  478. { (uchar *) "copyb", 0x42, St, 0, 2 },
  479. { (uchar *) "sexs", 0x44, St, 0, 2 },
  480. { (uchar *) "sexb", 0x45, St, 0, 2 },
  481. { (uchar *) "aload", 0x48, St, 0, 3 },
  482. { (uchar *) "aloads", 0x49, St, 0, 3 },
  483. { (uchar *) "aloadb", 0x4A, St, 0, 3 },
  484. { (uchar *) "aloadbit", 0x4B, St, 0, 3 },
  485. { (uchar *) "astore", 0x4C, 0, 0, 3 },
  486. { (uchar *) "astores", 0x4D, 0, 0, 3 },
  487. { (uchar *) "astoreb", 0x4E, 0, 0, 3 },
  488. { (uchar *) "astorebit", 0x4F, 0, 0, 3 },
  489. { (uchar *) "stkcount", 0x50, St, 0, 1 },
  490. { (uchar *) "stkpeek", 0x51, St, 0, 2 },
  491. { (uchar *) "stkswap", 0x52, 0, 0, 0 },
  492. { (uchar *) "stkroll", 0x53, 0, 0, 2 },
  493. { (uchar *) "stkcopy", 0x54, 0, 0, 1 },
  494. { (uchar *) "streamchar", 0x70, 0, 0, 1 },
  495. { (uchar *) "streamnum", 0x71, 0, 0, 1 },
  496. { (uchar *) "streamstr", 0x72, 0, 0, 1 },
  497. { (uchar *) "gestalt", 0x0100, St, 0, 3 },
  498. { (uchar *) "debugtrap", 0x0101, 0, 0, 1 },
  499. { (uchar *) "getmemsize", 0x0102, St, 0, 1 },
  500. { (uchar *) "setmemsize", 0x0103, St, 0, 2 },
  501. { (uchar *) "jumpabs", 0x0104, Rf, 0, 1 },
  502. { (uchar *) "random", 0x0110, St, 0, 2 },
  503. { (uchar *) "setrandom", 0x0111, 0, 0, 1 },
  504. { (uchar *) "quit", 0x0120, Rf, 0, 0 },
  505. { (uchar *) "verify", 0x0121, St, 0, 1 },
  506. { (uchar *) "restart", 0x0122, 0, 0, 0 },
  507. { (uchar *) "save", 0x0123, St, 0, 2 },
  508. { (uchar *) "restore", 0x0124, St, 0, 2 },
  509. { (uchar *) "saveundo", 0x0125, St, 0, 1 },
  510. { (uchar *) "restoreundo", 0x0126, St, 0, 1 },
  511. { (uchar *) "protect", 0x0127, 0, 0, 2 },
  512. { (uchar *) "glk", 0x0130, St, 0, 3 },
  513. { (uchar *) "getstringtbl", 0x0140, St, 0, 1 },
  514. { (uchar *) "setstringtbl", 0x0141, 0, 0, 1 },
  515. { (uchar *) "getiosys", 0x0148, St|St2, 0, 2 },
  516. { (uchar *) "setiosys", 0x0149, 0, 0, 2 },
  517. { (uchar *) "linearsearch", 0x0150, St, 0, 8 },
  518. { (uchar *) "binarysearch", 0x0151, St, 0, 8 },
  519. { (uchar *) "linkedsearch", 0x0152, St, 0, 7 },
  520. { (uchar *) "callf", 0x0160, St, 0, 2 },
  521. { (uchar *) "callfi", 0x0161, St, 0, 3 },
  522. { (uchar *) "callfii", 0x0162, St, 0, 4 },
  523. { (uchar *) "callfiii", 0x0163, St, 0, 5 },
  524. { (uchar *) "streamunichar", 0x73, 0, GOP_Unicode, 1 },
  525. { (uchar *) "mzero", 0x170, 0, GOP_MemHeap, 2 },
  526. { (uchar *) "mcopy", 0x171, 0, GOP_MemHeap, 3 },
  527. { (uchar *) "malloc", 0x178, St, GOP_MemHeap, 2 },
  528. { (uchar *) "mfree", 0x179, 0, GOP_MemHeap, 1 },
  529. { (uchar *) "accelfunc", 0x180, 0, GOP_Acceleration, 2 },
  530. { (uchar *) "accelparam", 0x181, 0, GOP_Acceleration, 2 },
  531. { (uchar *) "numtof", 0x190, St, GOP_Float, 2 },
  532. { (uchar *) "ftonumz", 0x191, St, GOP_Float, 2 },
  533. { (uchar *) "ftonumn", 0x192, St, GOP_Float, 2 },
  534. { (uchar *) "ceil", 0x198, St, GOP_Float, 2 },
  535. { (uchar *) "floor", 0x199, St, GOP_Float, 2 },
  536. { (uchar *) "fadd", 0x1A0, St, GOP_Float, 3 },
  537. { (uchar *) "fsub", 0x1A1, St, GOP_Float, 3 },
  538. { (uchar *) "fmul", 0x1A2, St, GOP_Float, 3 },
  539. { (uchar *) "fdiv", 0x1A3, St, GOP_Float, 3 },
  540. { (uchar *) "fmod", 0x1A4, St|St2, GOP_Float, 4 },
  541. { (uchar *) "sqrt", 0x1A8, St, GOP_Float, 2 },
  542. { (uchar *) "exp", 0x1A9, St, GOP_Float, 2 },
  543. { (uchar *) "log", 0x1AA, St, GOP_Float, 2 },
  544. { (uchar *) "pow", 0x1AB, St, GOP_Float, 3 },
  545. { (uchar *) "sin", 0x1B0, St, GOP_Float, 2 },
  546. { (uchar *) "cos", 0x1B1, St, GOP_Float, 2 },
  547. { (uchar *) "tan", 0x1B2, St, GOP_Float, 2 },
  548. { (uchar *) "asin", 0x1B3, St, GOP_Float, 2 },
  549. { (uchar *) "acos", 0x1B4, St, GOP_Float, 2 },
  550. { (uchar *) "atan", 0x1B5, St, GOP_Float, 2 },
  551. { (uchar *) "atan2", 0x1B6, St, GOP_Float, 3 },
  552. { (uchar *) "jfeq", 0x1C0, Br, GOP_Float, 4 },
  553. { (uchar *) "jfne", 0x1C1, Br, GOP_Float, 4 },
  554. { (uchar *) "jflt", 0x1C2, Br, GOP_Float, 3 },
  555. { (uchar *) "jfle", 0x1C3, Br, GOP_Float, 3 },
  556. { (uchar *) "jfgt", 0x1C4, Br, GOP_Float, 3 },
  557. { (uchar *) "jfge", 0x1C5, Br, GOP_Float, 3 },
  558. { (uchar *) "jisnan", 0x1C8, Br, GOP_Float, 2 },
  559. { (uchar *) "jisinf", 0x1C9, Br, GOP_Float, 2 },
  560. };
  561. /* The opmacros table is used for fake opcodes. The opcode numbers are
  562. ignored; this table is only used for argument parsing. */
  563. static opcodeg opmacros_table_g[] = {
  564. { (uchar *) "pull", 0, St, 0, 1 },
  565. { (uchar *) "push", 0, 0, 0, 1 },
  566. };
  567. static opcodeg custom_opcode_g;
  568. static opcodez internal_number_to_opcode_z(int32 i)
  569. { opcodez x;
  570. ASSERT_ZCODE();
  571. if (i == -1) return custom_opcode_z;
  572. x = opcodes_table_z[i];
  573. if (instruction_set_number < x.version1) return invalid_opcode_z;
  574. if (x.version2 == 0) return x;
  575. if (instruction_set_number <= x.version2) return x;
  576. i = x.extension;
  577. if (i < 0) return invalid_opcode_z;
  578. x = extension_table_z[i];
  579. if (instruction_set_number < x.version1) return invalid_opcode_z;
  580. if (x.version2 == 0) return x;
  581. if (instruction_set_number <= x.version2) return x;
  582. return extension_table_z[x.extension];
  583. }
  584. static void make_opcode_syntax_z(opcodez opco)
  585. { char *p = "", *q = opcode_syntax_string;
  586. sprintf(q, "%s", opco.name);
  587. switch(opco.no)
  588. { case ONE: p=" <operand>"; break;
  589. case TWO: p=" <operand1> <operand2>"; break;
  590. case EXT:
  591. case VAR: p=" <0 to 4 operands>"; break;
  592. case VAR_LONG: p=" <0 to 8 operands>"; break;
  593. }
  594. switch(opco.op_rules)
  595. { case TEXT: sprintf(q+strlen(q), " <text>"); return;
  596. case LABEL: sprintf(q+strlen(q), " <label>"); return;
  597. case VARIAB:
  598. sprintf(q+strlen(q), " <variable>");
  599. case CALL:
  600. if (opco.op_rules==CALL) sprintf(q+strlen(q), " <routine>");
  601. switch(opco.no)
  602. { case ONE: p=""; break;
  603. case TWO: p=" <operand>"; break;
  604. case EXT:
  605. case VAR: p=" <1 to 4 operands>"; break;
  606. case VAR_LONG: p=" <1 to 8 operands>"; break;
  607. }
  608. break;
  609. }
  610. sprintf(q+strlen(q), "%s", p);
  611. if ((opco.flags & St) != 0) sprintf(q+strlen(q), " -> <result-variable>");
  612. if ((opco.flags & Br) != 0) sprintf(q+strlen(q), " ?[~]<label>");
  613. }
  614. static opcodeg internal_number_to_opcode_g(int32 i)
  615. {
  616. opcodeg x;
  617. if (i == -1) return custom_opcode_g;
  618. x = opcodes_table_g[i];
  619. return x;
  620. }
  621. static opcodeg internal_number_to_opmacro_g(int32 i)
  622. {
  623. return opmacros_table_g[i];
  624. }
  625. static void make_opcode_syntax_g(opcodeg opco)
  626. {
  627. int ix;
  628. char *cx;
  629. char *q = opcode_syntax_string;
  630. sprintf(q, "%s", opco.name);
  631. sprintf(q+strlen(q), " <%d operand%s", opco.no,
  632. ((opco.no==1) ? "" : "s"));
  633. if (opco.no) {
  634. cx = q+strlen(q);
  635. strcpy(cx, ": ");
  636. cx += strlen(cx);
  637. for (ix=0; ix<opco.no; ix++) {
  638. if (ix) {
  639. *cx = ' ';
  640. cx++;
  641. }
  642. if (ix == opco.no-1) {
  643. if (opco.flags & Br) {
  644. strcpy(cx, "Lb");
  645. }
  646. else if (opco.flags & St) {
  647. strcpy(cx, "S");
  648. }
  649. else {
  650. strcpy(cx, "L");
  651. }
  652. }
  653. else if (ix == opco.no-2 && (opco.flags & Br) && (opco.flags & St)) {
  654. strcpy(cx, "S");
  655. }
  656. else if (ix == opco.no-2 && (opco.flags & St2)) {
  657. strcpy(cx, "S");
  658. }
  659. else {
  660. strcpy(cx, "L");
  661. }
  662. cx += strlen(cx);
  663. sprintf(cx, "%d", ix+1);
  664. cx += strlen(cx);
  665. }
  666. }
  667. sprintf(q+strlen(q), ">");
  668. }
  669. /* ========================================================================= */
  670. /* The assembler itself does four things: */
  671. /* */
  672. /* assembles instructions */
  673. /* sets label N to the current code position */
  674. /* assembles routine headers */
  675. /* assembles routine ends */
  676. /* ------------------------------------------------------------------------- */
  677. /* This is for Z-code only. */
  678. static void write_operand(assembly_operand op)
  679. { int32 j;
  680. if (module_switch && (op.marker != 0))
  681. { if ((op.marker != VARIABLE_MV) && (op.type == SHORT_CONSTANT_OT))
  682. op.type = LONG_CONSTANT_OT;
  683. }
  684. j=op.value;
  685. switch(op.type)
  686. { case LONG_CONSTANT_OT:
  687. byteout(j/256, op.marker); byteout(j%256, 0); return;
  688. case SHORT_CONSTANT_OT:
  689. if (op.marker == 0)
  690. byteout(j, 0);
  691. else byteout(j, 0x80 + op.marker); return;
  692. case VARIABLE_OT:
  693. byteout(j, (module_switch)?(0x80 + op.marker):0); return;
  694. case CONSTANT_OT:
  695. case HALFCONSTANT_OT:
  696. case BYTECONSTANT_OT:
  697. case ZEROCONSTANT_OT:
  698. case SYSFUN_OT:
  699. case DEREFERENCE_OT:
  700. case LOCALVAR_OT:
  701. case GLOBALVAR_OT:
  702. compiler_error("Glulx OT in Z-code assembly operand.");
  703. return;
  704. }
  705. }
  706. extern void assemblez_instruction(assembly_instruction *AI)
  707. {
  708. uchar *start_pc, *operands_pc;
  709. int32 offset, j, topbits, types_byte1, types_byte2;
  710. int operand_rules, min, max, no_operands_given, at_seq_point = FALSE;
  711. assembly_operand o1, o2;
  712. opcodez opco;
  713. ASSERT_ZCODE();
  714. offset = zmachine_pc;
  715. no_instructions++;
  716. if (veneer_mode) sequence_point_follows = FALSE;
  717. if (sequence_point_follows)
  718. { sequence_point_follows = FALSE; at_seq_point = TRUE;
  719. if (debugfile_switch)
  720. { sequence_point_labels[next_sequence_point] = next_label;
  721. sequence_point_locations[next_sequence_point] =
  722. statement_debug_location;
  723. set_label_offset(next_label++, zmachine_pc);
  724. }
  725. next_sequence_point++;
  726. }
  727. opco = internal_number_to_opcode_z(AI->internal_number);
  728. if (opco.version1==0)
  729. { error_named("Opcode unavailable in this Z-machine version",
  730. opcode_names.keywords[AI->internal_number]);
  731. return;
  732. }
  733. if (execution_never_reaches_here)
  734. warning("This statement can never be reached");
  735. operand_rules = opco.op_rules;
  736. execution_never_reaches_here = ((opco.flags & Rf) != 0);
  737. if (opco.flags2_set != 0) flags2_requirements[opco.flags2_set] = 1;
  738. no_operands_given = AI->operand_count;
  739. if ((opco.no == TWO) && ((no_operands_given==3)||(no_operands_given==4)))
  740. opco.no = VAR;
  741. /* 1. Write the opcode byte(s) */
  742. start_pc = zcode_holding_area + zcode_ha_size;
  743. switch(opco.no)
  744. { case VAR_LONG: topbits=0xc0; min=0; max=8; break;
  745. case VAR: topbits=0xc0; min=0; max=4; break;
  746. case ZERO: topbits=0xb0; min=0; max=0; break;
  747. case ONE: topbits=0x80; min=1; max=1; break;
  748. case TWO: topbits=0x00; min=2; max=2; break;
  749. case EXT: topbits=0x00; min=0; max=4;
  750. byteout(0xbe, 0); opco.no=VAR; break;
  751. case EXT_LONG: topbits=0x00; min=0; max=8;
  752. byteout(0xbe, 0); opco.no=VAR_LONG; break;
  753. }
  754. byteout(opco.code + topbits, 0);
  755. operands_pc = zcode_holding_area + zcode_ha_size;
  756. /* 2. Dispose of the special rules LABEL and TEXT */
  757. if (operand_rules==LABEL)
  758. { j = (AI->operand[0]).value;
  759. byteout(j/256, LABEL_MV); byteout(j%256, 0);
  760. goto Instruction_Done;
  761. }
  762. if (operand_rules==TEXT)
  763. { int32 i;
  764. uchar *tmp = translate_text(zcode_holding_area + zcode_ha_size, zcode_holding_area+MAX_ZCODE_SIZE, AI->text);
  765. if (!tmp)
  766. memoryerror("MAX_ZCODE_SIZE", MAX_ZCODE_SIZE);
  767. j = subtract_pointers(tmp, (zcode_holding_area + zcode_ha_size));
  768. for (i=0; i<j; i++) zcode_markers[zcode_ha_size++] = 0;
  769. zmachine_pc += j;
  770. goto Instruction_Done;
  771. }
  772. /* 3. Sort out the operands */
  773. if ((no_operands_given < min) || (no_operands_given > max))
  774. goto OpcodeSyntaxError;
  775. switch(opco.no)
  776. { case VAR:
  777. case VAR_LONG:
  778. byteout(0, 0);
  779. if (opco.no == VAR_LONG) byteout(0, 0);
  780. types_byte1=0xff; types_byte2=0xff;
  781. for (j=0; j<no_operands_given; j++)
  782. { int multi, mask;
  783. switch(j)
  784. { case 0: case 4: multi=0x40; mask=0xc0; break;
  785. case 1: case 5: multi=0x10; mask=0x30; break;
  786. case 2: case 6: multi=0x04; mask=0x0c; break;
  787. case 3: case 7: multi=0x01; mask=0x03; break;
  788. }
  789. o1 = AI->operand[j];
  790. write_operand(o1);
  791. if (j<4)
  792. types_byte1 = (types_byte1 & (~mask)) + o1.type*multi;
  793. else
  794. types_byte2 = (types_byte2 & (~mask)) + o1.type*multi;
  795. }
  796. *operands_pc=types_byte1;
  797. if (opco.no == VAR_LONG) *(operands_pc+1)=types_byte2;
  798. break;
  799. case ONE:
  800. o1 = AI->operand[0];
  801. *start_pc=(*start_pc) + o1.type*0x10;
  802. write_operand(o1);
  803. break;
  804. case TWO:
  805. o1 = AI->operand[0];
  806. o2 = AI->operand[1];
  807. /* Transfer to VAR form if either operand is a long constant */
  808. if ((o1.type==LONG_CONSTANT_OT)||(o2.type==LONG_CONSTANT_OT))
  809. { *start_pc=(*start_pc) + 0xc0;
  810. byteout(o1.type*0x40 + o2.type*0x10 + 0x0f, 0);
  811. }
  812. else
  813. { if (o1.type==VARIABLE_OT) *start_pc=(*start_pc) + 0x40;
  814. if (o2.type==VARIABLE_OT) *start_pc=(*start_pc) + 0x20;
  815. }
  816. write_operand(o1);
  817. write_operand(o2);
  818. break;
  819. }
  820. /* 4. Assemble a Store destination, if needed */
  821. if ((AI->store_variable_number) != -1)
  822. { o1.type = VARIABLE_OT;
  823. o1.value = AI->store_variable_number;
  824. variable_usage[o1.value] = TRUE;
  825. o1.marker = 0;
  826. /* Note that variable numbers 249 to 255 (i.e. globals 233 to 239)
  827. are used as scratch workspace, so need no mapping between
  828. modules and story files: nor do local variables 0 to 15 */
  829. if ((o1.value >= MAX_LOCAL_VARIABLES) && (o1.value < 249))
  830. o1.marker = VARIABLE_MV;
  831. write_operand(o1);
  832. }
  833. /* 5. Assemble a branch, if needed */
  834. if (AI->branch_label_number != -1)
  835. { int32 addr, long_form;
  836. int branch_on_true = (AI->branch_flag)?1:0;
  837. switch (AI->branch_label_number)
  838. { case -2: addr = 2; branch_on_true = 0; long_form = 0; break;
  839. /* branch nowhere, carry on */
  840. case -3: addr = 0; long_form = 0; break; /* rfalse on condition */
  841. case -4: addr = 1; long_form = 0; break; /* rtrue on condition */
  842. default:
  843. long_form = 1; addr = AI->branch_label_number;
  844. break;
  845. }
  846. if (addr > 0x7fff) fatalerror("Too many branch points in routine.");
  847. if (long_form==1)
  848. { byteout(branch_on_true*0x80 + addr/256, BRANCH_MV);
  849. byteout(addr%256, 0);
  850. }
  851. else
  852. byteout(branch_on_true*0x80+ 0x40 + (addr&0x3f), 0);
  853. }
  854. Instruction_Done:
  855. if (asm_trace_level > 0)
  856. { int i;
  857. printf("%5d +%05lx %3s %-12s ", ErrorReport.line_number,
  858. ((long int) offset),
  859. (at_seq_point)?"<*>":" ", opco.name);
  860. if ((AI->internal_number == print_zc)
  861. || (AI->internal_number == print_ret_zc))
  862. { printf("\"");
  863. for (i=0;(AI->text)[i]!=0 && i<35; i++) printf("%c",(AI->text)[i]);
  864. if (i == 35) printf("...");
  865. printf("\"");
  866. }
  867. for (i=0; i<AI->operand_count; i++)
  868. { if ((i==0) && (opco.op_rules == VARIAB))
  869. { if ((AI->operand[0]).type == VARIABLE_OT)
  870. { printf("["); print_operand_z(AI->operand[i]); }
  871. else
  872. printf("%s", variable_name((AI->operand[0]).value));
  873. }
  874. else
  875. if ((i==0) && (opco.op_rules == LABEL))
  876. { printf("L%d", AI->operand[0].value);
  877. }
  878. else print_operand_z(AI->operand[i]);
  879. printf(" ");
  880. }
  881. if (AI->store_variable_number != -1)
  882. { assembly_operand AO;
  883. printf("-> ");
  884. AO.type = VARIABLE_OT; AO.value = AI->store_variable_number;
  885. print_operand_z(AO); printf(" ");
  886. }
  887. switch(AI->branch_label_number)
  888. { case -4: printf("rtrue if %s", (AI->branch_flag)?"TRUE":"FALSE");
  889. break;
  890. case -3: printf("rfalse if %s", (AI->branch_flag)?"TRUE":"FALSE");
  891. break;
  892. case -2: printf("(no branch)"); break;
  893. case -1: break;
  894. default:
  895. printf("to L%d if %s", AI->branch_label_number,
  896. (AI->branch_flag)?"TRUE":"FALSE"); break;
  897. }
  898. if (asm_trace_level>=2)
  899. { for (j=0;start_pc<zcode_holding_area + zcode_ha_size;
  900. j++, start_pc++)
  901. { if (j%16==0) printf("\n ");
  902. printf("%02x ", *start_pc);
  903. }
  904. }
  905. printf("\n");
  906. }
  907. if (module_switch) flush_link_data();
  908. return;
  909. OpcodeSyntaxError:
  910. make_opcode_syntax_z(opco);
  911. error_named("Assembly mistake: syntax is", opcode_syntax_string);
  912. }
  913. static void assembleg_macro(assembly_instruction *AI)
  914. {
  915. /* validate macro syntax first */
  916. int ix, no_operands_given;
  917. opcodeg opco;
  918. opco = internal_number_to_opmacro_g(AI->internal_number);
  919. no_operands_given = AI->operand_count;
  920. if (no_operands_given != opco.no)
  921. goto OpcodeSyntaxError;
  922. for (ix = 0; ix < no_operands_given; ix++) {
  923. int type = AI->operand[ix].type;
  924. if ((opco.flags & St)
  925. && ((!(opco.flags & Br) && (ix == no_operands_given-1))
  926. || ((opco.flags & Br) && (ix == no_operands_given-2)))) {
  927. if (is_constant_ot(type)) {
  928. error("*** assembly macro tried to store to a constant ***");
  929. goto OpcodeSyntaxError;
  930. }
  931. }
  932. if ((opco.flags & St2)
  933. && (ix == no_operands_given-2)) {
  934. if (is_constant_ot(type)) {
  935. error("*** assembly macro tried to store to a constant ***");
  936. goto OpcodeSyntaxError;
  937. }
  938. }
  939. }
  940. /* expand the macro */
  941. switch (AI->internal_number) {
  942. case pull_gm:
  943. assembleg_store(AI->operand[0], stack_pointer);
  944. break;
  945. case push_gm:
  946. assembleg_store(stack_pointer, AI->operand[0]);
  947. break;
  948. default:
  949. compiler_error("Invalid Glulx assembly macro");
  950. break;
  951. }
  952. return;
  953. OpcodeSyntaxError:
  954. make_opcode_syntax_g(opco);
  955. error_named("Assembly mistake: syntax is", opcode_syntax_string);
  956. }
  957. extern void assembleg_instruction(assembly_instruction *AI)
  958. {
  959. uchar *start_pc, *opmodes_pc;
  960. int32 offset, j;
  961. int no_operands_given, at_seq_point = FALSE;
  962. int ix, k;
  963. opcodeg opco;
  964. ASSERT_GLULX();
  965. offset = zmachine_pc;
  966. no_instructions++;
  967. if (veneer_mode) sequence_point_follows = FALSE;
  968. if (sequence_point_follows)
  969. { sequence_point_follows = FALSE; at_seq_point = TRUE;
  970. if (debugfile_switch)
  971. { sequence_point_labels[next_sequence_point] = next_label;
  972. sequence_point_locations[next_sequence_point] =
  973. statement_debug_location;
  974. set_label_offset(next_label++, zmachine_pc);
  975. }
  976. next_sequence_point++;
  977. }
  978. opco = internal_number_to_opcode_g(AI->internal_number);
  979. if (execution_never_reaches_here)
  980. warning("This statement can never be reached");
  981. execution_never_reaches_here = ((opco.flags & Rf) != 0);
  982. if (opco.op_rules & GOP_Unicode) {
  983. uses_unicode_features = TRUE;
  984. }
  985. if (opco.op_rules & GOP_MemHeap) {
  986. uses_memheap_features = TRUE;
  987. }
  988. if (opco.op_rules & GOP_Acceleration) {
  989. uses_acceleration_features = TRUE;
  990. }
  991. if (opco.op_rules & GOP_Float) {
  992. uses_float_features = TRUE;
  993. }
  994. no_operands_given = AI->operand_count;
  995. /* 1. Write the opcode byte(s) */
  996. start_pc = zcode_holding_area + zcode_ha_size;
  997. if (opco.code < 0x80) {
  998. byteout(opco.code, 0);
  999. }
  1000. else if (opco.code < 0x4000) {
  1001. byteout(((opco.code >> 8) & 0xFF) | 0x80, 0);
  1002. byteout((opco.code & 0xFF), 0);
  1003. }
  1004. else {
  1005. byteout(((opco.code >> 24) & 0xFF) | 0xC0, 0);
  1006. byteout(((opco.code >> 16) & 0xFF), 0);
  1007. byteout(((opco.code >> 8) & 0xFF), 0);
  1008. byteout(((opco.code) & 0xFF), 0);
  1009. }
  1010. /* ... and the operand addressing modes. There's one byte for
  1011. every two operands (rounded up). We write zeroes for now;
  1012. when the operands are written, we'll go back and fix them. */
  1013. opmodes_pc = zcode_holding_area + zcode_ha_size;
  1014. for (ix=0; ix<opco.no; ix+=2) {
  1015. byteout(0, 0);
  1016. }
  1017. /* 2. Dispose of the special rules */
  1018. /* There aren't any in Glulx. */
  1019. /* 3. Sort out the operands */
  1020. if (no_operands_given != opco.no) {
  1021. goto OpcodeSyntaxError;
  1022. }
  1023. for (ix=0; ix<no_operands_given; ix++) {
  1024. int marker = AI->operand[ix].marker;
  1025. int type = AI->operand[ix].type;
  1026. k = AI->operand[ix].value;
  1027. if ((opco.flags & Br) && (ix == no_operands_given-1)) {
  1028. if (!(marker >= BRANCH_MV && marker < BRANCHMAX_MV)) {
  1029. compiler_error("Assembling branch without BRANCH_MV marker");
  1030. goto OpcodeSyntaxError;
  1031. }
  1032. if (k == -2) {
  1033. k = 2; /* branch no-op */
  1034. type = BYTECONSTANT_OT;
  1035. marker = 0;
  1036. }
  1037. else if (k == -3) {
  1038. k = 0; /* branch return 0 */
  1039. type = ZEROCONSTANT_OT;
  1040. marker = 0;
  1041. }
  1042. else if (k == -4) {
  1043. k = 1; /* branch return 1 */
  1044. type = BYTECONSTANT_OT;
  1045. marker = 0;
  1046. }
  1047. else {
  1048. /* branch to label k */
  1049. j = subtract_pointers((zcode_holding_area + zcode_ha_size),
  1050. opmodes_pc);
  1051. j = 2*j - ix;
  1052. marker = BRANCH_MV + j;
  1053. if (!(marker >= BRANCH_MV && marker < BRANCHMAX_MV)) {
  1054. error("*** branch marker too far from opmode byte ***");
  1055. goto OpcodeSyntaxError;
  1056. }
  1057. }
  1058. }
  1059. if ((opco.flags & St)
  1060. && ((!(opco.flags & Br) && (ix == no_operands_given-1))
  1061. || ((opco.flags & Br) && (ix == no_operands_given-2)))) {
  1062. if (type == BYTECONSTANT_OT || type == HALFCONSTANT_OT
  1063. || type == CONSTANT_OT) {
  1064. error("*** instruction tried to store to a constant ***");
  1065. goto OpcodeSyntaxError;
  1066. }
  1067. }
  1068. if ((opco.flags & St2)
  1069. && (ix == no_operands_given-2)) {
  1070. if (type == BYTECONSTANT_OT || type == HALFCONSTANT_OT
  1071. || type == CONSTANT_OT) {
  1072. error("*** instruction tried to store to a constant ***");
  1073. goto OpcodeSyntaxError;
  1074. }
  1075. }
  1076. if (marker && (type == HALFCONSTANT_OT
  1077. || type == BYTECONSTANT_OT
  1078. || type == ZEROCONSTANT_OT)) {
  1079. compiler_error("Assembling marker in less than 32-bit constant.");
  1080. /* Actually we should store marker|0x80 for a byte constant,
  1081. but let's hold off on that. */
  1082. }
  1083. switch (type) {
  1084. case LONG_CONSTANT_OT:
  1085. case SHORT_CONSTANT_OT:
  1086. case VARIABLE_OT:
  1087. j = 0;
  1088. compiler_error("Z-code OT in Glulx assembly operand.");
  1089. break;
  1090. case CONSTANT_OT:
  1091. j = 3;
  1092. byteout((k >> 24) & 0xFF, marker);
  1093. byteout((k >> 16) & 0xFF, 0);
  1094. byteout((k >> 8) & 0xFF, 0);
  1095. byteout((k & 0xFF), 0);
  1096. break;
  1097. case HALFCONSTANT_OT:
  1098. j = 2;
  1099. byteout((k >> 8) & 0xFF, marker);
  1100. byteout((k & 0xFF), 0);
  1101. break;
  1102. case BYTECONSTANT_OT:
  1103. j = 1;
  1104. byteout((k & 0xFF), marker);
  1105. break;
  1106. case ZEROCONSTANT_OT:
  1107. j = 0;
  1108. break;
  1109. case DEREFERENCE_OT:
  1110. j = 7;
  1111. byteout((k >> 24) & 0xFF, marker);
  1112. byteout((k >> 16) & 0xFF, 0);
  1113. byteout((k >> 8) & 0xFF, 0);
  1114. byteout((k & 0xFF), 0);
  1115. break;
  1116. case GLOBALVAR_OT:
  1117. /* Global variable -- a constant address. */
  1118. k -= MAX_LOCAL_VARIABLES;
  1119. if (0) {
  1120. /* We could write the value as a marker and patch it later... */
  1121. j = 7;
  1122. byteout(((k) >> 24) & 0xFF, VARIABLE_MV);
  1123. byteout(((k) >> 16) & 0xFF, 0);
  1124. byteout(((k) >> 8) & 0xFF, 0);
  1125. byteout(((k) & 0xFF), 0);
  1126. }
  1127. else {
  1128. /* ...but it's more efficient to write it as a RAM operand,
  1129. which can be 1, 2, or 4 bytes. Remember that global variables
  1130. are the very first thing in RAM. */
  1131. k = k * 4; /* each variable is four bytes */
  1132. if (k <= 255) {
  1133. j = 13;
  1134. byteout(((k) & 0xFF), 0);
  1135. }
  1136. else if (k <= 65535) {
  1137. j = 14;
  1138. byteout(((k) >> 8) & 0xFF, 0);
  1139. byteout(((k) & 0xFF), 0);
  1140. }
  1141. else {
  1142. j = 15;
  1143. byteout(((k) >> 24) & 0xFF, 0);
  1144. byteout(((k) >> 16) & 0xFF, 0);
  1145. byteout(((k) >> 8) & 0xFF, 0);
  1146. byteout(((k) & 0xFF), 0);
  1147. }
  1148. }
  1149. break;
  1150. case LOCALVAR_OT:
  1151. if (k == 0) {
  1152. /* Stack-pointer magic variable */
  1153. j = 8;
  1154. }
  1155. else {
  1156. /* Local variable -- a byte or short offset from the
  1157. frame pointer. It's an unsigned offset, so we can
  1158. fit up to long 63 (offset 4*63) in a byte. */
  1159. if ((k-1) < 64) {
  1160. j = 9;
  1161. byteout((k-1)*4, 0);
  1162. }
  1163. else {
  1164. j = 10;
  1165. byteout((((k-1)*4) >> 8) & 0xFF, 0);
  1166. byteout(((k-1)*4) & 0xFF, 0);
  1167. }
  1168. }
  1169. break;
  1170. default:
  1171. j = 0;
  1172. break;
  1173. }
  1174. if (ix & 1)
  1175. j = (j << 4);
  1176. opmodes_pc[ix/2] |= j;
  1177. }
  1178. /* Print assembly trace. */
  1179. if (asm_trace_level > 0) {
  1180. int i;
  1181. printf("%5d +%05lx %3s %-12s ", ErrorReport.line_number,
  1182. ((long int) offset),
  1183. (at_seq_point)?"<*>":" ", opco.name);
  1184. for (i=0; i<AI->operand_count; i++) {
  1185. if ((opco.flags & Br) && (i == opco.no-1)) {
  1186. if (AI->operand[i].value == -4)
  1187. printf("to rtrue");
  1188. else if (AI->operand[i].value == -3)
  1189. printf("to rfalse");
  1190. else
  1191. printf("to L%d", AI->operand[i].value);
  1192. }
  1193. else {
  1194. print_operand_g(AI->operand[i]);
  1195. }
  1196. printf(" ");
  1197. }
  1198. if (asm_trace_level>=2) {
  1199. for (j=0;
  1200. start_pc<zcode_holding_area + zcode_ha_size;
  1201. j++, start_pc++) {
  1202. if (j%16==0) printf("\n ");
  1203. if (0) {
  1204. printf("%02x ", *start_pc);
  1205. }
  1206. else {
  1207. printf("%02x", *start_pc);
  1208. if (zcode_markers[start_pc-zcode_holding_area])
  1209. printf("{%02x}", zcode_markers[start_pc-zcode_holding_area]);
  1210. printf(" ");
  1211. }
  1212. }
  1213. }
  1214. printf("\n");
  1215. }
  1216. if (module_switch) flush_link_data();
  1217. return;
  1218. OpcodeSyntaxError:
  1219. make_opcode_syntax_g(opco);
  1220. error_named("Assembly mistake: syntax is", opcode_syntax_string);
  1221. }
  1222. extern void assemble_label_no(int n)
  1223. {
  1224. if (asm_trace_level > 0)
  1225. printf("%5d +%05lx .L%d\n", ErrorReport.line_number,
  1226. ((long int) zmachine_pc), n);
  1227. set_label_offset(n, zmachine_pc);
  1228. execution_never_reaches_here = FALSE;
  1229. }
  1230. extern void define_symbol_label(int symbol)
  1231. { label_symbols[svals[symbol]] = symbol;
  1232. }
  1233. extern int32 assemble_routine_header(int no_locals,
  1234. int routine_asterisked, char *name, int embedded_flag, int the_symbol)
  1235. { int i, rv;
  1236. int stackargs = FALSE;
  1237. int name_length;
  1238. execution_never_reaches_here = FALSE;
  1239. routine_locals = no_locals;
  1240. for (i=0; i<MAX_LOCAL_VARIABLES; i++) variable_usage[i] = FALSE;
  1241. if (no_locals >= 1
  1242. && !strcmp(local_variables.keywords[0], "_vararg_count")) {
  1243. stackargs = TRUE;
  1244. }
  1245. if (veneer_mode) routine_starts_line = -1;
  1246. else routine_starts_line = ErrorReport.line_number
  1247. + FILE_LINE_SCALE_FACTOR*ErrorReport.file_number;
  1248. if (asm_trace_level > 0)
  1249. { printf("\n%5d +%05lx [ %s ", ErrorReport.line_number,
  1250. ((long int) zmachine_pc), name);
  1251. for (i=1; i<=no_locals; i++) printf("%s ", variable_name(i));
  1252. printf("\n\n");
  1253. }
  1254. routine_start_pc = zmachine_pc;
  1255. if (track_unused_routines) {
  1256. /* The name of an embedded function is in a temporary buffer,
  1257. so we shouldn't keep a reference to it. (It is sad that we
  1258. have to know this here.) */
  1259. char *funcname = name;
  1260. if (embedded_flag)
  1261. funcname = "<embedded>";
  1262. df_note_function_start(funcname, zmachine_pc, embedded_flag,
  1263. routine_starts_line);
  1264. }
  1265. routine_symbol = the_symbol;
  1266. name_length = strlen(name) + 1;
  1267. routine_name =
  1268. my_malloc(name_length * sizeof(char), "temporary copy of routine name");
  1269. strncpy(routine_name, name, name_length);
  1270. /* Update the routine counter */
  1271. no_routines++;
  1272. /* Actually assemble the routine header into the code area; note */
  1273. /* Inform doesn't support the setting of local variables to default */
  1274. /* values other than 0 in V3 and V4. (In V5+ the Z-Machine doesn't */
  1275. /* provide the possibility in any case.) */
  1276. if (!glulx_mode) {
  1277. if (stackargs)
  1278. warning("Z-code does not support stack-argument function definitions.");
  1279. byteout(no_locals, 0);
  1280. /* Not the packed address, but the scaled offset from code area start: */
  1281. rv = zmachine_pc/scale_factor;
  1282. if (instruction_set_number<5)
  1283. for (i=0; i<no_locals; i++) { byteout(0,0); byteout(0,0); }
  1284. next_label = 0; next_sequence_point = 0; last_label = -1;
  1285. /* Compile code to print out text like "a=3, b=4, c=5" when the */
  1286. /* function is called, if it's required. */
  1287. if ((routine_asterisked) || (define_INFIX_switch))
  1288. { char fnt[256]; assembly_operand PV, RFA, CON, STP, SLF; int ln, ln2;
  1289. ln = next_label++;
  1290. ln2 = next_label++;
  1291. if (define_INFIX_switch)
  1292. {
  1293. if (embedded_flag)
  1294. { SLF.value = 251; SLF.type = VARIABLE_OT; SLF.marker = 0;
  1295. CON.value = 0; CON.type = SHORT_CONSTANT_OT; CON.marker = 0;
  1296. assemblez_2_branch(test_attr_zc, SLF, CON, ln2, FALSE);
  1297. }
  1298. else
  1299. { i = no_named_routines++;
  1300. named_routine_symbols[i] = the_symbol;
  1301. CON.value = i/8; CON.type = LONG_CONSTANT_OT; CON.marker = 0;
  1302. RFA.value = routine_flags_array_SC;
  1303. RFA.type = LONG_CONSTANT_OT; RFA.marker = INCON_MV;
  1304. STP.value = 0; STP.type = VARIABLE_OT; STP.marker = 0;
  1305. assemblez_2_to(loadb_zc, RFA, CON, STP);
  1306. CON.value = (1 << (i%8)); CON.type = SHORT_CONSTANT_OT;
  1307. assemblez_2_to(and_zc, STP, CON, STP);
  1308. assemblez_1_branch(jz_zc, STP, ln2, TRUE);
  1309. }
  1310. }
  1311. sprintf(fnt, "[ %s(", name);
  1312. AI.text = fnt; assemblez_0(print_zc);
  1313. for (i=1; (i<=7)&&(i<=no_locals); i++)
  1314. { if (version_number >= 5)
  1315. { PV.type = SHORT_CONSTANT_OT;
  1316. PV.value = i; PV.marker = 0;
  1317. assemblez_1_branch(check_arg_count_zc, PV, ln, FALSE);
  1318. }
  1319. sprintf(fnt, "%s%s = ", (i==1)?"":", ", variable_name(i));
  1320. AI.text = fnt; assemblez_0(print_zc);
  1321. PV.type = VARIABLE_OT; PV.value = i; PV.marker = 0;
  1322. assemblez_1(print_num_zc, PV);
  1323. }
  1324. assemble_label_no(ln);
  1325. sprintf(fnt, ") ]^"); AI.text = fnt;
  1326. assemblez_0(print_zc);
  1327. assemble_label_no(ln2);
  1328. }
  1329. }
  1330. else {
  1331. rv = zmachine_pc;
  1332. if (stackargs)
  1333. byteout(0xC0, 0); /* Glulx type byte for function */
  1334. else
  1335. byteout(0xC1, 0); /* Glulx type byte for function */
  1336. /* Now the locals format list. This is simple; we only use
  1337. four-byte locals. That's a single pair, unless we have more
  1338. than 255 locals, or none at all. */
  1339. i = no_locals;
  1340. while (i) {
  1341. int j = i;
  1342. if (j > 255)
  1343. j = 255;
  1344. byteout(4, 0);
  1345. byteout(j, 0);
  1346. i -= j;
  1347. }
  1348. /* Terminate the list with a (0, 0) pair. */
  1349. byteout(0, 0);
  1350. byteout(0, 0);
  1351. if (stackargs) {
  1352. /* The top stack value is the number of function arguments. Let's
  1353. move that into the first local, which is _vararg_count. */
  1354. /* @copy sp _vararg_count; */
  1355. byteout(0x40, 0); byteout(0x98, 0); byteout(0x00, 0);
  1356. }
  1357. next_label = 0; next_sequence_point = 0; last_label = -1;
  1358. if ((routine_asterisked) || (define_INFIX_switch)) {
  1359. int ix;
  1360. char fnt[256];
  1361. assembly_operand AO, AO2;
  1362. if (define_INFIX_switch) {
  1363. /* This isn't supported */
  1364. if (embedded_flag) {
  1365. }
  1366. else {
  1367. i = no_named_routines++;
  1368. named_routine_symbols[i] = the_symbol;
  1369. }
  1370. }
  1371. sprintf(fnt, "[ %s(", name);
  1372. AO.marker = STRING_MV;
  1373. AO.type = CONSTANT_OT;
  1374. AO.value = compile_string(fnt, FALSE, FALSE);
  1375. assembleg_1(streamstr_gc, AO);
  1376. if (!stackargs) {
  1377. for (ix=1; ix<=no_locals; ix++) {
  1378. sprintf(fnt, "%s%s = ", (ix==1)?"":", ", variable_name(ix));
  1379. AO.marker = STRING_MV;
  1380. AO.type = CONSTANT_OT;
  1381. AO.value = compile_string(fnt, FALSE, FALSE);
  1382. assembleg_1(streamstr_gc, AO);
  1383. AO.marker = 0;
  1384. AO.type = LOCALVAR_OT;
  1385. AO.value = ix;
  1386. assembleg_1(streamnum_gc, AO);
  1387. }
  1388. }
  1389. else {
  1390. int lntop, lnbottom;
  1391. sprintf(fnt, "%s = ", variable_name(1));
  1392. AO.marker = STRING_MV;
  1393. AO.type = CONSTANT_OT;
  1394. AO.value = compile_string(fnt, FALSE, FALSE);
  1395. assembleg_1(streamstr_gc, AO);
  1396. AO.marker = 0;
  1397. AO.type = LOCALVAR_OT;
  1398. AO.value = 1;
  1399. assembleg_1(streamnum_gc, AO);
  1400. AO2.type = BYTECONSTANT_OT;
  1401. AO2.marker = 0;
  1402. AO2.value = ':';
  1403. assembleg_1(streamchar_gc, AO2);
  1404. AO2.type = BYTECONSTANT_OT;
  1405. AO2.marker = 0;
  1406. AO2.value = ' ';
  1407. /* for (temp_var4=0 : temp_var4<_vararg_count : temp_var4++) {
  1408. @streamchar ' ';
  1409. @stkpeek temp_var4 sp;
  1410. @stream_num sp;
  1411. }
  1412. */
  1413. assembleg_store(temp_var4, zero_operand);
  1414. lntop = next_label++;
  1415. lnbottom = next_label++;
  1416. assemble_label_no(lntop);
  1417. assembleg_2_branch(jge_gc, temp_var4, AO, lnbottom); /* AO is _vararg_count */
  1418. assembleg_1(streamchar_gc, AO2); /* AO2 is space */
  1419. assembleg_2(stkpeek_gc, temp_var4, stack_pointer);
  1420. assembleg_1(streamnum_gc, stack_pointer);
  1421. assembleg_3(add_gc, temp_var4, one_operand, temp_var4);
  1422. assembleg_0_branch(jump_gc, lntop);
  1423. assemble_label_no(lnbottom);
  1424. }
  1425. AO.marker = STRING_MV;
  1426. AO.type = CONSTANT_OT;
  1427. AO.value = compile_string(") ]^", FALSE, FALSE);
  1428. assembleg_1(streamstr_gc, AO);
  1429. }
  1430. }
  1431. return rv;
  1432. }
  1433. void assemble_routine_end(int embedded_flag, debug_locations locations)
  1434. { int32 i;
  1435. /* No marker is made in the Z-machine's code area to indicate the */
  1436. /* end of a routine. Instead, we simply assemble a return opcode if */
  1437. /* need be (it won't be if the last instruction was, say, a "quit"). */
  1438. /* The return value is true (1) for normal routines, false (0) for */
  1439. /* embedded routines (e.g. the library uses this for "before" */
  1440. /* properties). */
  1441. if (!execution_never_reaches_here)
  1442. {
  1443. if (!glulx_mode) {
  1444. if (embedded_flag) assemblez_0(rfalse_zc);
  1445. else assemblez_0(rtrue_zc);
  1446. }
  1447. else {
  1448. assembly_operand AO;
  1449. if (embedded_flag)
  1450. AO = zero_operand;
  1451. else
  1452. AO = one_operand;
  1453. assembleg_1(return_gc, AO);
  1454. }
  1455. }
  1456. /* Dump the contents of the current routine into longer-term Z-code
  1457. storage */
  1458. if (!glulx_mode)
  1459. transfer_routine_z();
  1460. else
  1461. transfer_routine_g();
  1462. if (track_unused_routines)
  1463. df_note_function_end(zmachine_pc);
  1464. /* Tell the debugging file about the routine just ended. */
  1465. if (debugfile_switch)
  1466. {
  1467. debug_file_printf("<routine>");
  1468. if (embedded_flag)
  1469. { debug_file_printf
  1470. ("<identifier artificial=\"true\">%s</identifier>",
  1471. routine_name);
  1472. }
  1473. else if (sflags[routine_symbol] & REPLACE_SFLAG)
  1474. { /* The symbol type will be set to ROUTINE_T once the replaced
  1475. version has been given; if it is already set, we must be dealing
  1476. with a replacement, and we can use the routine name as-is.
  1477. Otherwise we look for a rename. And if that doesn't work, we
  1478. fall back to an artificial identifier. */
  1479. if (stypes[routine_symbol] == ROUTINE_T)
  1480. { /* Optional because there may be further replacements. */
  1481. write_debug_optional_identifier(routine_symbol);
  1482. }
  1483. else if (find_symbol_replacement(&routine_symbol))
  1484. { debug_file_printf
  1485. ("<identifier>%s</identifier>", symbs[routine_symbol]);
  1486. }
  1487. else
  1488. { debug_file_printf
  1489. ("<identifier artificial=\"true\">%s (replaced)"
  1490. "</identifier>",
  1491. routine_name);
  1492. }
  1493. } else
  1494. { debug_file_printf("<identifier>%s</identifier>", routine_name);
  1495. }
  1496. debug_file_printf("<value>");
  1497. if (glulx_mode)
  1498. { write_debug_code_backpatch(routine_start_pc);
  1499. } else
  1500. { write_debug_packed_code_backpatch(routine_start_pc);
  1501. }
  1502. debug_file_printf("</value>");
  1503. debug_file_printf("<address>");
  1504. write_debug_code_backpatch(routine_start_pc);
  1505. debug_file_printf("</address>");
  1506. debug_file_printf
  1507. ("<byte-count>%d</byte-count>", zmachine_pc - routine_start_pc);
  1508. write_debug_locations(locations);
  1509. for (i = 1; i <= routine_locals; ++i)
  1510. { debug_file_printf("<local-variable>");
  1511. debug_file_printf("<identifier>%s</identifier>", variable_name(i));
  1512. if (glulx_mode)
  1513. { debug_file_printf
  1514. ("<frame-offset>%d</frame-offset>", 4 * (i - 1));
  1515. }
  1516. else
  1517. { debug_file_printf("<index>%d</index>", i);
  1518. }
  1519. debug_file_printf("</local-variable>");
  1520. }
  1521. for (i = 0; i < next_sequence_point; ++i)
  1522. { debug_file_printf("<sequence-point>");
  1523. debug_file_printf("<address>");
  1524. write_debug_code_backpatch
  1525. (label_offsets[sequence_point_labels[i]]);
  1526. debug_file_printf("</address>");
  1527. write_debug_location(sequence_point_locations[i]);
  1528. debug_file_printf("</sequence-point>");
  1529. }
  1530. debug_file_printf("</routine>");
  1531. }
  1532. my_free(&routine_name, "temporary copy of routine name");
  1533. /* Issue warnings about any local variables not used in the routine. */
  1534. for (i=1; i<=routine_locals; i++)
  1535. if (!(variable_usage[i]))
  1536. dbnu_warning("Local variable", variable_name(i),
  1537. routine_starts_line);
  1538. for (i=0; i<next_label; i++)
  1539. { int j = label_symbols[i];
  1540. if (j != -1)
  1541. { if (sflags[j] & CHANGE_SFLAG)
  1542. error_named_at("Routine contains no such label as",
  1543. (char *) symbs[j], slines[j]);
  1544. else
  1545. if ((sflags[j] & USED_SFLAG) == 0)
  1546. dbnu_warning("Label", (char *) symbs[j], slines[j]);
  1547. stypes[j] = CONSTANT_T;
  1548. sflags[j] = UNKNOWN_SFLAG;
  1549. }
  1550. }
  1551. no_sequence_points += next_sequence_point;
  1552. next_label = 0; next_sequence_point = 0;
  1553. }
  1554. /* ------------------------------------------------------------------------- */
  1555. /* Called when the holding area contains an entire routine of code: */
  1556. /* backpatches the labels, issues module markers, then dumps the routine */
  1557. /* into longer-term storage. */
  1558. /* Note that in the code received, all branches have long form, and their */
  1559. /* contents are not an offset but the label numbers they branch to. */
  1560. /* Similarly, LABEL operands (those of "jump" instructions) are label */
  1561. /* numbers. So this routine must change the label numbers to offsets, */
  1562. /* slimming the code down as it does so to take advantage of short-form */
  1563. /* branch operands where possible. */
  1564. /* ------------------------------------------------------------------------- */
  1565. static int32 adjusted_pc;
  1566. static void transfer_to_temp_file(uchar *c)
  1567. { fputc(*c,Temp2_fp);
  1568. adjusted_pc++;
  1569. }
  1570. static void transfer_to_zcode_area(uchar *c)
  1571. { write_byte_to_memory_block(&zcode_area, adjusted_pc++, *c);
  1572. }
  1573. static void transfer_routine_z(void)
  1574. { int32 i, j, pc, new_pc, label, long_form, offset_of_next, addr,
  1575. branch_on_true, rstart_pc;
  1576. void (* transfer_byte)(uchar *);
  1577. adjusted_pc = zmachine_pc - zcode_ha_size; rstart_pc = adjusted_pc;
  1578. if (asm_trace_level >= 3)
  1579. { printf("Backpatching routine at %05lx: initial size %d, %d labels\n",
  1580. (long int) adjusted_pc, zcode_ha_size, next_label);
  1581. }
  1582. transfer_byte =
  1583. (temporary_files_switch)?transfer_to_temp_file:transfer_to_zcode_area;
  1584. /* (1) Scan through for branches and make short/long decisions in each
  1585. case. Mark omitted bytes (2nd bytes in branches converted to
  1586. short form) with DELETED_MV. */
  1587. for (i=0, pc=adjusted_pc; i<zcode_ha_size; i++, pc++)
  1588. { if (zcode_markers[i] == BRANCH_MV)
  1589. { if (asm_trace_level >= 4)
  1590. printf("Branch detected at offset %04x\n", pc);
  1591. j = (256*zcode_holding_area[i] + zcode_holding_area[i+1]) & 0x7fff;
  1592. if (asm_trace_level >= 4)
  1593. printf("To label %d, which is %d from here\n",
  1594. j, label_offsets[j]-pc);
  1595. if ((label_offsets[j] >= pc+2) && (label_offsets[j] < pc+64))
  1596. { if (asm_trace_level >= 4) printf("Short form\n");
  1597. zcode_markers[i+1] = DELETED_MV;
  1598. }
  1599. }
  1600. }
  1601. /* (2) Calculate the new positions of the labels. Note that since the
  1602. long/short decision was taken on the basis of the old labels,
  1603. and since the new labels are slightly closer together because
  1604. of branch bytes deleted, there may be a few further branch
  1605. optimisations which are possible but which have been missed
  1606. (if two labels move inside the "short" range as a result of
  1607. a previous optimisation). However, this is acceptably uncommon. */
  1608. if (next_label > 0)
  1609. { if (asm_trace_level >= 4)
  1610. { printf("Opening label: %d\n", first_label);
  1611. for (i=0;i<next_label;i++)
  1612. printf("Label %d offset %04x next -> %d previous -> %d\n",
  1613. i, label_offsets[i], label_next[i], label_prev[i]);
  1614. }
  1615. for (i=0, pc=adjusted_pc, new_pc=adjusted_pc, label = first_label;
  1616. i<zcode_ha_size; i++, pc++)
  1617. { while ((label != -1) && (label_offsets[label] == pc))
  1618. { if (asm_trace_level >= 4)
  1619. printf("Position of L%d corrected from %04x to %04x\n",
  1620. label, label_offsets[label], new_pc);
  1621. label_offsets[label] = new_pc;
  1622. label = label_next[label];
  1623. }
  1624. if (zcode_markers[i] != DELETED_MV) new_pc++;
  1625. }
  1626. }
  1627. /* (3) As we are transferring, replace the label numbers in branch
  1628. operands with offsets to those labels. Also issue markers, now
  1629. that we know where they occur in the final Z-code area. */
  1630. for (i=0, new_pc=adjusted_pc; i<zcode_ha_size; i++)
  1631. { switch(zcode_markers[i])
  1632. { case BRANCH_MV:
  1633. long_form = 1; if (zcode_markers[i+1] == DELETED_MV) long_form = 0;
  1634. j = (256*zcode_holding_area[i] + zcode_holding_area[i+1]) & 0x7fff;
  1635. branch_on_true = ((zcode_holding_area[i]) & 0x80);
  1636. offset_of_next = new_pc + long_form + 1;
  1637. addr = label_offsets[j] - offset_of_next + 2;
  1638. if (addr<-0x2000 || addr>0x1fff)
  1639. fatalerror("Branch out of range: divide the routine up?");
  1640. if (addr<0) addr+=(int32) 0x10000L;
  1641. addr=addr&0x3fff;
  1642. if (long_form==1)
  1643. { zcode_holding_area[i] = branch_on_true + addr/256;
  1644. zcode_holding_area[i+1] = addr%256;
  1645. }
  1646. else
  1647. { if (addr >= 64)
  1648. { compiler_error("Label out of range for branch");
  1649. printf("Addr is %04x\n", addr);
  1650. }
  1651. zcode_holding_area[i] = branch_on_true + 0x40 + (addr&0x3f);
  1652. }
  1653. transfer_byte(zcode_holding_area + i); new_pc++;
  1654. break;
  1655. case LABEL_MV:
  1656. j = 256*zcode_holding_area[i] + zcode_holding_area[i+1];
  1657. addr = label_offsets[j] - new_pc;
  1658. if (addr<-0x8000 || addr>0x7fff)
  1659. fatalerror("Jump out of range: divide the routine up?");
  1660. if (addr<0) addr += (int32) 0x10000L;
  1661. zcode_holding_area[i] = addr/256;
  1662. zcode_holding_area[i+1] = addr%256;
  1663. transfer_byte(zcode_holding_area + i); new_pc++;
  1664. break;
  1665. case DELETED_MV:
  1666. break;
  1667. default:
  1668. switch(zcode_markers[i] & 0x7f)
  1669. { case NULL_MV: break;
  1670. case VARIABLE_MV:
  1671. case OBJECT_MV:
  1672. case ACTION_MV:
  1673. case IDENT_MV:
  1674. if (!module_switch) break;
  1675. default:
  1676. if ((zcode_markers[i] & 0x7f) > LARGEST_BPATCH_MV)
  1677. { compiler_error("Illegal code backpatch value");
  1678. printf("Illegal value of %02x at PC = %04x\n",
  1679. zcode_markers[i] & 0x7f, new_pc);
  1680. break;
  1681. }
  1682. write_byte_to_memory_block(&zcode_backpatch_table,
  1683. zcode_backpatch_size++,
  1684. zcode_markers[i] + 32*(new_pc/65536));
  1685. write_byte_to_memory_block(&zcode_backpatch_table,
  1686. zcode_backpatch_size++, (new_pc/256)%256);
  1687. write_byte_to_memory_block(&zcode_backpatch_table,
  1688. zcode_backpatch_size++, new_pc%256);
  1689. break;
  1690. }
  1691. transfer_byte(zcode_holding_area + i); new_pc++;
  1692. break;
  1693. }
  1694. }
  1695. if (asm_trace_level >= 3)
  1696. { printf("After branch optimisation, routine length is %d bytes\n",
  1697. new_pc - rstart_pc);
  1698. }
  1699. /* Insert null bytes if necessary to ensure the next routine address is */
  1700. /* expressible as a packed address */
  1701. { uchar zero[1];
  1702. zero[0] = 0;
  1703. if (oddeven_packing_switch)
  1704. while ((adjusted_pc%(scale_factor*2))!=0) transfer_byte(zero);
  1705. else
  1706. while ((adjusted_pc%scale_factor)!=0) transfer_byte(zero);
  1707. }
  1708. zmachine_pc = adjusted_pc;
  1709. zcode_ha_size = 0;
  1710. }
  1711. static void transfer_routine_g(void)
  1712. { int32 i, j, pc, new_pc, label, form_len, offset_of_next, addr,
  1713. rstart_pc;
  1714. void (* transfer_byte)(uchar *);
  1715. adjusted_pc = zmachine_pc - zcode_ha_size; rstart_pc = adjusted_pc;
  1716. if (asm_trace_level >= 3)
  1717. { printf("Backpatching routine at %05lx: initial size %d, %d labels\n",
  1718. (long int) adjusted_pc, zcode_ha_size, next_label);
  1719. }
  1720. transfer_byte =
  1721. (temporary_files_switch)?transfer_to_temp_file:transfer_to_zcode_area;
  1722. /* (1) Scan through for branches and make short/long decisions in each
  1723. case. Mark omitted bytes (bytes 2-4 in branches converted to
  1724. short form) with DELETED_MV. */
  1725. for (i=0, pc=adjusted_pc; i<zcode_ha_size; i++, pc++) {
  1726. if (zcode_markers[i] >= BRANCH_MV && zcode_markers[i] < BRANCHMAX_MV) {
  1727. int opmodeoffset = (zcode_markers[i] - BRANCH_MV);
  1728. int32 opmodebyte;
  1729. if (asm_trace_level >= 4)
  1730. printf("Branch detected at offset %04x\n", pc);
  1731. j = ((zcode_holding_area[i] << 24)
  1732. | (zcode_holding_area[i+1] << 16)
  1733. | (zcode_holding_area[i+2] << 8)
  1734. | (zcode_holding_area[i+3]));
  1735. offset_of_next = pc + 4;
  1736. addr = (label_offsets[j] - offset_of_next) + 2;
  1737. if (asm_trace_level >= 4)
  1738. printf("To label %d, which is (%d-2) = %d from here\n",
  1739. j, addr, label_offsets[j] - offset_of_next);
  1740. if (addr >= -0x80 && addr < 0x80) {
  1741. if (asm_trace_level >= 4) printf("...Byte form\n");
  1742. zcode_markers[i+1] = DELETED_MV;
  1743. zcode_markers[i+2] = DELETED_MV;
  1744. zcode_markers[i+3] = DELETED_MV;
  1745. opmodebyte = i - ((opmodeoffset+1)/2);
  1746. if ((opmodeoffset & 1) == 0)
  1747. zcode_holding_area[opmodebyte] =
  1748. (zcode_holding_area[opmodebyte] & 0xF0) | 0x01;
  1749. else
  1750. zcode_holding_area[opmodebyte] =
  1751. (zcode_holding_area[opmodebyte] & 0x0F) | 0x10;
  1752. }
  1753. else if (addr >= -0x8000 && addr < 0x8000) {
  1754. if (asm_trace_level >= 4) printf("...Short form\n");
  1755. zcode_markers[i+2] = DELETED_MV;
  1756. zcode_markers[i+3] = DELETED_MV;
  1757. opmodebyte = i - ((opmodeoffset+1)/2);
  1758. if ((opmodeoffset & 1) == 0)
  1759. zcode_holding_area[opmodebyte] =
  1760. (zcode_holding_area[opmodebyte] & 0xF0) | 0x02;
  1761. else
  1762. zcode_holding_area[opmodebyte] =
  1763. (zcode_holding_area[opmodebyte] & 0x0F) | 0x20;
  1764. }
  1765. }
  1766. }
  1767. /* (2) Calculate the new positions of the labels. Note that since the
  1768. long/short decision was taken on the basis of the old labels,
  1769. and since the new labels are slightly closer together because
  1770. of branch bytes deleted, there may be a few further branch
  1771. optimisations which are possible but which have been missed
  1772. (if two labels move inside the "short" range as a result of
  1773. a previous optimisation). However, this is acceptably uncommon. */
  1774. if (next_label > 0) {
  1775. if (asm_trace_level >= 4) {
  1776. printf("Opening label: %d\n", first_label);
  1777. for (i=0;i<next_label;i++)
  1778. printf("Label %d offset %04x next -> %d previous -> %d\n",
  1779. i, label_offsets[i], label_next[i], label_prev[i]);
  1780. }
  1781. for (i=0, pc=adjusted_pc, new_pc=adjusted_pc, label = first_label;
  1782. i<zcode_ha_size;
  1783. i++, pc++) {
  1784. while ((label != -1) && (label_offsets[label] == pc)) {
  1785. if (asm_trace_level >= 4)
  1786. printf("Position of L%d corrected from %04x to %04x\n",
  1787. label, label_offsets[label], new_pc);
  1788. label_offsets[label] = new_pc;
  1789. label = label_next[label];
  1790. }
  1791. if (zcode_markers[i] != DELETED_MV) new_pc++;
  1792. }
  1793. }
  1794. /* (3) As we are transferring, replace the label numbers in branch
  1795. operands with offsets to those labels. Also issue markers, now
  1796. that we know where they occur in the final Z-code area. */
  1797. for (i=0, new_pc=adjusted_pc; i<zcode_ha_size; i++) {
  1798. if (zcode_markers[i] >= BRANCH_MV && zcode_markers[i] < BRANCHMAX_MV) {
  1799. form_len = 4;
  1800. if (zcode_markers[i+1] == DELETED_MV) {
  1801. form_len = 1;
  1802. }
  1803. else {
  1804. if (zcode_markers[i+2] == DELETED_MV)
  1805. form_len = 2;
  1806. }
  1807. j = ((zcode_holding_area[i] << 24)
  1808. | (zcode_holding_area[i+1] << 16)
  1809. | (zcode_holding_area[i+2] << 8)
  1810. | (zcode_holding_area[i+3]));
  1811. /* At the moment, we can safely assume that the branch operand
  1812. is the end of the opcode, so the next opcode starts right
  1813. after it. */
  1814. offset_of_next = new_pc + form_len;
  1815. addr = (label_offsets[j] - offset_of_next) + 2;
  1816. if (asm_trace_level >= 4) {
  1817. printf("Branch at offset %04x: %04x (%s)\n",
  1818. new_pc, addr, ((form_len == 1) ? "byte" :
  1819. ((form_len == 2) ? "short" : "long")));
  1820. }
  1821. if (form_len == 1) {
  1822. if (addr < -0x80 && addr >= 0x80) {
  1823. error("*** Label out of range for byte branch ***");
  1824. }
  1825. zcode_holding_area[i] = (addr) & 0xFF;
  1826. }
  1827. else if (form_len == 2) {
  1828. if (addr < -0x8000 && addr >= 0x8000) {
  1829. error("*** Label out of range for short branch ***");
  1830. }
  1831. zcode_holding_area[i] = (addr >> 8) & 0xFF;
  1832. zcode_holding_area[i+1] = (addr) & 0xFF;
  1833. }
  1834. else {
  1835. zcode_holding_area[i] = (addr >> 24) & 0xFF;
  1836. zcode_holding_area[i+1] = (addr >> 16) & 0xFF;
  1837. zcode_holding_area[i+2] = (addr >> 8) & 0xFF;
  1838. zcode_holding_area[i+3] = (addr) & 0xFF;
  1839. }
  1840. transfer_byte(zcode_holding_area + i); new_pc++;
  1841. }
  1842. else if (zcode_markers[i] == LABEL_MV) {
  1843. error("*** No LABEL opcodes in Glulx ***");
  1844. }
  1845. else if (zcode_markers[i] == DELETED_MV) {
  1846. /* skip it */
  1847. }
  1848. else {
  1849. switch(zcode_markers[i] & 0x7f) {
  1850. case NULL_MV:
  1851. break;
  1852. case ACTION_MV:
  1853. case IDENT_MV:
  1854. if (!module_switch) break;
  1855. case OBJECT_MV:
  1856. case VARIABLE_MV:
  1857. default:
  1858. if ((zcode_markers[i] & 0x7f) > LARGEST_BPATCH_MV) {
  1859. error("*** Illegal code backpatch value ***");
  1860. printf("Illegal value of %02x at PC = %04x\n",
  1861. zcode_markers[i] & 0x7f, new_pc);
  1862. break;
  1863. }
  1864. /* The backpatch table format for Glulx:
  1865. First, the marker byte (0..LARGEST_BPATCH_MV).
  1866. Then a byte indicating the data size to be patched (1, 2, 4).
  1867. Then the four-byte address (new_pc).
  1868. */
  1869. write_byte_to_memory_block(&zcode_backpatch_table,
  1870. zcode_backpatch_size++,
  1871. zcode_markers[i]);
  1872. write_byte_to_memory_block(&zcode_backpatch_table,
  1873. zcode_backpatch_size++,
  1874. 4);
  1875. write_byte_to_memory_block(&zcode_backpatch_table,
  1876. zcode_backpatch_size++, ((new_pc >> 24) & 0xFF));
  1877. write_byte_to_memory_block(&zcode_backpatch_table,
  1878. zcode_backpatch_size++, ((new_pc >> 16) & 0xFF));
  1879. write_byte_to_memory_block(&zcode_backpatch_table,
  1880. zcode_backpatch_size++, ((new_pc >> 8) & 0xFF));
  1881. write_byte_to_memory_block(&zcode_backpatch_table,
  1882. zcode_backpatch_size++, (new_pc & 0xFF));
  1883. break;
  1884. }
  1885. transfer_byte(zcode_holding_area + i); new_pc++;
  1886. }
  1887. }
  1888. if (asm_trace_level >= 3)
  1889. { printf("After branch optimisation, routine length is %d bytes\n",
  1890. new_pc - rstart_pc);
  1891. }
  1892. zmachine_pc = adjusted_pc;
  1893. zcode_ha_size = 0;
  1894. }
  1895. /* ========================================================================= */
  1896. /* Front ends for the instruction assembler: convenient shorthand forms */
  1897. /* used in various code generation routines all over Inform. */
  1898. /* ------------------------------------------------------------------------- */
  1899. void assemble_jump(int n)
  1900. {
  1901. if (!glulx_mode)
  1902. assemblez_jump(n);
  1903. else
  1904. assembleg_jump(n);
  1905. }
  1906. void assemblez_0(int internal_number)
  1907. { AI.internal_number = internal_number;
  1908. AI.operand_count = 0;
  1909. AI.store_variable_number = -1;
  1910. AI.branch_label_number = -1;
  1911. assemblez_instruction(&AI);
  1912. }
  1913. void assemblez_0_to(int internal_number, assembly_operand o)
  1914. { AI.internal_number = internal_number;
  1915. AI.operand_count = 0;
  1916. AI.store_variable_number = o.value;
  1917. AI.branch_label_number = -1;
  1918. assemblez_instruction(&AI);
  1919. }
  1920. void assemblez_0_branch(int internal_number, int label, int flag)
  1921. { AI.internal_number = internal_number;
  1922. AI.operand_count = 0;
  1923. AI.store_variable_number = -1;
  1924. AI.branch_label_number = label;
  1925. AI.branch_flag = flag;
  1926. assemblez_instruction(&AI);
  1927. }
  1928. void assemblez_1(int internal_number, assembly_operand o1)
  1929. { AI.internal_number = internal_number;
  1930. AI.operand_count = 1;
  1931. AI.operand[0] = o1;
  1932. AI.store_variable_number = -1;
  1933. AI.branch_label_number = -1;
  1934. assemblez_instruction(&AI);
  1935. }
  1936. void assemblez_1_to(int internal_number,
  1937. assembly_operand o1, assembly_operand st)
  1938. { AI.internal_number = internal_number;
  1939. AI.operand_count = 1;
  1940. AI.operand[0] = o1;
  1941. AI.store_variable_number = st.value;
  1942. AI.branch_label_number = -1;
  1943. assemblez_instruction(&AI);
  1944. }
  1945. void assemblez_1_branch(int internal_number,
  1946. assembly_operand o1, int label, int flag)
  1947. { AI.internal_number = internal_number;
  1948. AI.operand_count = 1;
  1949. AI.operand[0] = o1;
  1950. AI.branch_label_number = label;
  1951. AI.store_variable_number = -1;
  1952. AI.branch_flag = flag;
  1953. assemblez_instruction(&AI);
  1954. }
  1955. void assemblez_2(int internal_number,
  1956. assembly_operand o1, assembly_operand o2)
  1957. { AI.internal_number = internal_number;
  1958. AI.operand_count = 2;
  1959. AI.operand[0] = o1;
  1960. AI.operand[1] = o2;
  1961. AI.store_variable_number = -1;
  1962. AI.branch_label_number = -1;
  1963. assemblez_instruction(&AI);
  1964. }
  1965. void assemblez_3(int internal_number,
  1966. assembly_operand o1, assembly_operand o2, assembly_operand o3)
  1967. { AI.internal_number = internal_number;
  1968. AI.operand_count = 3;
  1969. AI.operand[0] = o1;
  1970. AI.operand[1] = o2;
  1971. AI.operand[2] = o3;
  1972. AI.store_variable_number = -1;
  1973. AI.branch_label_number = -1;
  1974. assemblez_instruction(&AI);
  1975. }
  1976. void assemblez_3_to(int internal_number,
  1977. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  1978. assembly_operand st)
  1979. { AI.internal_number = internal_number;
  1980. AI.operand_count = 3;
  1981. AI.operand[0] = o1;
  1982. AI.operand[1] = o2;
  1983. AI.operand[2] = o3;
  1984. AI.store_variable_number = st.value;
  1985. AI.branch_label_number = -1;
  1986. assemblez_instruction(&AI);
  1987. }
  1988. void assemblez_3_branch(int internal_number,
  1989. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  1990. int label, int flag)
  1991. { AI.internal_number = internal_number;
  1992. AI.operand_count = 3;
  1993. AI.operand[0] = o1;
  1994. AI.operand[1] = o2;
  1995. AI.operand[2] = o3;
  1996. AI.store_variable_number = -1;
  1997. AI.branch_label_number = label;
  1998. AI.branch_flag = flag;
  1999. assemblez_instruction(&AI);
  2000. }
  2001. void assemblez_4(int internal_number,
  2002. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  2003. assembly_operand o4)
  2004. { AI.internal_number = internal_number;
  2005. AI.operand_count = 4;
  2006. AI.operand[0] = o1;
  2007. AI.operand[1] = o2;
  2008. AI.operand[2] = o3;
  2009. AI.operand[3] = o4;
  2010. AI.store_variable_number = -1;
  2011. AI.branch_label_number = -1;
  2012. assemblez_instruction(&AI);
  2013. }
  2014. void assemblez_5(int internal_number,
  2015. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  2016. assembly_operand o4, assembly_operand o5)
  2017. { AI.internal_number = internal_number;
  2018. AI.operand_count = 5;
  2019. AI.operand[0] = o1;
  2020. AI.operand[1] = o2;
  2021. AI.operand[2] = o3;
  2022. AI.operand[3] = o4;
  2023. AI.operand[4] = o5;
  2024. AI.store_variable_number = -1;
  2025. AI.branch_label_number = -1;
  2026. assemblez_instruction(&AI);
  2027. }
  2028. void assemblez_6(int internal_number,
  2029. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  2030. assembly_operand o4, assembly_operand o5, assembly_operand o6)
  2031. { AI.internal_number = internal_number;
  2032. AI.operand_count = 6;
  2033. AI.operand[0] = o1;
  2034. AI.operand[1] = o2;
  2035. AI.operand[2] = o3;
  2036. AI.operand[3] = o4;
  2037. AI.operand[4] = o5;
  2038. AI.operand[5] = o6;
  2039. AI.store_variable_number = -1;
  2040. AI.branch_label_number = -1;
  2041. assemblez_instruction(&AI);
  2042. }
  2043. void assemblez_4_branch(int internal_number,
  2044. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  2045. assembly_operand o4, int label, int flag)
  2046. { AI.internal_number = internal_number;
  2047. AI.operand_count = 4;
  2048. AI.operand[0] = o1;
  2049. AI.operand[1] = o2;
  2050. AI.operand[2] = o3;
  2051. AI.operand[3] = o4;
  2052. AI.store_variable_number = -1;
  2053. AI.branch_label_number = label;
  2054. AI.branch_flag = flag;
  2055. assemblez_instruction(&AI);
  2056. }
  2057. void assemblez_4_to(int internal_number,
  2058. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  2059. assembly_operand o4, assembly_operand st)
  2060. { AI.internal_number = internal_number;
  2061. AI.operand_count = 4;
  2062. AI.operand[0] = o1;
  2063. AI.operand[1] = o2;
  2064. AI.operand[2] = o3;
  2065. AI.operand[3] = o4;
  2066. AI.store_variable_number = st.value;
  2067. AI.branch_label_number = -1;
  2068. assemblez_instruction(&AI);
  2069. }
  2070. void assemblez_5_to(int internal_number,
  2071. assembly_operand o1, assembly_operand o2, assembly_operand o3,
  2072. assembly_operand o4, assembly_operand o5, assembly_operand st)
  2073. { AI.internal_number = internal_number;
  2074. AI.operand_count = 5;
  2075. AI.operand[0] = o1;
  2076. AI.operand[1] = o2;
  2077. AI.operand[2] = o3;
  2078. AI.operand[3] = o4;
  2079. AI.operand[4] = o5;
  2080. AI.store_variable_number = st.value;
  2081. AI.branch_label_number = -1;
  2082. assemblez_instruction(&AI);
  2083. }
  2084. void assemblez_2_to(int internal_number,
  2085. assembly_operand o1, assembly_operand o2, assembly_operand st)
  2086. { AI.internal_number = internal_number;
  2087. AI.operand_count = 2;
  2088. AI.operand[0] = o1;
  2089. AI.operand[1] = o2;
  2090. AI.store_variable_number = st.value;
  2091. AI.branch_label_number = -1;
  2092. assemblez_instruction(&AI);
  2093. }
  2094. void assemblez_2_branch(int internal_number,
  2095. assembly_operand o1, assembly_operand o2, int label, int flag)
  2096. { AI.internal_number = internal_number;
  2097. AI.operand_count = 2;
  2098. AI.operand[0] = o1;
  2099. AI.operand[1] = o2;
  2100. AI.branch_label_number = label;
  2101. AI.store_variable_number = -1;
  2102. AI.branch_flag = flag;
  2103. assemblez_instruction(&AI);
  2104. }
  2105. void assemblez_objcode(int internal_number,
  2106. assembly_operand o1, assembly_operand st, int label, int flag)
  2107. { AI.internal_number = internal_number;
  2108. AI.operand_count = 1;
  2109. AI.operand[0] = o1;
  2110. AI.branch_label_number = label;
  2111. AI.store_variable_number = st.value;
  2112. AI.branch_flag = flag;
  2113. assemblez_instruction(&AI);
  2114. }
  2115. extern void assemblez_inc(assembly_operand o1)
  2116. { int m = 0;
  2117. if ((o1.value >= MAX_LOCAL_VARIABLES)
  2118. && (o1.value<LOWEST_SYSTEM_VAR_NUMBER))
  2119. m = VARIABLE_MV;
  2120. AI.internal_number = inc_zc;
  2121. AI.operand_count = 1;
  2122. AI.operand[0].value = o1.value;
  2123. AI.operand[0].type = SHORT_CONSTANT_OT;
  2124. AI.operand[0].marker = m;
  2125. AI.store_variable_number = -1;
  2126. AI.branch_label_number = -1;
  2127. assemblez_instruction(&AI);
  2128. }
  2129. extern void assemblez_dec(assembly_operand o1)
  2130. { int m = 0;
  2131. if ((o1.value >= MAX_LOCAL_VARIABLES)
  2132. && (o1.value<LOWEST_SYSTEM_VAR_NUMBER))
  2133. m = VARIABLE_MV;
  2134. AI.internal_number = dec_zc;
  2135. AI.operand_count = 1;
  2136. AI.operand[0].value = o1.value;
  2137. AI.operand[0].type = SHORT_CONSTANT_OT;
  2138. AI.operand[0].marker = m;
  2139. AI.store_variable_number = -1;
  2140. AI.branch_label_number = -1;
  2141. assemblez_instruction(&AI);
  2142. }
  2143. extern void assemblez_store(assembly_operand o1, assembly_operand o2)
  2144. { int m = 0;
  2145. if ((o1.value >= MAX_LOCAL_VARIABLES)
  2146. && (o1.value<LOWEST_SYSTEM_VAR_NUMBER))
  2147. m = VARIABLE_MV;
  2148. if ((o2.type == VARIABLE_OT) && (o2.value == 0))
  2149. {
  2150. /* Assemble "pull VAR" rather than "store VAR sp",
  2151. saving 1 byte */
  2152. AI.internal_number = pull_zc;
  2153. if (instruction_set_number == 6)
  2154. { AI.operand_count = 0;
  2155. AI.store_variable_number = o1.value;
  2156. }
  2157. else
  2158. { AI.operand_count = 1;
  2159. AI.operand[0].value = o1.value;
  2160. AI.operand[0].type = SHORT_CONSTANT_OT;
  2161. AI.operand[0].marker = m;
  2162. AI.store_variable_number = -1;
  2163. }
  2164. AI.branch_label_number = -1;
  2165. assemblez_instruction(&AI);
  2166. return;
  2167. }
  2168. if ((o1.type == VARIABLE_OT) && (o1.value == 0))
  2169. { /* Assemble "push VAR" rather than "store sp VAR",
  2170. saving 1 byte */
  2171. AI.internal_number = push_zc;
  2172. AI.operand_count = 1;
  2173. AI.operand[0] = o2;
  2174. AI.store_variable_number = -1;
  2175. AI.branch_label_number = -1;
  2176. assemblez_instruction(&AI);
  2177. return;
  2178. }
  2179. AI.internal_number = store_zc;
  2180. AI.operand_count = 2;
  2181. AI.operand[0].value = o1.value;
  2182. AI.operand[0].type = SHORT_CONSTANT_OT;
  2183. AI.operand[0].marker = m;
  2184. AI.operand[1] = o2;
  2185. AI.store_variable_number = -1;
  2186. AI.branch_label_number = -1;
  2187. assemblez_instruction(&AI);
  2188. }
  2189. void assemblez_jump(int n)
  2190. { assembly_operand AO;
  2191. if (n==-4) assemblez_0(rtrue_zc);
  2192. else if (n==-3) assemblez_0(rfalse_zc);
  2193. else
  2194. { AO.type = LONG_CONSTANT_OT; AO.value = n; AO.marker = 0;
  2195. assemblez_1(jump_zc, AO);
  2196. }
  2197. }
  2198. void assembleg_0(int internal_number)
  2199. { AI.internal_number = internal_number;
  2200. AI.operand_count = 0;
  2201. assembleg_instruction(&AI);
  2202. }
  2203. void assembleg_1(int internal_number, assembly_operand o1)
  2204. { AI.internal_number = internal_number;
  2205. AI.operand_count = 1;
  2206. AI.operand[0] = o1;
  2207. assembleg_instruction(&AI);
  2208. }
  2209. void assembleg_2(int internal_number, assembly_operand o1,
  2210. assembly_operand o2)
  2211. { AI.internal_number = internal_number;
  2212. AI.operand_count = 2;
  2213. AI.operand[0] = o1;
  2214. AI.operand[1] = o2;
  2215. assembleg_instruction(&AI);
  2216. }
  2217. void assembleg_3(int internal_number, assembly_operand o1,
  2218. assembly_operand o2, assembly_operand o3)
  2219. { AI.internal_number = internal_number;
  2220. AI.operand_count = 3;
  2221. AI.operand[0] = o1;
  2222. AI.operand[1] = o2;
  2223. AI.operand[2] = o3;
  2224. assembleg_instruction(&AI);
  2225. }
  2226. void assembleg_4(int internal_number, assembly_operand o1,
  2227. assembly_operand o2, assembly_operand o3,
  2228. assembly_operand o4)
  2229. { AI.internal_number = internal_number;
  2230. AI.operand_count = 4;
  2231. AI.operand[0] = o1;
  2232. AI.operand[1] = o2;
  2233. AI.operand[2] = o3;
  2234. AI.operand[3] = o4;
  2235. assembleg_instruction(&AI);
  2236. }
  2237. void assembleg_5(int internal_number, assembly_operand o1,
  2238. assembly_operand o2, assembly_operand o3,
  2239. assembly_operand o4, assembly_operand o5)
  2240. { AI.internal_number = internal_number;
  2241. AI.operand_count = 5;
  2242. AI.operand[0] = o1;
  2243. AI.operand[1] = o2;
  2244. AI.operand[2] = o3;
  2245. AI.operand[3] = o4;
  2246. AI.operand[4] = o5;
  2247. assembleg_instruction(&AI);
  2248. }
  2249. void assembleg_0_branch(int internal_number,
  2250. int label)
  2251. {
  2252. AI.internal_number = internal_number;
  2253. AI.operand_count = 1;
  2254. AI.operand[0].type = CONSTANT_OT;
  2255. AI.operand[0].value = label;
  2256. AI.operand[0].marker = BRANCH_MV;
  2257. assembleg_instruction(&AI);
  2258. }
  2259. void assembleg_1_branch(int internal_number,
  2260. assembly_operand o1, int label)
  2261. {
  2262. /* Some clever optimizations first. A constant is always or never equal
  2263. to zero. */
  2264. if (o1.marker == 0 && is_constant_ot(o1.type)) {
  2265. if ((internal_number == jz_gc && o1.value == 0)
  2266. || (internal_number == jnz_gc && o1.value != 0)) {
  2267. assembleg_0_branch(jump_gc, label);
  2268. /* We clear the "can't reach statement" flag here,
  2269. so that "if (1)" doesn't produce that warning. */
  2270. execution_never_reaches_here = 0;
  2271. return;
  2272. }
  2273. if ((internal_number == jz_gc && o1.value != 0)
  2274. || (internal_number == jnz_gc && o1.value == 0)) {
  2275. /* assemble nothing at all! */
  2276. return;
  2277. }
  2278. }
  2279. AI.internal_number = internal_number;
  2280. AI.operand_count = 2;
  2281. AI.operand[0] = o1;
  2282. AI.operand[1].type = CONSTANT_OT;
  2283. AI.operand[1].value = label;
  2284. AI.operand[1].marker = BRANCH_MV;
  2285. assembleg_instruction(&AI);
  2286. }
  2287. void assembleg_2_branch(int internal_number,
  2288. assembly_operand o1, assembly_operand o2, int label)
  2289. {
  2290. AI.internal_number = internal_number;
  2291. AI.operand_count = 3;
  2292. AI.operand[0] = o1;
  2293. AI.operand[1] = o2;
  2294. AI.operand[2].type = CONSTANT_OT;
  2295. AI.operand[2].value = label;
  2296. AI.operand[2].marker = BRANCH_MV;
  2297. assembleg_instruction(&AI);
  2298. }
  2299. void assembleg_call_1(assembly_operand oaddr, assembly_operand o1,
  2300. assembly_operand odest)
  2301. {
  2302. assembleg_3(callfi_gc, oaddr, o1, odest);
  2303. }
  2304. void assembleg_call_2(assembly_operand oaddr, assembly_operand o1,
  2305. assembly_operand o2, assembly_operand odest)
  2306. {
  2307. assembleg_4(callfii_gc, oaddr, o1, o2, odest);
  2308. }
  2309. void assembleg_call_3(assembly_operand oaddr, assembly_operand o1,
  2310. assembly_operand o2, assembly_operand o3, assembly_operand odest)
  2311. {
  2312. assembleg_5(callfiii_gc, oaddr, o1, o2, o3, odest);
  2313. }
  2314. void assembleg_inc(assembly_operand o1)
  2315. {
  2316. AI.internal_number = add_gc;
  2317. AI.operand_count = 3;
  2318. AI.operand[0] = o1;
  2319. AI.operand[1] = one_operand;
  2320. AI.operand[2] = o1;
  2321. assembleg_instruction(&AI);
  2322. }
  2323. void assembleg_dec(assembly_operand o1)
  2324. {
  2325. AI.internal_number = sub_gc;
  2326. AI.operand_count = 3;
  2327. AI.operand[0] = o1;
  2328. AI.operand[1] = one_operand;
  2329. AI.operand[2] = o1;
  2330. assembleg_instruction(&AI);
  2331. }
  2332. void assembleg_store(assembly_operand o1, assembly_operand o2)
  2333. {
  2334. /* Note the order is reversed: "o1 = o2;" */
  2335. assembleg_2(copy_gc, o2, o1);
  2336. }
  2337. void assembleg_jump(int n)
  2338. {
  2339. if (n==-4) {
  2340. assembleg_1(return_gc, one_operand);
  2341. }
  2342. else if (n==-3) {
  2343. assembleg_1(return_gc, zero_operand);
  2344. }
  2345. else {
  2346. assembleg_0_branch(jump_gc, n);
  2347. }
  2348. }
  2349. /* ========================================================================= */
  2350. /* Parsing and then calling the assembler for @ (assembly language) */
  2351. /* statements */
  2352. /* ------------------------------------------------------------------------- */
  2353. static assembly_operand parse_operand_z(void)
  2354. { assembly_operand AO;
  2355. AO = parse_expression(ASSEMBLY_CONTEXT);
  2356. if (AO.type == EXPRESSION_OT)
  2357. { ebf_error("variable or constant", "expression");
  2358. AO.type = SHORT_CONSTANT_OT;
  2359. }
  2360. return(AO);
  2361. }
  2362. static void parse_assembly_z(void)
  2363. { int n, min, max, indirect_addressed, error_flag = FALSE;
  2364. opcodez O;
  2365. AI.operand_count = 0;
  2366. AI.store_variable_number = -1;
  2367. AI.branch_label_number = -1;
  2368. AI.text = NULL;
  2369. opcode_names.enabled = TRUE;
  2370. get_next_token();
  2371. opcode_names.enabled = FALSE;
  2372. if (token_type == DQ_TT)
  2373. { int i;
  2374. AI.internal_number = -1;
  2375. custom_opcode_z.name = (uchar *) token_text;
  2376. custom_opcode_z.version1 = instruction_set_number;
  2377. custom_opcode_z.version2 = instruction_set_number;
  2378. custom_opcode_z.extension = -1;
  2379. custom_opcode_z.flags = 0;
  2380. custom_opcode_z.op_rules = 0;
  2381. custom_opcode_z.flags2_set = 0;
  2382. custom_opcode_z.no = ZERO;
  2383. for (i=0; token_text[i]!=0; i++)
  2384. { if (token_text[i] == ':')
  2385. { token_text[i++] = 0;
  2386. break;
  2387. }
  2388. }
  2389. if (token_text[i] == 0)
  2390. error("Opcode specification should have form \"VAR:102\"");
  2391. n = -1;
  2392. if (strcmp(token_text, "0OP")==0) n=ZERO;
  2393. if (strcmp(token_text, "1OP")==0) n=ONE;
  2394. if (strcmp(token_text, "2OP")==0) n=TWO;
  2395. if (strcmp(token_text, "VAR")==0) n=VAR;
  2396. if (strcmp(token_text, "EXT")==0) n=EXT;
  2397. if (strcmp(token_text, "VAR_LONG")==0) n=VAR_LONG;
  2398. if (strcmp(token_text, "EXT_LONG")==0) n=EXT_LONG;
  2399. if (i>0) token_text[i-1] = ':';
  2400. if (n==-1)
  2401. { ebf_error("Expected 0OP, 1OP, 2OP, VAR, EXT, VAR_LONG or EXT_LONG",
  2402. token_text);
  2403. n = EXT;
  2404. }
  2405. custom_opcode_z.no = n;
  2406. custom_opcode_z.code = atoi(token_text+i);
  2407. while (isdigit(token_text[i])) i++;
  2408. { int max, min;
  2409. min = 0;
  2410. switch(n)
  2411. { case ZERO: case ONE: max = 16; break;
  2412. case VAR: case VAR_LONG: min = 32; max = 64; break;
  2413. case EXT: case EXT_LONG: max = 256; break;
  2414. case TWO: max = 32; break;
  2415. }
  2416. if ((custom_opcode_z.code < min) || (custom_opcode_z.code >= max))
  2417. { char range[32];
  2418. sprintf(range, "%d to %d", min, max-1);
  2419. error_named("For this operand type, opcode number must be in range",
  2420. range);
  2421. custom_opcode_z.code = min;
  2422. }
  2423. }
  2424. while (token_text[i++] != 0)
  2425. { switch(token_text[i-1])
  2426. { case 'B': custom_opcode_z.flags |= Br; break;
  2427. case 'S': custom_opcode_z.flags |= St; break;
  2428. case 'T': custom_opcode_z.op_rules = TEXT; break;
  2429. case 'I': custom_opcode_z.op_rules = VARIAB; break;
  2430. case 'F': custom_opcode_z.flags2_set = atoi(token_text+i);
  2431. while (isdigit(token_text[i])) i++; break;
  2432. default:
  2433. error("Unknown flag: options are B (branch), S (store), \
  2434. T (text), I (indirect addressing), F** (set this Flags 2 bit)");
  2435. break;
  2436. }
  2437. }
  2438. O = custom_opcode_z;
  2439. }
  2440. else
  2441. { if (token_type != OPCODE_NAME_TT)
  2442. { ebf_error("an opcode name", token_text);
  2443. panic_mode_error_recovery();
  2444. return;
  2445. }
  2446. AI.internal_number = token_value;
  2447. O = internal_number_to_opcode_z(AI.internal_number);
  2448. }
  2449. indirect_addressed = (O.op_rules == VARIAB);
  2450. if (O.op_rules == TEXT)
  2451. { get_next_token();
  2452. if (token_type != DQ_TT)
  2453. ebf_error("literal text in double-quotes", token_text);
  2454. AI.text = token_text;
  2455. if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)) return;
  2456. get_next_token();
  2457. if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
  2458. { assemblez_instruction(&AI);
  2459. return;
  2460. }
  2461. ebf_error("semicolon ';' after print string", token_text);
  2462. put_token_back();
  2463. return;
  2464. }
  2465. return_sp_as_variable = TRUE;
  2466. do
  2467. { get_next_token();
  2468. if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)) break;
  2469. if ((token_type == SEP_TT) && (token_value == ARROW_SEP))
  2470. { if (AI.store_variable_number != -1)
  2471. error("Only one '->' store destination can be given");
  2472. get_next_token();
  2473. if ((token_type != SYMBOL_TT)
  2474. && (token_type != LOCAL_VARIABLE_TT))
  2475. ebf_error("variable name or 'sp'", token_text);
  2476. n = 255;
  2477. if (token_type == LOCAL_VARIABLE_TT) n = token_value;
  2478. else
  2479. { if (strcmp(token_text, "sp") == 0) n = 0;
  2480. else
  2481. { if (stypes[token_value] != GLOBAL_VARIABLE_T)
  2482. error_named(
  2483. "Store '->' destination not 'sp' or a variable:",
  2484. token_text);
  2485. else n = svals[token_value];
  2486. }
  2487. }
  2488. AI.store_variable_number = n;
  2489. continue;
  2490. }
  2491. if ((token_type == SEP_TT) &&
  2492. ((token_value == BRANCH_SEP) || (token_value == NBRANCH_SEP)))
  2493. { if (AI.branch_label_number != -1)
  2494. error("Only one '?' branch destination can be given");
  2495. AI.branch_flag = (token_value == BRANCH_SEP);
  2496. opcode_names.enabled = TRUE;
  2497. get_next_token();
  2498. opcode_names.enabled = FALSE;
  2499. n = -2;
  2500. if ((token_type == OPCODE_NAME_TT)
  2501. && (token_value == rfalse_zc)) n = -3;
  2502. else
  2503. if ((token_type == OPCODE_NAME_TT)
  2504. && (token_value == rtrue_zc)) n = -4;
  2505. else
  2506. { if (token_type == SYMBOL_TT)
  2507. { put_token_back();
  2508. n = parse_label();
  2509. }
  2510. else
  2511. ebf_error("label name after '?' or '?~'", token_text);
  2512. }
  2513. AI.branch_label_number = n;
  2514. continue;
  2515. }
  2516. if (AI.operand_count == 8)
  2517. { error("No assembly instruction may have more than 8 operands");
  2518. panic_mode_error_recovery(); break;
  2519. }
  2520. if ((token_type == SEP_TT) && (token_value == OPEN_SQUARE_SEP))
  2521. { if (!indirect_addressed)
  2522. error("This opcode does not use indirect addressing");
  2523. if (AI.operand_count > 0)
  2524. error("Indirect addressing can only be used on the first operand");
  2525. AI.operand[AI.operand_count++] = parse_operand_z();
  2526. get_next_token();
  2527. if (!((token_type == SEP_TT) && (token_value == CLOSE_SQUARE_SEP)))
  2528. { ebf_error("']'", token_text);
  2529. put_token_back();
  2530. }
  2531. }
  2532. else
  2533. { put_token_back();
  2534. AI.operand[AI.operand_count++] = parse_operand_z();
  2535. if ((indirect_addressed) && (AI.operand_count == 1)
  2536. && (AI.operand[AI.operand_count-1].type == VARIABLE_OT))
  2537. { AI.operand[AI.operand_count-1].type = SHORT_CONSTANT_OT;
  2538. AI.operand[AI.operand_count-1].marker = VARIABLE_MV;
  2539. }
  2540. }
  2541. } while (TRUE);
  2542. return_sp_as_variable = FALSE;
  2543. if (O.version1 == 0)
  2544. { error_named("Opcode unavailable in this Z-machine version:",
  2545. opcode_names.keywords[AI.internal_number]);
  2546. return;
  2547. }
  2548. if (((O.flags) & Br) != 0)
  2549. { if (AI.branch_label_number == -1)
  2550. { error_flag = TRUE;
  2551. AI.branch_label_number = -2;
  2552. }
  2553. }
  2554. else
  2555. { if (AI.branch_label_number != -1)
  2556. { error_flag = TRUE;
  2557. AI.branch_label_number = -1;
  2558. }
  2559. }
  2560. if (((O.flags) & St) != 0)
  2561. { if (AI.store_variable_number == -1)
  2562. { if (AI.operand_count == 0)
  2563. { error_flag = TRUE;
  2564. AI.store_variable_number = 255;
  2565. }
  2566. else
  2567. { AI.store_variable_number
  2568. = AI.operand[--AI.operand_count].value;
  2569. if (AI.operand[AI.operand_count].type != VARIABLE_OT)
  2570. error("Store destination (the last operand) is not a variable");
  2571. }
  2572. }
  2573. }
  2574. else
  2575. { if (AI.store_variable_number != -1)
  2576. { error_flag = TRUE;
  2577. AI.store_variable_number = -1;
  2578. }
  2579. }
  2580. switch(O.no)
  2581. { case TWO: min = 2; max = 2;
  2582. /* Exception for the V6 set_colour, which can take
  2583. a third argument, thus forcing it into VAR form: */
  2584. if ((version_number == 6) && (O.code == 0x1b)) max = 3;
  2585. /* Also an exception for je, which can take from 1
  2586. argument (useless) to 4 arguments */
  2587. if (O.code == 0x01) { min = 1; max = 4; }
  2588. break;
  2589. case VAR: min = 0; max = 4; break;
  2590. case VAR_LONG: min = 0; max = 8; break;
  2591. case ONE: min = 1; max = 1; break;
  2592. case ZERO: min = 0; max = 0; break;
  2593. case EXT: min = 0; max = 4; break;
  2594. case EXT_LONG: min = 0; max = 8; break;
  2595. }
  2596. if ((AI.operand_count >= min) && (AI.operand_count <= max))
  2597. assemblez_instruction(&AI);
  2598. else error_flag = TRUE;
  2599. if (error_flag)
  2600. { make_opcode_syntax_z(O);
  2601. error_named("Assembly mistake: syntax is",
  2602. opcode_syntax_string);
  2603. }
  2604. }
  2605. static assembly_operand parse_operand_g(void)
  2606. { assembly_operand AO;
  2607. AO = parse_expression(ASSEMBLY_CONTEXT);
  2608. if (AO.type == EXPRESSION_OT)
  2609. { ebf_error("variable or constant", "expression");
  2610. AO.type = CONSTANT_OT;
  2611. }
  2612. return(AO);
  2613. }
  2614. static void parse_assembly_g(void)
  2615. {
  2616. opcodeg O;
  2617. assembly_operand AO;
  2618. int error_flag = FALSE, is_macro = FALSE;
  2619. AI.operand_count = 0;
  2620. opcode_names.enabled = TRUE;
  2621. opcode_macros.enabled = TRUE;
  2622. get_next_token();
  2623. opcode_names.enabled = FALSE;
  2624. opcode_macros.enabled = FALSE;
  2625. if (token_type == DQ_TT) {
  2626. char *cx;
  2627. int badflags;
  2628. AI.internal_number = -1;
  2629. /* The format is @"FlagsCount:Code". Flags (which are optional)
  2630. can include "S" for store, "SS" for two stores, "B" for branch
  2631. format, "R" if execution never continues after the opcode. The
  2632. Count is the number of arguments (currently limited to 0-9),
  2633. and the Code is a decimal integer representing the opcode
  2634. number.
  2635. So: @"S3:123" for a three-argument opcode (load, load, store)
  2636. whose opcode number is (decimal) 123. Or: @"2:234" for a
  2637. two-argument opcode (load, load) whose number is 234. */
  2638. custom_opcode_g.name = (uchar *) token_text;
  2639. custom_opcode_g.flags = 0;
  2640. custom_opcode_g.op_rules = 0;
  2641. custom_opcode_g.no = 0;
  2642. badflags = FALSE;
  2643. for (cx = token_text; *cx && *cx != ':'; cx++) {
  2644. if (badflags)
  2645. continue;
  2646. switch (*cx) {
  2647. case 'S':
  2648. if (custom_opcode_g.flags & St)
  2649. custom_opcode_g.flags |= St2;
  2650. else
  2651. custom_opcode_g.flags |= St;
  2652. break;
  2653. case 'B':
  2654. custom_opcode_g.flags |= Br;
  2655. break;
  2656. case 'R':
  2657. custom_opcode_g.flags |= Rf;
  2658. break;
  2659. default:
  2660. if (isdigit(*cx)) {
  2661. custom_opcode_g.no = (*cx) - '0';
  2662. break;
  2663. }
  2664. badflags = TRUE;
  2665. error("Unknown custom opcode flag: options are B (branch), \
  2666. S (store), SS (two stores), R (execution never continues)");
  2667. break;
  2668. }
  2669. }
  2670. if (*cx != ':') {
  2671. error("Custom opcode must have colon");
  2672. }
  2673. else {
  2674. cx++;
  2675. if (!(*cx))
  2676. error("Custom opcode must have colon followed by opcode number");
  2677. else
  2678. custom_opcode_g.code = atoi(cx);
  2679. }
  2680. O = custom_opcode_g;
  2681. }
  2682. else {
  2683. if (token_type != OPCODE_NAME_TT && token_type != OPCODE_MACRO_TT) {
  2684. ebf_error("an opcode name", token_text);
  2685. panic_mode_error_recovery();
  2686. return;
  2687. }
  2688. AI.internal_number = token_value;
  2689. if (token_type == OPCODE_MACRO_TT) {
  2690. O = internal_number_to_opmacro_g(AI.internal_number);
  2691. is_macro = TRUE;
  2692. }
  2693. else
  2694. O = internal_number_to_opcode_g(AI.internal_number);
  2695. }
  2696. return_sp_as_variable = TRUE;
  2697. while (1) {
  2698. get_next_token();
  2699. if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
  2700. break;
  2701. if (AI.operand_count == 8) {
  2702. error("No assembly instruction may have more than 8 operands");
  2703. panic_mode_error_recovery();
  2704. break;
  2705. }
  2706. if ((O.flags & Br) && (AI.operand_count == O.no-1)) {
  2707. if (!((token_type == SEP_TT) && (token_value == BRANCH_SEP))) {
  2708. error_flag = TRUE;
  2709. error("Branch opcode must have '?' label");
  2710. put_token_back();
  2711. }
  2712. AO.type = CONSTANT_OT;
  2713. AO.value = parse_label();
  2714. AO.marker = BRANCH_MV;
  2715. }
  2716. else {
  2717. put_token_back();
  2718. AO = parse_operand_g();
  2719. }
  2720. AI.operand[AI.operand_count] = AO;
  2721. AI.operand_count++;
  2722. }
  2723. return_sp_as_variable = FALSE;
  2724. if (O.no != AI.operand_count) {
  2725. error_flag = TRUE;
  2726. }
  2727. if (!error_flag) {
  2728. if (is_macro)
  2729. assembleg_macro(&AI);
  2730. else
  2731. assembleg_instruction(&AI);
  2732. }
  2733. if (error_flag) {
  2734. make_opcode_syntax_g(O);
  2735. error_named("Assembly mistake: syntax is",
  2736. opcode_syntax_string);
  2737. }
  2738. }
  2739. extern void parse_assembly(void)
  2740. {
  2741. if (!glulx_mode)
  2742. parse_assembly_z();
  2743. else
  2744. parse_assembly_g();
  2745. }
  2746. /* ========================================================================= */
  2747. /* Data structure management routines */
  2748. /* ------------------------------------------------------------------------- */
  2749. extern void asm_begin_pass(void)
  2750. { no_instructions = 0;
  2751. zmachine_pc = 0;
  2752. no_sequence_points = 0;
  2753. next_label = 0;
  2754. next_sequence_point = 0;
  2755. zcode_ha_size = 0;
  2756. }
  2757. extern void init_asm_vars(void)
  2758. { int i;
  2759. for (i=0;i<16;i++) flags2_requirements[i]=0;
  2760. uses_unicode_features = FALSE;
  2761. uses_memheap_features = FALSE;
  2762. uses_acceleration_features = FALSE;
  2763. uses_float_features = FALSE;
  2764. sequence_point_follows = TRUE;
  2765. label_moved_error_already_given = FALSE;
  2766. initialise_memory_block(&zcode_area);
  2767. }
  2768. extern void asm_allocate_arrays(void)
  2769. { if ((debugfile_switch) && (MAX_LABELS < 2000)) MAX_LABELS = 2000;
  2770. variable_tokens = my_calloc(sizeof(int32),
  2771. MAX_LOCAL_VARIABLES+MAX_GLOBAL_VARIABLES, "variable tokens");
  2772. variable_usage = my_calloc(sizeof(int),
  2773. MAX_LOCAL_VARIABLES+MAX_GLOBAL_VARIABLES, "variable usage");
  2774. label_offsets = my_calloc(sizeof(int32), MAX_LABELS, "label offsets");
  2775. label_symbols = my_calloc(sizeof(int32), MAX_LABELS, "label symbols");
  2776. label_next = my_calloc(sizeof(int), MAX_LABELS, "label dll 1");
  2777. label_prev = my_calloc(sizeof(int), MAX_LABELS, "label dll 1");
  2778. sequence_point_labels
  2779. = my_calloc(sizeof(int), MAX_LABELS, "sequence point labels");
  2780. sequence_point_locations
  2781. = my_calloc(sizeof(debug_location),
  2782. MAX_LABELS,
  2783. "sequence point locations");
  2784. zcode_holding_area = my_malloc(MAX_ZCODE_SIZE,"compiled routine code area");
  2785. zcode_markers = my_malloc(MAX_ZCODE_SIZE, "compiled routine code area");
  2786. named_routine_symbols
  2787. = my_calloc(sizeof(int32), MAX_SYMBOLS, "named routine symbols");
  2788. }
  2789. extern void asm_free_arrays(void)
  2790. {
  2791. my_free(&variable_tokens, "variable tokens");
  2792. my_free(&variable_usage, "variable usage");
  2793. my_free(&label_offsets, "label offsets");
  2794. my_free(&label_symbols, "label symbols");
  2795. my_free(&label_next, "label dll 1");
  2796. my_free(&label_prev, "label dll 2");
  2797. my_free(&sequence_point_labels, "sequence point labels");
  2798. my_free(&sequence_point_locations, "sequence point locations");
  2799. my_free(&zcode_holding_area, "compiled routine code area");
  2800. my_free(&zcode_markers, "compiled routine code markers");
  2801. my_free(&named_routine_symbols, "named routine symbols");
  2802. deallocate_memory_block(&zcode_area);
  2803. }
  2804. /* ========================================================================= */