File: porting.texi

package info (click to toggle)
lightning 1.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,020 kB
  • ctags: 3,791
  • sloc: ansic: 13,890; sh: 3,843; perl: 373; makefile: 57
file content (1606 lines) | stat: -rw-r--r-- 56,235 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
@node Structure of a port
@chapter An overview of the porting process

A particular port of @lightning{} is composed of four files. These
have a common suffix which identifies the port (for example,
@code{i386} or @code{ppc}), and a prefix that identifies their
function; they are:

@itemize @bullet
@item
@file{asm-@var{suffix}.h}, which contains the description of the
target machine's instruction format.  The creation of this file
is discussed in @ref{Run-time assemblers, , Creating the run-time
assembler}.

@item
@file{core-@var{suffix}.h}, which contains the mappings from
@lightning{}'s instruction set to the target machine's assembly
language format.  The creation of this file is discussed in
@ref{Standard macros, , Creating the platform-independent layer}.

@item
@file{funcs-@var{suffix}.h}, for now, only contains the definition
of @code{jit_flush_code}. The creation of this file is briefly
discussed in @ref{Standard functions, , More complex tasks in
the platform-independent layer}.

@item
@file{fp-@var{suffix}.h}, which contains the description of the
target machine's instruction format and the internal macros for doing
floating point computation. The creation of this file is discussed
in @ref{Floating-point macros, , Implementing macros for floating
point}.
@end itemize

Before doing anything, you have to add the ability to recognize the
new port during the configuration process.  This is explained in
@ref{Adjusting configure, , Automatically recognizing the new platform}.

@node Adjusting configure
@chapter Automatically recognizing the new platform

Before starting your port, you have to add the ability to recognize the
new port during the configure process.  You only have to run
@file{config.guess}, which you'll find in the main distribution
directory, and note down the first part of the output (up to the first
dash).

Then, in the two files @file{configure.in} and @file{lightning.m4},
lookup the line
@example
    case "$host_cpu" in
@end example

@noindent
and, right after it, add the line:
@example
    @var{cpu-name})  cpu=@var{file-suffix}           ;;
@end example

@noindent
where @var{cpu-name} is the cpu as output by @file{config.guess}, and
@var{file-suffix} is the suffix that you are going to use for your files
(@pxref{Structure of a port, , An overview of the porting process}).

Now create empty files for your new port:
@example
    touch lightning/asm-xxx.h
    touch lightning/fp-xxx.h
    touch lightning/core-xxx.h
    touch lightning/funcs-xxx.h
@end example

@noindent
and run @file{configure}, which should create the symlinks that are
needed by @code{lightning.h}.  This is important because it will allow
you to use @lightning{} (albeit in a limited way) for testing even
before the port is completed.

@node Run-time assemblers
@chapter Creating the run-time assembler

The run-time assembler is a set of macros whose purpose is to assemble
instructions for the target machine's assembly language, translating
mnemonics to machine language together with their operands.  While a
run-time assembler is not, strictly speaking, part of @lightning{}
(it is a private layer to be used while implementing the standard
macros that are ultimately used by clients), designing a run-time
assembler first allows you to think in terms of assembly language
rather than binary code (ouch!@dots{}), making it considerably easier
to write the standard macros.

Creating a run-time assembler is a tedious process rather than a
difficult one, because most of the time will be spent collecting and
copying information from the architecture's manual.

Macros defined by a run-time assembler are conventionally named after
the mnemonic and the type of its operands.  Examples took from the
SPARC's run-time assembler are @code{ADDrrr}, a macro that assembles
an @code{ADD} instruction with three register operands, and
@code{SUBCCrir}, which assembles a @code{SUBCC} instruction whose second
operand is an immediate and the remaining two are registers.

The first step in creating the assembler is to pick a convention for
operand specifiers (@code{r} and @code{i} in the example above) and for
register names.  On the SPARC, this convention is as follows

@table @code
@item @b{r}
A register name.  For every @code{r} in the macro name, a numeric
parameter @code{RR} is passed to the macro, and the operand is assembled
as @code{%r@var{RR}}.

@item @b{i}
An immediate, usually a 13-bit signed integer (with exception for
instructions such as @code{SETHI} and branches).  The macros check
the size of the passed parameter if @lightning{} is configured with
@code{--enable-assertions}.

@item @b{x}
A combination of two @code{r} parameters, which are summed to determine
the effective address in a memory load/store operation.

@item @b{m}
A combination of an @code{r} and @code{i} parameter, which are summed to
determine the effective address in a memory load/store operation.
@end table

Additional macros can be defined that provide easier access to register
names.  For example, on the SPARC, @code{_Ro(3)} and @code{_Rg(5)} map
respectively to @code{%o3} and @code{%g5}; on the x86, instead, symbolic
representations of the register names are provided (for example,
@code{_EAX} and @code{_EBX}).

CISC architectures sometimes have registers of different sizes--this is
the case on the x86 where @code{%ax} is a 16-bit register while
@code{%esp} is a 32-bit one.  In this case, it can be useful to embed
information on the size in the definition of register names.  The x86
machine language, for example, represents all three of @code{%bh},
@code{%di} and @code{%edi} as 7; but the x86 run-time assemblers defines
them with different numbers, putting the register's size in the upper
nybble (for example, @samp{17h} for @code{%bh} and @samp{27h} for
@code{%di}) so that consistency checks can be made on the operands'
sizes when @code{--enable-assertions} is used.

The next important part defines the native architecture's instruction
formats.  These can be as few as ten on RISC architectures, and as many
as fifty on CISC architectures.  In the latter case it can be useful
to define more macros for sub-formats (such as macros for different
addressing modes) or even for sub-fields in an instruction.  Let's see
an example of these macros.

@example
#define _2i( OP, RD, OP2, IMM)
        _I((_u2 (OP )<<30)  |  (_u5(RD)<<25)  |  (_u3(OP2)<<22)  |
            _u22(IMM)                                            )
@end example

The name of the macro, @code{_2i}, indicates a two-operand instruction
comprising an immediate operand.  The instruction format is:

@example
 .------.---------.------.-------------------------------------------.
 |  OP  |   RD    | OP2  |               IMM                         |
 |------+---------+------+-------------------------------------------|
 |2 bits|  5 bits |3 bits|             22 bits                       |
 |31-30 |  29-25  | 22-24|              0-21                         |
 '------'---------'------'-------------------------------------------'
@end example

@lightning{} provides macros named @code{_sXX(OP)} and @code{_uXX(OP)},
where XX is a number between 1 and 31, which test@footnote{Only when
@code{--enable-assertions} is used.} whether @code{OP} can be
represented as (respectively) a signed or unsigned integer of the
given size.  What the macro above does, then, is to shift and @sc{or}
together the different fields, ensuring that each of them fits the field.

Here is another definition, this time for the PowerPC architecture.

@example
#define _X(OP,RD,RA,RB,XO,RC)
        _I((_u6 (OP)<<26)  |  (_u5(RD)<<21)  |  (_u5(RA)<<16)  |
           ( _u5(RB)<<11)  |  (_u10(XO)<<1)  |   _u1(RC)       )
@end example

Here is the bit layout corresponding to this instruction format:

@example
 .--------.--------.--------.--------.---------------------.-------.
 |    OP  |   RD   |   RA   |   RB   |           X0        |   RC  |
 |--------+--------+--------+--------+-----------------------------|
 | 6 bits | 5 bits | 5 bits | 5 bits |         10 bits     | 1 bit |
 | 31-26  | 25-21  | 16-20  | 11-15  |         1-10        |   0   |
 '--------'---------'-------'--------'-----------------------------'
@end example

How do these macros actually generate code? The secret lies in the
@code{_I} macro, which is one of four predefined macros which actually
store machine language instructions in memory.  They are @code{_B},
@code{_W}, @code{_I} and @code{_L}, respectively for 8-bit, 16-bit,
32-bit, and @code{long} (either 32-bit or 64-bit, depending on the
architecture) values.

