ceg.tr 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. .nr PS 12
  2. .nr VS 14
  3. .nr LL 6i
  4. .tr ~
  5. .TL
  6. The Code Expander Generator
  7. .AU
  8. Frans Kaashoek
  9. Koen Langendoen
  10. .AI
  11. Dept. of Mathematics and Computer Science
  12. Vrije Universiteit
  13. Amsterdam, The Netherlands
  14. .NH
  15. Introduction
  16. .PP
  17. A \fBcode expander\fR (\fBce\fR for short) is a part of the
  18. Amsterdam Compiler Kit
  19. .[
  20. toolkit
  21. .]
  22. (\fBACK\fR) and provides the user with
  23. high-speed generation of medium-quality code. Although conceptually
  24. equivalent to the more usual \fBcode generator\fR, it differs in some
  25. aspects.
  26. .PP
  27. Normally, a program to be compiled with \fBACK\fR
  28. is first fed to the preprocessor. The output of the preprocessor goes
  29. into the appropriate front end, which produces EM
  30. .[
  31. block
  32. .]
  33. (a
  34. machine independent low level intermediate code). The generated EM code is fed
  35. into the peephole optimizer, which scans it with a window of a few instructions,
  36. replacing certain inefficient code sequences by better ones. After the
  37. peephole optimizer a back end follows, which produces high-quality assembly code.
  38. The assembly code goes via the target optimizer into the assembler and the
  39. object code then goes into the
  40. linker/loader, the final component in the pipeline.
  41. .PP
  42. For various applications
  43. this scheme is too slow. When debugging, for example,
  44. compile time is more important than execution time of a program.
  45. For this purpose a new scheme is introduced:
  46. .IP \ \ 1:
  47. The code generator and assembler are
  48. replaced by a library, the \fBcode expander\fR, consisting of a set of
  49. routines, one for every EM-instruction. Each routine expands its EM-instruction
  50. into relocatable object code. In contrast, the usual ACK code generator uses
  51. expensive pattern matching on sequences of EM-instructions.
  52. The peephole and target optimizer are not used.
  53. .IP \ \ 2:
  54. These routines replace the usual EM-generating routines in the front end; this
  55. eliminates the overhead of intermediate files.
  56. .LP
  57. This results in a fast compiler producing object file, ready to be
  58. linked and loaded, at the cost of unoptimized object code.
  59. .PP
  60. Because of the
  61. simple nature of the code expander, it is much easier to build, to debug, and to
  62. test. Experience has demonstrated that a code expander can be constructed,
  63. debugged, and tested in less than two weeks.
  64. .PP
  65. This document describes the tools for automatically generating a
  66. \fBce\fR (a library of C files) from two tables and
  67. a few machine-dependent functions.
  68. A thorough knowledge of EM is necessary to understand this document.
  69. .NH
  70. The code expander generator
  71. .PP
  72. The code expander generator (\fBceg\fR) generates a code expander from
  73. two tables and a few machine-dependent functions. This section explains how
  74. \fBceg\fR works. The first half describes the transformations that are done on
  75. the two tables. The
  76. second half tells how these transformations are done by the \fBceg\fR.
  77. .PP
  78. A code expander consists of a set of routines that convert EM-instructions
  79. directly to relocatable object code. These routines are called by a front
  80. end through the EM_CODE(3ACK)
  81. .[
  82. EM_CODE
  83. .]
  84. interface. To free the table writer of the burden of building
  85. an object file, we supply a set of routines that build an object file
  86. in the ACK.OUT(5ACK)
  87. .[
  88. aout
  89. .]
  90. format (see appendix B). This set of routines is called
  91. the
  92. \fBback\fR-primitives (see appendix A). In short, a code expander consists of a
  93. set of routines that map the EM_CODE interface on the
  94. \fBback\fR-primitives interface.
  95. .PP
  96. To avoid repetition of the same sequences of
  97. \fBback\fR-primitives in different
  98. EM-instructions
  99. and to improve readability, the EM-to-object information must be supplied in
  100. two
  101. tables. The EM_table maps EM to an assembly language, and the as_table
  102. maps
  103. assembly code to \fBback\fR-primitives. The assembly language is chosen by the
  104. table writer. It can either be an actual assembly language or his ad-hoc
  105. designed language.
  106. .LP
  107. The following picture shows the dependencies between the different components:
  108. .sp
  109. .PS
  110. linewid = 0.5i
  111. A: line down 2i
  112. B: line down 2i with .start at A.start + (1.5i, 0)
  113. C: line down 2i with .start at B.start + (1.5i, 0)
  114. D: arrow right with .start at A.center - (0.25i, 0)
  115. E: arrow right with .start at B.center - (0.25i, 0)
  116. F: arrow right with .start at C.center - (0.25i, 0)
  117. "EM_CODE(3ACK)" at A.start above
  118. "EM_table" at B.start above
  119. "as_table" at C.start above
  120. "source language " at D.start rjust
  121. "EM" at 0.5 of the way between D.end and E.start
  122. G: "assembly" at 0.5 of the way between E.end and F.start
  123. H: " back primitives" at F.end ljust
  124. "(user defined)" at G - (0, 0.2i)
  125. " (ACK.OUT)" at H - (0, 0.2i) ljust
  126. .PE
  127. .PP
  128. The picture suggests that, during compilation, the EM instructions are
  129. first transformed into assembly instructions and then the assembly instructions
  130. are transformed into object-generating calls. This
  131. is not what happens in practice, although the user is free to think it does.
  132. Actually, however the EM_table and the as_table are combined during code
  133. expander generation time, yielding an imaginary compound table that results in
  134. routines from the EM_CODE interface that generate object code directly.
  135. .PP
  136. As already indicated, the compound table does not exist either. Instead, each
  137. assembly instruction in the as_table is converted to a routine generating C
  138. .[
  139. Kernighan
  140. .]
  141. code
  142. to generate C code to call the \fBback\fR-primitives. The EM_table is
  143. converted into a program that for each EM instruction generates a routine,
  144. using the routines generated from the as_table. Execution of the latter program
  145. will then generate the code expander.
  146. .PP
  147. This scheme allows great flexibility
  148. in the table writing, while still
  149. resulting in a very efficient code expander. One implication is that the
  150. as_table is interpreted twice and the EM_table only once. This has consequences
  151. for their structure.
  152. .PP
  153. To illustrate what happens, we give an example. The example is an entry in
  154. the tables for the VAX-machine. The assembly language chosen is a subset of the
  155. VAX assembly language.
  156. .PP
  157. One of the most fundamental operations in EM is ``loc c'', load the value of c
  158. on the stack. To expand this instruction the
  159. tables contain the following information:
  160. .DS
  161. EM_table : \fCW
  162. C_loc ==> "pushl $$$1".
  163. /* $1 refers to the first argument of C_loc.
  164. * $$ is a quoted $. */
  165. \fRas_table :\fCW
  166. pushl src : CONST ==>
  167. @text1( 0xd0);
  168. @text1( 0xef);
  169. @text4( %$( src->num)).
  170. \fR
  171. .DE
  172. .LP
  173. The as_table is transformed in the following routine:
  174. .DS
  175. \fCW
  176. pushl_instr(src)
  177. t_operand *src;
  178. /* ``t_operand'' is a struct defined by the
  179. * table writer. */
  180. {
  181. printf("swtxt();");
  182. printf("text1( 0xd0 );");
  183. printf("text1( 0xef );");
  184. printf("text4(%s);", substitute_dollar( src->num));
  185. }
  186. \fR
  187. .DE
  188. Using ``pushl_instr()'', the following routine is generated from the EM_table:
  189. .DS
  190. \fCW
  191. C_loc( c)
  192. arith c;
  193. /* text1() and text4() are library routines that fill the
  194. * text segment. */
  195. {
  196. swtxt();
  197. text1( 0xd0);
  198. text1( 0xef);
  199. text4( c);
  200. }
  201. \fR
  202. .DE
  203. .LP
  204. A compiler call to ``C_loc()'' will cause the 1-byte numbers ``0xd0''
  205. and ``0xef''
  206. and the 4-byte value of the variable ``c'' to be stored in the text segment.
  207. .PP
  208. The transformations on the tables are done automatically by the code expander
  209. generator.
  210. The code expander generator is made up of two tools:
  211. \fBemg\fR and \fBasg\fR. \fBAsg\fR
  212. transforms
  213. each assembly instruction into a C routine. These C routines generate calls
  214. to the \fBback\fR-primitives. The generated C routines are used
  215. by \fBemg\fR to generate the actual code expander from the EM_table.
  216. .PP
  217. The link between \fBemg\fR and \fBasg\fR is an assembly language.
  218. We did not enforce a specific syntax for the assembly language;
  219. instead we have given the table writer the freedom
  220. to make an ad-hoc assembly language or to use an actual assembly language
  221. suitable for his purpose. Apart from a greater flexibility this
  222. has another advantage; if the table writer adopts the assembly language that
  223. runs on the machine at hand, he can test the EM_table independently from the
  224. as_table. Of course there is a price to pay: the table writer has to
  225. do the decoding of the operands himself. See section 4 for more details.
  226. .PP
  227. Before we describe the structure of the tables in detail, we will give
  228. an overview of the four main phases.
  229. .IP "phase 1:"
  230. .br
  231. The as_table is transformed by \fBasg\fR. This results in a set of C routines.
  232. Each assembly-opcode generates one C routine. Note that a call to such a
  233. routine does not generate the corresponding object code; it generates C code,
  234. which, when executed, generates the desired object code.
  235. .IP "phase 2:"
  236. .br
  237. The C routines generated by \fBasg\fR are used by emg to expand the EM_table.
  238. This
  239. results in a set of C routines, the code expander, which conform to the
  240. procedural interface EM_CODE(3ACK). A call to such a routine does indeed
  241. generate the desired object code.
  242. .IP "phase 3:"
  243. .br
  244. The front end that uses the procedural interface is linked/loaded with the
  245. code expander generated in phase 2 and the \fBback\fR-primitives (a supplied
  246. library). This results in a compiler.
  247. .IP "phase 4:"
  248. .br
  249. The compiler runs. The routines in the code expander are
  250. executed and produce object code.
  251. .RE
  252. .NH
  253. Description of the EM_table
  254. .PP
  255. This section describes the EM_table. It contains four subsections.
  256. The first 3 sections describe the syntax of the EM_table,
  257. the
  258. semantics of the EM_table, and the functions and
  259. constants that must be present in the EM_table, in the file ``mach.c'' or in
  260. the file ``mach.h''. The last section explains how a table writer can generate
  261. assembly code instead of object code. The section on
  262. semantics contains many examples.
  263. .NH 2
  264. Grammar
  265. .PP
  266. The following grammar describes the syntax of the EM_table.
  267. .VS +4
  268. .TS
  269. center tab(%);
  270. l c l.
  271. TABLE%::=%( RULE)*
  272. RULE%::=%C_instr ( COND_SEQUENCE | SIMPLE)
  273. COND_SEQUENCE%::=%( condition SIMPLE)* ``default'' SIMPLE
  274. SIMPLE%::=% ``==>'' ACTION_LIST
  275. ACTION_LIST%::=%[ ACTION ( ``;'' ACTION)* ] ``.''
  276. ACTION%::=%AS_INSTR
  277. %|%function-call
  278. AS_INSTR%::=%``"'' [ label ``:''] [ INSTR] ``"''
  279. INSTR%::=%mnemonic [ operand ( ``,'' operand)* ]
  280. .TE
  281. .VS -4
  282. .PP
  283. The ``('' ``)'' brackets are used for grouping, ``['' ... ``]''
  284. means ... 0 or 1 time,
  285. a ``*'' means zero or more times, and
  286. a ``|'' means
  287. a choice between left or right. A \fBC_instr\fR is
  288. a name in the EM_CODE(3ACK) interface. \fBcondition\fR is a C expression.
  289. \fBfunction-call\fR is a call of a C function. \fBlabel\fR, \fBmnemonic\fR,
  290. and \fBoperand\fR are arbitrary strings. If an \fBoperand\fR
  291. contains brackets, the
  292. brackets must match. There is an upper bound on the number of
  293. operands; the maximum number is defined by the constant MAX_OPERANDS in de
  294. file ``const.h'' in the directory assemble.c. Comments in the table should be
  295. placed between ``/*'' and ``*/''.
  296. The table is processed by the C preprocessor, before being parsed by
  297. \fBemg\fR.
  298. .NH 2
  299. Semantics
  300. .PP
  301. The EM_table is processed by \fBemg\fR. \fBEmg\fR generates a C function
  302. for every instruction in the EM_CODE(3ACK).
  303. For every EM-instruction not mentioned in the EM_table, a
  304. C function that prints an error message is generated.
  305. It is possible to divide the EM_CODE(3ACK)-interface into four parts :
  306. .IP \0\01:
  307. text instructions (e.g., C_loc, C_adi, ..)
  308. .IP \0\02:
  309. pseudo instructions (e.g., C_open, C_df_ilb, ..)
  310. .IP \0\03:
  311. storage instructions (e.g., C_rom_icon, ..)
  312. .IP \0\04:
  313. message instructions (e.g., C_mes_begin, ..)
  314. .LP
  315. This section starts with giving the semantics of the grammar. The examples
  316. are text instructions. The section ends with remarks on the pseudo
  317. instructions and the storage instructions. Since message instructions are not
  318. useful for a code expander, they are ignored.
  319. .PP
  320. .NH 3
  321. Actions
  322. .PP
  323. The EM_table is made up of rules describing how to expand a \fBC_instr\fR
  324. defined by the EM_CODE(3ACK)-interface (corresponding
  325. to an EM instruction) into actions.
  326. There are two kinds of actions: assembly instructions and C function calls.
  327. An assembly instruction is defined as a mnemonic followed by zero or more
  328. operands separated by commas. The semantics of an assembly instruction is
  329. defined by the table writer. When the assembly language is not expressive
  330. enough, then, as an escape route, function calls can be made. However, this
  331. reduces
  332. the speed of the actual code expander. Finally, actions can be grouped into
  333. a list of actions; actions are separated by a semicolon and terminated
  334. by a ``.''.
  335. .DS
  336. \fCW
  337. C_nop ==> .
  338. /* Empty action list : no operation. */
  339. C_inc ==> "incl (sp)".
  340. /* Assembler instruction, which is evaluated
  341. * during expansion of the EM_table */
  342. C_slu ==> C_sli( $1).
  343. /* Function call, which is evaluated during
  344. * execution of the compiler. */
  345. \fR
  346. .DE
  347. .NH 3
  348. Labels
  349. .PP
  350. Since an assembly language without instruction labels is a rather weak
  351. language, labels inside a contiguous block of assembly instructions are
  352. allowed. When using labels two rules must be observed:
  353. .IP \0\01:
  354. The name of a label should be unique inside an action list.
  355. .IP \0\02:
  356. The labels used in an assembler instruction should be defined in the same
  357. action list.
  358. .LP
  359. The following example illustrates the usage of labels.
  360. .DS
  361. \fCW
  362. /* Compare the two top elements on the stack. */
  363. C_cmp ==> "pop bx";
  364. "pop cx";
  365. "xor ax, ax";
  366. "cmp cx, bx";
  367. /* Forward jump to local label */
  368. "je 2f";
  369. "jb 1f";
  370. "inc ax";
  371. "jmp 2f";
  372. "1: dec ax";
  373. "2: push ax".
  374. \fR
  375. .DE
  376. We will come back to labels in the section on the as_table.
  377. .NH 3
  378. Arguments of an EM instruction
  379. .PP
  380. In most cases the translation of a \fBC_instr\fR depends on its arguments.
  381. The arguments of a \fBC_instr\fR are numbered from 1 to \fIn\fR, where \fIn\fR
  382. is the
  383. total number of arguments of the current \fBC_instr\fR (there are a few
  384. exceptions, see Implicit arguments). The table writer may
  385. refer to an argument as $\fIi\fR. If a plain $-sign is needed in an
  386. assembly instruction, it must be preceded by a extra $-sign.
  387. .PP
  388. There are two groups of \fBC_instr\fRs whose arguments are handled specially:
  389. .RS
  390. .IP "1: Instructions dealing with local offsets"
  391. .br
  392. The value of the $\fIi\fR argument referring to a parameter ($\fIi\fR >= 0)
  393. is increased by ``EM_BSIZE''. ``EM_BSIZE'' is the size of the return status block
  394. and must be defined in the file ``mach.h'' (see section 3.3). For example :
  395. .DS
  396. \fCW
  397. C_lol ==> "push $1(bp)".
  398. /* automatic conversion of $1 */
  399. \fR
  400. .DE
  401. .IP "2: Instructions using global names or instruction labels"
  402. .br
  403. All the arguments referring to global names or instruction labels will be
  404. transformed into a unique assembly name. To prevent name clashes with library
  405. names the table writer has to provide the
  406. conversions in the file ``mach.h''. For example :
  407. .DS
  408. \fCW
  409. C_bra ==> "jmp $1".
  410. /* automatic conversion of $1 */
  411. /* type arith is converted to string */
  412. \fR
  413. .DE
  414. .RE
  415. .NH 3
  416. Conditionals
  417. .PP
  418. The rules in the EM_table can be divided into two groups: simple rules and
  419. conditional rules. The simple rules are made up of a \fBC_instr\fR followed by
  420. a list of actions, as described above. The conditional rules (COND_SEQUENCE)
  421. allow the table writer to select an action list depending on the value of
  422. a condition.
  423. .PP
  424. A CONDITIONAL is a list of a boolean expression with the corresponding
  425. simple rule. If
  426. the expression evaluates to true then the corresponding simple rule is carried
  427. out. If more than one condition evaluates to true, the first one is chosen.
  428. The last case of a COND_SEQUENCE of a \fBC_instr\fR must handle
  429. the default case.
  430. The boolean expressions in a COND_SEQUENCE must be C expressions. Besides the
  431. ordinary C operators and constants, $\fIi\fR references can be used
  432. in an expression.
  433. .DS
  434. \fCW
  435. /* Load address of LB $1 levels back. */
  436. C_lxl
  437. $1 == 0 ==> "pushl fp".
  438. $1 == 1 ==> "pushl 4(ap)".
  439. default ==> "movl $$$1, r0";
  440. "jsb .lxl";
  441. "pushl r0".
  442. \fR
  443. .DE
  444. .NH 3
  445. Abbreviations
  446. .PP
  447. EM instructions with an external as an argument come in three variants in
  448. the EM_CODE(3ACK) interface. In most cases it will be possible to take
  449. these variants together. For this purpose the ``..'' notation is introduced.
  450. For the code expander there is no difference between the
  451. following instructions.
  452. .DS
  453. \fCW
  454. C_loe_dlb ==> "pushl $1 + $2".
  455. C_loe_dnam ==> "pushl $1 + $2".
  456. C_loe ==> "pushl $1 + $2".
  457. \fR
  458. .DE
  459. So it can be written in the following way.
  460. .DS
  461. \fCW
  462. C_loe.. ==> "pushl $1 + $2".
  463. \fR
  464. .DE
  465. .NH 3
  466. Implicit arguments
  467. .PP
  468. In the last example ``C_loe'' has two arguments, but in the EM_CODE interface
  469. it has one argument. This argument depends on the current ``hol''
  470. block; in the EM_table this is made explicit. Every \fBC_instr\fR whose
  471. argument depends on a ``hol'' block has one extra argument; argument 1 refers
  472. to the ``hol'' block.
  473. .NH 3
  474. Pseudo instructions
  475. .PP
  476. Most pseudo instructions are machine independent and are provided
  477. by \fBceg\fR. The table writer has only to supply the following functions,
  478. which are used to build a stackframe:
  479. .DS
  480. \fCW
  481. C_prolog()
  482. /* Performs the prolog, for example save
  483. * return address */
  484. C_locals( n)
  485. arith n;
  486. /* Allocate n bytes for locals on the stack */
  487. C_jump( label)
  488. char *label;
  489. /* Generates code for a jump to ``label'' */
  490. \fR
  491. .DE
  492. .LP
  493. These functions can be defined in ``mach.c'' or in the EM_table (see
  494. section 3.3).
  495. .NH 3
  496. Storage instructions
  497. .PP
  498. The storage instructions ``C_bss_\fIcstp()\fR'', ``C_hol_\fIcstp()\fR'',
  499. ''C_con_\fIcstp()\fR'', and ``C_rom_\fIcstp()\fR'', except for the instructions
  500. dealing with constants of type string (C_..._icon, C_..._ucon, C_..._fcon), are
  501. generated automatically. No information is needed in the table.
  502. To generate the C_..._icon, C_..._ucon, C_..._fcon instructions
  503. \fBceg\fR only has to know how to convert a number of type string to bytes;
  504. this can be defined with the constants ONE_BYTE, TWO_BYTES, and FOUR_BYTES.
  505. C_rom_icon, C_con_icon, C_bss_icon, C_hol_icon can be abbreviated by ..icon.
  506. This also holds for ..ucon and ..fcon.
  507. For example :
  508. .DS
  509. \fCW
  510. \\.\\.icon
  511. $2 == 1 ==> gen1( (ONE_BYTE) atoi( $1)).
  512. $2 == 2 ==> gen2( (TWO_BYTES) atoi( $1)).
  513. $2 == 4 ==> gen4( (FOUR_BYTES) atol( $1)).
  514. default ==> arg_error( "..icon", $2).
  515. \fR
  516. .DE
  517. Gen1(), gen2() and gen4() are \fBback\fR-primitives (see appendix A), and
  518. generate one, two, or four byte constants. Atoi() is a C library function that
  519. converts strings to integers.
  520. The constants ``ONE_BYTE'', ``TWO_BYTES'', and ``FOUR_BYTES'' must be defined in
  521. the file ``mach.h''.
  522. .NH 2
  523. User supplied definitions and functions
  524. .PP
  525. If the table writer uses all the default functions he has only to supply
  526. the following constants and functions :
  527. .TS
  528. tab(#);
  529. l c lw(10c).
  530. C_prolog()#:#T{
  531. Do prolog
  532. T}
  533. C_jump( l)#:#T{
  534. Perform a jump to label l
  535. T}
  536. C_locals( n)#:#T{
  537. Allocate n bytes on the stack
  538. T}
  539. #
  540. NAME_FMT#:#T{
  541. Print format describing name to a unique name conversion. The format must
  542. contain %s.
  543. T}
  544. DNAM_FMT#:#T{
  545. Print format describing data-label to a unique name conversion. The format
  546. must contain %s.
  547. T}
  548. DLB_FMT#:#T{
  549. Print format describing numerical-data-label to a unique name conversion.
  550. The format must contain a %ld.
  551. T}
  552. ILB_FMT#:#T{
  553. Print format describing instruction-label to a unique name conversion.
  554. The format must contain %d followed by %ld.
  555. T}
  556. HOL_FMT#:#T{
  557. Print format describing hol-block-number to a unique name conversion.
  558. The format must contain %d.
  559. T}
  560. #
  561. EM_WSIZE#:#T{
  562. Size of a word in bytes on the target machine
  563. T}
  564. EM_PSIZE#:#T{
  565. Size of a pointer in bytes on the target machine
  566. T}
  567. EM_BSIZE#:#T{
  568. Size of base block in bytes on the target machine
  569. T}
  570. #
  571. ONE_BYTE#:#T{
  572. \\C suitable type that can hold one byte on the machine where the \fBce\fR runs
  573. T}
  574. TWO_BYTES#:#T{
  575. \\C suitable type that can hold two bytes on the machine where the \fBce\fR runs
  576. T}
  577. FOUR_BYTES#:#T{
  578. \\C suitable type that can hold four bytes on the machine where the \fBce\fR runs
  579. T}
  580. #
  581. BSS_INIT#:#T{
  582. The default value that the loader puts in the bss segment
  583. T}
  584. #
  585. BYTES_REVERSED#:#T{
  586. Must be defined if you want the byte order reversed.
  587. By default the least significant byte is outputted first.\fR\(dg
  588. .FS
  589. \fR\(dg When both byte orders are used, for
  590. example NS 16032, the table writer has to
  591. supply his own set of routines.
  592. .FE
  593. T}
  594. WORDS_REVERSED#:#T{
  595. Must be defined if you want the word order reversed.
  596. By default the least significant word is outputted first.
  597. T}
  598. .TE
  599. .LP
  600. An example of the file ``mach.h'' for the vax4.
  601. .TS
  602. tab(:);
  603. l l l.
  604. #define : ONE_BYTE : int
  605. #define : TWO_BYTES : int
  606. #define : FOUR_BYTES : long
  607. :
  608. #define : EM_WSIZE : 4
  609. #define : EM_PSIZE : 4
  610. #define : EM_BSIZE : 0
  611. :
  612. #define : BSS_INIT : 0
  613. :
  614. #define : NAME_FMT : "_%s"
  615. #define : DNAM_FMT : "_%s"
  616. #define : DLB_FMT : "_%ld"
  617. #define : ILB_FMT : "I%03d%ld"
  618. #define : HOL_FMT : "hol%d"
  619. .TE
  620. Notice that EM_BSIZE is zero. The vax ``call'' instruction takes automatically
  621. care of the base block.
  622. .PP
  623. There are three primitives that have to be defined by the table writer, either
  624. as functions in the file ``mach.c'' or as rules in the EM_table.
  625. For example, for the 8086 they look like this:
  626. .DS
  627. \fCW
  628. C_jump ==> "jmp $1".
  629. C_prolog ==> "push bp";
  630. "mov bp, sp".
  631. C_locals
  632. $1 == 0 ==> .
  633. $1 == 2 ==> "push ax".
  634. $1 == 4 ==> "push ax";
  635. "push ax".
  636. default ==> "sub sp, $1".
  637. \fR
  638. .DE
  639. .NH 2
  640. Generating assembly code
  641. .PP
  642. When the code expander generator is used for generating assembly instead of
  643. object code (see section 5), additional print formats have to be defined
  644. in ``mach.h''. The following table lists these formats.
  645. .TS
  646. tab(#);
  647. l c lw(10c).
  648. BYTE_FMT#:#T{
  649. Print format to allocate and initialize one byte. The format must
  650. contain %ld.
  651. T}
  652. WORD_FMT#:#T{
  653. Print format to allocate and initialize one word. The format must
  654. contain %ld.
  655. T}
  656. LONG_FMT#:#T{
  657. Print format to allocate and initialize one long. The format must
  658. contain %ld.
  659. T}
  660. BSS_FMT#:#T{
  661. Print format to allocate space in the bss segment. The format must
  662. contain %ld (number of bytes).
  663. T}
  664. COMM_FMT#:#T{
  665. Print format to declare a "common". The format must contain a %s (name to be declared
  666. common), followed by a %ld (number of bytes).
  667. T}
  668. SEGTXT_FMT#:#T{
  669. Print format to switch to the text segment.
  670. T}
  671. SEGDAT_FMT#:#T{
  672. Print format to switch to the data segment.
  673. T}
  674. SEGBSS_FMT#:#T{
  675. Print format to switch to the bss segment.
  676. T}
  677. SYMBOL_DEF_FMT#:#T{
  678. Print format to define a label. The format must contain %s.
  679. T}
  680. GLOBAL_FMT#:#T{
  681. Print format to declare a global name. The format must contain %s.
  682. T}
  683. LOCAL_FMT#:#T{
  684. Print format to declare a local name. The format must contain %s.
  685. T}
  686. RELOC1_FMT#:#T{
  687. Print format to initialize a byte with an address expression. The format must
  688. contain %s (name) and %ld (offset).
  689. T}
  690. RELOC2_FMT#:#T{
  691. Print format to initialize a word with an address expression. The format must
  692. contain %s (name) and %ld (offset).
  693. T}
  694. RELOC4_FMT#:#T{
  695. Print format to initialize a long with an address expression. The format must
  696. contain %s (name) and %ld (offset).
  697. T}
  698. ALIGN_FMT#:#T{
  699. Print format to align a segment.
  700. T}
  701. .TE
  702. .NH 1
  703. Description of the as_table
  704. .PP
  705. This section describes the as_table. Like the previous section, it is divided
  706. into
  707. four parts: the first two parts describe the grammar and the semantics of the
  708. as_table; the third part gives an overview
  709. of the functions and the constants that must be present in the as_table (in
  710. the file ``as.h'' or in the file ``as.c''); the last part describes the case when
  711. assembly is generated instead of object code.
  712. The part on semantics contains examples that appear in the as_table for the
  713. VAX or for the 8086.
  714. .NH 2
  715. Grammar
  716. .PP
  717. The form of the as_table is given by the following grammar :
  718. .VS +4
  719. .TS
  720. center tab(#);
  721. l c l.
  722. TABLE#::=#( RULE)*
  723. RULE#::=#( mnemonic | ``...'') DECL_LIST ``==>'' ACTION_LIST
  724. DECL_LIST#::=#DECLARATION ( ``,'' DECLARATION)*
  725. DECLARATION#::=#operand [ ``:'' type]
  726. ACTION_LIST#::=#ACTION ( ``;'' ACTION) ``.''
  727. ACTION#::=#IF_STATEMENT
  728. #|#function-call
  729. #|#``@''function-call
  730. IF_STATEMENT#::=#''@if'' ``('' condition ``)'' ACTION_LIST
  731. ##( ``@elsif'' ``('' condition ``)'' ACTION_LIST)*
  732. ##[ ``@else'' ACTION_LIST]
  733. ##''@fi''
  734. function-call#::=#function-identifier ``('' [arg (,arg)*] ``)''
  735. arg#::=#argument
  736. #|#reference
  737. .TE
  738. .VS -4
  739. .LP
  740. \fBmnemonic\fR, \fBoperand\fR, and \fBtype\fR are all C identifiers;
  741. \fBcondition\fR is a normal C expression;
  742. \fBfunction-call\fR must be a C function call. A function can be called with
  743. standard C arguments or with a reference (see section 4.2.4).
  744. Since the as_table is
  745. interpreted during code expander generation as well as during code
  746. expander execution, two levels of calls are present in it. A ``function-call''
  747. is done during code expander generation, a ``@function-call'' during code
  748. expander execution.
  749. .NH 2
  750. Semantics
  751. .PP
  752. The as_table is made up of rules that map assembly instructions onto
  753. \fBback\fR-primitives, a set of functions that construct an object file.
  754. The table is processed by \fBasg\fR, which generates a C functions
  755. for each assembler mnemonic. The names of
  756. these functions are the assembler mnemonics postfixed
  757. with ``_instr'' (e.g., ``add'' becomes ``add_instr()''). These functions
  758. will be used by the function
  759. assemble() during the expansion of the EM_table.
  760. After explaining the semantics of the as_table the function
  761. assemble() will be described.
  762. .NH 3
  763. Rules
  764. .PP
  765. A rule in the as_table is made up of a left and a right hand side;
  766. the left hand side describes an assembler
  767. instruction (mnemonic and operands); the
  768. right hand side gives the corresponding actions as \fBback\fR-primitives or as
  769. functions defined by the table writer, which call \fBback-primitives\fR.
  770. Two simple examples from the VAX as_table and the 8086 as_table, resp.:
  771. .DS
  772. \fCW
  773. movl src, dst ==> @text1( 0xd0);
  774. gen_operand( src);
  775. gen_operand( dst).
  776. /* ``gen_operand'' is a function that encodes
  777. * operands by calling back-primitives. */
  778. rep ens:MOVS ==> @text1( 0xf3);
  779. @text1( 0xa5).
  780. \fR
  781. .DE
  782. .NH 3
  783. Declaration of types.
  784. .PP
  785. In general, a machine instruction is encoded as an opcode followed by zero or
  786. more
  787. the operands. There are two methods for mapping assembler mnemonics
  788. onto opcodes: the mnemonic determines the opcode, or mnemonic and operands
  789. together determine the opcode. Both cases can be
  790. easily expressed in the as_table.
  791. The first case is obvious.
  792. The second case is handled by introducing type fields for the operands.
  793. .PP
  794. When mnemonic and operands together determine the opcode, the table writer has
  795. to give several rules for each combination of mnemonic and operands. The rules
  796. differ in the type fields of the operands.
  797. The table writer has to supply functions that check the type
  798. of the operand. The name of such a function is the name of the type; it
  799. has one argument: a pointer to a struct of type \fIt_operand\fR; it returns
  800. non-zero when the operand is of this type, otherwise it returns 0.
  801. .PP
  802. This will usually lead to a list of rules per mnemonic. To reduce the amount of
  803. work an abbreviation is supplied. Once the mnemonic is specified it can be
  804. referred to in the following rules by ``...''.
  805. One has to make sure
  806. that each mnemonic is mentioned only once in the as_table, otherwise
  807. \fBasg\fR will generate more than one function with the same name.
  808. .PP
  809. The following example shows the usage of type fields.
  810. .DS
  811. \fCW
  812. mov dst:REG, src:EADDR ==>
  813. @text1( 0x8b); /* opcode */
  814. mod_RM( %d(dst->reg), src). /* operands */
  815. ... dst:EADDR, src:REG ==>
  816. @text1( 0x89); /* opcode */
  817. mod_RM( %d(src->reg), dst). /* operands */
  818. \fR
  819. .DE
  820. The table-writer must supply the restriction functions, \fCWREG\fR and
  821. \fCWEADDR\fR in the previous example, in ``as.c'' or ''as.h''.
  822. .NH 3
  823. The function of the @-sign and the if-statement.
  824. .PP
  825. The right hand side of a rule is made up of function calls.
  826. Since the as_table is
  827. interpreted on two levels, during code expander generation and during code
  828. expander execution, two levels of calls are present in it. A function-call
  829. without an ``@''-sign
  830. is called during code expander generation (e.g., the \fCWgen_operand()\fR in the
  831. first example).
  832. A function call with an ``@''-sign is called during code
  833. expander execution (e.g.,
  834. the \fBback\fR-primitives). So the last group will be part of the compiler.
  835. .PP
  836. The need for the ``@''-sign construction arises, for example, when you
  837. implement push/pop optimization (e.g., ``push x'' followed by ``pop y''
  838. can be replaced by ``move x, y'').
  839. In this case flags need to be set, unset, and tested during the execution of
  840. the compiler:
  841. .DS L
  842. \fCW
  843. PUSH src ==> /* save in ax */
  844. mov_instr( AX_oper, src);
  845. /* set flag */
  846. @assign( push_waiting, TRUE).
  847. \fR
  848. .DE
  849. .DS
  850. \fCW
  851. POP dst ==> @if ( push_waiting)
  852. /* ``mov_instr'' is asg-generated */
  853. mov_instr( dst, AX_oper);
  854. @assign( push_waiting, FALSE).
  855. @else
  856. /* ``pop_instr'' is asg-generated */
  857. pop_instr( dst).
  858. @fi.
  859. \fR
  860. .DE
  861. .LP
  862. Although the @-sign is followed syntactically by a
  863. function name, this function can very well be the name of a macro defined in C.
  864. This is in fact the case with ``@assign()'' in the above example.
  865. .PP
  866. The case may arise when information is needed that is not known
  867. until execution of
  868. the compiler. For example one needs to know if a ``$\fIi\fR'' argument fits in
  869. one byte.
  870. In this case one can use a special if-statement provided
  871. by \fBasg\fR: @if, @elsif, @else, @fi. This means that the conditions
  872. will be evaluated at
  873. run time of the \fBce\fR. In such a condition one may of course refer
  874. to the ''$\fIi\fR'' arguments. For example, constants can be
  875. packed into one or two byte arguments as follows:
  876. .DS
  877. \fCW
  878. mov dst:ACCU, src:DATA ==>
  879. @if ( fits_byte( %$(dst->expr)))
  880. @text1( 0xc0);
  881. @text1( %$(dst->expr)).
  882. @else
  883. @text1( 0xc8);
  884. @text2( %$(dst->expr)).
  885. @fi.
  886. .DE
  887. .NH 3
  888. References to operands
  889. .PP
  890. As noted before, the operands of an assembler instruction may be used as
  891. pointers to the struct \fIt_operand\fR in the right hand side of the table.
  892. Because of the free format assembler, the types of the fields in the struct
  893. \fIt_operand\fR are unknown to \fBasg\fR. As these fields can appear in calls
  894. to functions, \fBasg\fR must know
  895. these types. This section explains how these types must be specified.
  896. .PP
  897. References to operands come in three forms: ordinary operands, operands that
  898. contain ``$\fIi\fR'' references, and operands that refer to names of local labels.
  899. The ``$\fIi\fR'' in operands represent names or numbers of a \fBC_instr\fR and must
  900. be given as arguments to the \fBback\fR-primitives. Labels in operands
  901. must be converted to a number that tells the distance, the number of bytes,
  902. between the label and the current position in the text-segment.
  903. .LP
  904. All these three cases are treated in an uniform way. When the table writer
  905. makes a reference to an operand of an assembly instruction, he must describe
  906. the type of the operand in the following way.
  907. .VS +4
  908. .TS
  909. center tab(#);
  910. l c l.
  911. reference#::=#``%'' conversion
  912. ##``('' operand-name ``\->'' field-name ``)''
  913. conversion#::=# printformat
  914. #|#``$''
  915. #|#``dist''
  916. printformat#::=#see PRINT(3ACK)
  917. .[
  918. PRINT
  919. .]
  920. .TE
  921. .VS -4
  922. .LP
  923. The three cases differ only in the conversion field. The printformat conversion
  924. applies to ordinary operands. The ``%$'' applies to operands that contain
  925. a ``$\fIi\fR''. The expression between parentheses must result in a pointer to
  926. a char. The
  927. result of ``%$'' is of the type of ``$\fIi\fR''. The ``%dist''
  928. applies to operands that refer to a local label. The expression between
  929. the brackets must result in a pointer to a char. The result of ``%dist'' is
  930. of type arith.
  931. .PP
  932. The following example illustrates the usage of ``%$''. (For an
  933. example that illustrates the usage of ordinary fields see
  934. the section on ``User supplied definitions and functions'').
  935. .DS
  936. \fCW
  937. jmp dst ==>
  938. @text1( 0xe9);
  939. @reloc2( %$(dst->lab), %$(dst->off), PC_REL).
  940. \fR
  941. .DE
  942. .PP
  943. A useful function concerning $\fIi\fRs is arg_type(), which takes as input a
  944. string starting with $\fIi\fR and returns the type of the \fIi\fR''th argument
  945. of the current EM-instruction, which can be STRING, ARITH or INT. One may need
  946. this function while decoding operands if the context of the $\fIi\fR does not
  947. give enough information.
  948. If the function arg_type() is used, the file
  949. arg_type.h must contain the definition of STRING, ARITH and INT.
  950. .PP
  951. %dist is only guaranteed to work when called as a parameter of text1(), text2() or text4().
  952. The goal of the %dist conversion is to reduce the number of reloc1(), reloc2()
  953. and reloc4()
  954. calls, saving space and time (no relocation at compiler run time).
  955. The following example illustrates the usage of ``%dist''.
  956. .DS
  957. \fCW
  958. jmp dst:ILB ==> /* label in an instruction list */
  959. @text1( 0xeb);
  960. @text1( %dist( dst->lab)).
  961. ... dst:LABEL ==> /* global label */
  962. @text1( 0xe9);
  963. @reloc2( %$(dst->lab), %$(dst->off), PC_REL).
  964. \fR
  965. .DE
  966. .NH 3
  967. The functions assemble() and block_assemble()
  968. .PP
  969. The functions assemble() and block_assemble() are provided by \fBceg\fR.
  970. If, however, the table writer is not satisfied with the way they work
  971. he can
  972. supply his own assemble() or block_assemble().
  973. The default function assemble() splits an assembly string into a
  974. label, mnemonic,
  975. and operands and performs the following actions on them:
  976. .IP \0\01:
  977. It processes the local label; it records the name and current position. Thereafter it calls the function process_label() with one argument of type string,
  978. the label. The table writer has to define this function.
  979. .IP \0\02:
  980. Thereafter it calls the function process_mnemonic() with one argument of
  981. type string, the mnemonic. The table writer has to define this function.
  982. .IP \0\03:
  983. It calls process_operand() for each operand. Process_operand() must be
  984. written by the table-writer since no fixed representation for operands
  985. is enforced. It has two arguments: a string (the operand to decode)
  986. and a pointer to the struct \fIt_operand\fR. The declaration of the struct
  987. \fIt_operand\fR must be given in the
  988. file ``as.h'', and the table-writer can put all the information needed for
  989. encoding the operand in machine format in it.
  990. .IP \0\04:
  991. It examines the mnemonic and calls the associated function, generated by
  992. \fBasg\fR, with pointers to the decoded operands as arguments. This makes it
  993. possible to use the decoded operands in the right hand side of a rule (see
  994. below).
  995. .LP
  996. If the default assemble() does not work the way the table writer wants, he
  997. can supply his own version of it. Assemble() has the following arguments:
  998. .DS
  999. \fCW
  1000. assemble( instruction )
  1001. char *instruction;
  1002. \fR
  1003. .DE
  1004. \fIinstruction\fR points to a null-terminated string.
  1005. .PP
  1006. The default function block_assemble() is called with a sequence of assembly
  1007. instructions that belong to one action list. It calls assemble() for
  1008. every assembly instruction in
  1009. this block. But if a special action is
  1010. required on a block of assembly instructions, the table writer only has to
  1011. rewrite this function to get a new \fBceg\fR that obliges to his wishes.
  1012. The function block_assemble has the following arguments:
  1013. .DS
  1014. \fCW
  1015. block_assemble( instructions, nr, first, last)
  1016. char **instruction;
  1017. int nr, first, last;
  1018. \fR
  1019. .DE
  1020. \fIInstruction\fR point to an array of pointers to strings representing
  1021. assembly instructions. \fINr\fR is
  1022. the number of instructions that must be assembled. \fIFirst\fR
  1023. and \fIlast\fR have no function in the default block_assemble(), but are
  1024. useful when optimizations are done in block_assemble().
  1025. .PP
  1026. Four things have to be specified in ``as.h'' and ``as.c''. First the user must
  1027. give the declaration of struct \fIt_operand\fR in ``as.h'', and the functions
  1028. process_operand(), process_mnemonic(), and process_label() must be given
  1029. in ``as.c''. If the right hand side of the as_table
  1030. contains function calls other than the \fBback\fR-primitives, these functions
  1031. must also be present in ``as.c''. Note that both the ``@''-sign (see 4.2.3)
  1032. and ``references'' (see 4.2.4) also work in the functions defined in ``as.c''.
  1033. .PP
  1034. The following example shows the representative and essential parts of the
  1035. 8086 ``as.h'' and ``as.c'' files.
  1036. .nr PS 10
  1037. .nr VS 12
  1038. .LP
  1039. .DS L
  1040. \fCW
  1041. /* Constants and type definitions in as.h */
  1042. #define UNKNOWN 0
  1043. #define IS_REG 0x1
  1044. #define IS_ACCU 0x2
  1045. #define IS_DATA 0x4
  1046. #define IS_LABEL 0x8
  1047. #define IS_MEM 0x10
  1048. #define IS_ADDR 0x20
  1049. #define IS_ILB 0x40
  1050. #define AX 0
  1051. #define BX 3
  1052. #define CL 1
  1053. #define SP 4
  1054. #define BP 5
  1055. #define SI 6
  1056. #define DI 7
  1057. #define REG( op) ( op->type & IS_REG)
  1058. #define ACCU( op) ( op->type & IS_REG && op->reg == AX)
  1059. #define REG_CL( op) ( op->type & IS_REG && op->reg == CL)
  1060. #define DATA( op) ( op->type & IS_DATA)
  1061. #define LABEL( op) ( op->type & IS_LABEL)
  1062. #define ILB( op) ( op->type & IS_ILB)
  1063. #define MEM( op) ( op->type & IS_MEM)
  1064. #define ADDR( op) ( op->type & IS_ADDR)
  1065. #define EADDR( op) ( op->type & ( IS_ADDR | IS_MEM | IS_REG))
  1066. #define CONST1( op) ( op->type & IS_DATA && strcmp( "1", op->expr) == 0)
  1067. #define MOVS( op) ( op->type & IS_LABEL&&strcmp("\"movs\"", op->lab) == 0)
  1068. #define IMMEDIATE( op) ( op->type & ( IS_DATA | IS_LABEL))
  1069. struct t_operand {
  1070. unsigned type;
  1071. int reg;
  1072. char *expr, *lab, *off;
  1073. };
  1074. extern struct t_operand saved_op, *AX_oper;
  1075. \fR
  1076. .DE
  1077. .nr PS 12
  1078. .nr VS 14
  1079. .LP
  1080. .nr PS 10
  1081. .nr VS 12
  1082. .DS L
  1083. \fCW
  1084. /* Some functions in as.c. */
  1085. #include "arg_type.h"
  1086. #include "as.h"
  1087. #define last( s) ( s + strlen( s) - 1)
  1088. #define LEFT '('
  1089. #define RIGHT ')'
  1090. #define DOLLAR '$'
  1091. process_operand( str, op)
  1092. char *str;
  1093. struct t_operand *op;
  1094. /* expr -> IS_DATA en IS_LABEL
  1095. * reg -> IS_REG en IS_ACCU
  1096. * (expr) -> IS_ADDR
  1097. * expr(reg) -> IS_MEM
  1098. */
  1099. {
  1100. char *ptr, *index();
  1101. op->type = UNKNOWN;
  1102. if ( *last( str) == RIGHT) {
  1103. ptr = index( str, LEFT);
  1104. *last( str) = '\0';
  1105. *ptr = '\0';
  1106. if ( is_reg( ptr+1, op)) {
  1107. op->type = IS_MEM;
  1108. op->expr = ( *str == '\0' ? "0" : str);
  1109. }
  1110. else {
  1111. set_label( ptr+1, op);
  1112. op->type = IS_ADDR;
  1113. }
  1114. }
  1115. else
  1116. if ( is_reg( str, op))
  1117. op->type = IS_REG;
  1118. else {
  1119. if ( contains_label( str))
  1120. set_label( str, op);
  1121. else {
  1122. op->type = IS_DATA;
  1123. op->expr = str;
  1124. }
  1125. }
  1126. }
  1127. /*********************************************************************/
  1128. mod_RM( reg, op)
  1129. int reg;
  1130. struct t_operand *op;
  1131. /* This function helps to decode operands in machine format.
  1132. * Note the $-operators
  1133. */
  1134. {
  1135. if ( REG( op))
  1136. R233( 0x3, reg, op->reg);
  1137. else if ( ADDR( op)) {
  1138. R233( 0x0, reg, 0x6);
  1139. @reloc2( %$(op->lab), %$(op->off), ABSOLUTE);
  1140. }
  1141. else if ( strcmp( op->expr, "0") == 0)
  1142. switch( op->reg) {
  1143. case SI : R233( 0x0, reg, 0x4);
  1144. break;
  1145. case DI : R233( 0x0, reg, 0x5);
  1146. break;
  1147. case BP : R233( 0x1, reg, 0x6); /* exception! */
  1148. @text1( 0);
  1149. break;
  1150. case BX : R233( 0x0, reg, 0x7);
  1151. break;
  1152. default : fprint( STDERR, "Wrong index register %d\en",
  1153. op->reg);
  1154. }
  1155. else {
  1156. @if ( fit_byte( %$(op->expr)))
  1157. switch( op->reg) {
  1158. case SI : R233( 0x1, reg, 0x4);
  1159. break;
  1160. case DI : R233( 0x1, reg, 0x5);
  1161. break;
  1162. case BP : R233( 0x1, reg, 0x6);
  1163. break;
  1164. case BX : R233( 0x1, reg, 0x7);
  1165. break;
  1166. default : fprint( STDERR, "Wrong index register %d\en",
  1167. op->reg);
  1168. }
  1169. @text1( %$(op->expr));
  1170. @else
  1171. switch( op->reg) {
  1172. case SI : R233( 0x2, reg, 0x4);
  1173. break;
  1174. case DI : R233( 0x2, reg, 0x5);
  1175. break;
  1176. case BP : R233( 0x2, reg, 0x6);
  1177. break;
  1178. case BX : R233( 0x2, reg, 0x7);
  1179. break;
  1180. default : fprint( STDERR, "Wrong index register %d\en",
  1181. op->reg);
  1182. }
  1183. @text2( %$(op->expr));
  1184. @fi
  1185. }
  1186. }
  1187. \fR
  1188. .DE
  1189. .nr PS 12
  1190. .nr VS 14
  1191. .NH 2
  1192. Generating assembly code
  1193. .PP
  1194. It is possible to generate assembly instead of object files (see section 5), in
  1195. which case there is no need to supply ``as_table'', ``as.h'', and ``as.c''.
  1196. This option is useful for debugging the EM_table.
  1197. .NH 1
  1198. Building a code expander
  1199. .PP
  1200. This section describes how to generate a code expander in two phases.
  1201. In phase one, the EM_table is
  1202. written and assembly code is generated. If the assembly code is an actual
  1203. language, the EM_table can be tested by assembling and running the generated
  1204. code.
  1205. If an ad-hoc assembly language is used by the table writer, it is not possible
  1206. to test the EM_table, but the code generated is at least in readable form.
  1207. In the second phase, the as_table is written and object code is generated.
  1208. After the generated object code is fed into the loader, it can be tested.
  1209. .NH 2
  1210. Phase one
  1211. .PP
  1212. The following is a list of instructions to make a
  1213. code expander that generates assembly instructions.
  1214. .IP \0\01:
  1215. Create a new directory.
  1216. .IP \0\02:
  1217. Create the ``EM_table'', ``mach.h'', and ``mach.c'' files; there is no need
  1218. for ``as_table'', ``as.h'', and ``as.c'' at this moment.
  1219. .IP \0\03:
  1220. type
  1221. .br
  1222. \fCW
  1223. install_ceg -as
  1224. \fR
  1225. .br
  1226. install_ceg will create a Makefile and three directories : ceg, ce, and back.
  1227. Ceg will contain the program ceg; this program will be
  1228. used to turn ``EM_table'' into a set of C source files (in the ce directory),
  1229. one for each
  1230. EM-instruction. All these files will be compiled and put in a library called
  1231. \fBce.a\fR.
  1232. .br
  1233. The option \fCW-as\fR means that a \fBback\fR-library will be
  1234. generated (in the directory ``back'') that
  1235. supports the generation of assembly language. The library is named ``back.a''.
  1236. .IP \0\04:
  1237. Link a front end, ``ce.a'', and ``back.a'' together resulting in a compiler
  1238. that generates assembly code.
  1239. .LP
  1240. If the table writer has chosen an actual assembly language, the EM_table can be
  1241. tested (e.g., by running the compiler on the EM test set). If an error occurs,
  1242. change the EM_table and type
  1243. .IP
  1244. .br
  1245. \fCW
  1246. update_ceg\fR \fBC_instr
  1247. \fR
  1248. .br
  1249. .LP
  1250. where \fBC_instr\fR stands for the name of the erroneous EM-instruction.
  1251. If the table writer has chosen an ad-hoc assembly language, he can at least
  1252. read the generated code and look for possible errors. If an error is found,
  1253. the same procedure as described above can be followed.
  1254. .NH 2
  1255. Phase two
  1256. .PP
  1257. The next phase is to generate a \fBce\fR that produces relocatable object
  1258. code.
  1259. .IP \0\01:
  1260. Remove the ``ce'', ``ceg'', and ``back'' directories.
  1261. .IP \0\02:
  1262. Write the ``as_table'', ``as.h'', and ``as.c'' files.
  1263. .IP \0\03:
  1264. type
  1265. .sp
  1266. \fCW install_ceg -obj \fR
  1267. .sp
  1268. The option \fCW-obj\fR means that ``back.a'' will contain a library
  1269. for generating
  1270. ACK.OUT(5ACK) object files, see appendix B.
  1271. If the writer does not want to use the default ``back.a'',
  1272. the \fCW-obj\fR flag must omitted and a ``back.a'' should be supplied that
  1273. generates the generates object code in the desired format.
  1274. .IP \0\04:
  1275. Link a front end, ``ce.a'', and ``back.a'' together resulting in a compiler
  1276. that generates object code.
  1277. .LP
  1278. The as_table is ready to be tested. If an error occurs, adapt the table.
  1279. Then there are two ways to proceed:
  1280. .IP \0\01:
  1281. recompile the whole EM_table,
  1282. .sp
  1283. \fCW update_ceg ALL \fR
  1284. .sp
  1285. .IP \0\02:
  1286. recompile just the few EM-instructions that contained the error,
  1287. .sp
  1288. \fCW update_ceg \fBC_instr\fR
  1289. .sp
  1290. where \fBC_instr\fR is an erroneous EM-instruction.
  1291. This has to be done for every EM-instruction that contained the erroneous
  1292. assembly instruction.
  1293. .NH
  1294. Acknowledgements
  1295. .PP
  1296. We want to thank Henri Bal, Dick Grune, and Ceriel Jacobs for their
  1297. valuable suggestions and the critical reading of this paper.
  1298. .NH
  1299. References
  1300. .LP
  1301. .[
  1302. $LIST$
  1303. .]
  1304. .bp
  1305. .SH
  1306. Appendix A, \fRthe \fBback\fR-primitives
  1307. .PP
  1308. This appendix describes the routines available to generate relocatable
  1309. object code. If the default back.a is used, the object code is in
  1310. ACK.OUT(5ACK) format.
  1311. In de default back.a, the names defined here are remapped to more hidden names,
  1312. to avoid name conflicts with for instance names used in the front-end. This
  1313. remapping is done in an include-file, "back.h". If you implement your own
  1314. back.a library, you are advised to do the same thing. You need some parts of
  1315. the default "back.h" anyway.
  1316. .nr PS 10
  1317. .nr VS 12
  1318. .PP
  1319. .IP A1.
  1320. Text and data generation; with ONE_BYTE b; TWO_BYTES w; FOUR_BYTES l; arith n;
  1321. .VS +4
  1322. .TS
  1323. tab(#);
  1324. l c lw(10c).
  1325. text1( b)#:#T{
  1326. Put one byte in text-segment.
  1327. T}
  1328. text2( w)#:#T{
  1329. Put word (two bytes) in text-segment, byte-order is defined by
  1330. BYTES_REVERSED in mach.h.
  1331. T}
  1332. text4( l)#:#T{
  1333. Put long ( two words) in text-segment, word-order is defined by
  1334. WORDS_REVERSED in mach.h.
  1335. T}
  1336. #
  1337. con1( b)#:#T{
  1338. Same for CON-segment.
  1339. T}
  1340. con2( w)#:
  1341. con4( l)#:
  1342. #
  1343. rom1( b)#:#T{
  1344. Same for ROM-segment.
  1345. T}
  1346. rom2( w)#:
  1347. rom4( l)#:
  1348. #
  1349. gen1( b)#:#T{
  1350. Same for the current segment, only to be used in the ``..icon'', ``..ucon'', etc.
  1351. pseudo EM-instructions.
  1352. T}
  1353. gen2( w)#:
  1354. gen4( l)#:
  1355. #
  1356. bss( n)#:#T{
  1357. Put n bytes in bss-segment, value is BSS_INIT.
  1358. T}
  1359. common( n)#:#T{
  1360. If there is a saved label, generate a "common" for it, of size
  1361. n. Otherwise, it is equivalent to bss(n).
  1362. (see also the save_label routine).
  1363. T}
  1364. .TE
  1365. .VS -4
  1366. .IP A2.
  1367. Relocation; with char *s; arith o; int r;
  1368. .VS +4
  1369. .TS
  1370. tab(#);
  1371. l c lw(10c).
  1372. reloc1( s, o, r)#:#T{
  1373. Generates relocation-information for 1 byte in the current segment.
  1374. T}
  1375. ##s\0:\0the string which must be relocated
  1376. ##o\0:\0the offset in bytes from the string.
  1377. ##T{
  1378. r\0:\0relocation type. It can have the values ABSOLUTE or PC_REL. These
  1379. two constants are defined in the file ``back.h''
  1380. T}
  1381. reloc2( s, o, r)#:#T{
  1382. Generates relocation-information for 1 word in the
  1383. current segment. Byte-order according to BYTES_REVERSED in mach.h.
  1384. T}
  1385. reloc4( s, o, r)#:#T{
  1386. Generates relocation-information for 1 long in the
  1387. current segment. Word-order according to WORDS_REVERSED in mach.h.
  1388. T}
  1389. .TE
  1390. .VS -4
  1391. .IP A3.
  1392. Symbol table interaction; with int seg; char *s;
  1393. .VS +4
  1394. .TS
  1395. tab(#);
  1396. l c lw(10c).
  1397. switch_segment( seg)#:#T{
  1398. sets current segment to ``seg'', and does alignment if necessary. ``seg''
  1399. can be one of the four constants defined in ``back.h'': SEGTXT, SEGROM,
  1400. SEGCON, SEGBSS.
  1401. T}
  1402. #
  1403. symbol_definition( s)#:#T{
  1404. Define s in symbol-table.
  1405. T}
  1406. set_local_visible( s)#:#T{
  1407. Record scope-information in symbol table.
  1408. T}
  1409. set_global_visible( s)#:#T{
  1410. Record scope-information in symbol table.
  1411. T}
  1412. .TE
  1413. .VS -4
  1414. .IP A4.
  1415. Start/end actions; with char *f;
  1416. .VS +4
  1417. .TS
  1418. tab(#);
  1419. l c lw(10c).
  1420. open_back( f)#:#T{
  1421. Directs output to file ``f'', if f is the null pointer output must be given on
  1422. standard output.
  1423. T}
  1424. close_back()#:#T{
  1425. close output stream.
  1426. T}
  1427. init_back()#:#T{
  1428. Only used with user-written back-library, gives the opportunity to initialize.
  1429. T}
  1430. end_back()#:#T{
  1431. Only used with user-written back-library.
  1432. T}
  1433. .TE
  1434. .VS -4
  1435. .IP A5.
  1436. Label generation routines; with int n; arith g; char *l; These routines all
  1437. return a "char *" to a static area, which is overwritten at each call.
  1438. .VS +4
  1439. .TS
  1440. tab(#);
  1441. l c lw(10c).
  1442. extnd_pro( n)#:#T{
  1443. Label set at the end of procedure \fIn\fP, to generate space for locals.
  1444. T}
  1445. extnd_start( n)#:#T{
  1446. Label set at the beginning of procedure \fIn\fP, to jump back to after generating
  1447. space for locals.
  1448. T}
  1449. extnd_name( l)#:#T{
  1450. Create a name for a procedure named \fIl\fP.
  1451. T}
  1452. extnd_dnam( l)#:#T{
  1453. Create a name for an external variable named \fIl\fP.
  1454. T}
  1455. extnd_dlb( g)#:#T{
  1456. Create a name for numeric data label \fIg\fP.
  1457. T}
  1458. extnd_ilb( l, n)#:#T{
  1459. Create a name for instruction label \fIl\fP in procedure \fIn\fP.
  1460. T}
  1461. extnd_hol( n)#:#T{
  1462. Create a name for HOL block number \fIn\fP.
  1463. T}
  1464. extnd_part( n)#:#T{
  1465. Create a unique label for the C_insertpart mechanism.
  1466. T}
  1467. extnd_cont( n)#:#T{
  1468. Create another unique label for the C_insertpart mechanism.
  1469. T}
  1470. .TE
  1471. .VS -4
  1472. .IP A6.
  1473. Some miscellaneous routines, with char *l;
  1474. .VS +4
  1475. .TS
  1476. tab(#);
  1477. l c lw(10c).
  1478. save_label( l)#:#T{
  1479. Save label \fIl\fP. Unfortunately, in EM when you see a label, you don't
  1480. know yet in which segment it will end up. The save_label/dump_label mechanism
  1481. is there to solve this problem.
  1482. T}
  1483. dump_label()#:#T{
  1484. If there is a label saved, force definition for it now.
  1485. T}
  1486. align_word()#:#T{
  1487. Align to a word boundary, if the current segment is not a text segment.
  1488. T}
  1489. .TE
  1490. .VS -4
  1491. .nr PS 12
  1492. .nr VS 14
  1493. .bp
  1494. .SH
  1495. Appendix B, description of ACK-a.out library
  1496. .PP
  1497. The object file produced by \fBce\fR is by default in ACK.OUT(5ACK)
  1498. format. The object file is made up of one header, followed by
  1499. four segment headers, followed by text, data, relocation information,
  1500. symbol table, and the string area. The object file is tuned for the ACK-LED,
  1501. so there are some special things done just before the object file is dumped.
  1502. First, four relocation records are added which contain the names of the four
  1503. segments. Second, all the local relocation is resolved. This is done by the
  1504. function do_relo(). If there is a record belonging to a local
  1505. name this address is relocated in the segment to which the record belongs.
  1506. Besides doing the local relocation, do_relo() changes the ``nami''-field
  1507. of the local relocation records. This field receives the index of one of the
  1508. four
  1509. relocation records belonging to a segment. After the local
  1510. relocation has been resolved the routine output_back() dumps the
  1511. ACK object file.
  1512. .LP
  1513. If a different a.out format is wanted, one can choose between three strategies:
  1514. .IP \ \1:
  1515. The most simple one is to use a conversion program, which converts the ACK
  1516. a.out format to the wanted a.out format. This program exists for all most
  1517. all machines on which ACK runs. However,
  1518. not all conversion programs can generate relocation information.
  1519. The disadvantage is that the compiler will become slower.
  1520. .IP \ \2:
  1521. A better solution is to change the functions output_back(), do_relo(),
  1522. open_back(), and close_back() in such a way
  1523. that they produce the wanted a.out format. This strategy saves a lot of I/O.
  1524. .IP \ \3:
  1525. If you still are not satisfied and have a lot of spare time adapt the
  1526. \fBback\fR-primitives to produce the wanted a.out format.