12 Translation of Pascal to EM code
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.
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.
33 Operands are always evaluated, so the execution of
36 \fBif\fR ( p <> nil ) \fBand\fR ( p^.value <> 0 ) \fBthen\fR .....
38 might cause a run-time error, if p is equal to nil.
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:
43 the expression: set1 <= set2, is evaluated as follows :
48 - test set2 and set2+set1 for equality
51 the expression: set1 >= set2, is evaluated as follows :
56 - test set1 and set1+set2 for equality
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.
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.
80 \f5(variable-access | function-identifier) \fB:=\f5 expression
87 store in variable-access or function-identifier
92 In case of a function-identifier, a hidden temporary variable is used to
93 keep the function result.
104 Two cases can be distinguished :
108 in which a \fBbra\fR is generated.
113 a goto_descriptor is build, containing the ProgramCounter of the instruction
114 jumped to and an offset in the target procedure frame which contains the
115 value of the StackPointer after the jump. The code for the jump itself is to
116 load the address of the goto_descriptor, followed by a push of the LocalBase
117 of the target procedure and a \fBcal\fR $_gto. A message is generated to
118 indicate that a procedure or function contains a statement which is the
119 target of a non-local goto.
130 \fBIF\f5 boolean-expression \fBTHEN\f5 statement
136 \fIevaluation boolean-expression
137 \fBzeq \fR*exit_label
146 \fBIF\f5 boolean-expression \fBTHEN\f5 statement-1 \fBELSE\f5 statement-2
152 \fIevaluation boolean-expression
153 \fBzeq \fR*else_label
155 \fBbra \fR*exit_label
168 \fBREPEAT\f5 statement-sequence \fBUNTIL\f5 boolean-expression
175 \fIcode statement-sequence
176 \fIevaluation boolean-expression
177 \fBzeq\fR *repeat_label
187 \fBWHILE\f5 boolean-expression \fBDO\f5 statement
194 \fIevaluation boolean-expression
195 \fBzeq\fR *exit_label
197 \fBbra\fR *while_label
206 The case-statement is implemented using the \fBcsa\fR and \fBcsb\fR
211 \fBCASE\f5 case-expression \fBOF\f5
213 case-constant-list-1 \fB:\f5 statement-1 \fB;\f5
215 case-constant-list-2 \fB:\f5 statement-2 \fB;\f5
221 case-constant-list-n \fB:\f5 statement-n [\fB;\f5]
228 The \fBcsa\fR instruction is used if the range of the case-expression
232 \f5( upperbound \- lowerbound ) / number_of_cases\fR
234 is less than the constant DENSITY, defined in the file \fIdensity.h\fR.
236 If the range is sparse, a \fBcsb\fR instruction is used.
242 \fIevaluation case-expression
252 \fIgeneration case_descriptor
255 \fBlae\fR .case_descriptor
257 \fBcsa\fR size of (case-expression)
268 \fBFOR\f5 control-variable \fB:=\f5 initial-value (\fBTO\f5 | \fBDOWNTO\f5) final-value \fBDO\f5 statement
272 The initial-value and final-value are evaluated at the beginning of the loop.
273 If the values are not constant, they are evaluated once and stored in a
279 \fIload initial-value
281 \fBbgt\fR exit-label (* DOWNTO : \fBblt\fI exit-label\fR *)
282 \fIload initial-value
284 \fIstore in control-variable
286 \fIload control-variable
287 \fBdup\fI control-variable
290 \fBinc\fI control-variable\fR (* DOWNTO : \fBdec\fI control-variable\fR *)
296 Note: testing must be done before incrementing(decrementing) the
299 \h'\w'Note: 'u'because wraparound could occur, which could lead to an infinite
307 \fBWITH\f5 record-variable-list \fBDO\f5 statement
312 \fBWITH\fR r\s-3\d1\u\s0, r\s-3\d2\u\s0, ..., r\s-3\dn\u\s0 \fBDO\f5 statement
317 \fBWITH\fR r\s-3\d1\u\s0 \fBDO\fR
318 \fBWITH\fR r\s-3\d2\u\s0 \fBDO\fR
320 \fBWITH\fR r\s-3\dn\u\s0 \fBDO\f5 statement
326 \fBWITH\fR r\s-3\d1\u\s0 \fBDO\f5 statement
332 \fIpush address of r\s-3\d1\u\s0
333 \fIstore address in temporary
339 An occurrence of a field is translated into:
347 Procedure and Function Calls
352 p(a\s-3\d1\u\s0, a\s-3\d2\u\s0, ...., a\s-3\dn\u\s0)
354 is translated into the sequence:
358 \fIevaluate a\s-3\dn\u\s0
361 \fIevaluate a\s-3\d2\u\s0
362 \fIevaluate a\s-3\d1\u\s0
370 i.e. the order of evaluation and binding of the actual-parameters is from
371 right to left. In general, a copy of the actual-parameter is made when the
372 formal-parameter is a value-parameter. If the formal-parameter is a
373 variable-parameter, a pointer to the actual-parameter is pushed.
375 In case of a function call, a \fBlfr\fR is generated, which pushes the
376 function result on top of the stack.
382 A register message can be generated to indicate that a local variable is never
383 referenced indirectly. This implies that a register can be used for a variable.
384 We distinguish the following classes, given in decreasing priority:
386 \(bu control-variable and final-value of a for-statement
389 to speed up testing, and execution of the body of the for-statement
391 \(bu record-variable of a with-statement
394 to improve the field selection of a record
396 \(bu remaining local variables and parameters
399 Compile-time optimizations
402 The only optimization that is performed is the evaluation of constant
403 integral expressions. The optimization of constructs like
405 \fBif\f5 false \fBthen\f5 statement\fR,
407 is left to either the peephole optimizer, or a global optimizer.