Next comes another set of macros (usually the biggest) which represents
the actual mnemonics---macros such as @code{ADDrrr} and @code{SUBCCrir},
which were cited earlier in this chapter, belong to this set.  Most of
the times, all these macros will do is to use the ``instruction format''
macros, specifying the values of the fields in the different instruction
formats.  Let's see a few of these definitions, again taken from the
SPARC assembler:

@example
#define BAi(DISP)                       _2   (0, 0,  8, 2, DISP)
#define BA_Ai(DISP)                     _2   (0, 1,  8, 2, DISP)

#define SETHIir(IMM, RD)                _2i  (0, RD, 4, IMM)

#define ADDrrr(RS1, RS2, RD)            _3   (2, RD,  0, RS1, 0, 0, RS2)
#define ADDrir(RS1, IMM, RD)            _3i  (2, RD,  0, RS1, 1,    IMM)
#define ADDCCrrr(RS1, RS2, RD)          _3   (2, RD, 16, RS1, 0, 0, RS2)
#define ADDCCrir(RS1, IMM, RD)          _3i  (2, RD, 16, RS1, 1,    IMM)
#define ANDrrr(RS1, RS2, RD)            _3   (2, RD,  1, RS1, 0, 0, RS2)
#define ANDrir(RS1, IMM, RD)            _3i  (2, RD,  1, RS1, 1,    IMM)
#define ANDCCrrr(RS1, RS2, RD)          _3   (2, RD, 17, RS1, 0, 0, RS2)
#define ANDCCrir(RS1, IMM, RD)          _3i  (2, RD, 17, RS1, 1,    IMM)
@end example

A few things have to be noted.  For example:
@itemize @bullet
@item
The SPARC assembly language sometimes uses a comma inside a mnemonic
(for example, @code{ba,a}).  This symbol is not allowed inside a
@sc{cpp} macro name, so it is replaced with an underscore; the same
is done with the dots found in the PowerPC assembly language (for
example, @code{andi.} is defined as @code{ANDI_rri}).

@item
It can be useful to group together instructions with the same
instruction format, as doing this tends to make the source code
more readable (numbers are put in the same columns).

@item
Using an editor without automatic wrap at end of line can be useful,
since run-time assemblers tend to have very long lines.
@end itemize

A final touch is to define the synthetic instructions, which are
usually found on RISC machines.  For example, on the SPARC, the
@code{LD} instruction has two synonyms (@code{LDUW} and @code{LDSW})
which are defined thus:

@example
#define LDUWxr(RS1, RS2, RD)            LDxr(RS1, RS2, RD)
#define LDUWmr(RS1, IMM, RD)            LDmr(RS1, IMM, RD)
#define LDSWxr(RS1, RS2, RD)            LDxr(RS1, RS2, RD)
#define LDSWmr(RS1, IMM, RD)            LDmr(RS1, IMM, RD)
@end example

Other common case are instructions which take advantage of registers
whose value is hard-wired to zero, and short-cut instructions which
hard-code some or all of the operands:

@example
@rem{/* Destination is %g0\, which the processor never overwrites. */}
#define CMPrr(R1, R2)   SUBCCrrr(R1, R2, 0) @rem{/* subcc %r1\, %r2\, %g0 */}

@rem{/* One of the source registers is hard-coded to be %g0. */}
#define NEGrr(R,S)      SUBrrr(0, R, S)     @rem{/* sub %g0\, %rR\, %rS */}

@rem{/* All of the operands are hard-coded. */}
#define RET()           JMPLmr(31,8 ,0)     @rem{/* jmpl [%r31+8]\, %g0  */}

@rem{/* One of the operands acts as both source and destination */}
#define BSETrr(R,S)     ORrrr(R, S, S)      @rem{/* or %rR\, %rS\, %rS */}
@end example

Specific to RISC computers, finally, is the instruction to load an
arbitrarily sized immediate into a register.  This instruction is
usually implemented as one or two basic instructions:

@enumerate
@item
If the number is small enough, an instruction is sufficient
(@code{LI} or @code{ORI} on the PowerPC, @code{MOV} on the SPARC).

@item
If the lowest bits are all zeroed, an instruction is sufficient
(@code{LIS} on the PowerPC, @code{SETHI} on the SPARC).

@item
Otherwise, the high bits are set first (with @code{LIS} or
@code{SETHI}), and the result is then @sc{or}ed with the low
bits
@end enumerate

Here is the definition of such an instruction for the PowerPC:

@example
#define MOVEIri(R,I)      (_siP(16,I) ? LIri(R,I) :     \ @rem{/* case 1    */}
                          (_uiP(16,I) ? ORIrri(R,0,I) : \ @rem{/* case 1    */}
                          _MOVEIri(R, _HI(I), _LO(I)) ))  @rem{/* case 2/3  */}

#define _MOVEIri(H,L,R)  (LISri(R,H), (L ? ORIrri(R,R,L) : 0))
@end example

@noindent
and for the SPARC:

@example
#define SETir(I,R)      (_siP(13,I) ? MOVir(I,R) : \
			 _SETir(_HI(I), _LO(I), R))

#define _SETir(H,L,R)   (SETHIir(H,R), (L ? ORrir(R,L,R) : 0))
@end example

In both cases, @code{_HI} and @code{_LO} are macros for internal use
that extract different parts of the immediate operand.

You should take a look at the run-time assemblers distributed with
@lightning{} before trying to craft your own.  In particular, make
sure you understand the RISC run-time assemblers (the SPARC's is
the simplest) before trying to decypher the x86 run-time assembler,
which is significantly more complex.


@node Standard macros
@chapter Creating the platform-independent layer

The platform-independent layer is the one that is ultimately used
by @lightning{} clients.  Creating this layer is a matter of creating
a hundred or so macros that comprise part of the interface used by
the clients, as described in
@usingref{The instruction set, @lightning{}'s instruction set}.

Fortunately, a number of these definitions are common to the different
platforms and are defined just once in one of the header files that
make up @lightning{}, that is, @file{core-common.h}.

Most of the macros are relatively straight-forward to implement (with
a few caveats for architectures whose assembly language only offers
two-operand arithmetic instructions).  This section will cover the
tricky points, before presenting the complete listing of the macros
that make up the platform-independent interface provided by
@lightning{}.

@menu
@standardmacrosmenu{}
@end menu

@node Forward references
@section Implementing forward references

Implementation of forward references takes place in:

@itemize @bullet
@item
The branch macros

@item
The @code{jit_patch_at} macros
@end itemize

Roughly speaking, the branch macros, as seen in @usingref{GNU lightning
macros, Generating code at run-time}, return a value that later calls
to @code{jit_patch} or @code{jit_patch_at} use to complete the assembly
of the forward reference.  This value is usually the contents of the
program counter after the branch instruction is compiled (which is
accessible in the @code{_jit.pc} variable).  Let's see an example from
the x86 back-end:

@example
#define jit_bmsr_i(label, s1, s2)                            \
   (TESTLrr((s1), (s2)), JNZm(label,0,0,0), _jit.pc)
@end example

The @code{bms} (@dfn{branch if mask set}) instruction is assembled as
the combination of a @code{TEST} instruction (bit-wise @sc{and} between
the two operands) and a @code{JNZ} instruction (jump if non-zero).  The
macro then returns the final value of the program counter.

@code{jit_patch_at} is one of the few macros that need to possess a
knowledge of the machine's instruction formats.  Its purpose is to
patch a branch instruction (identified by the value returned at the
moment the branch was compiled) to jump to the current position (that
is, to the address identified by @code{_jit.pc}).

On the x86, the displacement between the jump and the landing point is
expressed as a 32-bit signed integer lying in the last four bytes of the
jump instruction.  The definition of @code{_jit_patch_at} is:

@example
#define jit_patch(jump_pc, pv)    (*_PSL((jump_pc) - 4) = \
				   (pv) - (jump_pc))
@end example

The @code{_PSL} macro is nothing more than a cast to @code{long *},
and is used here to shorten the definition and avoid cluttering it with
excessive parentheses.  These type-cast macros are:

