-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathasm.c
3768 lines (3364 loc) · 137 KB
/
asm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------------------------------------------------------------------- */
/* "asm" : The Inform assembler */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
static uchar *zcode_holding_area; /* Area holding code yet to be transferred
to either zcode_area or temp file no 1 */
static memory_list zcode_holding_area_memlist;
static uchar *zcode_markers; /* Bytes holding marker values for this
code */
static memory_list zcode_markers_memlist;
static int zcode_ha_size; /* Number of bytes in holding area */
uchar *zcode_area; /* Array to hold assembled code */
memory_list zcode_area_memlist; /* Manages zcode_area */
int32 zmachine_pc; /* PC position of assembly (byte offset
from start of Z-code area) */
int32 no_instructions; /* Number of instructions assembled */
int execution_never_reaches_here; /* nonzero if the current PC value in the
code area cannot be reached: e.g. if
the previous instruction was a "quit"
opcode and no label is set to here
(see EXECSTATE flags for more) */
int next_label, /* Used to count the labels created all
over Inform in current routine, from 0 */
next_sequence_point; /* Likewise, for sequence points */
int no_sequence_points; /* Total over all routines; kept for
statistics purposes only */
static int label_moved_error_already_given;
/* When one label has moved, all subsequent
ones probably have too, and this flag
suppresses the runaway chain of error
messages which would otherwise result */
int sequence_point_follows; /* Will the next instruction assembled */
/* be at a sequence point in the routine? */
int uses_unicode_features; /* Makes use of Glulx Unicode (3.0)
features? */
int uses_memheap_features; /* Makes use of Glulx mem/heap (3.1)
features? */
int uses_acceleration_features; /* Makes use of Glulx acceleration (3.1.1)
features? */
int uses_float_features; /* Makes use of Glulx floating-point (3.1.2)
features? */
int uses_extundo_features; /* Makes use of Glulx extended undo (3.1.3)
features? */
int uses_double_features; /* Makes use of Glulx double-prec (3.1.3)
features? */
debug_location statement_debug_location;
/* Location of current statement */
variableinfo *variables; /* The allocated size is
(MAX_LOCAL_VARIABLES + no_globals).
Local variables first, then globals. */
memory_list variables_memlist;
assembly_instruction AI; /* A structure used to hold the full
specification of a single Z-code
instruction: effectively this is the
input to the routine
assemble_instruction() */
static char opcode_syntax_string[128]; /* Text buffer holding the correct
syntax for an opcode: used to produce
helpful assembler error messages */
static int routine_symbol; /* The symbol index of the routine currently
being compiled */
static memory_list current_routine_name; /* The name of the routine currently
being compiled. (This may not be a
simple symbol, e.g. for an "obj.prop"
property routine.) */
static int32 routine_start_pc;
int32 *named_routine_symbols; /* Allocated up to no_named_routines */
static memory_list named_routine_symbols_memlist;
static void transfer_routine_z(void);
static void transfer_routine_g(void);
/* ------------------------------------------------------------------------- */
/* Label data */
/* ------------------------------------------------------------------------- */
static labelinfo *labels; /* Label offsets (i.e. zmachine_pc values).
These are allocated sequentially, but accessed
as a double-linked list from first_label
to last_label (in PC order). */
static memory_list labels_memlist;
static int first_label, last_label;
static int *labeluse; /* Flags indicating whether a given label has been
used as a branch target yet. */
static memory_list labeluse_memlist;
static int labeluse_size; /* Entries up to here are initialized */
static sequencepointinfo *sequence_points; /* Allocated to next_sequence_point.
Only used when debugfile_switch
is set. */
static memory_list sequence_points_memlist;
/* ------------------------------------------------------------------------- */
/* Label management */
/* ------------------------------------------------------------------------- */
/* The stripping of unreachable code requires a bit of explanation.
As we compile a function, we update the execution_never_reaches_here
flag to reflect whether the current line is reachable. If the flag
is set (EXECSTATE_UNREACHABLE), we skip generating opcodes,
compiling strings, and so on. (See assemblez_instruction(),
assembleg_instruction(), and compile_string() for these checks.)
If we're *between* functions, the execution_never_reaches_here flag
is always clear (EXECSTATE_REACHABLE), so global strings are
compiled correctly.
In general, every time we compile a JUMP or RETURN opcode, the flag
gets set. Every time we compile a label, the flag gets cleared.
This makes sense if you consider a common "while" loop:
while (true) {
...
if (flag) break;
...
}
...
This is compiled as:
.TopLabel;
...
@jnz flag ?ExitLabel;
...
@jump TopLabel;
.ExitLabel;
...
Code after an unconditional JUMP is obviously unreachable. But the
next thing that happens is the .ExitLabel, which is the target of a
branch, so the next line is reachable again.
However, if the unreachable flag is set when we *begin* a statement
(or braced block of statements), we can get tougher. We set the
EXECSTATE_ENTIRE flag for the entire duration of the statement or
block. This flag cannot be cleared by compiling labels. An example
of this:
if (DOSTUFF) {
while (true) {
if (flag) break;
}
}
If the DOSTUFF constant is false, the entire "while" loop is definitely
unreachable. So we should skip .TopLabel, .ExitLabel, and everything
in between. To ensure this, we set EXECSTATE_ENTIRE upon entering the
"if {...}" braced block, and reset it upon leaving.
(See parse_code_block() and parse_statement() for the (slightly fugly)
bit-fidding that accomplishes this.)
As an added optimization, some labels are known to be "forward"; they
are only reached by forward jumps. (.ExitLabel above is an example.)
If we reach a forward label and nothing has in fact jumped there,
the label is dead and we can skip it. (And thus also skip clearing
the unreachable flag!)
To understand *that*, consider a "while true" loop with no "break":
while (true) {
...
if (flag) return;
...
}
This never branches to .ExitLabel. So when we reach .ExitLabel,
we can say for sure that *nothing* branches there. So we skip
compiling that label. The unreachable flag is left set (because we
just finished the jump to .TopLabel). Thus, any code following the
entire loop is known to be unreachable, and we can righteously
complain about it.
(In contrast, .TopLabel cannot be skipped because it might be the
target of a *backwards* branch later on. In fact there might be
several -- any "continue" in the loop will jump to .TopLabel.)
*/
/* Set the position of the given label. The offset will be the current
zmachine_pc, or -1 if the label is definitely unused.
This adds the label to a linked list (via first_label, last_label).
The linked list must be in increasing PC order. We know this will
be true because we call this as we run through the function, so
zmachine_pc always increases.
(It won't necessarily be in *label index* order, though.)
*/
static void set_label_offset(int label, int32 offset)
{
ensure_memory_list_available(&labels_memlist, label+1);
labels[label].offset = offset;
labels[label].symbol = -1;
if (offset < 0) {
/* Mark this label as invalid and don't put it in the linked list. */
labels[label].prev = -1;
labels[label].next = -1;
return;
}
if (last_label == -1)
{ labels[label].prev = -1;
first_label = label;
}
else
{ labels[label].prev = last_label;
labels[last_label].next = label;
}
last_label = label;
labels[label].next = -1;
}
/* Set a flag indicating that the given label has been jumped to. */
static void mark_label_used(int label)
{
if (label < 0)
return;
/* Entries from 0 to labeluse_size have meaningful values.
If we have to increase labeluse_size, initialize the new
entries to FALSE. */
ensure_memory_list_available(&labeluse_memlist, label+1);
for (; labeluse_size < label+1; labeluse_size++) {
labeluse[labeluse_size] = FALSE;
}
labeluse[label] = TRUE;
}
/* ------------------------------------------------------------------------- */
/* Useful tool for building operands */
/* ------------------------------------------------------------------------- */
extern void set_constant_ot(assembly_operand *AO)
{
if (!glulx_mode) {
if (AO->value >= 0 && AO->value <= 255)
AO->type = SHORT_CONSTANT_OT;
else
AO->type = LONG_CONSTANT_OT;
}
else {
if (AO->value == 0)
AO->type = ZEROCONSTANT_OT;
else if (AO->value >= -0x80 && AO->value < 0x80)
AO->type = BYTECONSTANT_OT;
else if (AO->value >= -0x8000 && AO->value < 0x8000)
AO->type = HALFCONSTANT_OT;
else
AO->type = CONSTANT_OT;
}
}
extern int is_constant_ot(int otval)
{
if (!glulx_mode) {
return ((otval == LONG_CONSTANT_OT)
|| (otval == SHORT_CONSTANT_OT));
}
else {
return ((otval == CONSTANT_OT)
|| (otval == HALFCONSTANT_OT)
|| (otval == BYTECONSTANT_OT)
|| (otval == ZEROCONSTANT_OT));
}
}
extern int is_variable_ot(int otval)
{
if (!glulx_mode) {
return (otval == VARIABLE_OT);
}
else {
return ((otval == LOCALVAR_OT)
|| (otval == GLOBALVAR_OT));
}
}
/* ------------------------------------------------------------------------- */
/* Used in printing assembly traces */
/* ------------------------------------------------------------------------- */
extern char *variable_name(int32 i)
{
if (i==0) return("sp");
if (i<MAX_LOCAL_VARIABLES) return get_local_variable_name(i-1);
if (!glulx_mode) {
if (i == globalv_z_temp_var1) return("TEMP1");
if (i == globalv_z_temp_var2) return("TEMP2");
if (i == globalv_z_temp_var3) return("TEMP3");
if (i == globalv_z_temp_var4) return("TEMP4");
if (i == globalv_z_self) return("self");
if (i == globalv_z_sender) return("sender");
if (i == globalv_z_sw__var) return("sw__var");
if (i >= 256 && i < 286)
{ if (i - 256 < NUMBER_SYSTEM_FUNCTIONS) return system_functions.keywords[i - 256];
return "<unnamed system function>";
}
}
else {
switch (i - MAX_LOCAL_VARIABLES) {
case 0: return "temp_global";
case 1: return "temp__global2";
case 2: return "temp__global3";
case 3: return "temp__global4";
case 4: return "self";
case 5: return "sender";
case 6: return "sw__var";
case 7: return "sys__glob0";
case 8: return "sys__glob1";
case 9: return "sys__glob2";
case 10: return "sys_statusline_flag";
}
}
return (symbols[variables[i].token].name);
}
/* Print symbolic information about the AO, if there is any. */
static void print_operand_annotation(const assembly_operand *o)
{
int any = FALSE;
if (o->marker) {
printf((!any) ? " (" : ": ");
any = TRUE;
printf("%s", describe_mv(o->marker));
switch (o->marker) {
case VROUTINE_MV:
printf(": %s", veneer_routine_name(o->value));
break;
case INCON_MV:
printf(": %s", name_of_system_constant(o->value));
break;
case DWORD_MV:
printf(": '");
print_dict_word(o->value);
printf("'");
break;
}
}
if (o->symindex >= 0 && o->symindex < no_symbols) {
printf((!any) ? " (" : ": ");
any = TRUE;
printf("%s", symbols[o->symindex].name);
}
if (any) printf(")");
}
static void print_operand_z(const assembly_operand *o, int annotate)
{ switch(o->type)
{ case EXPRESSION_OT: printf("expr_"); break;
case LONG_CONSTANT_OT: printf("long_"); break;
case SHORT_CONSTANT_OT: printf("short_"); break;
case VARIABLE_OT:
if (o->value==0) { printf("sp"); return; }
printf("%s", variable_name(o->value)); return;
case OMITTED_OT: printf("<no value>"); return;
}
printf("%d", o->value);
if (annotate)
print_operand_annotation(o);
}
static void print_operand_g(const assembly_operand *o, int annotate)
{
switch (o->type) {
case EXPRESSION_OT: printf("expr_"); break;
case CONSTANT_OT: printf("long_"); break;
case HALFCONSTANT_OT: printf("short_"); break;
case BYTECONSTANT_OT: printf("byte_"); break;
case ZEROCONSTANT_OT: printf("zero_"); return;
case DEREFERENCE_OT: printf("*"); break;
case GLOBALVAR_OT:
printf("global_%d (%s)", o->value, variable_name(o->value));
return;
case LOCALVAR_OT:
if (o->value == 0)
printf("stackptr");
else
printf("local_%d (%s)", o->value-1, variable_name(o->value));
return;
case SYSFUN_OT:
if (o->value >= 0 && o->value < NUMBER_SYSTEM_FUNCTIONS)
printf("%s", system_functions.keywords[o->value]);
else
printf("<unnamed system function>");
return;
case OMITTED_OT: printf("<no value>"); return;
default: printf("???_"); break;
}
printf("%d", o->value);
if (annotate)
print_operand_annotation(o);
}
extern void print_operand(const assembly_operand *o, int annotate)
{
if (!glulx_mode)
print_operand_z(o, annotate);
else
print_operand_g(o, annotate);
}
/* ------------------------------------------------------------------------- */
/* Writing bytes to the code area */
/* ------------------------------------------------------------------------- */
static void byteout(int32 i, int mv)
{
ensure_memory_list_available(&zcode_markers_memlist, zcode_ha_size+1);
ensure_memory_list_available(&zcode_holding_area_memlist, zcode_ha_size+1);
zcode_markers[zcode_ha_size] = (uchar) mv;
zcode_holding_area[zcode_ha_size++] = (uchar) i;
zmachine_pc++;
}
/* ------------------------------------------------------------------------- */
/* A database of the 115 canonical Infocom opcodes in Versions 3 to 6 */
/* And of the however-many-there-are Glulx opcodes */
/* ------------------------------------------------------------------------- */
typedef struct opcodez
{ uchar *name; /* Lower case standard name */
int version1; /* Valid from this version number... */
int version2; /* ...until this one (or forever if this is 0) */
int extension; /* In later versions, see this line in extension table:
if -1, the opcode is illegal in later versions */
int code; /* Opcode number within its operand-number block */
int flags; /* Flags (see below) */
int op_rules; /* Any unusual operand rule applying (see below) */
int flags2_set; /* If not zero, set this bit in Flags 2 in the header
of any game using the opcode */
int no; /* Number of operands (see below) */
} opcodez;
typedef struct opcodeg
{ uchar *name; /* Lower case standard name */
int32 code; /* Opcode number */
int flags; /* Flags (see below) */
int op_rules; /* Any unusual operand rule applying (see below) */
int no; /* Number of operands */
} opcodeg;
/* Flags which can be set */
#define St 1 /* Store */
#define Br 2 /* Branch */
#define Rf 4 /* "Return flag": execution never continues after this
opcode (e.g., is a return or unconditional jump) */
#define St2 8 /* Store2 (second-to-last operand is store (Glulx)) */
/* Codes for any unusual operand assembly rules */
/* Z-code: */
#define VARIAB 1 /* First operand expected to be a variable name and
assembled to a short constant: the variable number */
#define TEXT 2 /* One text operand, to be Z-encoded into the program */
#define LABEL 3 /* One operand, a label, given as long constant offset */
#define CALL 4 /* First operand is name of a routine, to be assembled
as long constant (the routine's packed address):
as if the name were prefixed by #r$ */
/* Glulx: (bit flags for Glulx VM features) */
#define GOP_Unicode 1 /* uses_unicode_features */
#define GOP_MemHeap 2 /* uses_memheap_features */
#define GOP_Acceleration 4 /* uses_acceleration_features */
#define GOP_Float 8 /* uses_float_features */
#define GOP_ExtUndo 16 /* uses_extundo_features */
#define GOP_Double 32 /* uses_double_features */
/* Codes for the number of operands */
#define TWO 1 /* 2 (with certain types of operand, compiled as VAR) */
#define VAR 2 /* 0 to 4 */
#define VAR_LONG 3 /* 0 to 8 */
#define ONE 4 /* 1 */
#define ZERO 5 /* 0 */
#define EXT 6 /* Extended opcode set VAR: 0 to 4 */
#define EXT_LONG 7 /* Extended: 0 to 8 (not used by the canonical opcodes) */
static opcodez opcodes_table_z[] =
{
/* Opcodes introduced in Version 3 */
/* 0 */ { (uchar *) "je", 3, 0, -1, 0x01, Br, 0, 0, TWO },
/* 1 */ { (uchar *) "jl", 3, 0, -1, 0x02, Br, 0, 0, TWO },
/* 2 */ { (uchar *) "jg", 3, 0, -1, 0x03, Br, 0, 0, TWO },
/* 3 */ { (uchar *) "dec_chk", 3, 0, -1, 0x04, Br, VARIAB, 0, TWO },
/* 4 */ { (uchar *) "inc_chk", 3, 0, -1, 0x05, Br, VARIAB, 0, TWO },
/* 5 */ { (uchar *) "jin", 3, 0, -1, 0x06, Br, 0, 0, TWO },
/* 6 */ { (uchar *) "test", 3, 0, -1, 0x07, Br, 0, 0, TWO },
/* 7 */ { (uchar *) "or", 3, 0, -1, 0x08, St, 0, 0, TWO },
/* 8 */ { (uchar *) "and", 3, 0, -1, 0x09, St, 0, 0, TWO },
/* 9 */ { (uchar *) "test_attr", 3, 0, -1, 0x0A, Br, 0, 0, TWO },
/* 10 */ {(uchar *) "set_attr", 3, 0, -1, 0x0B, 0, 0, 0, TWO },
/* 11 */ {(uchar *) "clear_attr", 3, 0, -1, 0x0C, 0, 0, 0, TWO },
/* 12 */ {(uchar *) "store", 3, 0, -1, 0x0D, 0, VARIAB, 0, TWO },
/* 13 */ {(uchar *) "insert_obj", 3, 0, -1, 0x0E, 0, 0, 0, TWO },
/* 14 */ {(uchar *) "loadw", 3, 0, -1, 0x0F, St, 0, 0, TWO },
/* 15 */ {(uchar *) "loadb", 3, 0, -1, 0x10, St, 0, 0, TWO },
/* 16 */ {(uchar *) "get_prop", 3, 0, -1, 0x11, St, 0, 0, TWO },
/* 17 */ {(uchar *) "get_prop_addr", 3, 0, -1, 0x12, St, 0, 0, TWO },
/* 18 */ {(uchar *) "get_next_prop", 3, 0, -1, 0x13, St, 0, 0, TWO },
/* 19 */ {(uchar *) "add", 3, 0, -1, 0x14, St, 0, 0, TWO },
/* 20 */ {(uchar *) "sub", 3, 0, -1, 0x15, St, 0, 0, TWO },
/* 21 */ {(uchar *) "mul", 3, 0, -1, 0x16, St, 0, 0, TWO },
/* 22 */ {(uchar *) "div", 3, 0, -1, 0x17, St, 0, 0, TWO },
/* 23 */ {(uchar *) "mod", 3, 0, -1, 0x18, St, 0, 0, TWO },
/* 24 */ {(uchar *) "call", 3, 0, -1, 0x20, St, CALL, 0, VAR },
/* 25 */ {(uchar *) "storew", 3, 0, -1, 0x21, 0, 0, 0, VAR },
/* 26 */ {(uchar *) "storeb", 3, 0, -1, 0x22, 0, 0, 0, VAR },
/* 27 */ {(uchar *) "put_prop", 3, 0, -1, 0x23, 0, 0, 0, VAR },
/* This is the version of "read" called "sread" internally: */
/* 28 */ {(uchar *) "read", 3, 0, -1, 0x24, 0, 0, 0, VAR },
/* 29 */ {(uchar *) "print_char", 3, 0, -1, 0x25, 0, 0, 0, VAR },
/* 30 */ {(uchar *) "print_num", 3, 0, -1, 0x26, 0, 0, 0, VAR },
/* 31 */ {(uchar *) "random", 3, 0, -1, 0x27, St, 0, 0, VAR },
/* 32 */ {(uchar *) "push", 3, 0, -1, 0x28, 0, 0, 0, VAR },
/* 33 */ {(uchar *) "pull", 3, 5, 6, 0x29, 0, VARIAB, 0, VAR },
/* 34 */ {(uchar *) "split_window", 3, 0, -1, 0x2A, 0, 0, 0, VAR },
/* 35 */ {(uchar *) "set_window", 3, 0, -1, 0x2B, 0, 0, 0, VAR },
/* 36 */ {(uchar *) "output_stream", 3, 0, -1, 0x33, 0, 0, 0, VAR },
/* 37 */ {(uchar *) "input_stream", 3, 0, -1, 0x34, 0, 0, 0, VAR },
/* 38 */ {(uchar *) "sound_effect", 3, 0, -1, 0x35, 0, 0, 7, VAR },
/* 39 */ {(uchar *) "jz", 3, 0, -1, 0x00, Br, 0, 0, ONE },
/* 40 */ {(uchar *) "get_sibling", 3, 0, -1, 0x01, St+Br, 0, 0, ONE },
/* 41 */ {(uchar *) "get_child", 3, 0, -1, 0x02, St+Br, 0, 0, ONE },
/* 42 */ {(uchar *) "get_parent", 3, 0, -1, 0x03, St, 0, 0, ONE },
/* 43 */ {(uchar *) "get_prop_len", 3, 0, -1, 0x04, St, 0, 0, ONE },
/* 44 */ {(uchar *) "inc", 3, 0, -1, 0x05, 0, VARIAB, 0, ONE },
/* 45 */ {(uchar *) "dec", 3, 0, -1, 0x06, 0, VARIAB, 0, ONE },
/* 46 */ {(uchar *) "print_addr", 3, 0, -1, 0x07, 0, 0, 0, ONE },
/* 47 */ {(uchar *) "remove_obj", 3, 0, -1, 0x09, 0, 0, 0, ONE },
/* 48 */ {(uchar *) "print_obj", 3, 0, -1, 0x0A, 0, 0, 0, ONE },
/* 49 */ {(uchar *) "ret", 3, 0, -1, 0x0B, Rf, 0, 0, ONE },
/* 50 */ {(uchar *) "jump", 3, 0, -1, 0x0C, Rf, LABEL, 0, ONE },
/* 51 */ {(uchar *) "print_paddr", 3, 0, -1, 0x0D, 0, 0, 0, ONE },
/* 52 */ {(uchar *) "load", 3, 0, -1, 0x0E, St, VARIAB, 0, ONE },
/* 53 */ {(uchar *) "not", 3, 3, 0, 0x0F, St, 0, 0, ONE },
/* 54 */ {(uchar *) "rtrue", 3, 0, -1, 0x00, Rf, 0, 0,ZERO },
/* 55 */ {(uchar *) "rfalse", 3, 0, -1, 0x01, Rf, 0, 0,ZERO },
/* 56 */ {(uchar *) "print", 3, 0, -1, 0x02, 0, TEXT, 0,ZERO },
/* 57 */ {(uchar *) "print_ret", 3, 0, -1, 0x03, Rf, TEXT, 0,ZERO },
/* 58 */ {(uchar *) "nop", 3, 0, -1, 0x04, 0, 0, 0,ZERO },
/* 59 */ {(uchar *) "save", 3, 3, 1, 0x05, Br, 0, 0,ZERO },
/* 60 */ {(uchar *) "restore", 3, 3, 2, 0x06, Br, 0, 0,ZERO },
/* 61 */ {(uchar *) "restart", 3, 0, -1, 0x07, 0, 0, 0,ZERO },
/* 62 */ {(uchar *) "ret_popped", 3, 0, -1, 0x08, Rf, 0, 0,ZERO },
/* 63 */ {(uchar *) "pop", 3, 4, -1, 0x09, 0, 0, 0,ZERO },
/* 64 */ {(uchar *) "quit", 3, 0, -1, 0x0A, Rf, 0, 0,ZERO },
/* 65 */ {(uchar *) "new_line", 3, 0, -1, 0x0B, 0, 0, 0,ZERO },
/* 66 */ {(uchar *) "show_status", 3, 3, -1, 0x0C, 0, 0, 0,ZERO },
/* 67 */ {(uchar *) "verify", 3, 0, -1, 0x0D, Br, 0, 0,ZERO },
/* Opcodes introduced in Version 4 */
/* 68 */ {(uchar *) "call_2s", 4, 0, -1, 0x19, St, CALL, 0, TWO },
/* 69 */ {(uchar *) "call_vs", 4, 0, -1, 0x20, St, CALL, 0, VAR },
/* This is the version of "read" called "aread" internally: */
/* 70 */ {(uchar *) "read", 4, 0, -1, 0x24, St, 0, 0, VAR },
/* 71 */ {(uchar *) "call_vs2", 4, 0, -1, 0x2C, St, CALL, 0,
VAR_LONG },
/* 72 */ {(uchar *) "erase_window", 4, 0, -1, 0x2D, 0, 0, 0, VAR },
/* 73 */ {(uchar *) "erase_line", 4, 0, -1, 0x2E, 0, 0, 0, VAR },
/* 74 */ {(uchar *) "set_cursor", 4, 0, -1, 0x2F, 0, 0, 0, VAR },
/* 75 */ {(uchar *) "get_cursor", 4, 0, -1, 0x30, 0, 0, 0, VAR },
/* 76 */ {(uchar *) "set_text_style", 4, 0, -1, 0x31, 0, 0, 0, VAR },
/* 77 */ {(uchar *) "buffer_mode", 4, 0, -1, 0x32, 0, 0, 0, VAR },
/* 78 */ {(uchar *) "read_char", 4, 0, -1, 0x36, St, 0, 0, VAR },
/* 79 */ {(uchar *) "scan_table", 4, 0, -1, 0x37, St+Br, 0, 0, VAR },
/* 80 */ {(uchar *) "call_1s", 4, 0, -1, 0x08, St, CALL, 0, ONE },
/* Opcodes introduced in Version 5 */
/* 81 */ {(uchar *) "call_2n", 5, 0, -1, 0x1a, 0, CALL, 0, TWO },
/* 82 */ {(uchar *) "set_colour", 5, 0, -1, 0x1b, 0, 0, 6, TWO },
/* 83 */ {(uchar *) "throw", 5, 0, -1, 0x1c, 0, 0, 0, TWO },
/* 84 */ {(uchar *) "call_vn", 5, 0, -1, 0x39, 0, CALL, 0, VAR },
/* 85 */ {(uchar *) "call_vn2", 5, 0, -1, 0x3a, 0, CALL, 0,
VAR_LONG },
/* 86 */ {(uchar *) "tokenise", 5, 0, -1, 0x3b, 0, 0, 0, VAR },
/* 87 */ {(uchar *) "encode_text", 5, 0, -1, 0x3c, 0, 0, 0, VAR },
/* 88 */ {(uchar *) "copy_table", 5, 0, -1, 0x3d, 0, 0, 0, VAR },
/* 89 */ {(uchar *) "print_table", 5, 0, -1, 0x3e, 0, 0, 0, VAR },
/* 90 */ {(uchar *) "check_arg_count", 5, 0, -1, 0x3f, Br, 0, 0, VAR },
/* 91 */ {(uchar *) "call_1n", 5, 0, -1, 0x0F, 0, CALL, 0, ONE },
/* 92 */ {(uchar *) "catch", 5, 0, -1, 0x09, St, 0, 0, ZERO },
/* 93 */ {(uchar *) "piracy", 5, 0, -1, 0x0F, Br, 0, 0, ZERO },
/* 94 */ {(uchar *) "log_shift", 5, 0, -1, 0x02, St, 0, 0, EXT },
/* 95 */ {(uchar *) "art_shift", 5, 0, -1, 0x03, St, 0, 0, EXT },
/* 96 */ {(uchar *) "set_font", 5, 0, -1, 0x04, St, 0, 0, EXT },
/* 97 */ {(uchar *) "save_undo", 5, 0, -1, 0x09, St, 0, 4, EXT },
/* 98 */ {(uchar *) "restore_undo", 5, 0, -1, 0x0A, St, 0, 4, EXT },
/* Opcodes introduced in Version 6 */
/* 99 */ { (uchar *) "draw_picture", 6, 6, -1, 0x05, 0, 0, 3, EXT },
/* 100 */ { (uchar *) "picture_data", 6, 6, -1, 0x06, Br, 0, 3, EXT },
/* 101 */ { (uchar *) "erase_picture", 6, 6, -1, 0x07, 0, 0, 3, EXT },
/* 102 */ { (uchar *) "set_margins", 6, 6, -1, 0x08, 0, 0, 0, EXT },
/* 103 */ { (uchar *) "move_window", 6, 6, -1, 0x10, 0, 0, 0, EXT },
/* 104 */ { (uchar *) "window_size", 6, 6, -1, 0x11, 0, 0, 0, EXT },
/* 105 */ { (uchar *) "window_style", 6, 6, -1, 0x12, 0, 0, 0, EXT },
/* 106 */ { (uchar *) "get_wind_prop", 6, 6, -1, 0x13, St, 0, 0, EXT },
/* 107 */ { (uchar *) "scroll_window", 6, 6, -1, 0x14, 0, 0, 0, EXT },
/* 108 */ { (uchar *) "pop_stack", 6, 6, -1, 0x15, 0, 0, 0, EXT },
/* 109 */ { (uchar *) "read_mouse", 6, 6, -1, 0x16, 0, 0, 5, EXT },
/* 110 */ { (uchar *) "mouse_window", 6, 6, -1, 0x17, 0, 0, 5, EXT },
/* 111 */ { (uchar *) "push_stack", 6, 6, -1, 0x18, Br, 0, 0, EXT },
/* 112 */ { (uchar *) "put_wind_prop", 6, 6, -1, 0x19, 0, 0, 0, EXT },
/* 113 */ { (uchar *) "print_form", 6, 6, -1, 0x1a, 0, 0, 0, EXT },
/* 114 */ { (uchar *) "make_menu", 6, 6, -1, 0x1b, Br, 0, 8, EXT },
/* 115 */ { (uchar *) "picture_table", 6, 6, -1, 0x1c, 0, 0, 3, EXT },
/* Opcodes introduced in Z-Machine Specification Standard 1.0 */
/* 116 */ { (uchar *) "print_unicode", 5, 0, -1, 0x0b, 0, 0, 0, EXT },
/* 117 */ { (uchar *) "check_unicode", 5, 0, -1, 0x0c, St, 0, 0, EXT },
/* Opcodes introduced in Z-Machine Specification Standard 1.1 */
/* 118 */ { (uchar *) "set_true_colour", 5, 0, -1, 0x0d, 0, 0, 0, EXT },
/* 119 */ { (uchar *) "buffer_screen", 6, 6, -1, 0x1d, St, 0, 0, EXT }
};
/* Subsequent forms for opcodes whose meaning changes with version */
static opcodez extension_table_z[] =
{
/* 0 */ { (uchar *) "not", 4, 4, 3, 0x0F, St, 0, 0, ONE },
/* 1 */ { (uchar *) "save", 4, 4, 4, 0x05, St, 0, 0,ZERO },
/* 2 */ { (uchar *) "restore", 4, 4, 5, 0x06, St, 0, 0,ZERO },
/* 3 */ { (uchar *) "not", 5, 0, -1, 0x38, St, 0, 0, VAR },
/* 4 */ { (uchar *) "save", 5, 0, -1, 0x00, St, 0, 0, EXT },
/* 5 */ { (uchar *) "restore", 5, 0, -1, 0x01, St, 0, 0, EXT },
/* 6 */ { (uchar *) "pull", 6, 6, -1, 0x29, St, 0, 0, VAR }
};
static opcodez invalid_opcode_z =
{ (uchar *) "invalid", 0, 0, -1, 0xff, 0, 0, 0, ZERO};
static opcodez custom_opcode_z;
/* Note that this table assumes that all opcodes have at most two
branch-label or store operands, and that if they exist, they are the
last operands. Glulx does not actually guarantee this. But it is
true for all opcodes in the current Glulx spec, so we will assume
it for now.
Also note that Inform can only compile branches to constant offsets,
even though the Glulx machine can handle stack or memory-loaded
operands in a branch instruction.
*/
static opcodeg opcodes_table_g[] = {
{ (uchar *) "nop", 0x00, 0, 0, 0 },
{ (uchar *) "add", 0x10, St, 0, 3 },
{ (uchar *) "sub", 0x11, St, 0, 3 },
{ (uchar *) "mul", 0x12, St, 0, 3 },
{ (uchar *) "div", 0x13, St, 0, 3 },
{ (uchar *) "mod", 0x14, St, 0, 3 },
{ (uchar *) "neg", 0x15, St, 0, 2 },
{ (uchar *) "bitand", 0x18, St, 0, 3 },
{ (uchar *) "bitor", 0x19, St, 0, 3 },
{ (uchar *) "bitxor", 0x1A, St, 0, 3 },
{ (uchar *) "bitnot", 0x1B, St, 0, 2 },
{ (uchar *) "shiftl", 0x1C, St, 0, 3 },
{ (uchar *) "sshiftr", 0x1D, St, 0, 3 },
{ (uchar *) "ushiftr", 0x1E, St, 0, 3 },
{ (uchar *) "jump", 0x20, Br|Rf, 0, 1 },
{ (uchar *) "jz", 0x22, Br, 0, 2 },
{ (uchar *) "jnz", 0x23, Br, 0, 2 },
{ (uchar *) "jeq", 0x24, Br, 0, 3 },
{ (uchar *) "jne", 0x25, Br, 0, 3 },
{ (uchar *) "jlt", 0x26, Br, 0, 3 },
{ (uchar *) "jge", 0x27, Br, 0, 3 },
{ (uchar *) "jgt", 0x28, Br, 0, 3 },
{ (uchar *) "jle", 0x29, Br, 0, 3 },
{ (uchar *) "jltu", 0x2A, Br, 0, 3 },
{ (uchar *) "jgeu", 0x2B, Br, 0, 3 },
{ (uchar *) "jgtu", 0x2C, Br, 0, 3 },
{ (uchar *) "jleu", 0x2D, Br, 0, 3 },
{ (uchar *) "call", 0x30, St, 0, 3 },
{ (uchar *) "return", 0x31, Rf, 0, 1 },
{ (uchar *) "catch", 0x32, Br|St, 0, 2 },
{ (uchar *) "throw", 0x33, Rf, 0, 2 },
{ (uchar *) "tailcall", 0x34, Rf, 0, 2 },
{ (uchar *) "copy", 0x40, St, 0, 2 },
{ (uchar *) "copys", 0x41, St, 0, 2 },
{ (uchar *) "copyb", 0x42, St, 0, 2 },
{ (uchar *) "sexs", 0x44, St, 0, 2 },
{ (uchar *) "sexb", 0x45, St, 0, 2 },
{ (uchar *) "aload", 0x48, St, 0, 3 },
{ (uchar *) "aloads", 0x49, St, 0, 3 },
{ (uchar *) "aloadb", 0x4A, St, 0, 3 },
{ (uchar *) "aloadbit", 0x4B, St, 0, 3 },
{ (uchar *) "astore", 0x4C, 0, 0, 3 },
{ (uchar *) "astores", 0x4D, 0, 0, 3 },
{ (uchar *) "astoreb", 0x4E, 0, 0, 3 },
{ (uchar *) "astorebit", 0x4F, 0, 0, 3 },
{ (uchar *) "stkcount", 0x50, St, 0, 1 },
{ (uchar *) "stkpeek", 0x51, St, 0, 2 },
{ (uchar *) "stkswap", 0x52, 0, 0, 0 },
{ (uchar *) "stkroll", 0x53, 0, 0, 2 },
{ (uchar *) "stkcopy", 0x54, 0, 0, 1 },
{ (uchar *) "streamchar", 0x70, 0, 0, 1 },
{ (uchar *) "streamnum", 0x71, 0, 0, 1 },
{ (uchar *) "streamstr", 0x72, 0, 0, 1 },
{ (uchar *) "gestalt", 0x0100, St, 0, 3 },
{ (uchar *) "debugtrap", 0x0101, 0, 0, 1 },
{ (uchar *) "getmemsize", 0x0102, St, 0, 1 },
{ (uchar *) "setmemsize", 0x0103, St, 0, 2 },
{ (uchar *) "jumpabs", 0x0104, Rf, 0, 1 },
{ (uchar *) "random", 0x0110, St, 0, 2 },
{ (uchar *) "setrandom", 0x0111, 0, 0, 1 },
{ (uchar *) "quit", 0x0120, Rf, 0, 0 },
{ (uchar *) "verify", 0x0121, St, 0, 1 },
{ (uchar *) "restart", 0x0122, 0, 0, 0 },
{ (uchar *) "save", 0x0123, St, 0, 2 },
{ (uchar *) "restore", 0x0124, St, 0, 2 },
{ (uchar *) "saveundo", 0x0125, St, 0, 1 },
{ (uchar *) "restoreundo", 0x0126, St, 0, 1 },
{ (uchar *) "protect", 0x0127, 0, 0, 2 },
{ (uchar *) "glk", 0x0130, St, 0, 3 },
{ (uchar *) "getstringtbl", 0x0140, St, 0, 1 },
{ (uchar *) "setstringtbl", 0x0141, 0, 0, 1 },
{ (uchar *) "getiosys", 0x0148, St|St2, 0, 2 },
{ (uchar *) "setiosys", 0x0149, 0, 0, 2 },
{ (uchar *) "linearsearch", 0x0150, St, 0, 8 },
{ (uchar *) "binarysearch", 0x0151, St, 0, 8 },
{ (uchar *) "linkedsearch", 0x0152, St, 0, 7 },
{ (uchar *) "callf", 0x0160, St, 0, 2 },
{ (uchar *) "callfi", 0x0161, St, 0, 3 },
{ (uchar *) "callfii", 0x0162, St, 0, 4 },
{ (uchar *) "callfiii", 0x0163, St, 0, 5 },
{ (uchar *) "streamunichar", 0x73, 0, GOP_Unicode, 1 },
{ (uchar *) "mzero", 0x170, 0, GOP_MemHeap, 2 },
{ (uchar *) "mcopy", 0x171, 0, GOP_MemHeap, 3 },
{ (uchar *) "malloc", 0x178, St, GOP_MemHeap, 2 },
{ (uchar *) "mfree", 0x179, 0, GOP_MemHeap, 1 },
{ (uchar *) "accelfunc", 0x180, 0, GOP_Acceleration, 2 },
{ (uchar *) "accelparam", 0x181, 0, GOP_Acceleration, 2 },
{ (uchar *) "hasundo", 0x128, St, GOP_ExtUndo, 1 },
{ (uchar *) "discardundo",0x129, 0, GOP_ExtUndo, 0 },
{ (uchar *) "numtof", 0x190, St, GOP_Float, 2 },
{ (uchar *) "ftonumz", 0x191, St, GOP_Float, 2 },
{ (uchar *) "ftonumn", 0x192, St, GOP_Float, 2 },
{ (uchar *) "ceil", 0x198, St, GOP_Float, 2 },
{ (uchar *) "floor", 0x199, St, GOP_Float, 2 },
{ (uchar *) "fadd", 0x1A0, St, GOP_Float, 3 },
{ (uchar *) "fsub", 0x1A1, St, GOP_Float, 3 },
{ (uchar *) "fmul", 0x1A2, St, GOP_Float, 3 },
{ (uchar *) "fdiv", 0x1A3, St, GOP_Float, 3 },
{ (uchar *) "fmod", 0x1A4, St|St2, GOP_Float, 4 },
{ (uchar *) "sqrt", 0x1A8, St, GOP_Float, 2 },
{ (uchar *) "exp", 0x1A9, St, GOP_Float, 2 },
{ (uchar *) "log", 0x1AA, St, GOP_Float, 2 },
{ (uchar *) "pow", 0x1AB, St, GOP_Float, 3 },
{ (uchar *) "sin", 0x1B0, St, GOP_Float, 2 },
{ (uchar *) "cos", 0x1B1, St, GOP_Float, 2 },
{ (uchar *) "tan", 0x1B2, St, GOP_Float, 2 },
{ (uchar *) "asin", 0x1B3, St, GOP_Float, 2 },
{ (uchar *) "acos", 0x1B4, St, GOP_Float, 2 },
{ (uchar *) "atan", 0x1B5, St, GOP_Float, 2 },
{ (uchar *) "atan2", 0x1B6, St, GOP_Float, 3 },
{ (uchar *) "jfeq", 0x1C0, Br, GOP_Float, 4 },
{ (uchar *) "jfne", 0x1C1, Br, GOP_Float, 4 },
{ (uchar *) "jflt", 0x1C2, Br, GOP_Float, 3 },
{ (uchar *) "jfle", 0x1C3, Br, GOP_Float, 3 },
{ (uchar *) "jfgt", 0x1C4, Br, GOP_Float, 3 },
{ (uchar *) "jfge", 0x1C5, Br, GOP_Float, 3 },
{ (uchar *) "jisnan", 0x1C8, Br, GOP_Float, 2 },
{ (uchar *) "jisinf", 0x1C9, Br, GOP_Float, 2 },
{ (uchar *) "numtod", 0x200, St|St2, GOP_Double, 3 },
{ (uchar *) "dtonumz", 0x201, St, GOP_Double, 3 },
{ (uchar *) "dtonumn", 0x202, St, GOP_Double, 3 },
{ (uchar *) "ftod", 0x203, St|St2, GOP_Double, 3 },
{ (uchar *) "dtof", 0x204, St, GOP_Double, 3 },
{ (uchar *) "dceil", 0x208, St|St2, GOP_Double, 4 },
{ (uchar *) "dfloor", 0x209, St|St2, GOP_Double, 4 },
{ (uchar *) "dadd", 0x210, St|St2, GOP_Double, 6 },
{ (uchar *) "dsub", 0x211, St|St2, GOP_Double, 6 },
{ (uchar *) "dmul", 0x212, St|St2, GOP_Double, 6 },
{ (uchar *) "ddiv", 0x213, St|St2, GOP_Double, 6 },
{ (uchar *) "dmodr", 0x214, St|St2, GOP_Double, 6 },
{ (uchar *) "dmodq", 0x215, St|St2, GOP_Double, 6 },
{ (uchar *) "dsqrt", 0x218, St|St2, GOP_Double, 4 },
{ (uchar *) "dexp", 0x219, St|St2, GOP_Double, 4 },
{ (uchar *) "dlog", 0x21A, St|St2, GOP_Double, 4 },
{ (uchar *) "dpow", 0x21B, St|St2, GOP_Double, 6 },
{ (uchar *) "dsin", 0x220, St|St2, GOP_Double, 4 },
{ (uchar *) "dcos", 0x221, St|St2, GOP_Double, 4 },
{ (uchar *) "dtan", 0x222, St|St2, GOP_Double, 4 },
{ (uchar *) "dasin", 0x223, St|St2, GOP_Double, 4 },
{ (uchar *) "dacos", 0x224, St|St2, GOP_Double, 4 },
{ (uchar *) "datan", 0x225, St|St2, GOP_Double, 4 },
{ (uchar *) "datan2", 0x226, St|St2, GOP_Double, 6 },
{ (uchar *) "jdeq", 0x230, Br, GOP_Double, 7 },
{ (uchar *) "jdne", 0x231, Br, GOP_Double, 7 },
{ (uchar *) "jdlt", 0x232, Br, GOP_Double, 5 },
{ (uchar *) "jdle", 0x233, Br, GOP_Double, 5 },
{ (uchar *) "jdgt", 0x234, Br, GOP_Double, 5 },
{ (uchar *) "jdge", 0x235, Br, GOP_Double, 5 },
{ (uchar *) "jdisnan", 0x238, Br, GOP_Double, 3 },
{ (uchar *) "jdisinf", 0x239, Br, GOP_Double, 3 },
};
/* The opmacros table is used for fake opcodes. The opcode numbers are
ignored; this table is only used for argument parsing. */
static opcodeg opmacros_table_g[] = {
{ (uchar *) "pull", pull_gm, St, 0, 1 },
{ (uchar *) "push", push_gm, 0, 0, 1 },
{ (uchar *) "dload", dload_gm, St|St2, 0, 3 },
{ (uchar *) "dstore", dstore_gm, 0, 0, 3 },
};
static opcodeg custom_opcode_g;
static opcodez internal_number_to_opcode_z(int32 i)
{ opcodez x;
ASSERT_ZCODE();
if (i == -1) return custom_opcode_z;
x = opcodes_table_z[i];
if (instruction_set_number < x.version1) return invalid_opcode_z;
if (x.version2 == 0) return x;
if (instruction_set_number <= x.version2) return x;
i = x.extension;
if (i < 0) return invalid_opcode_z;
x = extension_table_z[i];
if (instruction_set_number < x.version1) return invalid_opcode_z;
if (x.version2 == 0) return x;
if (instruction_set_number <= x.version2) return x;
return extension_table_z[x.extension];
}
static char *opcode_name_z(int32 i)
{
if (i >= 0 && i < MAX_KEYWORD_GROUP_SIZE && opcode_names.keywords[i])
return opcode_names.keywords[i];
return "(unnamed)";
}
static void make_opcode_syntax_z(opcodez opco)
{ char *p = "", *q = opcode_syntax_string;
/* TODO: opcode_syntax_string[128] is unsafe */
sprintf(q, "%s", opco.name);
switch(opco.no)
{ case ONE: p=" <operand>"; break;
case TWO: p=" <operand1> <operand2>"; break;
case EXT:
case VAR: p=" <0 to 4 operands>"; break;
case VAR_LONG: p=" <0 to 8 operands>"; break;
}
switch(opco.op_rules)
{ case TEXT: sprintf(q+strlen(q), " <text>"); return;
case LABEL: sprintf(q+strlen(q), " <label>"); return;
case VARIAB:
sprintf(q+strlen(q), " <variable>");
/* Fall through */
case CALL:
if (opco.op_rules==CALL) sprintf(q+strlen(q), " <routine>");
switch(opco.no)
{ case ONE: p=""; break;
case TWO: p=" <operand>"; break;
case EXT:
case VAR: p=" <1 to 4 operands>"; break;
case VAR_LONG: p=" <1 to 8 operands>"; break;
}
break;
}
sprintf(q+strlen(q), "%s", p);
if ((opco.flags & St) != 0) sprintf(q+strlen(q), " -> <result-variable>");
if ((opco.flags & Br) != 0) sprintf(q+strlen(q), " ?[~]<label>");
}
static opcodeg internal_number_to_opcode_g(int32 i)
{
opcodeg x;
if (i == -1) return custom_opcode_g;
x = opcodes_table_g[i];
return x;
}
static opcodeg internal_number_to_opmacro_g(int32 i)
{
return opmacros_table_g[i];
}
static void make_opcode_syntax_g(opcodeg opco)
{
int ix;
char *cx;
char *q = opcode_syntax_string;
/* TODO: opcode_syntax_string[128] is unsafe */
sprintf(q, "%s", opco.name);
sprintf(q+strlen(q), " <%d operand%s", opco.no,
((opco.no==1) ? "" : "s"));
if (opco.no) {
cx = q+strlen(q);
strcpy(cx, ": ");
cx += strlen(cx);
for (ix=0; ix<opco.no; ix++) {
if (ix) {
*cx = ' ';
cx++;
}
if (ix == opco.no-1) {
if (opco.flags & Br) {
strcpy(cx, "Lb");
}
else if (opco.flags & St) {
strcpy(cx, "S");
}
else {
strcpy(cx, "L");
}
}
else if (ix == opco.no-2 && (opco.flags & Br) && (opco.flags & St)) {
strcpy(cx, "S");
}
else if (ix == opco.no-2 && (opco.flags & St2)) {
strcpy(cx, "S");
}
else {
strcpy(cx, "L");
}
cx += strlen(cx);
sprintf(cx, "%d", ix+1);
cx += strlen(cx);
}
}
sprintf(q+strlen(q), ">");
}
/* ========================================================================= */
/* The assembler itself does four things: */
/* */
/* assembles instructions */
/* sets label N to the current code position */
/* assembles routine headers */
/* assembles routine ends */
/* ------------------------------------------------------------------------- */
/* This is for Z-code only. */
static void write_operand(assembly_operand op)
{ int32 j;
j=op.value;
switch(op.type)
{ case LONG_CONSTANT_OT:
byteout(j/256, op.marker); byteout(j%256, 0); return;
case SHORT_CONSTANT_OT:
if (op.marker == 0)
byteout(j, 0);
else
byteout(j, 0x80 + op.marker);
return;
case VARIABLE_OT:
byteout(j, 0); return;
case CONSTANT_OT:
case HALFCONSTANT_OT:
case BYTECONSTANT_OT:
case ZEROCONSTANT_OT:
case SYSFUN_OT:
case DEREFERENCE_OT:
case LOCALVAR_OT:
case GLOBALVAR_OT:
compiler_error("Glulx OT in Z-code assembly operand.");
return;
}
}