transpem.doc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. .sp 1.5i
  2. .de CL
  3. .ft R
  4. c\\$1
  5. .ft 5
  6. \fIcode statement-\\$1
  7. .ft 5
  8. \fBbra *\fRexit_label
  9. .ft 5
  10. ..
  11. .NH
  12. Translation of Pascal to EM code
  13. .nh
  14. .LP
  15. .sp
  16. A short description of the translation of Pascal constructs to EM code is
  17. given in the following paragraphs. The EM instructions and Pascal terminal
  18. symbols are printed in \fBboldface\fR. A sentence in \fIitalics\fR is a
  19. description of a group of EM (pseudo)instructions.
  20. .sp
  21. .NH 2
  22. Global Variables
  23. .LP
  24. .sp
  25. For every global variable, a \fBbss\fR block is reserved. To enhance the
  26. readability of the EM-code generated, the variable-identifier is used as
  27. a data label to address the block.
  28. .sp
  29. .NH 2
  30. Expressions
  31. .LP
  32. .sp
  33. Operands are always evaluated, so the execution of
  34. .br
  35. .ti +3m
  36. \fBif\fR ( p <> nil ) \fBand\fR ( p^.value <> 0 ) \fBthen\fR .....
  37. .br
  38. might cause a run-time error, if p is equal to nil.
  39. .LP
  40. The left-hand operand of a dyadic operator is almost always evaluated before
  41. the right-hand side. Peculiar evaluations exist for the following cases:
  42. .sp
  43. the expression: set1 <= set2, is evaluated as follows :
  44. .nf
  45. - evaluate set2
  46. - evaluate set1
  47. - compute set2+set1
  48. - test set2 and set2+set1 for equality
  49. .fi
  50. .sp
  51. the expression: set1 >= set2, is evaluated as follows :
  52. .nf
  53. - evaluate set1
  54. - evaluate set2
  55. - compute set1+set2
  56. - test set1 and set1+set2 for equality
  57. .fi
  58. .sp
  59. Where allowed, according to the standard, constant integral expressions are
  60. compile-time evaluated while an effort is made to report overflow on target
  61. machine basis. The integral expressions are evaluated in the type \fIarith\fR.
  62. The size of an arith is assumed to be at least the size of the integer type
  63. on the target machine. If the target machine's integer size is less than the
  64. size of an arith, overflow can be detected at compile-time. However, the
  65. following call to the standard procedure new, \fInew(p, 3+5)\fR, is illegal,
  66. because the second parameter is not a constant according to the grammar.
  67. .sp
  68. Constant floating expressions are not compile-time evaluated, because the
  69. precision on the target machine and the precision on the machine on which the
  70. compiler runs could be different. The boolean expression \fI(1.0 + 1.0) = 2.0\fR
  71. could evaluate to false.
  72. .sp
  73. .NH 2
  74. Statements
  75. .NH 3
  76. Assignment Statement
  77. \fRPASCAL :
  78. .ti +3m
  79. \f5(variable-access | function-identifier) \fB:=\f5 expression
  80. \fREM :
  81. .nf
  82. .in +3m
  83. .ft I
  84. evaluate expression
  85. store in variable-access or function-identifier
  86. .ft R
  87. .in -3m
  88. .fi
  89. In case of a function-identifier, a hidden temporary variable is used to
  90. keep the function result.
  91. .bp
  92. .NH 3
  93. Goto Statement
  94. \fRPASCAL :
  95. .ti +3m
  96. \fBGOTO\f5 label
  97. \fREM :
  98. .in +3m
  99. Two cases can be distinguished :
  100. .br
  101. - local goto,
  102. .ti +2m
  103. in which a \fBbra\fR is generated.
  104. - non-local goto,
  105. .in +2m
  106. .ll -1i
  107. a goto_descriptor is build, containing the ProgramCounter of the instruction
  108. jumped to and an offset in the target procedure frame which contains the
  109. value of the StackPointer after the jump. The code for the jump itself is to
  110. load the address of the goto_descriptor, followed by a push of the LocalBase
  111. of the target procedure and a \fBcal\fR $_gto. A message is generated to
  112. indicate that a procedure or function contains a statement which is the
  113. target of a non-local goto.
  114. .ll +1i
  115. .in -2m
  116. .in -3m
  117. .sp 2
  118. .NH 3
  119. If Statement
  120. \fRPASCAL :
  121. .in +3m
  122. .ft 5
  123. \fBIF\f5 boolean-expression \fBTHEN\f5 statement
  124. .in -3m
  125. \fREM :
  126. .nf
  127. .in +3m
  128. \fIevaluation boolean-expression
  129. \fBzeq \fR*exit_label
  130. \fIcode statement
  131. \fRexit_label
  132. .in -3m
  133. .fi
  134. .sp 2
  135. \fRPASCAL :
  136. .in +3m
  137. .ft 5
  138. \fBIF\f5 boolean-expression \fBTHEN\f5 statement-1 \fBELSE\f5 statement-2
  139. .in -3m
  140. \fREM :
  141. .nf
  142. .in +3m
  143. \fIevaluation boolean-expression
  144. \fBzeq \fR*else_label
  145. \fIcode statement-1
  146. \fBbra \fR*exit_label
  147. \fRelse_label
  148. \fIcode statement-2
  149. \fRexit_label
  150. .in -3m
  151. .fi
  152. .sp 2
  153. .NH 3
  154. Repeat Statement
  155. \fRPASCAL :
  156. .in +3m
  157. .ft 5
  158. \fBREPEAT\f5 statement-sequence \fBUNTIL\f5 boolean-expression
  159. .in -3m
  160. \fREM :
  161. .nf
  162. .in +3m
  163. \fRrepeat_label
  164. \fIcode statement-sequence
  165. \fIevaluation boolean-expression
  166. \fBzeq\fR *repeat_label
  167. .in -3m
  168. .fi
  169. .bp
  170. .NH 3
  171. While Statement
  172. \fRPASCAL :
  173. .in +3m
  174. .ft 5
  175. \fBWHILE\f5 boolean-expression \fBDO\f5 statement
  176. .in -3m
  177. \fREM :
  178. .nf
  179. .in +3m
  180. \fRwhile_label
  181. \fIevaluation boolean-expression
  182. \fBzeq\fR *exit_label
  183. \fIcode statement
  184. \fBbra\fR *while_label
  185. \fRexit_label
  186. .in -3m
  187. .fi
  188. .sp 2
  189. .NH 3
  190. Case Statement
  191. .LP
  192. .sp
  193. The case-statement is implemented using the \fBcsa\fR and \fBcsb\fR
  194. instructions.
  195. \fRPASCAL :
  196. .in +3m
  197. \fBCASE\f5 case-expression \fBOF\f5
  198. .in +5m
  199. case-constant-list-1 \fB:\f5 statement-1 \fB;\f5
  200. .br
  201. case-constant-list-2 \fB:\f5 statement-2 \fB;\f5
  202. .br
  203. \&.
  204. .br
  205. \&.
  206. .br
  207. case-constant-list-n \fB:\f5 statement-n [\fB;\f5]
  208. .in -5m
  209. \fBEND\fR
  210. .in -3m
  211. .sp 2
  212. .LP
  213. .ll -1i
  214. The \fBcsa\fR instruction is used if the range of the case-expression
  215. value is dense, i.e.
  216. .br
  217. .ti +3m
  218. \f5( upperbound \- lowerbound ) / number_of_cases\fR
  219. .br
  220. is less than the constant DENSITY, defined in the file \fIdensity.h\fR.
  221. If the range is sparse, a \fBcsb\fR instruction is used.
  222. .ll +1i
  223. \fREM :
  224. .nf
  225. .in +3m
  226. \fIevaluation case-expression
  227. \fBbra\fR *l1
  228. .CL 1
  229. .CL 2
  230. .
  231. .
  232. .CL n
  233. .ft R
  234. \&.case_descriptor
  235. .ft 5
  236. \fIgeneration case_descriptor
  237. \fRl1
  238. .ft 5
  239. \fBlae\fR .case_descriptor
  240. .ft 5
  241. \fBcsa\fR size of (case-expression)
  242. \fRexit_label
  243. .in -3m
  244. .fi
  245. .bp
  246. .NH 3
  247. For Statement
  248. \fRPASCAL :
  249. .in +3m
  250. .ft 5
  251. \fBFOR\f5 control-variable \fB:=\f5 initial-value (\fBTO\f5 | \fBDOWNTO\f5) final-value \fBDO\f5 statement
  252. .ft R
  253. .in -3m
  254. The initial-value and final-value are evaluated at the beginning of the loop.
  255. If the values are not constant, they are evaluated once and stored in a
  256. temporary.
  257. EM :
  258. .nf
  259. .in +3m
  260. \fIload initial-value
  261. \fIload final-value
  262. \fBbgt\fR exit-label (* DOWNTO : \fBblt\fI exit-label\fR *)
  263. \fIload initial-value
  264. \fRl1
  265. \fIstore in control-variable
  266. \fIcode statement
  267. \fIload control-variable
  268. \fBdup\fI control-variable
  269. \fIload final-value
  270. \fBbeq\fR exit_label
  271. \fBinc\fI control-variable\fR (* DOWNTO : \fBdec\fI control-variable\fR *)
  272. \fBbra *\fRl1
  273. \fRexit_label
  274. .in -3m
  275. .fi
  276. Note: testing must be done before incrementing(decrementing) the
  277. control-variable,
  278. .br
  279. \h'\w'Note: 'u'because wraparound could occur, which could lead to an infinite
  280. loop.
  281. .sp 2
  282. .NH 3
  283. With Statement
  284. \fRPASCAL :
  285. .ti +3m
  286. \fBWITH\f5 record-variable-list \fBDO\f5 statement
  287. .ft R
  288. The statement
  289. .ti +3m
  290. \fBWITH\fR r\s-3\d1\u\s0, r\s-3\d2\u\s0, ..., r\s-3\dn\u\s0 \fBDO\f5 statement
  291. .ft R
  292. is equivalent to
  293. .in +3m
  294. \fBWITH\fR r\s-3\d1\u\s0 \fBDO\fR
  295. \fBWITH\fR r\s-3\d2\u\s0 \fBDO\fR
  296. ...
  297. \fBWITH\fR r\s-3\dn\u\s0 \fBDO\f5 statement
  298. .ft R
  299. .in -3m
  300. The translation of
  301. .ti +3m
  302. \fBWITH\fR r\s-3\d1\u\s0 \fBDO\f5 statement
  303. .br
  304. .ft R
  305. is
  306. .nf
  307. .in +3m
  308. \fIpush address of r\s-3\d1\u\s0
  309. \fIstore address in temporary
  310. \fIcode statement
  311. .in -3m
  312. .fi
  313. .ft R
  314. An occurrence of a field is translated into:
  315. .in +3m
  316. \fIload temporary
  317. .br
  318. \fIadd field-offset
  319. .in -3m
  320. .bp
  321. .NH 2
  322. Procedure and Function Calls
  323. .ft R
  324. In general, the call
  325. .ti +5m
  326. p(a\s-3\d1\u\s0, a\s-3\d2\u\s0, ...., a\s-3\dn\u\s0)
  327. .br
  328. is translated into the sequence:
  329. .in +5m
  330. .nf
  331. \fIevaluate a\s-3\dn\u\s0
  332. \&.
  333. \&.
  334. \fIevaluate a\s-3\d2\u\s0
  335. \fIevaluate a\s-3\d1\u\s0
  336. \fIpush localbase
  337. \fBcal\fR $p
  338. \fIpop parameters
  339. .ft R
  340. .fi
  341. .in -5m
  342. i.e. the order of evaluation and binding of the actual-parameters is from
  343. right to left. In general, a copy of the actual-parameter is made when the
  344. formal-parameter is a value-parameter. If the formal-parameter is a
  345. variable-parameter, a pointer to the actual-parameter is pushed.
  346. In case of a function call, a \fBlfr\fR is generated, which pushes the
  347. function result on top of the stack.
  348. .sp 2
  349. .NH 2
  350. Register Messages
  351. .ft R
  352. A register message can be generated to indicate that a local variable is never
  353. referenced indirectly. This implies that a register can be used for a variable.
  354. We distinguish the following classes, given in decreasing priority:
  355. \(bu control-variable and final-value of a for-statement
  356. .br
  357. .ti +5m
  358. to speed up testing, and execution of the body of the for-statement
  359. .sp
  360. \(bu record-variable of a with-statement
  361. .br
  362. .ti +5m
  363. to improve the field selection of a record
  364. .sp
  365. \(bu remaining local variables and parameters
  366. .sp 2
  367. .NH 2
  368. Compile-time optimizations
  369. .ft R
  370. The only optimization that is performed is the evaluation of constant
  371. integral expressions. The optimization of constructs like
  372. .ti +5m
  373. \fBif\f5 false \fBthen\f5 statement\fR,
  374. .br
  375. is left to either the peephole optimizer, or a global optimizer.