@itemize @bullet
@item
@code{_PUC(X)} to cast to a @code{unsigned char *}.

@item
@code{_PUS(X)} to cast to a @code{unsigned short *}.

@item
@code{_PUI(X)} to cast to a @code{unsigned int *}.

@item
@code{_PSL(X)} to cast to a @code{long *}.

@item
@code{_PUL(X)} to cast to a @code{unsigned long *}.
@end itemize

On other platforms, notably RISC ones, the displacement is embedded into
the instruction itself.  In this case, @code{jit_patch_at} must first zero
out the field, and then @sc{or} in the correct displacement.  The SPARC,
for example, encodes the displacement in the bottom 22 bits; in addition
the right-most two bits are suppressed, which are always zero because
instruction have to be word-aligned.

@example
#define jit_patch_at(delay_pc, pv)   jit_patch_ (((delay_pc) - 1), (pv))

@rem{/* branch instructions return the address of the @emph{delay}
 * instruction---this is just a helper macro that makes the code more
 * readable.
 */}
#define jit_patch_(jump_pc, pv)   (*jump_pc =		    \
	 (*jump_pc & ~_MASK(22)) |			    \
         ((_UL(pv) - _UL(jump_pc)) >> 2) & _MASK(22))
@end example

This introduces more predefined shortcut macros:
@itemize @bullet
@item
@code{_UC(X)} to cast to a @code{unsigned char}.

@item
@code{_US(X)} to cast to a @code{unsigned short}.

@item
@code{_UI(X)} to cast to a @code{unsigned int}.

@item
@code{_SL(X)} to cast to a @code{long}.

@item
@code{_UL(X)} to cast to a @code{unsigned long}.

@item
@code{_MASK(N)} gives a binary number made of N ones.
@end itemize

Dual to branches and @code{jit_patch_at} are @code{jit_movi_p}
and @code{jit_patch_movi}, since they can also be used to implement
forward references.  @code{jit_movi_p} should be carefully implemented
to use an encoding that is as long as possible, and it should return
an address which is then passed to @code{jit_patch_movi}.  The
implementation of @code{jit_patch_movi} is similar to
@code{jit_patch_at}.

@node Common features
@section Common features supported by @file{core-common.h}

The @file{core-common.h} file contains hundreds of macro definitions
which will spare you defining a lot of things in the files the are
specific to your port.  Here is a list of the features that 
@file{core-common.h} provides.

@table @b
@item Support for common synthetic instructions
These are instructions that can be represented as a simple operation,
for example a bit-wise @sc{and} or a subtraction.  @file{core-common.h}
recognizes when the port-specific header file defines these macros and
avoids compiler warnings about redefined macros, but there should be
no need to define them.  They are:
@example
#define jit_extr_c_ui(d, rs)
#define jit_extr_s_ui(d, rs)
#define jit_extr_c_ul(d, rs)
#define jit_extr_s_ul(d, rs)
#define jit_extr_i_ul(d, rs)
#define jit_negr_i(d, rs)
#define jit_negr_l(d, rs)
@end example

@item Support for the @sc{abi}
All of @code{jit_prolog}, @code{jit_leaf} and @code{jit_finish} are not
mandatory.  If not defined, they will be defined respectively as an
empty macro, as a synonym for @code{jit_prolog}, and as a synonym for
@code{jit_calli}.  Whether to define them or not in the port-specific
header file, it depends on the underlying architecture's @sc{abi}---in
general, however, you'll need to define at least @code{jit_prolog}.

@item Support for uncommon instructions
These are instructions that many widespread architectures lack.
@file{core-common.h} is able to provide default definitions, but they
are usually inefficient if the hardware provides a way to do these
operations with a single instruction.  They are extension with sign
and ``reverse subtraction'' (that is, REG2@math{=}IMM@math{-}REG1):
@example
#define jit_extr_c_i(d, rs)
#define jit_extr_s_i(d, rs)
#define jit_extr_c_l(d, rs)
#define jit_extr_s_l(d, rs)
#define jit_extr_i_l(d, rs)
#define jit_rsbi_i(d, rs, is)
#define jit_rsbi_l(d, rs, is)
#define jit_rsbi_p(d, rs, is)
@end example

@item Conversion between network and host byte ordering
These macros are no-ops on big endian systems.  Don't define them on
such systems; on the other hand, they are mandatory on little endian
systems.  They are:
@example
#define jit_ntoh_ui(d, rs)
#define jit_ntoh_us(d, rs)
@end example

@item Support for a ``zero'' register
Many RISC architectures provide a read-only register whose value is
hard-coded to be zero; this register is then used implicitly when
referring to a memory location using a single register.  For example,
on the SPARC, an operand like @code{[%l6]} is actually assembled as
@code{[%l6+%g0]}.  If this is the case, you should define
@code{JIT_RZERO} to be the number of this register; @file{core-common.h}
will use it to implement all variations of the @code{ld} and @code{st}
instructions.  For example:
@example
#define jit_ldi_c(d, is)         jit_ldxi_c(d, JIT_RZERO, is)
#define jit_ldr_i(d, rs)         jit_ldxr_c(d, JIT_RZERO, rs)
@end example

If available, JIT_RZERO is also used to provide more efficient
definitions of the @code{neg} instruction (see ``Support for common
synthetic instructions'', above).

@item Synonyms
@file{core-common.h} provides a lot of trivial definitions which make
the instruction set as orthogonal as possible.  For example, adding two
unsigned integers is exactly the same as adding two signed integers
(assuming a two's complement representation of negative numbers); yet,
@lightning{} provides both @code{jit_addr_i} and @code{jit_addr_ui}
macros.  Similarly, pointers and unsigned long integers behave in the
same way, but @lightning{} has separate instruction for the two data
types---those that operate on pointers usually include a typecast
that makes programs clearer.

@item Shortcuts
These define ``synthetic'' instructions whose definition is not as
trivial as in the case of synonyms, but is anyway standard.  This
is the case for bitwise @sc{not} (which is implemented by XORing a
string of ones), ``reverse subtraction'' between registers (which is
converted to a normal subtraction with the two source operands
inverted), and subtraction of an immediate from a register (which is
converted to an addition).  Unlike @code{neg} and @code{ext} (see
``Support for common synthetic instructions'', above), which are
simply non-mandatory, you must not define these functions.

@item Support for @code{long}s
On most systems, @code{long}s and @code{unsigned long}s are the same
as, respectively, @code{int}s and @code{unsigned int}s.  In this case,
@file{core-common.h} defines operations on these types to be synonyms.

@item @code{jit_state}
Last but not least, @file{core-common.h} defines the @code{jit_state}
type.  Part of this @code{struct} is machine-dependent and includes
all kinds of state needed by the back-end; this part is always
accessible in a re-entrant way as @code{_jitl}.  @code{_jitl} will be
of type @code{struct jit_local_state}; this struct must be defined
even if no state is required.

@end table

@node Delay slots
@section Supporting scheduling of delay slots

Delay slot scheduling is obtained by clients through the
@code{jit_delay} macro.  However this macro is not to be defined
in the platform-independent layer, because @lightning{} provides
a common definition in @file{core-common.h}.

Instead, the platform-independent layer must define another macro,
called @code{jit_fill_delay_after}, which has to exchange the
instruction to be scheduled in the delay slot with the branch
instruction.  The only parameter accepted by the macro is a call
to a branch macro, which must be expanded @strong{exactly once} by
@code{jit_fill_delay_after}.  The client must be able to pass the
return value of @code{jit_fill_delay_after} to @code{jit_patch_at}.

There are two possible approaches that can be used in
@code{jit_fill_delay_after}.  They are summarized in the following
pictures:

@itemize @bullet
@item
The branch instructions assemble a @sc{nop} instruction which is
then removed by @code{jit_fill_delay_after}.

@example
     before                         |   after
   ---------------------------------+-----------------------------
     ...                            |
     <would-be delay instruction>   |    <branch instruction>
     <branch instruction>           |    <delay instruction>
     NOP                            |           <--- _jit.pc
              <--- _jit.pc          |
@end example

@item
The branch instruction assembles the branch so that the delay
slot is annulled, @code{jit_fill_delay_after} toggles the bit:

@example
     before                         |   after
   ---------------------------------+-----------------------------
     ...                            |
     <would-be delay instruction>   |    <branch instruction>
     <branch with annulled delay>   |    <delay instruction>
              <--- _jit.pc          |           <--- _jit.pc
@end example
@end itemize

Don't forget that you can take advantage of delay slots in the
implementation of boolean instructions such as @code{le} or @code{gt}.

@node Immediate values
@section Supporting arbitrarily sized immediate values

This is a problem that is endemic to RISC machines.  The basic idea
is to reserve one or two register to represent large immediate values.
Let's see an example from the SPARC:

@example
   addi_i R0, V2, 45         |  addi_i R0, V2, 10000
  ---------------------------+---------------------------
   add    %l5, 45, %l0       |  set    10000, %l6
                             |  add    %l5, %l6, %l0
@end example

In this case, @code{%l6} is reserved to be used for large immediates.
An elegant solution is to use an internal macro which automatically
decides which version is to be compiled.

Beware of register conflicts on machines with delay slots.  This is
the case for the SPARC, where @code{%l7} is used instead for large
immediates in compare-and-branch instructions.  So the sequence

@example
   jit_delay(
      jit_addi_i(JIT_R0, JIT_V2, 10000),
      jit_blei_i(label, JIT_R1, 20000)
   );
@end example

@noindent
is assembled this way:

@example
   set 10000, %l6       @rem{! prepare immediate for add}
   set 20000, %l7       @rem{! prepare immediate for cmp}
   cmp %l1, %l7
   ble label
   add %l5, %l6, %l0    @rem{! delay slot}
	@end example

Note that using @code{%l6} in the branch instruction would have given
an incorrect result---@code{R0} would have been filled with the value of
@code{V2+@i{20000}} rather than @code{V2+@i{10000}}.

@node Implementing the ABI
@section Implementing the ABI

Implementing the underlying architecture's @sc{abi} is done in the
macros that handle function prologs and epilogs and argument passing.

Let's look at the prologs and epilogs first.  These are usually pretty
simple and, what's more important, with constant content---that is,
they always generate exactly the same instruction sequence.  Here is
an example:

@example
          SPARC                        x86
          save %sp, -96, %sp           push %ebp
                                       push %ebx
                                       push %esi
                                       push %edi
                                       movl %esp, %ebp
          ...                          ...
          ret                          popl %edi
          restore                      popl %esi
                                       popl %ebx
                                       popl %ebp
                                       ret
@end example

The registers that are saved (@code{%ebx}, @code{%esi}, @code{%edi}) are
mapped to the @code{V0} through @code{V2} registers in the @lightning{}
instruction set.

Argument passing is more tricky.  There are basically three
cases@footnote{For speed and ease of implementation, @lightning{} does not
currently support passing some of the parameters on the stack and some
in registers.}:
@table @b
@item Register windows
Output registers are different from input registers---the prolog takes
care of moving the caller's output registers to the callee's input
registers.  This is the case with the SPARC.

@item Passing parameters via registers
In this case, output registers are the same as input registers.  The
program must take care of saving input parameters somewhere (on the
stack, or in non-argument registers).  This is the case with the
PowerPC.

@item All the parameters are passed on the stack
This case is by far the simplest and is the most common in CISC
architectures, like the x86 and Motorola 68000.
@end table

In all cases, the port-specific header file will define two variable
for private use---one to be used by the caller during the
@code{prepare}/@code{pusharg}/@code{finish} sequence, one to be used
by the callee, specifically in the @code{jit_prolog} and @code{jit_arg}
macros.

Let's look again, this time with more detail, at each of the cases.

@table @b
@item Register windows
@code{jit_finish} is the same as @code{jit_calli}, and is defined
in @file{core-common.h} (@pxref{Common features, , Common features
supported by @file{core-common.h}}).

@example
#define jit_prepare_i(numargs)  (_jitl.pusharg = _Ro(numargs))
#define jit_pusharg_i(rs)       (--_jitl.pusharg,         \
                                 MOVrr((rs), _jitl.pusharg))
@end example

Remember that arguments pushing takes place in reverse order, thus
giving a pre-decrement (rather than post-increment) in
@code{jit_pusharg_i}.

Here is what happens on the callee's side:

@example
#define jit_arg_c()           (_jitl.getarg++)
#define jit_getarg_c(rd, ofs) jit_extr_c_i  ((rd), (ofs))
#define jit_prolog(numargs)   (SAVErir(JIT_SP, -96, JIT_SP), \
                               _jitl.getarg = _Ri(0))
@end example

The @code{jit_arg} macros return nothing more than a register index,
which is then used by the @code{jit_getarg} macros.  @code{jit_prolog}
resets the counter used by @code{jit_arg} to zero; the @code{numargs}
parameter is not used. It is sufficient for @code{jit_leaf} to be a
synonym for @code{jit_prolog}.

@item Passing parameter via registers
The code is almost the same as that for the register windows case, but
with an additional complexity---@code{jit_arg} will transfer the
argument from the input register to a non-argument register so that
function calls will not clobber it.  The prolog and epilog code can then
become unbearably long, up to 20 instructions on the PPC; a common
solution in this case is that of @dfn{trampolines}.

The prolog does nothing more than put the function's actual address in a
caller-preserved register and then call the trampoline:
@example
       mflr    r0                 @rem{! grab return address}
       movei   r10, trampo_2args  @rem{! jump to trampoline}
       mtlr    r10
       blrl
here:  mflr    r31                @rem{! r31 = address of epilog}
       @rem{...actual code...}
       mtlr    r31                @rem{! return to the trampoline}
       blr
@end example

In this case, @code{jit_prolog} does use its argument containing the
number of parameters to pick the appropriate trampoline. Here,
@code{trampo_2args} is the address of a trampoline designed for
2-argument functions.

The trampoline executes the prolog code, jumps to the contents of
@code{r10}, and upon return from the subroutine it executes the
epilog code.

@item All the parameters are passed on the stack
@code{jit_pusharg} uses a hardware push operation, which is commonly
available on CISC machines (where this approach is most likely
followed).  Since the stack has to be cleaned up after the call,
@code{jit_prepare_i} remembers how many parameters have been put there,
and @code{jit_finish} adjusts the stack pointer after the call.

@example
#define jit_prepare_i(numargs) (_jitl.args += (numargs))
#define jit_pusharg_i(rs)      PUSHLr(rs)
#define jit_finish(sub)        (jit_calli((sub)),              \
                               ADDLir(4 * _jitl.args, JIT_SP), \
                               _jitl.numargs = 0)
@end example

Note the usage of @code{+=} in @code{jit_prepare_i}.  This is done
so that one can defer the popping of the arguments that were saved
on the stack (@dfn{stack pollution}).  To do so, it is sufficient to
use @code{jit_calli} instead of @code{jit_finish} in all but the
last call.

On the caller's side, @code{arg} returns an offset relative to the
frame pointer, and @code{getarg} loads the argument from the stack:

@example
#define jit_getarg_c(rd, ofs) jit_ldxi_c((rd), _EBP, (ofs));
#define jit_arg_c()           ((_jitl.frame += sizeof(int) \
                                            -  sizeof(int))
@end example

The @code{_jitl.frame} variable is initialized by @code{jit_prolog}
with the displacement between the value of the frame pointer
(@code{%ebp}) and the address of the first parameter.
@end table

These schemes are the most used, so @file{core-common.h} provides a way
to employ them automatically.  If you do not define the
@code{jit_getarg_c} macro and its companions, @file{core-common.h} will
presume that you intend to pass parameters through either the registers
or the stack.

If you define @code{JIT_FP}, stack-based parameter passing will be
employed and the @code{jit_getarg} macros will be defined like this:

@example
#define jit_getarg_c(reg, ofs)  jit_ldxi_c((reg), JIT_FP, (ofs));
@end example

In other words, the @code{jit_arg} macros (which are still to be defined
by the platform-specific back-end) shall return an offset into the stack
frame.  On the other hand, if you don't define @code{JIT_FP},
register-based parameter passing will be employed and the @code{jit_arg}
macros shall return a register number; in this case, @code{jit_getarg}
will be implemented in terms of @code{jit_extr} and @code{jit_movr}
operations:

@example
#define jit_getarg_c(reg, ofs)		jit_extr_c_i  ((reg), (ofs))
#define jit_getarg_i(reg, ofs)		jit_movr_i    ((reg), (ofs))
@end example


@node Macro list
@section Macros composing the platform-independent layer

@table @b
@item Register names (all mandatory but the last two)
@example
#define JIT_R
#define JIT_R_NUM
#define JIT_V
#define JIT_V_NUM
#define JIT_FPR
#define JIT_FPR_NUM
#define JIT_SP
#define JIT_FP
#define JIT_RZERO
@end example

@item Helper macros (non-mandatory):
@example
#define jit_fill_delay_after(branch)
@end example

@item Mandatory:
@example
#define jit_arg_c()
#define jit_arg_i()
#define jit_arg_l()
#define jit_arg_p()
#define jit_arg_s()
#define jit_arg_uc()
#define jit_arg_ui()
#define jit_arg_ul()
#define jit_arg_us()
#define jit_abs_d(rd,rs)
#define jit_addi_i(d, rs, is)
#define jit_addr_d(rd,s1,s2)
#define jit_addr_i(d, s1, s2)
#define jit_addxi_i(d, rs, is)
#define jit_addxr_i(d, s1, s2)
#define jit_andi_i(d, rs, is)
#define jit_andr_i(d, s1, s2)
#define jit_beqi_i(label, rs, is)
#define jit_beqr_d(label, s1, s2)
#define jit_beqr_i(label, s1, s2)
#define jit_bgei_i(label, rs, is)
#define jit_bgei_ui(label, rs, is)
#define jit_bger_d(label, s1, s2)
#define jit_bger_i(label, s1, s2)
#define jit_bger_ui(label, s1, s2)
#define jit_bgti_i(label, rs, is)
#define jit_bgti_ui(label, rs, is)
#define jit_bgtr_d(label, s1, s2)
#define jit_bgtr_i(label, s1, s2)
#define jit_bgtr_ui(label, s1, s2)
#define jit_blei_i(label, rs, is)
#define jit_blei_ui(label, rs, is)
#define jit_bler_d(label, s1, s2)
#define jit_bler_i(label, s1, s2)
#define jit_bler_ui(label, s1, s2)
#define jit_bltgtr_d(label, s1, s2)
#define jit_blti_i(label, rs, is)
#define jit_blti_ui(label, rs, is)
#define jit_bltr_d(label, s1, s2)
#define jit_bltr_i(label, s1, s2)
#define jit_bltr_ui(label, s1, s2)
#define jit_bmci_i(label, rs, is)
#define jit_bmcr_i(label, s1, s2)
#define jit_bmsi_i(label, rs, is)
#define jit_bmsr_i(label, s1, s2)
#define jit_bnei_i(label, rs, is)
#define jit_bner_d(label, s1, s2)
#define jit_bner_i(label, s1, s2)
#define jit_boaddi_i(label, rs, is)
#define jit_boaddi_ui(label, rs, is)
#define jit_boaddr_i(label, s1, s2)
#define jit_boaddr_ui(label, s1, s2)
#define jit_bordr_d(label, s1, s2)
#define jit_bosubi_i(label, rs, is)
#define jit_bosubi_ui(label, rs, is)
#define jit_bosubr_i(label, s1, s2)
#define jit_bosubr_ui(label, s1, s2)
#define jit_buneqr_d(label, s1, s2)
#define jit_bunger_d(label, s1, s2)
#define jit_bungtr_d(label, s1, s2)
#define jit_bunler_d(label, s1, s2)
#define jit_bunltr_d(label, s1, s2)
#define jit_bunordr_d(label, s1, s2)
#define jit_calli(label)
#define jit_callr(label)
#define jit_ceilr_d_i(rd, rs)
#define jit_divi_i(d, rs, is)
#define jit_divi_ui(d, rs, is)
#define jit_divr_d(rd,s1,s2)
#define jit_divr_i(d, s1, s2)
#define jit_divr_ui(d, s1, s2)
#define jit_eqi_i(d, rs, is)
#define jit_eqr_d(d, s1, s2)
#define jit_eqr_i(d, s1, s2)
#define jit_extr_i_d(rd, rs)
#define jit_floorr_d_i(rd, rs)
#define jit_gei_i(d, rs, is)
#define jit_gei_ui(d, s1, s2)
#define jit_ger_d(d, s1, s2)
#define jit_ger_i(d, s1, s2)
#define jit_ger_ui(d, s1, s2)
#define jit_gti_i(d, rs, is)
#define jit_gti_ui(d, s1, s2)
#define jit_gtr_d(d, s1, s2)
#define jit_gtr_i(d, s1, s2)
#define jit_gtr_ui(d, s1, s2)
#define jit_hmuli_i(d, rs, is)
#define jit_hmuli_ui(d, rs, is)
#define jit_hmulr_i(d, s1, s2)
#define jit_hmulr_ui(d, s1, s2)
#define jit_jmpi(label)
#define jit_jmpr(reg)
#define jit_ldxi_f(rd, rs, is)
#define jit_ldxr_f(rd, s1, s2)
#define jit_ldxi_c(d, rs, is)
#define jit_ldxi_d(rd, rs, is)
#define jit_ldxi_i(d, rs, is)
#define jit_ldxi_s(d, rs, is)
#define jit_ldxi_uc(d, rs, is)
#define jit_ldxi_us(d, rs, is)
#define jit_ldxr_c(d, s1, s2)
#define jit_ldxr_d(rd, s1, s2)
#define jit_ldxr_i(d, s1, s2)
#define jit_ldxr_s(d, s1, s2)
#define jit_ldxr_uc(d, s1, s2)
#define jit_ldxr_us(d, s1, s2)
#define jit_lei_i(d, rs, is)
#define jit_lei_ui(d, s1, s2)
#define jit_ler_d(d, s1, s2)
#define jit_ler_i(d, s1, s2)
#define jit_ler_ui(d, s1, s2)
#define jit_lshi_i(d, rs, is)
#define jit_lshr_i(d, r1, r2)
#define jit_ltgtr_d(d, s1, s2)
#define jit_lti_i(d, rs, is)
#define jit_lti_ui(d, s1, s2)
#define jit_ltr_d(d, s1, s2)
#define jit_ltr_i(d, s1, s2)
#define jit_ltr_ui(d, s1, s2)
#define jit_modi_i(d, rs, is)
#define jit_modi_ui(d, rs, is)
#define jit_modr_i(d, s1, s2)
#define jit_modr_ui(d, s1, s2)
#define jit_movi_d(rd,immd)
#define jit_movi_f(rd,immf)
#define jit_movi_i(d, is)
#define jit_movi_p(d, is)
#define jit_movr_d(rd,rs)
#define jit_movr_i(d, rs)
#define jit_muli_i(d, rs, is)
#define jit_muli_ui(d, rs, is)
#define jit_mulr_d(rd,s1,s2)
#define jit_mulr_i(d, s1, s2)
#define jit_mulr_ui(d, s1, s2)
#define jit_negr_d(rd,rs)
#define jit_nei_i(d, rs, is)
#define jit_ner_d(d, s1, s2)
#define jit_ner_i(d, s1, s2)
#define jit_nop()
#define jit_ordr_d(d, s1, s2)
#define jit_ori_i(d, rs, is)
#define jit_orr_i(d, s1, s2)
#define jit_patch_at(jump_pc, value)
#define jit_patch_movi(jump_pc, value)
#define jit_pop_i(rs)
#define jit_prepare_d(numargs)
#define jit_prepare_f(numargs)
#define jit_prepare_i(numargs)
#define jit_push_i(rs)
#define jit_pusharg_i(rs)
#define jit_ret()
#define jit_retval_i(rd)
#define jit_roundr_d_i(rd, rs)
#define jit_rshi_i(d, rs, is)
#define jit_rshi_ui(d, rs, is)
#define jit_rshr_i(d, r1, r2)
#define jit_rshr_ui(d, r1, r2)
#define jit_sqrt_d(rd,rs)
#define jit_stxi_c(rd, id, rs)
#define jit_stxi_d(id, rd, rs)
#define jit_stxi_f(id, rd, rs)
#define jit_stxi_i(rd, id, rs)
#define jit_stxi_s(rd, id, rs)
#define jit_stxr_c(d1, d2, rs)
#define jit_stxr_d(d1, d2, rs)
#define jit_stxr_f(d1, d2, rs)
#define jit_stxr_i(d1, d2, rs)
#define jit_stxr_s(d1, d2, rs)
#define jit_subr_d(rd,s1,s2)
#define jit_subr_i(d, s1, s2)
#define jit_subxi_i(d, rs, is)
#define jit_subxr_i(d, s1, s2)
#define jit_truncr_d_i(rd, rs)
#define jit_uneqr_d(d, s1, s2)
#define jit_unger_d(d, s1, s2)
#define jit_ungtr_d(d, s1, s2)
#define jit_unler_d(d, s1, s2)
#define jit_unltr_d(d, s1, s2)
#define jit_unordr_d(d, s1, s2)
#define jit_xori_i(d, rs, is)
#define jit_xorr_i(d, s1, s2)
@end example

@item Non mandatory---there should be no need to define them:
@example
#define jit_extr_c_ui(d, rs)
#define jit_extr_s_ui(d, rs)
#define jit_extr_c_ul(d, rs)
#define jit_extr_s_ul(d, rs)
#define jit_extr_i_ul(d, rs)
#define jit_negr_i(d, rs)
#define jit_negr_l(d, rs)
@end example

@item Non mandatory---whether to define them depends on the @sc{abi}:
@example
#define jit_prolog(n)
#define jit_finish(sub)
#define jit_finishr(reg)
#define jit_leaf(n)
#define jit_getarg_c(reg, ofs)
#define jit_getarg_i(reg, ofs)
#define jit_getarg_l(reg, ofs)
#define jit_getarg_p(reg, ofs)
#define jit_getarg_s(reg, ofs)
#define jit_getarg_uc(reg, ofs)
#define jit_getarg_ui(reg, ofs)
#define jit_getarg_ul(reg, ofs)
#define jit_getarg_us(reg, ofs)
#define jit_getarg_f(reg, ofs)
#define jit_getarg_d(reg, ofs)
@end example

@item Non mandatory---define them if instructions that do this exist:
@example
#define jit_extr_c_i(d, rs)
#define jit_extr_s_i(d, rs)
#define jit_extr_c_l(d, rs)
#define jit_extr_s_l(d, rs)
#define jit_extr_i_l(d, rs)
#define jit_rsbi_i(d, rs, is)
#define jit_rsbi_l(d, rs, is)
@end example

@item Non mandatory if condition code are always set by add/sub, needed on other systems:
@example
#define jit_addci_i(d, rs, is)
#define jit_addci_l(d, rs, is)
#define jit_subci_i(d, rs, is)
#define jit_subci_l(d, rs, is)
@end example

@item Mandatory on little endian systems---don't define them on other systems:
@example
#define jit_ntoh_ui(d, rs)
#define jit_ntoh_us(d, rs)
@end example

@item Mandatory if JIT_RZERO not defined---don't define them if it is defined:
@example
#define jit_ldi_c(d, is)
#define jit_ldi_i(d, is)
#define jit_ldi_s(d, is)
#define jit_ldr_c(d, rs)
#define jit_ldr_i(d, rs)
#define jit_ldr_s(d, rs)
#define jit_ldi_uc(d, is)
#define jit_ldi_ui(d, is)
#define jit_ldi_ul(d, is)
#define jit_ldi_us(d, is)
#define jit_ldr_uc(d, rs)
#define jit_ldr_ui(d, rs)
#define jit_ldr_ul(d, rs)
#define jit_ldr_us(d, rs)
#define jit_sti_c(id, rs)
#define jit_sti_i(id, rs)
#define jit_sti_s(id, rs)
#define jit_str_c(rd, rs)
#define jit_str_i(rd, rs)
#define jit_str_s(rd, rs)
#define jit_ldi_f(rd, is)
#define jit_sti_f(id, rs)
#define jit_ldi_d(rd, is)
#define jit_sti_d(id, rs)
#define jit_ldr_f(rd, rs)
#define jit_str_f(rd, rs)
#define jit_ldr_d(rd, rs)
#define jit_str_d(rd, rs)
@end example

@item Synonyms---don't define them:
@example
#define jit_addi_p(d, rs, is)
#define jit_addi_ui(d, rs, is)
#define jit_addi_ul(d, rs, is)
#define jit_addr_p(d, s1, s2)
#define jit_addr_ui(d, s1, s2)
#define jit_addr_ul(d, s1, s2)
#define jit_andi_ui(d, rs, is)
#define jit_andi_ul(d, rs, is)
#define jit_andr_ui(d, s1, s2)
#define jit_andr_ul(d, s1, s2)
#define jit_beqi_p(label, rs, is)
#define jit_beqi_ui(label, rs, is)
#define jit_beqi_ul(label, rs, is)
#define jit_beqr_p(label, s1, s2)
#define jit_beqr_ui(label, s1, s2)
#define jit_beqr_ul(label, s1, s2)
#define jit_bmci_ui(label, rs, is)
#define jit_bmci_ul(label, rs, is)
#define jit_bmcr_ui(label, s1, s2)
#define jit_bmcr_ul(label, s1, s2)
#define jit_bmsi_ui(label, rs, is)
#define jit_bmsi_ul(label, rs, is)
#define jit_bmsr_ui(label, s1, s2)
#define jit_bmsr_ul(label, s1, s2)
#define jit_bgei_p(label, rs, is)
#define jit_bger_p(label, s1, s2)
#define jit_bgti_p(label, rs, is)
#define jit_bgtr_p(label, s1, s2)
#define jit_blei_p(label, rs, is)
#define jit_bler_p(label, s1, s2)
#define jit_blti_p(label, rs, is)
#define jit_bltr_p(label, s1, s2)
#define jit_bnei_p(label, rs, is)
#define jit_bnei_ui(label, rs, is)
#define jit_bnei_ul(label, rs, is)
#define jit_bner_p(label, s1, s2)
#define jit_bner_ui(label, s1, s2)
#define jit_bner_ul(label, s1, s2)
#define jit_eqi_p(d, rs, is)
#define jit_eqi_ui(d, rs, is)
#define jit_eqi_ul(d, rs, is)
#define jit_eqr_p(d, s1, s2)
#define jit_eqr_ui(d, s1, s2)
#define jit_eqr_ul(d, s1, s2)
#define jit_extr_c_s(d, rs)
#define jit_extr_c_us(d, rs)
#define jit_extr_uc_s(d, rs)
#define jit_extr_uc_us(d, rs)
#define jit_extr_uc_i(d, rs)
#define jit_extr_uc_ui(d, rs)
#define jit_extr_us_i(d, rs)
#define jit_extr_us_ui(d, rs)
#define jit_extr_uc_l(d, rs)
#define jit_extr_uc_ul(d, rs)
#define jit_extr_us_l(d, rs)
#define jit_extr_us_ul(d, rs)
#define jit_extr_ui_l(d, rs)
#define jit_extr_ui_ul(d, rs)
#define jit_gei_p(d, rs, is)
#define jit_ger_p(d, s1, s2)
#define jit_gti_p(d, rs, is)
#define jit_gtr_p(d, s1, s2)
#define jit_ldr_p(d, rs)
#define jit_ldi_p(d, is)
#define jit_ldxi_p(d, rs, is)
#define jit_ldxr_p(d, s1, s2)
#define jit_lei_p(d, rs, is)
#define jit_ler_p(d, s1, s2)
#define jit_lshi_ui(d, rs, is)
#define jit_lshi_ul(d, rs, is)
#define jit_lshr_ui(d, s1, s2)
#define jit_lshr_ul(d, s1, s2)
#define jit_lti_p(d, rs, is)
#define jit_ltr_p(d, s1, s2)
#define jit_movi_p(d, is)
#define jit_movi_ui(d, rs)
#define jit_movi_ul(d, rs)
#define jit_movr_p(d, rs)
#define jit_movr_ui(d, rs)
#define jit_movr_ul(d, rs)
#define jit_nei_p(d, rs, is)
#define jit_nei_ui(d, rs, is)
#define jit_nei_ul(d, rs, is)
#define jit_ner_p(d, s1, s2)
#define jit_ner_ui(d, s1, s2)
#define jit_ner_ul(d, s1, s2)
#define jit_hton_ui(d, rs)
#define jit_hton_us(d, rs)
#define jit_ori_ui(d, rs, is)
#define jit_ori_ul(d, rs, is)
#define jit_orr_ui(d, s1, s2)
#define jit_orr_ul(d, s1, s2)
#define jit_pop_ui(rs)
#define jit_pop_ul(rs)
#define jit_push_ui(rs)
#define jit_push_ul(rs)
#define jit_pusharg_c(rs)
#define jit_pusharg_p(rs)
#define jit_pusharg_s(rs)
#define jit_pusharg_uc(rs)
#define jit_pusharg_ui(rs)
#define jit_pusharg_ul(rs)
#define jit_pusharg_us(rs)
#define jit_retval_c(rd)
#define jit_retval_p(rd)
#define jit_retval_s(rd)
#define jit_retval_uc(rd)
#define jit_retval_ui(rd)
#define jit_retval_ul(rd)
#define jit_retval_us(rd)
#define jit_rsbi_p(d, rs, is)
#define jit_rsbi_ui(d, rs, is)
#define jit_rsbi_ul(d, rs, is)
#define jit_rsbr_p(d, rs, is)
#define jit_rsbr_ui(d, s1, s2)
#define jit_rsbr_ul(d, s1, s2)
#define jit_sti_p(d, is)
#define jit_sti_uc(d, is)
#define jit_sti_ui(d, is)
#define jit_sti_ul(d, is)
#define jit_sti_us(d, is)
#define jit_str_p(d, rs)
#define jit_str_uc(d, rs)
#define jit_str_ui(d, rs)
#define jit_str_ul(d, rs)
#define jit_str_us(d, rs)
#define jit_stxi_p(d, rs, is)
#define jit_stxi_uc(d, rs, is)
#define jit_stxi_ui(d, rs, is)
#define jit_stxi_ul(d, rs, is)
#define jit_stxi_us(d, rs, is)
#define jit_stxr_p(d, s1, s2)
#define jit_stxr_uc(d, s1, s2)
#define jit_stxr_ui(d, s1, s2)
#define jit_stxr_ul(d, s1, s2)
#define jit_stxr_us(d, s1, s2)
#define jit_subi_p(d, rs, is)
#define jit_subi_ui(d, rs, is)
#define jit_subi_ul(d, rs, is)
#define jit_subr_p(d, s1, s2)
#define jit_subr_ui(d, s1, s2)
#define jit_subr_ul(d, s1, s2)
#define jit_subxi_p(d, rs, is)
#define jit_subxi_ui(d, rs, is)
#define jit_subxi_ul(d, rs, is)
#define jit_subxr_p(d, s1, s2)
#define jit_subxr_ui(d, s1, s2)
#define jit_subxr_ul(d, s1, s2)
#define jit_xori_ui(d, rs, is)
#define jit_xori_ul(d, rs, is)
#define jit_xorr_ui(d, s1, s2)
#define jit_xorr_ul(d, s1, s2)
@end example

@item Shortcuts---don't define them:
@example
#define JIT_R0
#define JIT_R1
#define JIT_R2
#define JIT_V0
#define JIT_V1
#define JIT_V2
#define JIT_FPR0
#define JIT_FPR1
#define JIT_FPR2
#define JIT_FPR3
#define JIT_FPR4
#define JIT_FPR5
#define jit_patch(jump_pc)
#define jit_notr_c(d, rs)
#define jit_notr_i(d, rs)
#define jit_notr_l(d, rs)
#define jit_notr_s(d, rs)
#define jit_notr_uc(d, rs)
#define jit_notr_ui(d, rs)
#define jit_notr_ul(d, rs)
#define jit_notr_us(d, rs)
#define jit_rsbr_d(d, s1, s2)
#define jit_rsbr_i(d, s1, s2)
#define jit_rsbr_l(d, s1, s2)
#define jit_subi_i(d, rs, is)
#define jit_subi_l(d, rs, is)
@end example

@item Mandatory unless target arithmetic is always done in the same precision:
@example
#define jit_abs_f(rd,rs)
#define jit_addr_f(rd,s1,s2)
#define jit_beqr_f(label, s1, s2)
#define jit_bger_f(label, s1, s2)
#define jit_bgtr_f(label, s1, s2)
#define jit_bler_f(label, s1, s2)
#define jit_bltgtr_f(label, s1, s2)
#define jit_bltr_f(label, s1, s2)
#define jit_bner_f(label, s1, s2)
#define jit_bordr_f(label, s1, s2)
#define jit_buneqr_f(label, s1, s2)
#define jit_bunger_f(label, s1, s2)
#define jit_bungtr_f(label, s1, s2)
#define jit_bunler_f(label, s1, s2)
#define jit_bunltr_f(label, s1, s2)
#define jit_bunordr_f(label, s1, s2)
#define jit_ceilr_f_i(rd, rs)
#define jit_divr_f(rd,s1,s2)
#define jit_eqr_f(d, s1, s2)
#define jit_extr_d_f(rs, rd)
#define jit_extr_f_d(rs, rd)
#define jit_extr_i_f(rd, rs)
#define jit_floorr_f_i(rd, rs)
#define jit_ger_f(d, s1, s2)
#define jit_gtr_f(d, s1, s2)
#define jit_ler_f(d, s1, s2)
#define jit_ltgtr_f(d, s1, s2)
#define jit_ltr_f(d, s1, s2)
#define jit_movr_f(rd,rs)
#define jit_mulr_f(rd,s1,s2)
#define jit_negr_f(rd,rs)
#define jit_ner_f(d, s1, s2)
#define jit_ordr_f(d, s1, s2)
#define jit_roundr_f_i(rd, rs)
#define jit_rsbr_f(d, s1, s2)
#define jit_sqrt_f(rd,rs)
#define jit_subr_f(rd,s1,s2)
#define jit_truncr_f_i(rd, rs)
#define jit_uneqr_f(d, s1, s2)
#define jit_unger_f(d, s1, s2)
#define jit_ungtr_f(d, s1, s2)
#define jit_unler_f(d, s1, s2)
#define jit_unltr_f(d, s1, s2)
#define jit_unordr_f(d, s1, s2)
@end example

@item Mandatory if sizeof(long) != sizeof(int)---don't define them on other systems:
@example
#define jit_addi_l(d, rs, is)
#define jit_addr_l(d, s1, s2)
#define jit_andi_l(d, rs, is)
#define jit_andr_l(d, s1, s2)
#define jit_beqi_l(label, rs, is)
#define jit_beqr_l(label, s1, s2)
#define jit_bgei_l(label, rs, is)
#define jit_bgei_ul(label, rs, is)
#define jit_bger_l(label, s1, s2)
#define jit_bger_ul(label, s1, s2)
#define jit_bgti_l(label, rs, is)
#define jit_bgti_ul(label, rs, is)
#define jit_bgtr_l(label, s1, s2)
#define jit_bgtr_ul(label, s1, s2)
#define jit_blei_l(label, rs, is)
#define jit_blei_ul(label, rs, is)
#define jit_bler_l(label, s1, s2)
#define jit_bler_ul(label, s1, s2)
#define jit_blti_l(label, rs, is)
#define jit_blti_ul(label, rs, is)
#define jit_bltr_l(label, s1, s2)
#define jit_bltr_ul(label, s1, s2)
#define jit_bosubi_l(label, rs, is)
#define jit_bosubi_ul(label, rs, is)
#define jit_bosubr_l(label, s1, s2)
#define jit_bosubr_ul(label, s1, s2)
#define jit_boaddi_l(label, rs, is)
#define jit_boaddi_ul(label, rs, is)
#define jit_boaddr_l(label, s1, s2)
#define jit_boaddr_ul(label, s1, s2)
#define jit_bmci_l(label, rs, is)
#define jit_bmcr_l(label, s1, s2)
#define jit_bmsi_l(label, rs, is)
#define jit_bmsr_l(label, s1, s2)
#define jit_bnei_l(label, rs, is)
#define jit_bner_l(label, s1, s2)
#define jit_divi_l(d, rs, is)
#define jit_divi_ul(d, rs, is)
#define jit_divr_l(d, s1, s2)
#define jit_divr_ul(d, s1, s2)
#define jit_eqi_l(d, rs, is)
#define jit_eqr_l(d, s1, s2)
#define jit_extr_c_l(d, rs)
#define jit_extr_c_ul(d, rs)
#define jit_extr_s_l(d, rs)
#define jit_extr_s_ul(d, rs)
#define jit_extr_i_l(d, rs)
#define jit_extr_i_ul(d, rs)
#define jit_gei_l(d, rs, is)
#define jit_gei_ul(d, rs, is)
#define jit_ger_l(d, s1, s2)
#define jit_ger_ul(d, s1, s2)
#define jit_gti_l(d, rs, is)
#define jit_gti_ul(d, rs, is)
#define jit_gtr_l(d, s1, s2)
#define jit_gtr_ul(d, s1, s2)
#define jit_hmuli_l(d, rs, is)
#define jit_hmuli_ul(d, rs, is)
#define jit_hmulr_l(d, s1, s2)
#define jit_hmulr_ul(d, s1, s2)
#define jit_ldi_l(d, is)
#define jit_ldi_ui(d, is)
#define jit_ldr_l(d, rs)
#define jit_ldr_ui(d, rs)
#define jit_ldxi_l(d, rs, is)
#define jit_ldxi_ui(d, rs, is)
#define jit_ldxi_ul(d, rs, is)
#define jit_ldxr_l(d, s1, s2)
#define jit_ldxr_ui(d, s1, s2)
#define jit_ldxr_ul(d, s1, s2)
#define jit_lei_l(d, rs, is)
#define jit_lei_ul(d, rs, is)
#define jit_ler_l(d, s1, s2)
#define jit_ler_ul(d, s1, s2)
#define jit_lshi_l(d, rs, is)
#define jit_lshr_l(d, s1, s2)
#define jit_lti_l(d, rs, is)
#define jit_lti_ul(d, rs, is)
#define jit_ltr_l(d, s1, s2)
#define jit_ltr_ul(d, s1, s2)
#define jit_modi_l(d, rs, is)
#define jit_modi_ul(d, rs, is)
#define jit_modr_l(d, s1, s2)
#define jit_modr_ul(d, s1, s2)
#define jit_movi_l(d, rs)
#define jit_movr_l(d, rs)
#define jit_muli_l(d, rs, is)
#define jit_muli_ul(d, rs, is)
#define jit_mulr_l(d, s1, s2)
#define jit_mulr_ul(d, s1, s2)
#define jit_nei_l(d, rs, is)
#define jit_ner_l(d, s1, s2)
#define jit_ori_l(d, rs, is)
#define jit_orr_l(d, s1, s2)
#define jit_pop_l(rs)
#define jit_push_l(rs)
#define jit_pusharg_l(rs)
#define jit_retval_l(rd)
#define jit_rshi_l(d, rs, is)
#define jit_rshi_ul(d, rs, is)
#define jit_rshr_l(d, s1, s2)
#define jit_rshr_ul(d, s1, s2)
#define jit_sti_l(d, is)
#define jit_str_l(d, rs)
#define jit_stxi_l(d, rs, is)
#define jit_stxr_l(d, s1, s2)
#define jit_subr_l(d, s1, s2)
#define jit_xori_l(d, rs, is)
#define jit_xorr_l(d, s1, s2)
@end example
@end table

@node Standard functions
@chapter More complex tasks in the platform-independent layer

There is actually a single function that you @strong{must} define
in the @file{funcs-@var{suffix}.h} file, that is, @code{jit_flush_code}.

As explained in @usingref{GNU lightning macros, Generating code at
run-time}, its purpose is to flush part of the processor's
instruction cache (usually the part of memory that contains the
generated code), avoiding the processor executing bogus data
that it happens to find in the cache.  The @code{jit_flush_code}
function takes the first and the last address to flush.

On many processors (for example, the x86 and the all the processors
in the 68k family up to the 68030), it is not even necessary to flush
the cache.  In this case, the contents of the file will simply be

@example
#ifndef __lightning_funcs_h
#define __lightning_funcs_h

#define jit_flush_code(dest, end)

#endif @rem{/* __lightning_core_h */}
@end example

On other processors, flushing the cache is necessary for
proper behavior of the program; in this case, the file will contain
a proper definition of the function.  However, we must make yet
another distinction.

On some processors, flushing the cache is obtained through a call
to the operating system or to the C run-time library.  In this case,
the definition of @code{jit_flush_code} will be very simple: two
examples are the Alpha and the 68040. For the Alpha the code will
be:
@example
#define jit_flush_code(dest, end) \
        __asm__ __volatile__("call_pal 0x86");
@end example

@noindent
and, for the Motorola
@example
#define jit_flush_code(start, end) \
        __clear_cache((start), (end))
@end example

As you can see, the Alpha does not even need to pass the start and
end address to the function.  It is good practice to protect usage of
the @acronym{GNU CC}-specific @code{__asm__} directive by relying
on the preprocessor.  For example:

@example
#if !defined(__GNUC__) && !defined(__GNUG__)
#error Go get GNU C, I do not know how to flush the cache
#error with this compiler.
#else
#define jit_flush_code(dest, end) \
        __asm__ __volatile__("call_pal 0x86");
#endif
@end example

@lightning{}'s configuration process tries to compile a dummy file that
includes @code{lightning.h}, and gives a warning if there are problem
with the compiler that is installed on the system.

In more complex cases, you'll need to write a full-fledged function.
Don't forget to make it @code{static}, otherwise you'll have problems
linking programs that include @code{lightning.h} multiple times. An
example, taken from the @file{funcs-ppc.h} file, is:

@example
#ifndef __lightning_funcs_h
#define __lightning_funcs_h

#if !defined(__GNUC__) && !defined(__GNUG__)
#error Go get GNU C, I do not know how to flush the cache
#error with this compiler.
#else
static void
jit_flush_code(start, end)
     void       *start;
     void       *end;
@{
  register char *dest = start;

  for (; dest <= end; dest += SIZEOF_CHAR_P)
    __asm__ __volatile__ 
      ("dcbst 0,%0; sync; icbi 0,%0; isync"::"r"(dest));
@}
#endif

#endif /* __lightning_funcs_h */
@end example

The @file{funcs-@var{suffix}.h} file is also the right place to put
helper functions that do complex tasks for the
@file{core-@var{suffix}.h} file.  For example, the PowerPC assembler
defines @code{jit_prolog} as a function and puts it in that file (for more
information, @pxref{Implementing the ABI}).  Take special care when
defining such a function, as explained in @usingref{Reentrancy,
Reentrant usage of @lightning{}}.


@node Floating-point macros
@chapter Implementing macros for floating point