Debian Codegraph @

gcc/m2/gm2-compiler/M2GenGCC.mod

raw
1 (* M2GenGCC.mod convert the quadruples into GCC trees.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2GenGCC ;
23
24 FROM SYSTEM IMPORT ADDRESS, WORD ;
25
26 FROM SymbolTable IMPORT PushSize, PopSize, PushValue, PopValue,
27 PushVarSize,
28 MakeConstLit,
29 RequestSym, FromModuleGetSym,
30 StartScope, EndScope, GetScope,
31 GetMainModule, GetModuleScope,
32 GetSymName, ModeOfAddr, GetMode,
33 GetGnuAsm, IsGnuAsmVolatile, IsGnuAsmSimple,
34 GetGnuAsmInput, GetGnuAsmOutput, GetGnuAsmTrash,
35 GetLowestType,
36 GetLocalSym, GetVarWritten,
37 GetVarient, GetVarBackEndType, GetModuleCtors,
38 NoOfVariables,
39 NoOfParamAny, GetParent, GetDimension, IsAModula2Type,
40 IsModule, IsDefImp, IsType, IsModuleWithinProcedure,
41 IsConstString, GetString, GetStringLength,
42 IsConstStringCnul, IsConstStringM2nul,
43 IsConst, IsConstSet, IsProcedure, IsProcType,
44 IsVar, IsVarParamAny, IsTemporary, IsTuple,
45 IsEnumeration,
46 IsUnbounded, IsArray, IsSet, IsConstructor, IsConstructorConstant,
47 IsProcedureVariable,
48 IsUnboundedParamAny,
49 IsRecordField, IsFieldVarient, IsVarient, IsRecord,
50 IsExportQualified,
51 IsExported,
52 IsSubrange, IsPointer,
53 IsProcedureBuiltinAvailable, IsProcedureInline,
54 IsParameter, IsParameterVar,
55 IsValueSolved, IsSizeSolved,
56 IsProcedureNested, IsInnerModule, IsArrayLarge,
57 IsComposite, IsVariableSSA, IsPublic, IsCtor,
58 IsConstStringKnown,
59 ForeachExportedDo,
60 ForeachImportedDo,
61 ForeachProcedureDo,
62 ForeachInnerModuleDo,
63 ForeachLocalSymDo,
64 GetLType, GetDType,
65 GetType, GetNth, GetNthParamAny,
66 SkipType, SkipTypeAndSubrange,
67 GetUnboundedHighOffset,
68 GetUnboundedAddressOffset,
69 GetSubrange, NoOfElements, GetArraySubscript,
70 GetFirstUsed, GetDeclaredMod,
71 GetProcedureBeginEnd,
72 GetRegInterface,
73 GetProcedureQuads,
74 GetProcedureBuiltin,
75 GetPriority, GetNeedSavePriority,
76 PutConstStringKnown,
77 PutConst, PutConstSet, PutConstructor,
78 GetSType, GetTypeMode,
79 HasVarParameters, CopyConstString,
80 GetVarDeclFullTok,
81 NulSym ;
82
83 FROM M2Batch IMPORT MakeDefinitionSource ;
84
85 FROM M2LexBuf IMPORT FindFileNameFromToken, TokenToLineNo, TokenToLocation,
86 MakeVirtualTok, UnknownTokenNo, BuiltinTokenNo ;
87
88 FROM M2Code IMPORT CodeBlock ;
89 FROM M2Debug IMPORT Assert ;
90 FROM M2Error IMPORT InternalError, WriteFormat0, WriteFormat1, WriteFormat2, WarnStringAt ;
91
92 FROM M2MetaError IMPORT MetaErrorT0, MetaErrorT1, MetaErrorT2, MetaErrorT3,
93 MetaError1, MetaError2, MetaErrorStringT1,
94 MetaErrorDecl ;
95
96 FROM M2Options IMPORT UnboundedByReference, PedanticCast,
97 VerboseUnbounded, Iso, Pim, DebugBuiltins, WholeProgram,
98 StrictTypeChecking, AutoInit, cflag, ScaffoldMain,
99 ScaffoldDynamic, ScaffoldStatic, GetDebugTraceQuad ;
100
101 FROM M2Printf IMPORT printf0, printf1, printf2, printf4 ;
102 FROM M2Quiet IMPORT qprintf0 ;
103
104 FROM M2Base IMPORT MixTypes, MixTypesDecl, NegateType, ActivationPointer, IsMathType,
105 IsRealType, IsComplexType, IsBaseType,
106 IsOrdinalType,
107 Cardinal, Char, Integer, IsTrunc,
108 Boolean, True,
109 Im, Re, Cmplx, GetCmplxReturnType, GetBaseTypeMinMax,
110 CheckAssignmentCompatible,
111 IsAssignmentCompatible, IsExpressionCompatible ;
112
113 FROM M2Bitset IMPORT Bitset ;
114 FROM NameKey IMPORT Name, MakeKey, KeyToCharStar, LengthKey, makekey, NulName ;
115
116 FROM DynamicStrings IMPORT string, InitString, KillString, String,
117 InitStringCharStar, Mark, Slice, ConCat, ConCatChar,
118 InitStringChar, Dup ;
119
120 FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2, Sprintf3, Sprintf4 ;
121 FROM M2System IMPORT Address, Word, System, TBitSize, MakeAdr, IsSystemType, IsGenericSystemType, IsRealN, IsComplexN, IsSetN, IsWordN, Loc, Byte ;
122 FROM M2FileName IMPORT CalculateFileName ;
123 FROM SymbolConversion IMPORT AddModGcc, Mod2Gcc, GccKnowsAbout, RemoveMod2Gcc ;
124
125 FROM M2StackWord IMPORT InitStackWord, StackOfWord, PeepWord, ReduceWord,
126 PushWord, PopWord, IsEmptyWord ;
127
128 FROM Lists IMPORT List, InitList, KillList,
129 PutItemIntoList,
130 RemoveItemFromList, IncludeItemIntoList,
131 NoOfItemsInList, GetItemFromList ;
132
133 FROM M2ALU IMPORT PtrToValue,
134 IsValueTypeReal, IsValueTypeSet,
135 IsValueTypeConstructor, IsValueTypeArray,
136 IsValueTypeRecord, IsValueTypeComplex,
137 PushIntegerTree, PopIntegerTree,
138 PushSetTree, PopSetTree,
139 PopRealTree, PushCard,
140 PushRealTree,
141 PopComplexTree, PopChar,
142 Gre, Sub, Equ, NotEqu, LessEqu,
143 BuildRange, SetOr, SetAnd, SetNegate,
144 SetSymmetricDifference, SetDifference,
145 SetShift, SetRotate,
146 AddBit, SubBit, Less, Addn, GreEqu, SetIn,
147 CheckOrResetOverflow, GetRange, GetValue,
148 ConvertToType ;
149
150 FROM M2GCCDeclare IMPORT WalkAction,
151 DeclareConstant, TryDeclareConstant, TryDeclareType,
152 DeclareConstructor, TryDeclareConstructor,
153 StartDeclareScope, EndDeclareScope,
154 PromoteToString, PromoteToCString, DeclareLocalVariable,
155 CompletelyResolved,
156 PoisonSymbols, GetTypeMin, GetTypeMax,
157 IsProcedureGccNested, DeclareParameters,
158 ConstantKnownAndUsed, PrintSym ;
159
160 FROM M2Range IMPORT CodeRangeCheck, FoldRangeCheck, CodeErrorCheck, GetMinMax ;
161
162 FROM m2builtins IMPORT BuiltInAlloca,
163 BuiltinMemSet, BuiltinMemCopy,
164 GetBuiltinConst, GetBuiltinTypeInfo,
165 BuiltinExists, BuildBuiltinTree ;
166
167 FROM m2expr IMPORT GetIntegerZero, GetIntegerOne,
168 GetCardinalOne,
169 GetPointerZero,
170 GetCardinalZero,
171 GetSizeOfInBits,
172 TreeOverflow,
173 FoldAndStrip,
174 CompareTrees,
175 StringLength,
176 AreConstantsEqual,
177 GetCstInteger,
178 BuildForeachWordInSetDoIfExpr,
179 BuildIfConstInVar,
180 BuildIfVarInVar,
181 BuildIfNotConstInVar,
182 BuildIfNotVarInVar,
183 BuildBinCheckProcedure, BuildUnaryCheckProcedure,
184 BuildBinProcedure, BuildUnaryProcedure,
185 BuildSetProcedure, BuildUnarySetFunction,
186 BuildAddCheck, BuildSubCheck, BuildMultCheck, BuildDivTruncCheck,
187 BuildDivM2Check, BuildModM2Check,
188 BuildAdd, BuildSub, BuildMult, BuildLSL,
189 BuildDivCeil, BuildModCeil,
190 BuildDivTrunc, BuildModTrunc, BuildDivFloor, BuildModFloor,
191 BuildDivM2, BuildModM2,
192 BuildRDiv,
193 BuildLogicalOrAddress,
194 BuildLogicalOr, BuildLogicalAnd, BuildSymmetricDifference,
195 BuildLogicalDifference,
196 BuildLogicalShift, BuildLogicalRotate,
197 BuildNegate, BuildNegateCheck, BuildAddr, BuildSize,
198 BuildTBitSize, BuildSystemTBitSize,
199 BuildOffset, BuildOffset1,
200 BuildLessThan, BuildGreaterThan,
201 BuildLessThanOrEqual, BuildGreaterThanOrEqual,
202 BuildEqualTo, BuildNotEqualTo,
203 BuildIsSuperset, BuildIsNotSuperset,
204 BuildIsSubset, BuildIsNotSubset,
205 BuildIndirect, BuildArray,
206 BuildTrunc, BuildCoerce,
207 BuildBinaryForeachWordDo,
208 BuildBinarySetDo,
209 BuildSetNegate,
210 BuildComponentRef,
211 BuildCap, BuildAbs, BuildIm, BuildRe, BuildCmplx,
212 BuildAddAddress,
213 BuildIfInRangeGoto, BuildIfNotInRangeGoto ;
214
215 FROM m2tree IMPORT debug_tree, skip_const_decl ;
216 FROM gcctypes IMPORT location_t, tree ;
217
218 FROM m2decl IMPORT BuildStringConstant, BuildCStringConstant,
219 DeclareKnownConstant, GetBitsPerBitset,
220 BuildIntegerConstant,
221 BuildModuleCtor, DeclareModuleCtor ;
222
223 FROM m2statement IMPORT BuildAsm, BuildProcedureCallTree, BuildParam, BuildFunctValue,
224 DoJump, BuildUnaryForeachWordDo, BuildGoto, BuildCall2, BuildCall3,
225 BuildStart, BuildEnd, BuildCallInner, BuildStartFunctionCode,
226 BuildEndFunctionCode,
227 BuildAssignmentTree, DeclareLabel,
228 BuildFunctionCallTree,
229 BuildAssignmentStatement,
230 BuildIndirectProcedureCallTree,
231 BuildPushFunctionContext, BuildPopFunctionContext,
232 BuildReturnValueCode, SetLastFunction,
233 BuildIncludeVarConst, BuildIncludeVarVar,
234 BuildExcludeVarConst, BuildExcludeVarVar,
235 BuildBuiltinCallTree, CopyByField,
236 GetParamTree, BuildCleanUp,
237 BuildTryFinally,
238 GetLastFunction, SetLastFunction,
239 SetBeginLocation, SetEndLocation ;
240
241 FROM m2type IMPORT ChainOnParamValue, GetPointerType, GetIntegerType, AddStatement,
242 GetCardinalType, GetWordType, GetM2ZType, GetM2RType, GetM2CType,
243 BuildCharConstant, AddStringToTreeList, BuildArrayStringConstructor,
244 GetArrayNoOfElements, GetTreeType, IsGccStrictTypeEquivalent ;
245
246 FROM m2block IMPORT RememberConstant, pushGlobalScope, popGlobalScope, finishFunctionDecl,
247 pushFunctionScope, popFunctionScope,
248 push_statement_list, pop_statement_list, begin_statement_list,
249 addStmtNote, removeStmtNote ;
250
251 FROM m2misc IMPORT DebugTree ;
252
253 FROM m2convert IMPORT BuildConvert, ConvertConstantAndCheck, ToCardinal, ConvertString ;
254
255 FROM m2except IMPORT BuildThrow, BuildTryBegin, BuildTryEnd,
256 BuildCatchBegin, BuildCatchEnd ;
257
258 FROM M2Quads IMPORT QuadOperator, GetQuad, IsReferenced, GetNextQuad,
259 SubQuad, PutQuad, MustCheckOverflow, GetQuadOtok,
260 GetQuadOTypetok,
261 QuadToTokenNo, DisplayQuad, GetQuadtok,
262 GetM2OperatorDesc, GetQuadOp,
263 IsQuadConstExpr, IsBecomes, IsGoto, IsConditional,
264 IsDummy, IsConditionalBooleanQuad,
265 GetQuadOp1, GetQuadOp3, GetQuadDest, SetQuadConstExpr ;
266
267 FROM M2Check IMPORT ParameterTypeCompatible, AssignmentTypeCompatible, ExpressionTypeCompatible ;
268 FROM M2SSA IMPORT EnableSSA ;
269 FROM M2Optimize IMPORT FoldBranches ;
270
271 FROM M2BasicBlock IMPORT BasicBlock, IsBasicBlockFirst,
272 GetBasicBlockStart, GetBasicBlockEnd ;
273
274
275 CONST
276 Debugging = FALSE ;
277 PriorityDebugging = FALSE ;
278 CascadedDebugging = FALSE ;
279
280 TYPE
281 DoProcedure = PROCEDURE (CARDINAL) ;
282 DoUnaryProcedure = PROCEDURE (CARDINAL) ;
283
284 VAR
285 Memset, Memcpy : CARDINAL ;
286 CurrentQuadToken : CARDINAL ;
287 UnboundedLabelNo : CARDINAL ;
288 LastLine : CARDINAL ;(* The Last Line number emitted with the *)
289 (* generated code. *)
290 LastOperator : QuadOperator ; (* The last operator processed. *)
291 ScopeStack : StackOfWord ; (* keeps track of the current scope *)
292 (* under translation. *)
293 NoChange : BOOLEAN ; (* has any constant been resolved? *)
294
295
296 (*
297 Rules for Quadruples
298 ====================
299
300 Rules
301 =====
302
303 All program declared variables are given the mode, Offset.
304 All constants have mode, Immediate.
305
306 Operators
307 =========
308
309 ------------------------------------------------------------------------------
310 Array Operators
311 ------------------------------------------------------------------------------
312 Sym<I> Base a Delivers a constant result if a is a
313 Global variable. If a is a local variable
314 then the Frame pointer needs to be added.
315 Base yields the effective location in memory
316 of, a, array [0,0, .. ,0] address.
317 Sym<I> ElementSize 1 Always delivers a constant. The number
318 indicates which specified element is chosen.
319 ElementSize is the TypeSize for that element.
320 Unbounded Op1 Op3 Initializes the op1 StartAddress of the array
321 op3. Op3 can be a normal array or unbounded array.
322 op1 (is the Unbounded.ArrayAddress) := ADR(op3).
323 In GNU Modula-2 the callee saves non var unbounded
324 arrays. This is direct contrast to the M2F native
325 code generators.
326 ------------------------------------------------------------------------------
327 := Operator
328 ------------------------------------------------------------------------------
329 Sym1<I> := Sym3<I> := produces a constant
330 Sym1<O> := Sym3<O> := has the effect Mem[Sym1<I>] := Mem[Sym3<I>]
331 ------------------------------------------------------------------------------
332 Addr Operator - contains the address of a variable - may need to add
333 ------------------------------------------------------------------------------
334 Yields the address of a variable - need to add the frame pointer if
335 a variable is local to a procedure.
336
337 Sym1<O> Addr Sym2<O> meaning Mem[Sym1<I>] := Sym2<I>
338 Sym1<V> Addr Sym2<O> meaning Mem[Sym1<I>] := Sym2<I>
339 Sym1<O> Addr Sym2<V> meaning Mem[Sym1<I>] := Mem[Sym2<I>]
340 Sym1<V> Addr Sym2<V> meaning Mem[Sym1<I>] := Mem[Sym2<I>]
341 ------------------------------------------------------------------------------
342 Xindr Operator ( *a = b)
343 ------------------------------------------------------------------------------
344 Sym1<O> Copy Sym2<I> Meaning Mem[Sym1<I>] := constant
345 Sym1<V> Copy Sym2<I> Meaning Mem[Sym1<I>] := constant
346 Sym1<O> Copy Sym2<O> meaning Mem[Sym1<I>] := Mem[Sym2<I>]
347 Sym1<V> Copy Sym2<O> meaning Mem[Sym1<I>] := Mem[Sym2<I>]
348 Sym1<O> Copy Sym2<V> meaning Mem[Sym1<I>] := Mem[Mem[Sym2<I>]]
349 Sym1<V> Copy Sym2<V> meaning Mem[Sym1<I>] := Mem[Mem[Sym2<I>]]
350 ------------------------------------------------------------------------------
351 IndrX Operator (a = *b) where <X> means any value
352 ------------------------------------------------------------------------------
353 Sym1<X> IndrX Sym2<I> meaning Mem[Sym1<I>] := Mem[constant]
354 Sym1<X> IndrX Sym2<I> meaning Mem[Sym1<I>] := Mem[constant]
355
356 Sym1<X> IndrX Sym2<X> meaning Mem[Sym1<I>] := Mem[Mem[Sym2<I>]]
357 Sym1<X> IndrX Sym2<X> meaning Mem[Sym1<I>] := Mem[Mem[Sym2<I>]]
358 ------------------------------------------------------------------------------
359 + - / * Operators
360 ------------------------------------------------------------------------------
361 Sym1<I> + Sym2<I> Sym3<I> meaning Sym1<I> := Sym2<I> + Sym3<I>
362 Sym1<O> + Sym2<O> Sym3<I> meaning Mem[Sym1<I>] :=
363 Mem[Sym2<I>] + Sym3<I>
364 Sym1<O> + Sym2<O> Sym3<O> meaning Mem[Sym1<I>] :=
365 Mem[Sym2<I>] + Mem[Sym3<I>]
366 Sym1<O> + Sym2<O> Sym3<V> meaning Mem[Sym1<I>] :=
367 Mem[Sym2<I>] + Mem[Sym3<I>]
368 Sym1<V> + Sym2<O> Sym3<V> meaning Mem[Sym1<I>] :=
369 Mem[Sym2<I>] + Mem[Sym3<I>]
370 Sym1<V> + Sym2<V> Sym3<V> meaning Mem[Sym1<I>] :=
371 Mem[Sym2<I>] + Mem[Sym3<I>]
372 ------------------------------------------------------------------------------
373 Base Operator
374 ------------------------------------------------------------------------------
375 Sym1<O> Base Sym2 Sym3<O> meaning Mem[Sym1<I>] := Sym3<I>
376 Sym1<V> Base Sym2 Sym3<O> meaning Should Never Occur But If it did..
377 Mem[Mem[Sym1<I>]] := Sym3<I>
378 Sym1<O> Base Sym2 Sym3<V> meaning Mem[Sym1<I>] := Mem[Sym3<I>]
379 Sym1<V> Base Sym2 Sym3<V> meaning Should Never Occur But If it did..
380 Mem[Mem[Sym1<I>]] := Mem[Sym3<I>]
381 Sym2 is the array type
382 ------------------------------------------------------------------------------
383 *)
384
385
386 (*
387 ErrorMessageDecl - emit an error message together with declaration fragments of left
388 and right if they are parameters or variables.
389 *)
390
391 PROCEDURE ErrorMessageDecl (tok: CARDINAL; message: ARRAY OF CHAR;
392 left, right: CARDINAL; iserror: BOOLEAN) ;
393 BEGIN
394 MetaErrorT2 (tok, message, left, right) ;
395 MetaErrorDecl (left, iserror) ;
396 MetaErrorDecl (right, iserror)
397 END ErrorMessageDecl ;
398
399
400 (*
401 IsExportedGcc - returns TRUE if this symbol should be (as far as the middle/backend of GCC)
402 is concerned, exported.
403 *)
404
405 PROCEDURE IsExportedGcc (sym: CARDINAL) : BOOLEAN ;
406 VAR
407 scope: CARDINAL ;
408 BEGIN
409 (* Has a procedure been overridden as public? *)
410 IF IsProcedure (sym) AND IsPublic (sym)
411 THEN
412 RETURN TRUE
413 END ;
414 (* Check for whole program. *)
415 IF WholeProgram
416 THEN
417 scope := GetScope (sym) ;
418 WHILE scope # NulSym DO
419 IF IsDefImp (scope)
420 THEN
421 RETURN IsExported (scope, sym)
422 ELSIF IsModule (scope)
423 THEN
424 RETURN FALSE
425 END ;
426 scope := GetScope (scope)
427 END ;
428 InternalError ('expecting scope to eventually reach a module or defimp symbol')
429 ELSE
430 (* Otherwise it is public if it were exported. *)
431 RETURN IsExported (GetMainModule (), sym)
432 END
433 END IsExportedGcc ;
434
435
436 (*
437 ConvertQuadsToTree - runs through the quadruple list and converts it into
438 the GCC tree structure.
439 *)
440
441 PROCEDURE ConvertQuadsToTree (Start, End: CARDINAL) ;
442 BEGIN
443 REPEAT
444 CodeStatement (Start) ;
445 Start := GetNextQuad (Start)
446 UNTIL (Start > End) OR (Start = 0) ;
447 END ConvertQuadsToTree ;
448
449
450 (*
451 IsCompilingMainModule -
452 *)
453
454 PROCEDURE IsCompilingMainModule (sym: CARDINAL) : BOOLEAN ;
455 BEGIN
456 WHILE (sym # NulSym) AND (GetMainModule () # sym) DO
457 sym := GetModuleScope (sym)
458 END ;
459 RETURN sym # NulSym
460 END IsCompilingMainModule ;
461
462
463 (*
464 CodeLastForIterator - call PerformLastForIterator allowing for
465 a non constant last iterator value.
466 *)
467
468 PROCEDURE CodeLastForIterator (quad: CARDINAL) ;
469 BEGIN
470 PerformLastForIterator (quad, NoWalkProcedure, FALSE)
471 END CodeLastForIterator ;
472
473
474 (*
475 FoldLastForIterator - call PerformLastForIterator providing
476 all operands are constant and are known by GCC.
477 *)
478
479 PROCEDURE FoldLastForIterator (quad: CARDINAL; p: WalkAction) ;
480 VAR
481 op : QuadOperator ;
482 e1, e2,
483 op1, tuple, incr: CARDINAL ;
484 BEGIN
485 GetQuad (quad, op, op1, tuple, incr) ;
486 Assert (IsTuple (tuple)) ;
487 e1 := GetNth (tuple, 1) ;
488 e2 := GetNth (tuple, 2) ;
489 IF IsConst (op1) AND IsConst (e1) AND IsConst (e2) AND IsConst (incr) AND
490 GccKnowsAbout (e1) AND GccKnowsAbout (e2) AND GccKnowsAbout (incr)
491 THEN
492 PerformLastForIterator (quad, p, TRUE)
493 END
494 END FoldLastForIterator ;
495
496
497 (*
498 FoldLastForIterator - generates code to calculate the last iterator value
499 in a for loop. It examines the increment constant
500 and generates different code depending whether it is
501 negative or positive.
502 *)
503
504 PROCEDURE PerformLastForIterator (quad: CARDINAL; p: WalkAction; constant: BOOLEAN) ;
505 VAR
506 success,
507 constExpr,
508 overflowChecking : BOOLEAN ;
509 op : QuadOperator ;
510 lastpos, op1pos,
511 op2pos, incrpos,
512 last, tuple, incr: CARDINAL ;
513 e1, e2 : CARDINAL ;
514 lasttree,
515 e1tree, e2tree,
516 expr, incrtree : tree ;
517 location : location_t ;
518 BEGIN
519 GetQuadOtok (quad, lastpos, op, last, tuple, incr,
520 overflowChecking, constExpr,
521 op1pos, op2pos, incrpos) ;
522 DeclareConstant (incrpos, incr) ;
523 lasttree := Mod2Gcc (last) ;
524 success := TRUE ;
525 IF IsConst (incr)
526 THEN
527 incrtree := Mod2Gcc (incr) ;
528 location := TokenToLocation (lastpos) ;
529 e1 := GetNth (tuple, 1) ;
530 e2 := GetNth (tuple, 2) ;
531 e1tree := Mod2Gcc (e1) ;
532 e2tree := Mod2Gcc (e2) ;
533 IF CompareTrees (incrtree, GetIntegerZero (location)) = 0
534 THEN
535 MetaErrorT0 (lastpos,
536 'the {%kFOR} loop step value must not be zero') ;
537 MetaErrorDecl (incr, TRUE) ;
538 NoChange := FALSE ;
539 SubQuad (quad) ;
540 success := FALSE
541 ELSIF CompareTrees (incrtree, GetIntegerZero (location)) > 0
542 THEN
543 (* If incr > 0 then LastIterator := ((e2-e1) DIV incr) * incr + e1. *)
544 expr := BuildSub (location, e2tree, e1tree, FALSE) ;
545 incrtree := BuildConvert (location, GetTreeType (expr), incrtree, FALSE) ;
546 IF TreeOverflow (incrtree)
547 THEN
548 MetaErrorT0 (lastpos,
549 'the intemediate calculation for the last iterator value in the {%kFOR} loop has caused an overflow') ;
550 NoChange := FALSE ;
551 SubQuad (quad) ;
552 success := FALSE
553 ELSE
554 expr := BuildDivFloor (location, expr, incrtree, FALSE) ;
555 expr := BuildMult (location, expr, incrtree, FALSE) ;
556 expr := BuildAdd (location, expr, e1tree, FALSE)
557 END
558 ELSE
559 (* Else use LastIterator := e1 - ((e1-e2) DIV PositiveBy) * PositiveBy
560 to avoid unsigned div signed arithmetic. *)
561 expr := BuildSub (location, e1tree, e2tree, FALSE) ;
562 incrtree := BuildConvert (location, GetM2ZType (), incrtree, FALSE) ;
563 incrtree := BuildNegate (location, incrtree, FALSE) ;
564 incrtree := BuildConvert (location, GetTreeType (expr), incrtree, FALSE) ;
565 IF TreeOverflow (incrtree)
566 THEN
567 MetaErrorT0 (lastpos,
568 'the intemediate calculation for the last iterator value in the {%kFOR} loop has caused an overflow') ;
569 NoChange := FALSE ;
570 SubQuad (quad) ;
571 success := FALSE
572 ELSE
573 expr := BuildSub (location, e1tree, e2tree, FALSE) ;
574 expr := BuildDivFloor (location, expr, incrtree, FALSE) ;
575 expr := BuildMult (location, expr, incrtree, FALSE) ;
576 expr := BuildSub (location, e1tree, expr, FALSE)
577 END
578 END ;
579 IF success
580 THEN
581 IF IsConst (last)
582 THEN
583 AddModGcc (last, expr) ;
584 p (last) ;
585 NoChange := FALSE ;
586 SubQuad (quad)
587 ELSE
588 Assert (NOT constant) ;
589 BuildAssignmentStatement (location, lasttree, expr)
590 END
591 END
592 ELSE
593 MetaErrorT1 (lastpos,
594 'the value {%1Ead} in the {%kBY} clause of the {%kFOR} loop must be constant',
595 incr) ;
596 MetaErrorDecl (incr, TRUE) ;
597 NoChange := FALSE ;
598 SubQuad (quad)
599 END
600 END PerformLastForIterator ;
601
602
603 (*
604 CodeStatement - A multi-way decision call depending on the current
605 quadruple.
606 *)
607
608 PROCEDURE CodeStatement (q: CARDINAL) ;
609 VAR
610 op : QuadOperator ;
611 op1, op2, op3: CARDINAL ;
612 location : location_t ;
613 BEGIN
614 InitBuiltinSyms (BuiltinTokenNo) ;
615 GetQuad(q, op, op1, op2, op3) ;
616 IF op=StatementNoteOp
617 THEN
618 FoldStatementNote (op3) (* Will change CurrentQuadToken using op3. *)
619 ELSE
620 CurrentQuadToken := QuadToTokenNo (q)
621 END ;
622 location := TokenToLocation (CurrentQuadToken) ;
623 CheckReferenced(q, op) ;
624 IF GetDebugTraceQuad ()
625 THEN
626 printf0 ('building: ') ;
627 DisplayQuad (q)
628 END ;
629
630 CASE op OF
631
632 StartDefFileOp : CodeStartDefFile (op3) |
633 StartModFileOp : CodeStartModFile (op3) |
634 ModuleScopeOp : CodeModuleScope (op3) |
635 EndFileOp : CodeEndFile |
636 InitStartOp : CodeInitStart (op3, IsCompilingMainModule (op3)) |
637 InitEndOp : CodeInitEnd (op3, IsCompilingMainModule (op3)) |
638 FinallyStartOp : CodeFinallyStart (op3, IsCompilingMainModule (op3)) |
639 FinallyEndOp : CodeFinallyEnd (op3, IsCompilingMainModule (op3)) |
640 NewLocalVarOp : CodeNewLocalVar (op1, op3) |
641 KillLocalVarOp : CodeKillLocalVar (op3) |
642 ProcedureScopeOp : CodeProcedureScope (op3) |
643 ReturnOp : (* Not used as return is achieved by KillLocalVar. *) |
644 ReturnValueOp : CodeReturnValue (q) |
645 TryOp : CodeTry |
646 ThrowOp : CodeThrow (op3) |
647 CatchBeginOp : CodeCatchBegin |
648 CatchEndOp : CodeCatchEnd |
649 RetryOp : CodeRetry (op3) |
650 DummyOp : |
651 InitAddressOp : CodeInitAddress(q, op1, op2, op3) |
652 BecomesOp : CodeBecomes(q) |
653 ArithAddOp,
654 AddOp : CodeAddChecked (q, op2, op3) |
655 SubOp : CodeSubChecked (q, op2, op3) |
656 MultOp : CodeMultChecked (q, op2, op3) |
657 DivM2Op : CodeDivM2Checked (q, op2, op3) |
658 ModM2Op : CodeModM2Checked (q, op2, op3) |
659 DivTruncOp : CodeDivTrunc (q, op2, op3) |
660 ModTruncOp : CodeModTrunc (q, op2, op3) |
661 DivCeilOp : CodeDivCeil (q, op2, op3) |
662 ModCeilOp : CodeModCeil (q, op2, op3) |
663 DivFloorOp : CodeDivFloor (q, op2, op3) |
664 ModFloorOp : CodeModFloor (q, op2, op3) |
665 GotoOp : CodeGoto (op3) |
666 InclOp : CodeIncl (op1, op3) |
667 ExclOp : CodeExcl (op1, op3) |
668 NegateOp : CodeNegateChecked (q, op1, op3) |
669 LastForIteratorOp : CodeLastForIterator (q) |
670 LogicalShiftOp : CodeSetShift (q, op1, op2, op3) |
671 LogicalRotateOp : CodeSetRotate (q, op1, op2, op3) |
672 LogicalOrOp : CodeSetOr (q) |
673 LogicalAndOp : CodeSetAnd (q) |
674 LogicalXorOp : CodeSetSymmetricDifference (q) |
675 LogicalDiffOp : CodeSetLogicalDifference (q) |
676 IfLessOp : CodeIfLess (q) |
677 IfEquOp : CodeIfEqu (q) |
678 IfNotEquOp : CodeIfNotEqu (q) |
679 IfGreEquOp : CodeIfGreEqu (q) |
680 IfLessEquOp : CodeIfLessEqu (q) |
681 IfGreOp : CodeIfGre (q) |
682 IfInOp : CodeIfIn (q) |
683 IfNotInOp : CodeIfNotIn (q) |
684 IndrXOp : CodeIndrX (q) |
685 XIndrOp : CodeXIndr (q) |
686 CallOp : CodeCall (CurrentQuadToken, op3) |
687 ParamOp : CodeParam (q) |
688 FunctValueOp : CodeFunctValue (location, op1) |
689 AddrOp : CodeAddr (CurrentQuadToken, q, op1, op3) |
690 SizeOp : CodeSize (op1, op3) |
691 UnboundedOp : CodeUnbounded (op1, op3) |
692 RecordFieldOp : CodeRecordField (op1, op2, op3) |
693 HighOp : CodeHigh (op1, op2, op3) |
694 ArrayOp : CodeArray (op1, op2, op3) |
695 ElementSizeOp : InternalError ('ElementSizeOp is expected to have been folded via constant evaluation') |
696 ConvertOp : CodeConvert (q, op1, op2, op3) |
697 CoerceOp : CodeCoerce (q, op1, op2, op3) |
698 CastOp : CodeCast (q, op1, op2, op3) |
699 StandardFunctionOp : CodeStandardFunction (q, op1, op2, op3) |
700 SavePriorityOp : CodeSavePriority (op1, op2, op3) |
701 RestorePriorityOp : CodeRestorePriority (op1, op2, op3) |
702
703 InlineOp : CodeInline (q) |
704 StatementNoteOp : CodeStatementNote (op3) |
705 CodeOnOp : | (* The following make no sense with gcc. *)
706 CodeOffOp : |
707 ProfileOnOp : |
708 ProfileOffOp : |
709 OptimizeOnOp : |
710 OptimizeOffOp : |
711 RangeCheckOp : CodeRange (op3) |
712 ErrorOp : CodeError (op3) |
713 SaveExceptionOp : CodeSaveException (op1, op3) |
714 RestoreExceptionOp : CodeRestoreException (op1, op3)
715
716 ELSE
717 WriteFormat1 ('quadruple %d not yet implemented', q) ;
718 InternalError ('quadruple not implemented yet')
719 END ;
720 LastOperator := op
721 END CodeStatement ;
722
723
724 (*
725 ResolveConstantExpressions - resolves constant expressions from the quadruple list.
726 It returns TRUE if one or more constants were folded.
727 When a constant symbol value is solved, the call back
728 p(sym) is invoked.
729 *)
730
731 PROCEDURE ResolveConstantExpressions (p: WalkAction; bb: BasicBlock) : BOOLEAN ;
732 VAR
733 tokenno: CARDINAL ;
734 quad : CARDINAL ;
735 op : QuadOperator ;
736 op1,
737 op2,
738 op3,
739 op1pos,
740 op2pos,
741 op3pos : CARDINAL ;
742 Changed: BOOLEAN ;
743 start,
744 end : CARDINAL ;
745 BEGIN
746 InitBuiltinSyms (BuiltinTokenNo) ;
747 start := GetBasicBlockStart (bb) ;
748 end := GetBasicBlockEnd (bb) ;
749 Changed := FALSE ;
750 REPEAT
751 NoChange := TRUE ;
752 quad := start ;
753 WHILE (quad<=end) AND (quad#0) DO
754 tokenno := CurrentQuadToken ;
755 IF tokenno=0
756 THEN
757 tokenno := QuadToTokenNo (quad)
758 END ;
759 IF GetDebugTraceQuad ()
760 THEN
761 printf0('examining fold: ') ;
762 DisplayQuad (quad)
763 END ;
764 GetQuadtok (quad, op, op1, op2, op3,
765 op1pos, op2pos, op3pos) ;
766 CASE op OF
767
768 StandardFunctionOp : FoldStandardFunction (tokenno, p, quad, op1, op2, op3) |
769 BuiltinConstOp : FoldBuiltinConst (tokenno, p, quad, op1, op3) |
770 BuiltinTypeInfoOp : FoldBuiltinTypeInfo (tokenno, p, quad, op1, op2, op3) |
771 LogicalOrOp : FoldSetOr (tokenno, p, quad, op1, op2, op3) |
772 LogicalAndOp : FoldSetAnd (tokenno, p, quad, op1, op2, op3) |
773 LogicalXorOp : FoldSymmetricDifference (tokenno, p, quad, op1, op2, op3) |
774 BecomesOp : FoldBecomes (p, bb, quad) |
775 ArithAddOp : FoldArithAdd (op1pos, p, quad, op1, op2, op3) |
776 AddOp : FoldAdd (op1pos, p, quad, op1, op2, op3) |
777 SubOp : FoldSub (op1pos, p, quad, op1, op2, op3) |
778 MultOp : FoldMult (op1pos, p, quad, op1, op2, op3) |
779 DivM2Op : FoldDivM2 (op1pos, p, quad, op1, op2, op3) |
780 ModM2Op : FoldModM2 (op1pos, p, quad, op1, op2, op3) |
781 DivTruncOp : FoldDivTrunc (op1pos, p, quad, op1, op2, op3) |
782 ModTruncOp : FoldModTrunc (op1pos, p, quad, op1, op2, op3) |
783 DivCeilOp : FoldDivCeil (op1pos, p, quad, op1, op2, op3) |
784 ModCeilOp : FoldModCeil (op1pos, p, quad, op1, op2, op3) |
785 DivFloorOp : FoldDivFloor (op1pos, p, quad, op1, op2, op3) |
786 ModFloorOp : FoldModFloor (op1pos, p, quad, op1, op2, op3) |
787 NegateOp : FoldNegate (op1pos, p, quad, op1, op3) |
788 SizeOp : FoldSize (tokenno, p, quad, op1, op2, op3) |
789 RecordFieldOp : FoldRecordField (tokenno, p, quad, op1, op2, op3) |
790 HighOp : FoldHigh (tokenno, p, quad, op1, op2, op3) |
791 ElementSizeOp : FoldElementSize (tokenno, p, quad, op1, op2) |
792 ConvertOp : FoldConvert (tokenno, p, quad, op1, op2, op3) |
793 CoerceOp : FoldCoerce (tokenno, p, quad, op1, op2, op3) |
794 CastOp : FoldCast (tokenno, p, quad, op1, op2, op3) |
795 InclOp : FoldIncl (tokenno, p, quad, op1, op3) |
796 ExclOp : FoldExcl (tokenno, p, quad, op1, op3) |
797 IfEquOp : FoldIfEqu (tokenno, quad, op1, op2, op3) |
798 IfNotEquOp : FoldIfNotEqu (tokenno, quad, op1, op2, op3) |
799 IfLessOp : FoldIfLess (tokenno, quad, op1, op2, op3) |
800 IfLessEquOp : FoldIfLessEqu (tokenno, quad, op1, op2, op3) |
801 IfGreOp : FoldIfGre (tokenno, quad, op1, op2, op3) |
802 IfGreEquOp : FoldIfGreEqu (tokenno, quad, op1, op2, op3) |
803 IfInOp : FoldIfIn (tokenno, quad, op1, op2, op3) |
804 IfNotInOp : FoldIfNotIn (tokenno, quad, op1, op2, op3) |
805 LogicalShiftOp : FoldSetShift(tokenno, p, quad, op1, op2, op3) |
806 LogicalRotateOp : FoldSetRotate (tokenno, p, quad, op1, op2, op3) |
807 ParamOp : FoldBuiltinFunction (tokenno, p, quad, op1, op2, op3) |
808 RangeCheckOp : FoldRange (tokenno, quad, op3) |
809 StatementNoteOp : FoldStatementNote (op3) |
810 StringLengthOp : FoldStringLength (quad, p) |
811 StringConvertM2nulOp: FoldStringConvertM2nul (quad, p) |
812 StringConvertCnulOp : FoldStringConvertCnul (quad, p) |
813 LastForIteratorOp : FoldLastForIterator (quad, p)
814
815 ELSE
816 (* Ignore quadruple as it is not associated with a constant expression. *)
817 END ;
818 quad := GetNextQuad (quad)
819 END ;
820 IF NOT NoChange
821 THEN
822 Changed := TRUE
823 END
824 UNTIL NoChange ;
825 RETURN Changed
826 END ResolveConstantExpressions ;
827
828
829 (*
830 FindSize - given a Modula-2 symbol sym return a gcc tree
831 constant representing the storage size in bytes.
832 *)
833
834 PROCEDURE FindSize (tokenno: CARDINAL; sym: CARDINAL) : tree ;
835 VAR
836 location: location_t ;
837 BEGIN
838 location := TokenToLocation (tokenno) ;
839 IF IsConstString (sym)
840 THEN
841 Assert (IsConstStringKnown (sym)) ;
842 PushCard (GetStringLength (tokenno, sym)) ;
843 RETURN PopIntegerTree ()
844 ELSIF IsSizeSolved (sym)
845 THEN
846 PushSize (sym) ;
847 RETURN PopIntegerTree ()
848 ELSE
849 IF GccKnowsAbout (sym)
850 THEN
851 IF IsVar (sym) AND IsVariableSSA (sym)
852 THEN
853 sym := GetType (sym)
854 END ;
855 PushIntegerTree (BuildSize (location, Mod2Gcc (sym), FALSE)) ;
856 PopSize (sym) ;
857 PushSize (sym) ;
858 RETURN PopIntegerTree ()
859 ELSIF IsVar (sym) AND GccKnowsAbout (GetType (sym))
860 THEN
861 PushIntegerTree (BuildSize (location, Mod2Gcc (GetType (sym)), FALSE)) ;
862 RETURN PopIntegerTree ()
863 ELSE
864 InternalError ('expecting gcc to already know about this symbol')
865 END
866 END
867 END FindSize ;
868
869
870 (*
871 FindType - returns the type of, Sym, if Sym is a TYPE then return Sym otherwise return GetType(Sym)
872 *)
873
874 PROCEDURE FindType (Sym: CARDINAL) : CARDINAL ;
875 BEGIN
876 IF IsType (Sym)
877 THEN
878 RETURN Sym
879 ELSE
880 RETURN GetType (Sym)
881 END
882 END FindType ;
883
884
885 (*
886 BuildTreeFromInterface - generates a GCC tree from an interface definition.
887 *)
888
889 PROCEDURE BuildTreeFromInterface (sym: CARDINAL) : tree ;
890 CONST
891 DebugTokPos = FALSE ;
892 VAR
893 tok : CARDINAL ;
894 i : CARDINAL ;
895 name : Name ;
896 str,
897 obj : CARDINAL ;
898 gccName,
899 asmTree : tree ;
900 BEGIN
901 asmTree := tree (NIL) ;
902 IF sym#NulSym
903 THEN
904 i := 1 ;
905 REPEAT
906 GetRegInterface (sym, i, tok, name, str, obj) ;
907 IF str # NulSym
908 THEN
909 IF IsConstString (str)
910 THEN
911 DeclareConstant (tok, obj) ;
912 IF name = NulName
913 THEN
914 gccName := NIL
915 ELSE
916 gccName := BuildCStringConstant (KeyToCharStar (name), LengthKey (name))
917 END ;
918 asmTree := ChainOnParamValue (asmTree, gccName, PromoteToCString (tok, str),
919 skip_const_decl (Mod2Gcc (obj))) ;
920 IF DebugTokPos
921 THEN
922 WarnStringAt (InitString ('input expression'), tok)
923 END
924 ELSE
925 MetaErrorT1 (tok,
926 'a constraint to the GNU ASM statement must be a constant string and not a {%1Ed}',
927 str)
928 END
929 END ;
930 INC(i)
931 UNTIL (str = NulSym) AND (obj = NulSym) ;
932 END ;
933 RETURN asmTree
934 END BuildTreeFromInterface ;
935
936
937 (*
938 BuildTrashTreeFromInterface - generates a GCC string tree from an interface definition.
939 *)
940
941 PROCEDURE BuildTrashTreeFromInterface (sym: CARDINAL) : tree ;
942 CONST
943 DebugTokPos = FALSE ;
944 VAR
945 tok : CARDINAL ;
946 i : CARDINAL ;
947 str,
948 obj : CARDINAL ;
949 name : Name ;
950 asmTree: tree ;
951 BEGIN
952 asmTree := tree (NIL) ;
953 IF sym # NulSym
954 THEN
955 i := 1 ;
956 REPEAT
957 GetRegInterface (sym, i, tok, name, str, obj) ;
958 IF str # NulSym
959 THEN
960 IF IsConstString (str)
961 THEN
962 asmTree := AddStringToTreeList (asmTree, PromoteToCString (tok, str)) ;
963 IF DebugTokPos
964 THEN
965 WarnStringAt (InitString ('trash expression'), tok)
966 END
967 ELSE
968 MetaErrorT1 (tok,
969 'a constraint to the GNU ASM statement must be a constant string and not a {%1Ed}',
970 str)
971 END
972 END ;
973 INC (i)
974 UNTIL (str = NulSym) AND (obj = NulSym)
975 END ;
976 RETURN asmTree
977 END BuildTrashTreeFromInterface ;
978
979
980 (*
981 CodeInline - InlineOp is a quadruple which has the following format:
982
983 InlineOp NulSym NulSym Sym
984 *)
985
986 PROCEDURE CodeInline (quad: CARDINAL) ;
987 VAR
988 constExpr,
989 overflowChecking: BOOLEAN ;
990 op : QuadOperator ;
991 op1, op2, GnuAsm: CARDINAL ;
992 op1pos, op2pos,
993 op3pos, asmpos : CARDINAL ;
994 string : CARDINAL ;
995 inputs,
996 outputs,
997 trash,
998 labels : tree ;
999 location : location_t ;
1000 BEGIN
1001 GetQuadOtok (quad, asmpos, op, op1, op2, GnuAsm,
1002 overflowChecking, constExpr,
1003 op1pos, op2pos, op3pos) ;
1004 location := TokenToLocation (asmpos) ;
1005 inputs := BuildTreeFromInterface (GetGnuAsmInput (GnuAsm)) ;
1006 outputs := BuildTreeFromInterface (GetGnuAsmOutput (GnuAsm)) ;
1007 trash := BuildTrashTreeFromInterface (GetGnuAsmTrash (GnuAsm)) ;
1008 labels := NIL ; (* At present it makes no sence for Modula-2 to jump to a label,
1009 given that labels are not allowed in Modula-2. *)
1010 string := GetGnuAsm (GnuAsm) ;
1011 BuildAsm (location,
1012 PromoteToCString (GetDeclaredMod (string), string),
1013 IsGnuAsmVolatile (GnuAsm), IsGnuAsmSimple (GnuAsm),
1014 inputs, outputs, trash, labels)
1015 END CodeInline ;
1016
1017
1018 (*
1019 FoldStatementNote - set CurrentQuadToken to tokennno.
1020 *)
1021
1022 PROCEDURE FoldStatementNote (tokenno: CARDINAL) ;
1023 BEGIN
1024 CurrentQuadToken := tokenno
1025 END FoldStatementNote ;
1026
1027
1028 (*
1029 CodeStatementNote - set CurrentQuadToken to tokennno and
1030 add a statement note.
1031 *)
1032
1033 PROCEDURE CodeStatementNote (tokenno: CARDINAL) ;
1034 BEGIN
1035 IF Debugging
1036 THEN
1037 MetaErrorT0 (tokenno, '{%W} statement note')
1038 END ;
1039 CurrentQuadToken := tokenno ;
1040 addStmtNote (TokenToLocation (tokenno))
1041 END CodeStatementNote ;
1042
1043
1044 (*
1045 FoldRange - attempts to fold the range test.
1046 --fixme-- complete this.
1047 *)
1048
1049 PROCEDURE FoldRange (tokenno: CARDINAL; (* p: WalkAction; *)
1050 quad: CARDINAL; rangeno: CARDINAL) ;
1051 BEGIN
1052 FoldRangeCheck (tokenno, quad, rangeno)
1053 END FoldRange ;
1054
1055
1056 (*
1057 CodeSaveException - op1 := op3(TRUE)
1058 *)
1059
1060 PROCEDURE CodeSaveException (des, exceptionProcedure: CARDINAL) ;
1061 VAR
1062 functValue: tree ;
1063 location : location_t;
1064 BEGIN
1065 location := TokenToLocation (CurrentQuadToken) ;
1066 BuildParam (location, Mod2Gcc (True)) ;
1067 BuildFunctionCallTree (location,
1068 Mod2Gcc (exceptionProcedure),
1069 Mod2Gcc (GetType (exceptionProcedure))) ;
1070 functValue := BuildFunctValue (location, Mod2Gcc (des)) ;
1071 AddStatement (location, functValue)
1072 END CodeSaveException ;
1073
1074
1075 (*
1076 CodeRestoreException - op1 := op3(op1).
1077 *)
1078
1079 PROCEDURE CodeRestoreException (des, exceptionProcedure: CARDINAL) ;
1080 VAR
1081 functValue: tree ;
1082 location : location_t;
1083 BEGIN
1084 location := TokenToLocation (CurrentQuadToken) ;
1085 BuildParam (location, Mod2Gcc (des)) ;
1086 BuildFunctionCallTree (location,
1087 Mod2Gcc (exceptionProcedure),
1088 Mod2Gcc (GetType (exceptionProcedure))) ;
1089 functValue := BuildFunctValue (location, Mod2Gcc (des)) ;
1090 AddStatement (location, functValue)
1091 END CodeRestoreException ;
1092
1093
1094 (*
1095 PushScope -
1096 *)
1097
1098 PROCEDURE PushScope (sym: CARDINAL) ;
1099 BEGIN
1100 PushWord (ScopeStack, sym)
1101 END PushScope ;
1102
1103
1104 (*
1105 PopScope -
1106 *)
1107
1108 PROCEDURE PopScope ;
1109 VAR
1110 sym: CARDINAL ;
1111 BEGIN
1112 sym := PopWord (ScopeStack) ;
1113 Assert (sym # NulSym)
1114 END PopScope ;
1115
1116
1117 (*
1118 GetCurrentScopeDescription - returns a description of the current scope.
1119 *)
1120
1121 PROCEDURE GetCurrentScopeDescription () : String ;
1122 VAR
1123 sym : CARDINAL ;
1124 n : String ;
1125 BEGIN
1126 IF IsEmptyWord(ScopeStack)
1127 THEN
1128 InternalError ('not expecting scope stack to be empty')
1129 ELSE
1130 sym := PeepWord(ScopeStack, 1) ;
1131 n := Mark(InitStringCharStar(KeyToCharStar(GetSymName(sym)))) ;
1132 IF IsDefImp(sym)
1133 THEN
1134 RETURN( Sprintf1(Mark(InitString('implementation module %s')), n) )
1135 ELSIF IsModule(sym)
1136 THEN
1137 IF IsInnerModule(sym)
1138 THEN
1139 RETURN( Sprintf1(Mark(InitString('inner module %s')), n) )
1140 ELSE
1141 RETURN( Sprintf1(Mark(InitString('program module %s')), n) )
1142 END
1143 ELSIF IsProcedure(sym)
1144 THEN
1145 IF IsProcedureNested(sym)
1146 THEN
1147 RETURN( Sprintf1(Mark(InitString('nested procedure %s')), n) )
1148 ELSE
1149 RETURN( Sprintf1(Mark(InitString('procedure %s')), n) )
1150 END
1151 ELSE
1152 InternalError ('unexpected scope symbol')
1153 END
1154 END
1155 END GetCurrentScopeDescription ;
1156
1157
1158 (*
1159 CodeRange - encode the range test associated with op3.
1160 *)
1161
1162 PROCEDURE CodeRange (rangeId: CARDINAL) ;
1163 BEGIN
1164 CodeRangeCheck (rangeId, GetCurrentScopeDescription ())
1165 END CodeRange ;
1166
1167
1168 (*
1169 CodeError - encode the error test associated with op3.
1170 *)
1171
1172 PROCEDURE CodeError (errorId: CARDINAL) ;
1173 BEGIN
1174 (* We would like to test whether this position is in the same basicblock
1175 as any known entry point. If so we could emit an error message.
1176 *)
1177 AddStatement (TokenToLocation (CurrentQuadToken),
1178 CodeErrorCheck (errorId, GetCurrentScopeDescription (), NIL))
1179 END CodeError ;
1180
1181
1182 (*
1183 CodeModuleScope - ModuleScopeOp is a quadruple which has the following
1184 format:
1185
1186 ModuleScopeOp _ _ moduleSym
1187
1188 Its purpose is to reset the source file to another
1189 file, hence all line numbers emitted with the
1190 generated code will be relative to this source file.
1191 *)
1192
1193 PROCEDURE CodeModuleScope (moduleSym: CARDINAL) ;
1194 BEGIN
1195 PushScope (moduleSym)
1196 END CodeModuleScope ;
1197
1198
1199 (*
1200 CodeStartModFile - StartModFileOp is a quadruple which has the following
1201 format:
1202
1203 StartModFileOp _ _ moduleSym
1204
1205 A new source file has been encountered therefore
1206 set LastLine to 1.
1207 Call pushGlobalScope.
1208 *)
1209
1210 PROCEDURE CodeStartModFile (moduleSym: CARDINAL) ;
1211 BEGIN
1212 pushGlobalScope ;
1213 LastLine := 1 ;
1214 PushScope (moduleSym)
1215 END CodeStartModFile ;
1216
1217
1218 (*
1219 CodeStartDefFile - StartDefFileOp is a quadruple with the following
1220 format:
1221
1222 StartDefFileOp _ _ moduleSym
1223
1224 A new source file has been encountered therefore
1225 set LastLine to 1.
1226 Call pushGlobalScope.
1227 *)
1228
1229 PROCEDURE CodeStartDefFile (moduleSym: CARDINAL) ;
1230 BEGIN
1231 pushGlobalScope ;
1232 PushScope (moduleSym) ;
1233 LastLine := 1
1234 END CodeStartDefFile ;
1235
1236
1237 (*
1238 CodeEndFile - pops the GlobalScope.
1239 *)
1240
1241 PROCEDURE CodeEndFile ;
1242 BEGIN
1243 popGlobalScope
1244 END CodeEndFile ;
1245
1246
1247 (*
1248 CallInnerInit - produce a call to inner module initialization routine.
1249 *)
1250
1251 PROCEDURE CallInnerInit (moduleSym: WORD) ;
1252 VAR
1253 location : location_t;
1254 ctor, init, fini, dep: CARDINAL ;
1255 BEGIN
1256 location := TokenToLocation (CurrentQuadToken) ;
1257 GetModuleCtors (moduleSym, ctor, init, fini, dep) ;
1258 BuildCallInner (location, Mod2Gcc (init))
1259 END CallInnerInit ;
1260
1261
1262 (*
1263 CallInnerFinally - produce a call to inner module finalization routine.
1264 *)
1265
1266 PROCEDURE CallInnerFinally (moduleSym: WORD) ;
1267 VAR
1268 location : location_t;
1269 ctor, init, fini, dep: CARDINAL ;
1270 BEGIN
1271 location := TokenToLocation (CurrentQuadToken) ;
1272 GetModuleCtors (moduleSym, ctor, init, fini, dep) ;
1273 BuildCallInner (location, Mod2Gcc (fini))
1274 END CallInnerFinally ;
1275
1276
1277 (*
1278 CodeInitStart - emits starting code before the main BEGIN END of the
1279 current module.
1280 *)
1281
1282 PROCEDURE CodeInitStart (moduleSym: CARDINAL;
1283 CompilingMainModule: BOOLEAN) ;
1284 VAR
1285 location : location_t;
1286 ctor, init,
1287 fini, dep : CARDINAL ;
1288 BEGIN
1289 IF CompilingMainModule OR WholeProgram
1290 THEN
1291 location := TokenToLocation (CurrentQuadToken) ;
1292 GetModuleCtors (moduleSym, ctor, init, fini, dep) ;
1293 BuildStartFunctionCode (location, Mod2Gcc (init),
1294 IsExportedGcc (init), FALSE) ;
1295 ForeachInnerModuleDo (moduleSym, CallInnerInit)
1296 END
1297 END CodeInitStart ;
1298
1299
1300 (*
1301 CodeInitEnd - emits terminating code after the main BEGIN END of the
1302 current module.
1303 *)
1304
1305 PROCEDURE CodeInitEnd (moduleSym: CARDINAL;
1306 CompilingMainModule: BOOLEAN) ;
1307 VAR
1308 location : location_t;
1309 ctor, init,
1310 fini, dep : CARDINAL ;
1311 BEGIN
1312 IF CompilingMainModule OR WholeProgram
1313 THEN
1314 location := TokenToLocation (GetDeclaredMod (moduleSym)) ;
1315 GetModuleCtors (moduleSym, ctor, init, fini, dep) ;
1316 finishFunctionDecl (location, Mod2Gcc (init)) ;
1317 BuildEndFunctionCode (location, Mod2Gcc (init),
1318 IsModuleWithinProcedure (moduleSym))
1319 END
1320 END CodeInitEnd ;
1321
1322
1323 (*
1324 CodeFinallyStart - emits starting code before the main BEGIN END of the
1325 current module.
1326 *)
1327
1328 PROCEDURE CodeFinallyStart (moduleSym: CARDINAL;
1329 CompilingMainModule: BOOLEAN) ;
1330 VAR
1331 location : location_t;
1332 ctor, init,
1333 fini, dep : CARDINAL ;
1334 BEGIN
1335 IF CompilingMainModule OR WholeProgram
1336 THEN
1337 location := TokenToLocation (CurrentQuadToken) ;
1338 GetModuleCtors (moduleSym, ctor, init, fini, dep) ;
1339 BuildStartFunctionCode (location, Mod2Gcc (fini),
1340 IsExportedGcc (fini), FALSE) ;
1341 ForeachInnerModuleDo (moduleSym, CallInnerFinally)
1342 END
1343 END CodeFinallyStart ;
1344
1345
1346 (*
1347 CodeFinallyEnd - emits terminating code after the main BEGIN END of the
1348 current module. It also creates the scaffold if the
1349 cflag was not present.
1350 *)
1351
1352 PROCEDURE CodeFinallyEnd (moduleSym: CARDINAL;
1353 CompilingMainModule: BOOLEAN) ;
1354 VAR
1355 location : location_t;
1356 tokenpos : CARDINAL ;
1357 ctor, init,
1358 fini, dep : CARDINAL ;
1359 BEGIN
1360 IF CompilingMainModule OR WholeProgram
1361 THEN
1362 tokenpos := GetDeclaredMod (moduleSym) ;
1363 location := TokenToLocation (tokenpos) ;
1364 GetModuleCtors (moduleSym, ctor, init, fini, dep) ;
1365 finishFunctionDecl (location, Mod2Gcc (fini)) ;
1366 BuildEndFunctionCode (location, Mod2Gcc (fini),
1367 IsModuleWithinProcedure (moduleSym))
1368 END
1369 END CodeFinallyEnd ;
1370
1371
1372 (*
1373 GetAddressOfUnbounded - returns the address of the unbounded array contents.
1374 *)
1375
1376 PROCEDURE GetAddressOfUnbounded (location: location_t; param: CARDINAL) : tree ;
1377 VAR
1378 UnboundedType: CARDINAL ;
1379 BEGIN
1380 UnboundedType := GetType (param) ;
1381 Assert (IsUnbounded (UnboundedType)) ;
1382
1383 RETURN BuildConvert (TokenToLocation (GetDeclaredMod (param)),
1384 GetPointerType (),
1385 BuildComponentRef (location, Mod2Gcc (param), Mod2Gcc (GetUnboundedAddressOffset (UnboundedType))),
1386 FALSE)
1387 END GetAddressOfUnbounded ;
1388
1389
1390 (*
1391 GetHighFromUnbounded - returns a Tree containing the value of
1392 param.HIGH.
1393 *)
1394
1395 PROCEDURE GetHighFromUnbounded (location: location_t; dim, param: CARDINAL) : tree ;
1396 VAR
1397 UnboundedType,
1398 ArrayType,
1399 HighField : CARDINAL ;
1400 HighTree : tree ;
1401 accessibleDim: CARDINAL ;
1402 (* remainingDim : CARDINAL ; *)
1403 BEGIN
1404 UnboundedType := GetType (param) ;
1405 Assert (IsUnbounded (UnboundedType)) ;
1406 ArrayType := GetType (UnboundedType) ;
1407 HighField := GetUnboundedHighOffset (UnboundedType, dim) ;
1408 IF HighField = NulSym
1409 THEN
1410 (* It might be a dynamic array of static arrays,
1411 so lets see if there is an earlier dimension available. *)
1412 accessibleDim := dim ;
1413 WHILE (HighField = NulSym) AND (accessibleDim > 1) DO
1414 DEC (accessibleDim) ;
1415 HighField := GetUnboundedHighOffset(UnboundedType, accessibleDim)
1416 END ;
1417 IF HighField = NulSym
1418 THEN
1419 MetaError1 ('{%EkHIGH} dimension number {%1N} for array does not exist', dim) ;
1420 RETURN GetCardinalZero (location)
1421 ELSE
1422 (* remainingDim := dim - accessibleDim ; --fixme-- write tests to stress this code. *)
1423 HighTree := BuildHighFromStaticArray (location, (* remainingDim, *) ArrayType) ;
1424 IF HighTree = NIL
1425 THEN
1426 MetaError1 ('{%EkHIGH} dimension number {%1N} for array does not exist', dim) ;
1427 RETURN GetCardinalZero (location)
1428 END ;
1429 RETURN HighTree
1430 END
1431 ELSE
1432 RETURN BuildComponentRef (location, Mod2Gcc (param), Mod2Gcc (HighField))
1433 END
1434 END GetHighFromUnbounded ;
1435
1436
1437 (*
1438 GetSizeOfHighFromUnbounded - returns a Tree containing the value of
1439 param.HIGH * sizeof(unboundedType).
1440 The number of legal bytes this array
1441 occupies.
1442 *)
1443
1444 PROCEDURE GetSizeOfHighFromUnbounded (tokenno: CARDINAL; param: CARDINAL) : tree ;
1445 VAR
1446 t : tree ;
1447 UnboundedType,
1448 ArrayType : CARDINAL ;
1449 i, n : CARDINAL ;
1450 location : location_t;
1451 BEGIN
1452 location := TokenToLocation(tokenno) ;
1453 UnboundedType := GetType(param) ;
1454 Assert(IsUnbounded(UnboundedType)) ;
1455 ArrayType := GetType(UnboundedType) ;
1456
1457 i := 1 ;
1458 n := GetDimension(UnboundedType) ;
1459 t := GetCardinalOne(location) ;
1460 WHILE i<=n DO
1461 t := BuildMult(location,
1462 BuildAdd(location,
1463 GetHighFromUnbounded(location, i, param),
1464 GetCardinalOne(location),
1465 FALSE),
1466 t, FALSE) ;
1467 (* Remember we must add one as a[HIGH(a)] is the last accessible element of the array. *)
1468 INC(i)
1469 END ;
1470 RETURN( BuildConvert(location,
1471 GetCardinalType(),
1472 BuildMult(location,
1473 t, BuildConvert(location,
1474 GetCardinalType(),
1475 FindSize(tokenno, ArrayType), FALSE), FALSE),
1476 FALSE) )
1477 END GetSizeOfHighFromUnbounded ;
1478
1479
1480 (*
1481 MaybeDebugBuiltinAlloca - if DebugBuiltins is set
1482 then call Builtins.alloca_trace
1483 else call Builtins.alloca.
1484 *)
1485
1486 PROCEDURE MaybeDebugBuiltinAlloca (location: location_t; tok: CARDINAL; high: tree) : tree ;
1487 VAR
1488 call,
1489 memptr,
1490 func : tree ;
1491 BEGIN
1492 IF DebugBuiltins
1493 THEN
1494 func := Mod2Gcc (FromModuleGetSym (tok,
1495 MakeKey ('alloca_trace'),
1496 MakeDefinitionSource (tok,
1497 MakeKey ('Builtins')))) ;
1498 call := BuiltInAlloca (location, high) ;
1499 SetLastFunction (call) ;
1500 memptr := BuildFunctValue (location, call) ;
1501 call := BuildCall2 (location, func, GetPointerType(), memptr, high) ;
1502 ELSE
1503 call := BuiltInAlloca (location, high)
1504 END ;
1505 SetLastFunction (call) ;
1506 RETURN BuildFunctValue (location, call)
1507 END MaybeDebugBuiltinAlloca ;
1508
1509
1510 (*
1511 MaybeDebugBuiltinMemcpy - if DebugBuiltins is set
1512 then call memcpy
1513 else call Builtins.memcpy.
1514 *)
1515
1516 PROCEDURE MaybeDebugBuiltinMemcpy (location: location_t; src, dest, nbytes: tree) : tree ;
1517 VAR
1518 call,
1519 func: tree ;
1520 BEGIN
1521 IF DebugBuiltins
1522 THEN
1523 func := Mod2Gcc (Memcpy) ;
1524 call := BuildCall3 (location, func, GetPointerType (), src, dest, nbytes) ;
1525 ELSE
1526 call := BuiltinMemCopy (location, src, dest, nbytes)
1527 END ;
1528 SetLastFunction (call) ;
1529 RETURN BuildFunctValue (location, call)
1530 END MaybeDebugBuiltinMemcpy ;
1531
1532
1533 (*
1534 MakeCopyUse - make a copy of the unbounded array and alter all references
1535 from the old unbounded array to the new unbounded array.
1536 The parameter, param, contains a RECORD
1537 ArrayAddress: ADDRESS ;
1538 ArrayHigh : CARDINAL ;
1539 END
1540 we simply declare a new array of size, ArrayHigh
1541 and set ArrayAddress to the address of the copy.
1542
1543 Remember ArrayHigh == sizeof(Array)-sizeof(typeof(array))
1544 so we add 1 for the size and add 1 for a possible <nul>
1545 *)
1546
1547 PROCEDURE MakeCopyUse (tokenno: CARDINAL; param: CARDINAL) ;
1548 VAR
1549 location : location_t;
1550 UnboundedType: CARDINAL ;
1551 Addr,
1552 High,
1553 NewArray : tree ;
1554 BEGIN
1555 location := TokenToLocation (tokenno) ;
1556 UnboundedType := GetType (param) ;
1557 Assert (IsUnbounded (UnboundedType)) ;
1558
1559 High := GetSizeOfHighFromUnbounded (tokenno, param) ;
1560 Addr := GetAddressOfUnbounded (location, param) ;
1561
1562 NewArray := MaybeDebugBuiltinAlloca (location, tokenno, High) ;
1563 NewArray := MaybeDebugBuiltinMemcpy (location, NewArray, Addr, High) ;
1564
1565 (* Now assign param.Addr := ADR(NewArray). *)
1566
1567 BuildAssignmentStatement (location,
1568 BuildComponentRef (location,
1569 Mod2Gcc (param),
1570 Mod2Gcc (GetUnboundedAddressOffset (UnboundedType))),
1571 NewArray)
1572 END MakeCopyUse ;
1573
1574
1575 (*
1576 GetParamAddress - returns the address of parameter, param.
1577 *)
1578
1579 PROCEDURE GetParamAddress (location: location_t; proc, param: CARDINAL) : tree ;
1580 VAR
1581 sym,
1582 type: CARDINAL ;
1583 BEGIN
1584 IF IsParameter (param)
1585 THEN
1586 type := GetType (param) ;
1587 sym := GetLocalSym (proc, GetSymName (param)) ;
1588 IF IsUnbounded (type)
1589 THEN
1590 RETURN( GetAddressOfUnbounded (location, sym) )
1591 ELSE
1592 Assert (GetMode (sym) = LeftValue) ;
1593 RETURN( Mod2Gcc (sym) )
1594 END
1595 ELSE
1596 Assert (IsVar (param)) ;
1597 Assert (GetMode (param) = LeftValue) ;
1598 RETURN( Mod2Gcc(param) )
1599 END
1600 END GetParamAddress ;
1601
1602
1603 (*
1604 IsUnboundedWrittenTo - returns TRUE if the unbounded parameter
1605 might be written to, or if -funbounded-by-reference
1606 was _not_ specified.
1607 *)
1608
1609 PROCEDURE IsUnboundedWrittenTo (proc, param: CARDINAL) : BOOLEAN ;
1610 VAR
1611 f : String ;
1612 l : CARDINAL ;
1613 sym : CARDINAL ;
1614 n1, n2: Name ;
1615 BEGIN
1616 sym := GetLocalSym(proc, GetSymName(param)) ;
1617 IF sym=NulSym
1618 THEN
1619 InternalError ('should find symbol in table')
1620 ELSE
1621 IF UnboundedByReference
1622 THEN
1623 IF (NOT GetVarWritten(sym)) AND VerboseUnbounded
1624 THEN
1625 n1 := GetSymName(sym) ;
1626 n2 := GetSymName(proc) ;
1627 f := FindFileNameFromToken(GetDeclaredMod(sym), 0) ;
1628 l := TokenToLineNo(GetDeclaredMod(sym), 0) ;
1629 printf4('%s:%d:non VAR unbounded parameter %a in procedure %a does not need to be copied\n',
1630 f, l, n1, n2)
1631 END ;
1632 RETURN( GetVarWritten(sym) )
1633 ELSE
1634 RETURN( TRUE )
1635 END
1636 END
1637 END IsUnboundedWrittenTo ;
1638
1639
1640 (*
1641 GetParamSize - returns the size in bytes of, param.
1642 *)
1643
1644 PROCEDURE GetParamSize (tokenno: CARDINAL; param: CARDINAL) : tree ;
1645 BEGIN
1646 Assert(IsVar(param) OR IsParameter(param)) ;
1647 IF IsUnbounded(param)
1648 THEN
1649 RETURN GetSizeOfHighFromUnbounded(tokenno, param)
1650 ELSE
1651 RETURN BuildSize (TokenToLocation (tokenno), Mod2Gcc (GetType (param)), FALSE)
1652 END
1653 END GetParamSize ;
1654
1655
1656 (*
1657 DoIsIntersection - jumps to, tLabel, if the ranges i1..i2 j1..j2 overlap
1658 else jump to, fLabel.
1659 *)
1660
1661 PROCEDURE DoIsIntersection (tokenno: CARDINAL; ta, tb, tc, td: tree; tLabel, fLabel: String) ;
1662 VAR
1663 location: location_t ;
1664 BEGIN
1665 location := TokenToLocation(tokenno) ;
1666 (*
1667 if (ta>td) OR (tb<tc)
1668 then
1669 goto fLabel
1670 else
1671 goto tLabel
1672 fi
1673 *)
1674 DoJump(location, BuildGreaterThan(location, ta, td), NIL, string(fLabel)) ;
1675 DoJump(location, BuildLessThan(location, tb, tc), NIL, string(fLabel)) ;
1676 BuildGoto(location, string(tLabel)) ;
1677 IF CascadedDebugging
1678 THEN
1679 printf1('label used %s\n', tLabel) ;
1680 printf1('label used %s\n', fLabel)
1681 END
1682 END DoIsIntersection ;
1683
1684
1685 (*
1686 BuildCascadedIfThenElsif - mustCheck contains a list of variables which
1687 must be checked against the address of (proc, param, i).
1688 If the address matches we make a copy of the unbounded
1689 parameter (proc, param) and quit further checking.
1690 *)
1691
1692 PROCEDURE BuildCascadedIfThenElsif (tokenno: CARDINAL;
1693 mustCheck: List;
1694 proc, param: CARDINAL) ;
1695 VAR
1696 ta, tb,
1697 tc, td : tree ;
1698 n, j : CARDINAL ;
1699 tLabel,
1700 fLabel,
1701 nLabel : String ;
1702 location: location_t ;
1703 BEGIN
1704 location := TokenToLocation(tokenno) ;
1705 n := NoOfItemsInList(mustCheck) ;
1706 (* We want a sequence of if then elsif statements. *)
1707 IF n>0
1708 THEN
1709 INC(UnboundedLabelNo) ;
1710 j := 1 ;
1711 ta := GetAddressOfUnbounded(location, param) ;
1712 tb := BuildConvert(TokenToLocation(tokenno),
1713 GetPointerType(),
1714 BuildAddAddress(location, ta, GetSizeOfHighFromUnbounded(tokenno, param)),
1715 FALSE) ;
1716 WHILE j<=n DO
1717 IF j>1
1718 THEN
1719 nLabel := CreateLabelProcedureN(proc, "n", UnboundedLabelNo, j) ;
1720 IF CascadedDebugging
1721 THEN
1722 printf1('label declared %s\n', nLabel)
1723 END ;
1724 DeclareLabel(location, string(nLabel)) ;
1725 END ;
1726 tc := GetParamAddress(location, proc, GetItemFromList(mustCheck, j)) ;
1727 td := BuildConvert(TokenToLocation(tokenno),
1728 GetPointerType(),
1729 BuildAddAddress(location, tc, GetParamSize(tokenno, param)),
1730 FALSE) ;
1731 tLabel := CreateLabelProcedureN(proc, "t", UnboundedLabelNo, j+1) ;
1732 fLabel := CreateLabelProcedureN(proc, "f", UnboundedLabelNo, j+1) ;
1733 DoIsIntersection(tokenno, ta, tb, tc, td, tLabel, fLabel) ;
1734 IF CascadedDebugging
1735 THEN
1736 printf1('label declared %s\n', tLabel)
1737 END ;
1738 DeclareLabel (location, string (tLabel)) ;
1739 MakeCopyUse (tokenno, param) ;
1740 IF j<n
1741 THEN
1742 nLabel := CreateLabelProcedureN(proc, "n", UnboundedLabelNo, n+1) ;
1743 BuildGoto(location, string(nLabel)) ;
1744 IF CascadedDebugging
1745 THEN
1746 printf1('goto %s\n', nLabel)
1747 END
1748 END ;
1749 IF CascadedDebugging
1750 THEN
1751 printf1('label declared %s\n', fLabel)
1752 END ;
1753 DeclareLabel(location, string(fLabel)) ;
1754 INC(j)
1755 END ;
1756 (*
1757 nLabel := CreateLabelProcedureN(proc, "fin", UnboundedLabelNo, n+1) ;
1758 IF CascadedDebugging
1759 THEN
1760 printf1('label declared %s\n', nLabel)
1761 END ;
1762 DeclareLabel(location, string(nLabel))
1763 *)
1764 END
1765 END BuildCascadedIfThenElsif ;
1766
1767
1768 (*
1769 CheckUnboundedNonVarParameter - if non var unbounded parameter is written to
1770 then
1771 make a copy of the contents of this parameter
1772 and use the copy
1773 else if param
1774 is type compatible with any parameter, symv
1775 and at runtime its address matches symv
1776 then
1777 make a copy of the contents of this parameter
1778 and use the copy
1779 fi
1780 *)
1781
1782 PROCEDURE CheckUnboundedNonVarParameter (tokenno: CARDINAL;
1783 trashed: List;
1784 proc, param: CARDINAL) ;
1785 VAR
1786 mustCheck : List ;
1787 paramTrashed,
1788 n, j : CARDINAL ;
1789 f : String ;
1790 l : CARDINAL ;
1791 n1, n2 : Name ;
1792 BEGIN
1793 IF IsUnboundedWrittenTo(proc, param)
1794 THEN
1795 MakeCopyUse (tokenno, param)
1796 ELSE
1797 InitList(mustCheck) ;
1798 n := NoOfItemsInList(trashed) ;
1799 j := 1 ;
1800 WHILE j<=n DO
1801 paramTrashed := GetItemFromList(trashed, j) ;
1802 IF IsAssignmentCompatible(GetLowestType(param), GetLowestType(paramTrashed))
1803 THEN
1804 (* We must check whether this unbounded parameter has the same
1805 address as the trashed parameter. *)
1806 IF VerboseUnbounded
1807 THEN
1808 n1 := GetSymName(paramTrashed) ;
1809 n2 := GetSymName(proc) ;
1810 f := FindFileNameFromToken(GetDeclaredMod(paramTrashed), 0) ;
1811 l := TokenToLineNo(GetDeclaredMod(paramTrashed), 0) ;
1812 printf4('%s:%d:must check at runtime the address of parameter, %a, in procedure, %a, whose contents will be trashed\n',
1813 f, l, n1, n2) ;
1814 n1 := GetSymName(param) ;
1815 n2 := GetSymName(paramTrashed) ;
1816 printf4('%s:%d:against address of parameter, %a, possibly resulting in a copy of parameter, %a\n',
1817 f, l, n1, n2)
1818 END ;
1819 PutItemIntoList(mustCheck, paramTrashed)
1820 END ;
1821 INC(j)
1822 END ;
1823 (* Now we build a sequence of if then { elsif then } end to check addresses. *)
1824 BuildCascadedIfThenElsif (tokenno, mustCheck, proc, param) ;
1825 KillList(mustCheck)
1826 END
1827 END CheckUnboundedNonVarParameter ;
1828
1829
1830 (*
1831 IsParameterWritten - returns TRUE if a parameter, sym, is written to.
1832 *)
1833
1834 PROCEDURE IsParameterWritten (proc: CARDINAL; sym: CARDINAL) : BOOLEAN ;
1835 BEGIN
1836 IF IsParameter(sym)
1837 THEN
1838 sym := GetLocalSym(proc, GetSymName(sym))
1839 END ;
1840 IF IsVar(sym)
1841 THEN
1842 (* Unbounded arrays will appear as vars. *)
1843 RETURN GetVarWritten(sym)
1844 END ;
1845 InternalError ('expecting IsVar to return TRUE')
1846 END IsParameterWritten ;
1847
1848
1849 (*
1850 SaveNonVarUnboundedParameters - for each var parameter, symv, do
1851 (* not just unbounded var parameters, but _all_
1852 parameters *)
1853 if symv is written to
1854 then
1855 add symv to a compile list
1856 fi
1857 done
1858
1859 for each parameter of procedure, symu, do
1860 if non var unbounded parameter is written to
1861 then
1862 make a copy of the contents of this parameter
1863 and use the copy
1864 else if
1865 symu is type compatible with any parameter, symv
1866 and at runtime its address matches symv
1867 then
1868 make a copy of the contents of this parameter
1869 and use the copy
1870 fi
1871 done
1872 *)
1873
1874 PROCEDURE SaveNonVarUnboundedParameters (tokenno: CARDINAL; proc: CARDINAL) ;
1875 VAR
1876 i, p : CARDINAL ;
1877 trashed: List ;
1878 f : String ;
1879 sym : CARDINAL ;
1880 l : CARDINAL ;
1881 n1, n2 : Name ;
1882 BEGIN
1883 InitList(trashed) ;
1884 i := 1 ;
1885 p := NoOfParamAny (proc) ;
1886 WHILE i<=p DO
1887 sym := GetNthParamAny (proc, i) ;
1888 IF IsParameterWritten(proc, sym)
1889 THEN
1890 IF VerboseUnbounded
1891 THEN
1892 n1 := GetSymName(sym) ;
1893 n2 := GetSymName(proc) ;
1894 f := FindFileNameFromToken(GetDeclaredMod(sym), 0) ;
1895 l := TokenToLineNo(GetDeclaredMod(sym), 0) ;
1896 printf4('%s:%d:parameter, %a, in procedure, %a, is trashed\n',
1897 f, l, n1, n2)
1898 END ;
1899 PutItemIntoList(trashed, sym)
1900 END ;
1901 INC(i)
1902 END ;
1903 (* Now see whether we need to copy any unbounded array parameters. *)
1904 i := 1 ;
1905 p := NoOfParamAny (proc) ;
1906 WHILE i<=p DO
1907 IF IsUnboundedParamAny (proc, i) AND (NOT IsVarParamAny (proc, i))
1908 THEN
1909 CheckUnboundedNonVarParameter (tokenno, trashed, proc, GetNth (proc, i))
1910 END ;
1911 INC(i)
1912 END ;
1913 KillList(trashed)
1914 END SaveNonVarUnboundedParameters ;
1915
1916
1917 (*
1918 AutoInitVariable -
1919 *)
1920
1921 PROCEDURE AutoInitVariable (location: location_t; sym: CARDINAL) ;
1922 VAR
1923 type: CARDINAL ;
1924 BEGIN
1925 IF (NOT IsParameter (sym)) AND IsVar (sym) AND
1926 (NOT IsTemporary (sym))
1927 THEN
1928 (* PrintSym (sym) ; *)
1929 type := SkipType (GetType (sym)) ;
1930 (* The type SYSTEM.ADDRESS is a pointer type. *)
1931 IF IsPointer (type)
1932 THEN
1933 BuildAssignmentStatement (location,
1934 Mod2Gcc (sym),
1935 BuildConvert (location,
1936 Mod2Gcc (GetType (sym)),
1937 GetPointerZero (location),
1938 TRUE))
1939 END
1940 END
1941 END AutoInitVariable ;
1942
1943
1944 (*
1945 AutoInitialize - scope will be a procedure, module or defimp. All pointer
1946 variables are assigned to NIL.
1947 *)
1948
1949 PROCEDURE AutoInitialize (location: location_t; scope: CARDINAL) ;
1950 VAR
1951 i, n: CARDINAL ;
1952 BEGIN
1953 IF AutoInit
1954 THEN
1955 n := NoOfVariables (scope) ;
1956 i := 1 ;
1957 IF IsProcedure (scope)
1958 THEN
1959 (* The parameters are stored as local variables. *)
1960 INC (i, NoOfParamAny (scope))
1961 END ;
1962 WHILE i <= n DO
1963 AutoInitVariable (location, GetNth (scope, i)) ;
1964 INC (i)
1965 END
1966 END
1967 END AutoInitialize ;
1968
1969
1970 (*
1971 CodeNewLocalVar - Builds a new frame on the stack to contain the procedure
1972 local variables.
1973 *)
1974
1975 PROCEDURE CodeNewLocalVar (tokenno, CurrentProcedure: CARDINAL) ;
1976 VAR
1977 begin, end: CARDINAL ;
1978 BEGIN
1979 (* Callee saves non var unbounded parameter contents. *)
1980 SaveNonVarUnboundedParameters (tokenno, CurrentProcedure) ;
1981 BuildPushFunctionContext ;
1982 GetProcedureBeginEnd (CurrentProcedure, begin, end) ;
1983 CurrentQuadToken := begin ;
1984 SetBeginLocation (TokenToLocation (begin)) ;
1985 AutoInitialize (TokenToLocation (begin), CurrentProcedure) ;
1986 ForeachProcedureDo (CurrentProcedure, CodeBlock) ;
1987 ForeachInnerModuleDo (CurrentProcedure, CodeBlock) ;
1988 BuildPopFunctionContext ;
1989 ForeachInnerModuleDo (CurrentProcedure, CallInnerInit)
1990 END CodeNewLocalVar ;
1991
1992
1993 (*
1994 CodeKillLocalVar - removes local variables and returns to previous scope.
1995 *)
1996
1997 PROCEDURE CodeKillLocalVar (CurrentProcedure: CARDINAL) ;
1998 VAR
1999 begin, end: CARDINAL ;
2000 proc : tree ;
2001 BEGIN
2002 GetProcedureBeginEnd (CurrentProcedure, begin, end) ;
2003 CurrentQuadToken := end ;
2004 proc := NIL ;
2005 IF IsCtor (CurrentProcedure)
2006 THEN
2007 proc := DeclareModuleCtor (Mod2Gcc (CurrentProcedure))
2008 END ;
2009 BuildEndFunctionCode (TokenToLocation (end),
2010 Mod2Gcc (CurrentProcedure),
2011 IsProcedureGccNested (CurrentProcedure)) ;
2012 IF IsCtor (CurrentProcedure) AND (proc # NIL)
2013 THEN
2014 BuildModuleCtor (proc)
2015 END ;
2016 PoisonSymbols (CurrentProcedure) ;
2017 removeStmtNote () ;
2018 PopScope
2019 END CodeKillLocalVar ;
2020
2021
2022 (*
2023 CodeProcedureScope - start a procedure scope for CurrentProcedure.
2024 *)
2025
2026 PROCEDURE CodeProcedureScope (CurrentProcedure: CARDINAL) ;
2027 VAR
2028 begin, end: CARDINAL ;
2029 BEGIN
2030 removeStmtNote () ;
2031 GetProcedureBeginEnd (CurrentProcedure, begin, end) ;
2032 BuildStartFunctionCode (TokenToLocation (begin),
2033 Mod2Gcc (CurrentProcedure),
2034 IsExportedGcc (CurrentProcedure),
2035 IsProcedureInline (CurrentProcedure)) ;
2036 StartDeclareScope (CurrentProcedure) ;
2037 PushScope (CurrentProcedure) ;
2038 (* DeclareParameters(CurrentProcedure) *)
2039 END CodeProcedureScope ;
2040
2041
2042 (*
2043 CodeReturnValue - places the operand into the return value space
2044 allocated by the function call.
2045 *)
2046
2047 PROCEDURE CodeReturnValue (quad: CARDINAL) ;
2048 VAR
2049 op : QuadOperator ;
2050 constExpr,
2051 overflowChecking : BOOLEAN ;
2052 expr, none, procedure : CARDINAL ;
2053 combinedpos,
2054 returnpos, exprpos, nonepos, procpos: CARDINAL ;
2055 value, length : tree ;
2056 location : location_t ;
2057 BEGIN
2058 GetQuadOtok (quad, returnpos, op, expr, none, procedure,
2059 overflowChecking, constExpr,
2060 exprpos, nonepos, procpos) ;
2061 combinedpos := MakeVirtualTok (returnpos, returnpos, exprpos) ;
2062 location := TokenToLocation (combinedpos) ;
2063 TryDeclareConstant (exprpos, expr) ; (* Checks to see whether it is a constant and declares it. *)
2064 TryDeclareConstructor (exprpos, expr) ;
2065 IF IsConstString (expr) AND (SkipTypeAndSubrange (GetType (procedure)) # Char)
2066 THEN
2067 IF NOT PrepareCopyString (returnpos, length, value, expr, GetType (procedure))
2068 THEN
2069 MetaErrorT3 (MakeVirtualTok (returnpos, returnpos, exprpos),
2070 'string constant {%1Ea} is too large to be returned from procedure {%2a} via the {%3d} {%3a}',
2071 expr, procedure, GetType (procedure))
2072 END ;
2073 value := BuildArrayStringConstructor (location,
2074 Mod2Gcc (GetType (procedure)),
2075 value, length)
2076 ELSE
2077 value := Mod2Gcc (expr)
2078 END ;
2079 BuildReturnValueCode (location, Mod2Gcc (procedure), value)
2080 END CodeReturnValue ;
2081
2082
2083 (*
2084 CodeCall - determines whether the procedure call is a direct call
2085 or an indirect procedure call.
2086 *)
2087
2088 PROCEDURE CodeCall (tokenno: CARDINAL; procedure: CARDINAL) ;
2089 VAR
2090 callTree: tree ;
2091 location: location_t ;
2092 BEGIN
2093 IF IsProcedure (procedure)
2094 THEN
2095 DeclareParameters (procedure) ;
2096 callTree := CodeDirectCall (tokenno, procedure)
2097 ELSIF IsProcType (SkipType (GetType (procedure)))
2098 THEN
2099 DeclareParameters (SkipType (GetType (procedure))) ;
2100 callTree := CodeIndirectCall (tokenno, procedure) ;
2101 procedure := SkipType (GetType (procedure))
2102 ELSE
2103 InternalError ('expecting Procedure or ProcType')
2104 END ;
2105 IF GetType (procedure) = NulSym
2106 THEN
2107 location := TokenToLocation (tokenno) ;
2108 AddStatement (location, callTree)
2109 ELSE
2110 (* Leave tree alone - as it will be picked up when processing FunctValue. *)
2111 END
2112 END CodeCall ;
2113
2114
2115 (*
2116 UseBuiltin - returns a Tree containing the builtin function
2117 and parameters. It should only be called if
2118 CanUseBuiltin or IsProcedureBuiltinAvailable returns TRUE.
2119 *)
2120
2121 PROCEDURE UseBuiltin (tokenno: CARDINAL; Sym: CARDINAL) : tree ;
2122 BEGIN
2123 IF BuiltinExists(KeyToCharStar(GetProcedureBuiltin(Sym)))
2124 THEN
2125 RETURN( BuildBuiltinTree(TokenToLocation (tokenno), KeyToCharStar (GetProcedureBuiltin (Sym))) )
2126 ELSE
2127 RETURN( BuildBuiltinTree(TokenToLocation (tokenno), KeyToCharStar (GetSymName (Sym))) )
2128 END
2129 END UseBuiltin ;
2130
2131
2132 (*
2133 CodeDirectCall - calls a function/procedure.
2134 *)
2135
2136 PROCEDURE CodeDirectCall (tokenno: CARDINAL; procedure: CARDINAL) : tree ;
2137 VAR
2138 location: location_t ;
2139 call : tree ;
2140 BEGIN
2141 location := TokenToLocation (tokenno) ;
2142 IF IsProcedureBuiltinAvailable (procedure)
2143 THEN
2144 call := UseBuiltin (tokenno, procedure) ;
2145 IF call # NIL
2146 THEN
2147 call := BuildBuiltinCallTree (call)
2148 END
2149 ELSE
2150 call := NIL
2151 END ;
2152 IF call = NIL
2153 THEN
2154 IF GetType (procedure) = NulSym
2155 THEN
2156 call := BuildProcedureCallTree (location, Mod2Gcc (procedure), NIL)
2157 ELSE
2158 call := BuildProcedureCallTree (location, Mod2Gcc (procedure), Mod2Gcc (GetType (procedure)))
2159 END
2160 END ;
2161 IF GetType (procedure) = NulSym
2162 THEN
2163 SetLastFunction (NIL)
2164 ELSE
2165 SetLastFunction (call)
2166 END ;
2167 RETURN call
2168 END CodeDirectCall ;
2169
2170
2171 (*
2172 CodeIndirectCall - calls a function/procedure indirectly.
2173 *)
2174
2175 PROCEDURE CodeIndirectCall (tokenno: CARDINAL; ProcVar: CARDINAL) : tree ;
2176 VAR
2177 ReturnType: tree ;
2178 proc : CARDINAL ;
2179 location : location_t ;
2180 BEGIN
2181 location := TokenToLocation(tokenno) ;
2182 proc := SkipType(GetType(ProcVar)) ;
2183 IF GetType(proc)=NulSym
2184 THEN
2185 ReturnType := tree(NIL)
2186 ELSE
2187 ReturnType := tree(Mod2Gcc(GetType(proc)))
2188 END ;
2189
2190 (* Now we dereference the lvalue if necessary. *)
2191
2192 IF GetMode(ProcVar)=LeftValue
2193 THEN
2194 RETURN BuildIndirectProcedureCallTree(location,
2195 BuildIndirect(location, Mod2Gcc(ProcVar), Mod2Gcc(proc)),
2196 ReturnType)
2197 ELSE
2198 RETURN BuildIndirectProcedureCallTree(location, Mod2Gcc(ProcVar), ReturnType)
2199 END
2200 END CodeIndirectCall ;
2201
2202
2203 (*
2204 StringToChar - if type=Char and str is a string (of size <= 1)
2205 then convert the string into a character constant.
2206 *)
2207
2208 PROCEDURE StringToChar (t: tree; type, str: CARDINAL) : tree ;
2209 VAR
2210 s: String ;
2211 n: Name ;
2212 tokenno : CARDINAL ;
2213 location: location_t ;
2214 BEGIN
2215 tokenno := GetDeclaredMod(str) ;
2216 location := TokenToLocation(tokenno) ;
2217 type := SkipType (type) ;
2218 IF (type=Char) AND IsConstString(str)
2219 THEN
2220 Assert (IsConstStringKnown (str)) ;
2221 IF GetStringLength (tokenno, str) = 0
2222 THEN
2223 s := InitString('') ;
2224 t := BuildCharConstant(location, s) ;
2225 s := KillString(s) ;
2226 ELSIF GetStringLength (tokenno, str)>1
2227 THEN
2228 n := GetSymName(str) ;
2229 WriteFormat1("type incompatibility, attempting to use a string ('%a') when a CHAR is expected", n) ;
2230 s := InitString('') ; (* Do something safe. *)
2231 t := BuildCharConstant(location, s)
2232 END ;
2233 s := InitStringCharStar(KeyToCharStar(GetString(str))) ;
2234 s := Slice(s, 0, 1) ;
2235 t := BuildCharConstant(location, string(s)) ;
2236 s := KillString(s) ;
2237 END ;
2238 RETURN( t )
2239 END StringToChar ;
2240
2241
2242 (*
2243 ConvertTo - convert gcc tree, t, (which currently represents Modula-2 op3) into
2244 a symbol of, type.
2245 *)
2246
2247 PROCEDURE ConvertTo (t: tree; type, op3: CARDINAL) : tree ;
2248 BEGIN
2249 IF SkipType(type)#SkipType(GetType(op3))
2250 THEN
2251 IF IsConst(op3) AND (NOT IsConstString(op3))
2252 THEN
2253 PushValue(op3) ;
2254 RETURN( BuildConvert(TokenToLocation(GetDeclaredMod(op3)),
2255 Mod2Gcc(type), t, FALSE) )
2256 END
2257 END ;
2258 RETURN( t )
2259 END ConvertTo ;
2260
2261
2262 (*
2263 ConvertRHS - convert (t, rhs) into, type. (t, rhs) refer to the
2264 same entity t is a GCC Tree and, rhs, is a Modula-2
2265 symbol. It checks for char and strings
2266 first and then the remaining types.
2267 *)
2268
2269 PROCEDURE ConvertRHS (t: tree; type, rhs: CARDINAL) : tree ;
2270 BEGIN
2271 t := StringToChar (Mod2Gcc (rhs), type, rhs) ;
2272 RETURN ConvertTo (t, type, rhs)
2273 END ConvertRHS ;
2274
2275
2276 (*
2277 IsCoerceableParameter - returns TRUE if symbol, sym, is a
2278 coerceable parameter.
2279 *)
2280
2281 PROCEDURE IsCoerceableParameter (sym: CARDINAL) : BOOLEAN ;
2282 BEGIN
2283 RETURN(
2284 IsSet(sym) OR
2285 (IsOrdinalType(sym) AND (sym#Boolean) AND (NOT IsEnumeration(sym))) OR
2286 IsComplexType(sym) OR IsRealType(sym) OR
2287 IsComplexN(sym) OR IsRealN(sym) OR IsSetN(sym)
2288 )
2289 END IsCoerceableParameter ;
2290
2291
2292 (*
2293 IsConstProcedure - returns TRUE if, p, is a const procedure.
2294 *)
2295
2296 PROCEDURE IsConstProcedure (p: CARDINAL) : BOOLEAN ;
2297 BEGIN
2298 RETURN( IsConst(p) AND (GetType(p)#NulSym) AND IsProcType(GetType(p)) )
2299 END IsConstProcedure ;
2300
2301
2302 (*
2303 IsConstant - returns TRUE if symbol, p, is either a const or procedure.
2304 *)
2305
2306 PROCEDURE IsConstant (p: CARDINAL) : BOOLEAN ;
2307 BEGIN
2308 RETURN IsConst (p) OR IsProcedure (p)
2309 END IsConstant ;
2310
2311
2312 (*
2313 CheckConvertCoerceParameter - ensure that actual parameter is the same as the nth of callee.
2314 *)
2315
2316 PROCEDURE CheckConvertCoerceParameter (tokenno: CARDINAL; nth, callee, actual: CARDINAL) : tree ;
2317 VAR
2318 OperandType,
2319 ParamType : CARDINAL ;
2320 location : location_t ;
2321 BEGIN
2322 location := TokenToLocation(tokenno) ;
2323 IF GetNthParamAny (callee, nth)=NulSym
2324 THEN
2325 (* We reach here if the argument is being passed to a C vararg function. *)
2326 RETURN( Mod2Gcc(actual) )
2327 ELSE
2328 OperandType := SkipType(GetType(actual)) ;
2329 ParamType := SkipType(GetType(GetNthParamAny (callee, nth)))
2330 END ;
2331 IF IsProcType(ParamType)
2332 THEN
2333 IF IsProcedure(actual) OR IsConstProcedure(actual) OR (OperandType = ParamType)
2334 THEN
2335 RETURN( Mod2Gcc(actual) )
2336 ELSE
2337 RETURN( BuildConvert(location, Mod2Gcc(ParamType), Mod2Gcc(actual), FALSE) )
2338 END
2339 ELSIF IsRealType(OperandType) AND IsRealType(ParamType) AND
2340 (ParamType#OperandType)
2341 THEN
2342 (* SHORTREAL, LONGREAL and REAL conversion during parameter passing. *)
2343 RETURN( BuildConvert(location, Mod2Gcc(ParamType),
2344 Mod2Gcc(actual), FALSE) )
2345 ELSIF (OperandType#NulSym) AND IsSet(OperandType) AND IsConst(actual)
2346 THEN
2347 RETURN( DeclareKnownConstant(location,
2348 Mod2Gcc(ParamType),
2349 Mod2Gcc(actual)) )
2350 ELSIF IsConst(actual) AND
2351 (IsOrdinalType(ParamType) OR IsSystemType(ParamType))
2352 THEN
2353 RETURN( BuildConvert(location, Mod2Gcc(ParamType),
2354 StringToChar(Mod2Gcc(actual), ParamType, actual),
2355 FALSE) )
2356 ELSIF IsConstString(actual) OR ((OperandType#NulSym) AND IsCoerceableParameter(OperandType) AND (OperandType#ParamType))
2357 THEN
2358 RETURN( BuildConvert(location, Mod2Gcc(ParamType), Mod2Gcc(actual), FALSE) )
2359 ELSE
2360 RETURN( Mod2Gcc(actual) )
2361 END
2362 END CheckConvertCoerceParameter ;
2363
2364
2365 (*
2366 CheckConstant - checks to see whether we should declare the constant.
2367 *)
2368
2369 PROCEDURE CheckConstant (tokenno: CARDINAL; des, expr: CARDINAL) : tree ;
2370 VAR
2371 location: location_t ;
2372 BEGIN
2373 location := TokenToLocation(tokenno) ;
2374 IF IsProcedure(expr)
2375 THEN
2376 RETURN( Mod2Gcc(expr) )
2377 ELSE
2378 RETURN( DeclareKnownConstant(location, Mod2Gcc(GetType(des)), Mod2Gcc(expr)) )
2379 END
2380 END CheckConstant ;
2381
2382
2383 (*
2384 CodeMakeAdr - code the function MAKEADR.
2385 *)
2386
2387 PROCEDURE CodeMakeAdr (q: CARDINAL; op1, op2, op3: CARDINAL) ;
2388 VAR
2389 r : CARDINAL ;
2390 n : CARDINAL ;
2391 type : CARDINAL ;
2392 op : QuadOperator ;
2393 bits,
2394 max,
2395 tmp,
2396 res,
2397 val : tree ;
2398 location: location_t ;
2399 BEGIN
2400 location := TokenToLocation (CurrentQuadToken) ;
2401 n := q ;
2402 REPEAT
2403 IF op1 > 0
2404 THEN
2405 DeclareConstant (CurrentQuadToken, op3)
2406 END ;
2407 n := GetNextQuad (n) ;
2408 GetQuad (n, op, r, op2, op3)
2409 UNTIL op = FunctValueOp ;
2410
2411 n := q ;
2412 GetQuad (n, op, op1, op2, op3) ;
2413 res := Mod2Gcc (r) ;
2414 max := GetSizeOfInBits (Mod2Gcc(Address)) ;
2415 bits := GetIntegerZero (location) ;
2416 val := GetPointerZero (location) ;
2417 REPEAT
2418 location := TokenToLocation (CurrentQuadToken) ;
2419 IF (op = ParamOp) AND (op1 > 0)
2420 THEN
2421 IF GetType (op3) = NulSym
2422 THEN
2423 WriteFormat0 ('must supply typed constants to MAKEADR')
2424 ELSE
2425 type := GetType (op3) ;
2426 tmp := BuildConvert (location, GetPointerType (), Mod2Gcc (op3), FALSE) ;
2427 IF CompareTrees (bits, GetIntegerZero (location)) > 0
2428 THEN
2429 tmp := BuildLSL (location, tmp, bits, FALSE)
2430 END ;
2431 bits := BuildAdd (location, bits, GetSizeOfInBits (Mod2Gcc (type)), FALSE) ;
2432 val := BuildLogicalOrAddress (location, val, tmp, FALSE)
2433 END
2434 END ;
2435 SubQuad (n) ;
2436 n := GetNextQuad (n) ;
2437 GetQuad (n, op, op1, op2, op3)
2438 UNTIL op=FunctValueOp ;
2439 IF CompareTrees(bits, max) > 0
2440 THEN
2441 MetaErrorT0 (CurrentQuadToken,
2442 'total number of bits specified as parameters to {%kMAKEADR} exceeds address width')
2443 END ;
2444 SubQuad(n) ;
2445 BuildAssignmentStatement (location, res, val)
2446 END CodeMakeAdr ;
2447
2448
2449 (*
2450 CodeBuiltinFunction - attempts to inline a function. Currently it only
2451 inlines the SYSTEM function MAKEADR.
2452 *)
2453
2454 PROCEDURE CodeBuiltinFunction (q: CARDINAL; nth, func, parameter: CARDINAL) ;
2455 BEGIN
2456 IF nth = 0
2457 THEN
2458 InitBuiltinSyms (BuiltinTokenNo) ;
2459 IF func = MakeAdr
2460 THEN
2461 CodeMakeAdr (q, nth, func, parameter)
2462 END
2463 END
2464 END CodeBuiltinFunction ;
2465
2466
2467 (*
2468 FoldMakeAdr - attempts to fold the function MAKEADR.
2469 *)
2470
2471 PROCEDURE FoldMakeAdr (tokenno: CARDINAL; p: WalkAction;
2472 q: CARDINAL; op1, op2, op3: CARDINAL) ;
2473 VAR
2474 resolved: BOOLEAN ;
2475 r : CARDINAL ;
2476 n : CARDINAL ;
2477 op : QuadOperator ;
2478 type : CARDINAL ;
2479 bits,
2480 max,
2481 tmp,
2482 val : tree ;
2483 location: location_t ;
2484 BEGIN
2485 location := TokenToLocation (tokenno) ;
2486 resolved := TRUE ;
2487 n := q ;
2488 r := op1 ;
2489 REPEAT
2490 IF r>0
2491 THEN
2492 TryDeclareConstant (tokenno, op3) ;
2493 IF NOT GccKnowsAbout (op3)
2494 THEN
2495 resolved := FALSE
2496 END
2497 END ;
2498 n := GetNextQuad (n) ;
2499 GetQuad (n, op, r, op2, op3)
2500 UNTIL op = FunctValueOp ;
2501
2502 IF resolved AND IsConst (r)
2503 THEN
2504 n := q ;
2505 GetQuad (n, op, op1, op2, op3) ;
2506 max := GetSizeOfInBits (Mod2Gcc(Address)) ;
2507 bits := GetIntegerZero (location) ;
2508 val := GetPointerZero (location) ;
2509 REPEAT
2510 location := TokenToLocation (tokenno) ;
2511 IF (op = ParamOp) AND (op1 > 0)
2512 THEN
2513 IF GetType (op3) = NulSym
2514 THEN
2515 MetaErrorT0 (tokenno,
2516 'constants passed to {%kMAKEADR} must be typed')
2517 ELSE
2518 type := GetType (op3) ;
2519 tmp := BuildConvert (location, GetPointerType (), Mod2Gcc (op3), FALSE) ;
2520 IF CompareTrees (bits, GetIntegerZero (location)) > 0
2521 THEN
2522 tmp := BuildLSL (location, tmp, bits, FALSE)
2523 END ;
2524 bits := BuildAdd (location, bits, GetSizeOfInBits (Mod2Gcc (type)), FALSE) ;
2525 val := BuildLogicalOrAddress (location, val, tmp, FALSE)
2526 END
2527 END ;
2528 SubQuad (n) ;
2529 n := GetNextQuad (n) ;
2530 GetQuad (n, op, op1, op2, op3)
2531 UNTIL op = FunctValueOp ;
2532 IF CompareTrees (bits, max) > 0
2533 THEN
2534 MetaErrorT0 (tokenno,
2535 'total number of bits specified as parameters to {%kMAKEADR} exceeds address width')
2536 END ;
2537 PutConst (r, Address) ;
2538 AddModGcc (r, DeclareKnownConstant (location, Mod2Gcc (Address), val)) ;
2539 p (r) ;
2540 NoChange := FALSE ;
2541 SubQuad (n)
2542 END
2543 END FoldMakeAdr ;
2544
2545
2546 (*
2547 doParam - builds the parameter, op3, which is to be passed to
2548 procedure, op2. The number of the parameter is op1.
2549 *)
2550
2551 PROCEDURE doParam (quad: CARDINAL; paramtok: CARDINAL; op1, op2, op3: CARDINAL) ;
2552 VAR
2553 location: location_t ;
2554 BEGIN
2555 location := TokenToLocation (paramtok) ;
2556 DeclareConstant (paramtok, op3) ;
2557 DeclareConstructor (paramtok, quad, op3) ;
2558 BuildParam (location, CheckConvertCoerceParameter (paramtok, op1, op2, op3))
2559 END doParam ;
2560
2561
2562 (*
2563 FoldBuiltin - attempts to fold the gcc builtin function.
2564 *)
2565
2566 PROCEDURE FoldBuiltin (tokenno: CARDINAL; p: WalkAction; q: CARDINAL) ;
2567 VAR
2568 resolved : BOOLEAN ;
2569 procedure,
2570 r : CARDINAL ;
2571 n : CARDINAL ;
2572 op1, op2,
2573 op3 : CARDINAL ;
2574 op : QuadOperator ;
2575 val, call : tree ;
2576 location : location_t ;
2577 BEGIN
2578 GetQuad (q, op, op1, op2, op3) ;
2579 resolved := TRUE ;
2580 procedure := NulSym ;
2581 n := q ;
2582 r := op1 ;
2583 REPEAT
2584 IF r>0
2585 THEN
2586 TryDeclareConstant(tokenno, op3) ;
2587 IF NOT GccKnowsAbout(op3)
2588 THEN
2589 resolved := FALSE
2590 END
2591 END ;
2592 IF (op=CallOp) AND (NOT IsProcedure(op3))
2593 THEN
2594 (* Cannot fold an indirect procedure function call. *)
2595 resolved := FALSE
2596 END ;
2597 n := GetNextQuad(n) ;
2598 GetQuad(n, op, r, op2, op3)
2599 UNTIL op=FunctValueOp ;
2600
2601 IF resolved AND IsConst(r)
2602 THEN
2603 n := q ;
2604 GetQuad(n, op, op1, op2, op3) ;
2605 REPEAT
2606 IF (op=ParamOp) AND (op1>0)
2607 THEN
2608 doParam (tokenno, n, op1, op2, op3)
2609 ELSIF op=CallOp
2610 THEN
2611 procedure := op3
2612 END ;
2613 SubQuad(n) ;
2614 n := GetNextQuad(n) ;
2615 GetQuad(n, op, op1, op2, op3)
2616 UNTIL op=FunctValueOp ;
2617
2618 IF IsProcedureBuiltinAvailable (procedure)
2619 THEN
2620 location := TokenToLocation(tokenno) ;
2621 call := UseBuiltin (tokenno, procedure) ;
2622 val := BuildFunctValue (location, call) ;
2623 val := FoldAndStrip (val) ;
2624 PutConst(r, GetType(procedure)) ;
2625 AddModGcc(r, DeclareKnownConstant(location, Mod2Gcc(GetType(procedure)), val)) ;
2626 p(r) ;
2627 SetLastFunction(NIL)
2628 ELSE
2629 MetaErrorT1 (tokenno, 'gcc builtin procedure {%1Ead} cannot be used in a constant expression', procedure) ;
2630 END ;
2631 NoChange := FALSE ;
2632 SubQuad(n)
2633 END
2634 END FoldBuiltin ;
2635
2636
2637 (*
2638 FoldBuiltinFunction - attempts to inline a function. Currently it only
2639 inlines the SYSTEM function MAKEADR.
2640 *)
2641
2642 PROCEDURE FoldBuiltinFunction (tokenno: CARDINAL; p: WalkAction;
2643 q: CARDINAL; op1, op2, op3: CARDINAL) ;
2644 BEGIN
2645 IF op1=0
2646 THEN
2647 (* Must be a function as op1 is the return parameter. *)
2648 IF op3=MakeAdr
2649 THEN
2650 FoldMakeAdr (tokenno, p, q, op1, op2, op3)
2651 ELSIF IsProcedure (op3) AND IsProcedureBuiltinAvailable (op3)
2652 THEN
2653 FoldBuiltin (tokenno, p, q)
2654 END
2655 END
2656 END FoldBuiltinFunction ;
2657
2658
2659 (*
2660 CodeParam - builds a parameter list.
2661 Note that we can ignore ModeOfAddr as any lvalue will
2662 have been created in a preceeding quadruple.
2663 *)
2664
2665 PROCEDURE CodeParam (quad: CARDINAL) ;
2666 VAR
2667 nopos,
2668 procedure,
2669 parameter,
2670 parampos : CARDINAL ;
2671 nth : CARDINAL ;
2672 compatible,
2673 constExpr,
2674 overflow : BOOLEAN ;
2675 op : QuadOperator ;
2676 BEGIN
2677 GetQuadOtok (quad, parampos, op,
2678 nth, procedure, parameter,
2679 overflow, constExpr,
2680 nopos, nopos, nopos) ;
2681 compatible := TRUE ;
2682 IF nth=0
2683 THEN
2684 CodeBuiltinFunction (quad, nth, procedure, parameter)
2685 ELSE
2686 IF StrictTypeChecking
2687 THEN
2688 IF (nth <= NoOfParamAny (procedure))
2689 THEN
2690 compatible := ParameterTypeCompatible (parampos,
2691 'parameter incompatibility when attempting to pass actual parameter {%2ad} to a {%kVAR} formal parameter {%3Ead} during call to procedure {%1ad}',
2692 procedure, GetNthParamAny (procedure, nth),
2693 parameter, nth, IsVarParamAny (procedure, nth))
2694 END
2695 END ;
2696
2697 IF (nth <= NoOfParamAny (procedure)) AND
2698 IsVarParamAny (procedure, nth) AND IsConst (parameter)
2699 THEN
2700 MetaErrorT1 (parampos,
2701 'cannot pass a constant {%1Ead} as a VAR parameter', parameter)
2702 ELSIF IsAModula2Type (parameter)
2703 THEN
2704 MetaErrorT2 (parampos,
2705 'cannot pass a type {%1Ead} as a parameter to procedure {%2ad}',
2706 parameter, procedure)
2707 ELSIF compatible
2708 THEN
2709 doParam (quad, parampos, nth, procedure, parameter)
2710 END
2711 END
2712 END CodeParam ;
2713
2714
2715 (*
2716 Replace - replace the entry for sym in the double entry bookkeeping with sym/tree.
2717 *)
2718
2719 PROCEDURE Replace (sym: CARDINAL; gcc: tree) ;
2720 BEGIN
2721 IF GccKnowsAbout (sym)
2722 THEN
2723 RemoveMod2Gcc (sym)
2724 END ;
2725 AddModGcc (sym, gcc)
2726 END Replace ;
2727
2728
2729 (*
2730 CodeFunctValue - retrieves the function return value and assigns it
2731 into a variable.
2732 *)
2733
2734 PROCEDURE CodeFunctValue (location: location_t; op1: CARDINAL) ;
2735 VAR
2736 call,
2737 value: tree ;
2738 BEGIN
2739 (*
2740 operator : FunctValueOp
2741 op1 : The Returned Variable
2742 op3 : The Function Returning this Variable
2743 *)
2744 IF EnableSSA AND IsVariableSSA (op1)
2745 THEN
2746 call := GetLastFunction () ;
2747 SetLastFunction (NIL) ;
2748 Replace (op1, call)
2749 ELSE
2750 value := BuildFunctValue (location, Mod2Gcc (op1)) ;
2751 (* AddStatement (location, CheckCleanup (location, op3, value, call)) *)
2752 AddStatement (location, value)
2753 END
2754 END CodeFunctValue ;
2755
2756
2757 (*
2758 FoldStringLength -
2759 *)
2760
2761 PROCEDURE FoldStringLength (quad: CARDINAL; p: WalkAction) ;
2762 VAR
2763 op : QuadOperator ;
2764 des, none, expr : CARDINAL ;
2765 stroppos,
2766 despos, nonepos,
2767 exprpos : CARDINAL ;
2768 constExpr,
2769 overflowChecking: BOOLEAN ;
2770 location : location_t ;
2771 BEGIN
2772 GetQuadOtok (quad, stroppos, op, des, none, expr,
2773 overflowChecking, constExpr,
2774 despos, nonepos, exprpos) ;
2775 IF IsConstStr (expr) AND IsConstStrKnown (expr)
2776 THEN
2777 location := TokenToLocation (stroppos) ;
2778 PushCard (GetStringLength (exprpos, expr)) ;
2779 AddModGcc (des, BuildConvert (location, Mod2Gcc (GetType (des)), PopIntegerTree (), FALSE)) ;
2780 RemoveQuad (p, des, quad)
2781 END
2782 END FoldStringLength ;
2783
2784
2785 (*
2786 FoldStringConvertM2nul - attempt to assign the des with the string contents from expr.
2787 It also marks the des as a m2 string which must be nul terminated.
2788 The front end uses double book keeping and it is easier to have
2789 different m2 string symbols each of which map onto a slightly different
2790 gcc string tree.
2791 *)
2792
2793 PROCEDURE FoldStringConvertM2nul (quad: CARDINAL; p: WalkAction) ;
2794 VAR
2795 op : QuadOperator ;
2796 des, none, expr : CARDINAL ;
2797 stroppos,
2798 despos, nonepos,
2799 exprpos : CARDINAL ;
2800 s : String ;
2801 constExpr,
2802 overflowChecking: BOOLEAN ;
2803 BEGIN
2804 GetQuadOtok (quad, stroppos, op, des, none, expr,
2805 overflowChecking, constExpr,
2806 despos, nonepos, exprpos) ;
2807 IF IsConstStr (expr) AND IsConstStrKnown (expr)
2808 THEN
2809 s := GetStr (exprpos, expr) ;
2810 PutConstStringKnown (stroppos, des, makekey (string (s)), FALSE, TRUE) ;
2811 TryDeclareConstant (despos, des) ;
2812 p (des) ;
2813 NoChange := FALSE ;
2814 SubQuad (quad) ;
2815 s := KillString (s)
2816 END
2817 END FoldStringConvertM2nul ;
2818
2819
2820 (*
2821 FoldStringConvertCnul -attempt to assign the des with the string contents from expr.
2822 It also marks the des as a C string which must be nul terminated.
2823 *)
2824
2825 PROCEDURE FoldStringConvertCnul (quad: CARDINAL; p: WalkAction) ;
2826 VAR
2827 op : QuadOperator ;
2828 des, none, expr : CARDINAL ;
2829 stroppos,
2830 despos, nonepos,
2831 exprpos : CARDINAL ;
2832 s : String ;
2833 constExpr,
2834 overflowChecking: BOOLEAN ;
2835 BEGIN
2836 GetQuadOtok (quad, stroppos, op, des, none, expr,
2837 overflowChecking, constExpr,
2838 despos, nonepos, exprpos) ;
2839 IF IsConstStr (expr) AND IsConstStrKnown (expr)
2840 THEN
2841 s := GetStr (exprpos, expr) ;
2842 PutConstStringKnown (stroppos, des, makekey (string (s)), TRUE, TRUE) ;
2843 TryDeclareConstant (despos, des) ;
2844 p (des) ;
2845 NoChange := FALSE ;
2846 SubQuad (quad) ;
2847 s := KillString (s)
2848 END
2849 END FoldStringConvertCnul ;
2850
2851
2852 (*
2853 Addr Operator - generates the address of a variable (op1 = &op3).
2854 *)
2855
2856 PROCEDURE CodeAddr (tokenno: CARDINAL; quad: CARDINAL; op1, op3: CARDINAL) ;
2857 VAR
2858 value : tree ;
2859 type : CARDINAL ;
2860 location: location_t ;
2861 BEGIN
2862 IF IsConst(op3) AND (NOT IsConstString(op3))
2863 THEN
2864 MetaErrorT1 (tokenno, 'error in expression, trying to find the address of a constant {%1Ead}', op3)
2865 ELSE
2866 IF IsConstString (op3) AND (NOT IsConstStringKnown (op3))
2867 THEN
2868 printf1 ("failure in quad: %d\n", quad)
2869 END ;
2870 location := TokenToLocation (tokenno) ;
2871 type := SkipType (GetType (op3)) ;
2872 DeclareConstant (tokenno, op3) ; (* We might be asked to find the address of a constant string. *)
2873 DeclareConstructor (tokenno, quad, op3) ;
2874 IF (IsConst (op3) AND (type=Char)) OR IsConstString (op3)
2875 THEN
2876 value := BuildStringConstant (KeyToCharStar (GetString (op3)), GetStringLength (tokenno, op3))
2877 ELSE
2878 value := Mod2Gcc (op3)
2879 END ;
2880 BuildAssignmentStatement (location,
2881 Mod2Gcc (op1),
2882 BuildAddr (location, value, FALSE))
2883 END
2884 END CodeAddr ;
2885
2886
2887 PROCEDURE stop ; BEGIN END stop ;
2888
2889 PROCEDURE CheckStop (q: CARDINAL) ;
2890 BEGIN
2891 IF q=3827
2892 THEN
2893 stop
2894 END
2895 END CheckStop ;
2896
2897
2898 (*
2899 ------------------------------------------------------------------------------
2900 := Operator
2901 ------------------------------------------------------------------------------
2902 Sym1<I> := Sym3<I> := produces a constant
2903 *)
2904
2905 PROCEDURE FoldBecomes (p: WalkAction; bb: BasicBlock; quad: CARDINAL) ;
2906 VAR
2907 op : QuadOperator ;
2908 des, op2, expr: CARDINAL ;
2909 BEGIN
2910 IF DeclaredOperandsBecomes (p, quad)
2911 THEN
2912 IF (NOT IsConditionalBooleanQuad (quad)) OR IsBasicBlockFirst (bb)
2913 THEN
2914 IF TypeCheckBecomes (p, quad)
2915 THEN
2916 PerformFoldBecomes (p, quad)
2917 END
2918 END
2919 END
2920 END FoldBecomes ;
2921
2922
2923 (*
2924 TryDeclareConst -
2925 *)
2926
2927 PROCEDURE TryDeclareConst (tokenno: CARDINAL; sym: CARDINAL) ;
2928 BEGIN
2929 (* Check whether expr is a constant literal and if so declare it. *)
2930 TryDeclareConstant (tokenno, sym) ;
2931 (* Check whether expr is a const constructor and if so declare it. *)
2932 TryDeclareConstructor (tokenno, sym)
2933 END TryDeclareConst ;
2934
2935
2936 (*
2937 RemoveQuad - remove quad and ensure p (des) is called.
2938 *)
2939
2940 PROCEDURE RemoveQuad (p: WalkAction; des: CARDINAL; quad: CARDINAL) ;
2941 BEGIN
2942 p (des) ;
2943 NoChange := FALSE ;
2944 SubQuad (quad)
2945 END RemoveQuad ;
2946
2947
2948 (*
2949 DeclaredOperandsBecomes -
2950 *)
2951
2952 PROCEDURE DeclaredOperandsBecomes (p: WalkAction; quad: CARDINAL) : BOOLEAN ;
2953 VAR
2954 des, op2, expr : CARDINAL ;
2955 constExpr,
2956 overflowChecking : BOOLEAN ;
2957 despos, op2pos,
2958 exprpos, becomespos: CARDINAL ;
2959 op : QuadOperator ;
2960 BEGIN
2961 GetQuadOtok (quad, becomespos, op,
2962 des, op2, expr,
2963 overflowChecking, constExpr,
2964 despos, op2pos, exprpos) ;
2965 Assert (op2pos = UnknownTokenNo) ;
2966 TryDeclareConst (exprpos, expr) ;
2967 IF IsConst (des) AND IsConstant (expr)
2968 THEN
2969 (* Constant folding taking place, but have we resolved op3 yet? *)
2970 IF GccKnowsAbout (expr)
2971 THEN
2972 (* Now we can tell gcc about the relationship between des and expr. *)
2973 (* RemoveSSAPlaceholder (quad, des) ; *)
2974 IF GccKnowsAbout (des)
2975 THEN
2976 MetaErrorT1 (despos, 'constant {%1Ead} should not be reassigned', des) ;
2977 RemoveQuad (p, des, quad) ;
2978 RETURN FALSE
2979 ELSE
2980 RETURN TRUE
2981 END
2982 END
2983 END ;
2984 RETURN FALSE
2985 END DeclaredOperandsBecomes ;
2986
2987
2988 (*
2989 TypeCheckBecomes - returns TRUE if the type check succeeds.
2990 *)
2991
2992 PROCEDURE TypeCheckBecomes (p: WalkAction; quad: CARDINAL) : BOOLEAN ;
2993 VAR
2994 des, op2, expr : CARDINAL ;
2995 constExpr,
2996 overflowChecking : BOOLEAN ;
2997 despos, op2pos,
2998 exprpos, becomespos: CARDINAL ;
2999 op : QuadOperator ;
3000 BEGIN
3001 GetQuadOtok (quad, becomespos, op,
3002 des, op2, expr,
3003 overflowChecking, constExpr,
3004 despos, op2pos, exprpos) ;
3005 Assert (op2pos = UnknownTokenNo) ;
3006 IF StrictTypeChecking AND
3007 (NOT AssignmentTypeCompatible (despos, "", des, expr, TRUE))
3008 THEN
3009 MetaErrorT2 (MakeVirtualTok (becomespos, despos, exprpos),
3010 'assignment check caught mismatch between {%1Ead} and {%2ad}',
3011 des, expr) ;
3012 RemoveQuad (p, des, quad) ;
3013 RETURN FALSE
3014 END ;
3015 RETURN TRUE
3016 END TypeCheckBecomes ;
3017
3018
3019 (*
3020 PerformFoldBecomes - attempts to fold quad. It propagates constant strings
3021 and attempts to declare des providing it is a constant
3022 and expr is resolved.
3023 *)
3024
3025 PROCEDURE PerformFoldBecomes (p: WalkAction; quad: CARDINAL) ;
3026 VAR
3027 des, op2, expr : CARDINAL ;
3028 constExpr,
3029 overflowChecking : BOOLEAN ;
3030 despos, op2pos,
3031 exprpos, becomespos,
3032 virtpos : CARDINAL ;
3033 op : QuadOperator ;
3034 BEGIN
3035 GetQuadOtok (quad, becomespos, op,
3036 des, op2, expr,
3037 overflowChecking, constExpr,
3038 despos, op2pos, exprpos) ;
3039 Assert (op2pos = UnknownTokenNo) ;
3040 IF IsConst (des) AND IsConstString (expr)
3041 THEN
3042 IF IsConstStringKnown (expr) AND (NOT IsConstStringKnown (des))
3043 THEN
3044 CopyConstString (exprpos, des, expr)
3045 END
3046 ELSIF GetType (des) = NulSym
3047 THEN
3048 Assert (GetType (expr) # NulSym) ;
3049 PutConst (des, GetType (expr))
3050 END ;
3051 IF GetType (expr) = NulSym
3052 THEN
3053 CheckOrResetOverflow (exprpos, Mod2Gcc (expr), MustCheckOverflow (quad)) ;
3054 AddModGcc (des, Mod2Gcc (expr))
3055 ELSE
3056 IF NOT GccKnowsAbout (GetType (des))
3057 THEN
3058 RETURN
3059 END ;
3060 IF IsProcedure (expr)
3061 THEN
3062 AddModGcc (des,
3063 BuildConvert (TokenToLocation (exprpos),
3064 Mod2Gcc (GetType (des)),
3065 BuildAddr (TokenToLocation (exprpos),
3066 Mod2Gcc (expr), FALSE), TRUE))
3067 ELSIF IsValueSolved (expr)
3068 THEN
3069 PushValue (expr) ;
3070 IF IsValueTypeReal ()
3071 THEN
3072 CheckOrResetOverflow (exprpos, PopRealTree (), MustCheckOverflow (quad)) ;
3073 PushValue (expr) ;
3074 AddModGcc (des, PopRealTree ())
3075 ELSIF IsValueTypeSet ()
3076 THEN
3077 PopValue (des) ;
3078 PutConstSet (des)
3079 ELSIF IsValueTypeConstructor () OR IsValueTypeArray () OR IsValueTypeRecord ()
3080 THEN
3081 PopValue (des) ;
3082 PutConstructor (des)
3083 ELSIF IsValueTypeComplex ()
3084 THEN
3085 CheckOrResetOverflow (exprpos, PopComplexTree (), MustCheckOverflow (quad)) ;
3086 PushValue (expr) ;
3087 PopValue (des)
3088 ELSE
3089 CheckOrResetOverflow (exprpos, PopIntegerTree (), MustCheckOverflow (quad)) ;
3090 IF GetType (des) = NulSym
3091 THEN
3092 PushValue (expr) ;
3093 AddModGcc (des, PopIntegerTree ())
3094 ELSE
3095 virtpos := MakeVirtualTok (becomespos, despos, exprpos) ;
3096 PushValue (expr) ;
3097 AddModGcc (des, BuildConvert (TokenToLocation (virtpos),
3098 Mod2Gcc (GetType (des)), PopIntegerTree (), FALSE))
3099 END
3100 END
3101 ELSE
3102 virtpos := MakeVirtualTok (becomespos, despos, exprpos) ;
3103 CheckOrResetOverflow (exprpos, Mod2Gcc (des), MustCheckOverflow (quad)) ;
3104 AddModGcc (des,
3105 BuildConvert (TokenToLocation (virtpos),
3106 Mod2Gcc (GetType (des)),
3107 DeclareKnownConstant (TokenToLocation (virtpos),
3108 Mod2Gcc (GetType (expr)),
3109 Mod2Gcc (expr)), FALSE))
3110 END
3111 END ;
3112 RemoveQuad (p, des, quad) ;
3113 Assert (RememberConstant(Mod2Gcc (des)) = Mod2Gcc (des))
3114 END PerformFoldBecomes ;
3115
3116
3117 VAR
3118 tryBlock: tree ; (* This must be placed into gccgm2 and it must follow the
3119 current function scope - ie it needs work with nested procedures. *)
3120 handlerBlock: tree ;
3121
3122
3123 (*
3124 CodeTry - starts building a GCC 'try' node.
3125 *)
3126
3127 PROCEDURE CodeTry ;
3128 VAR
3129 location: location_t ;
3130 BEGIN
3131 location := TokenToLocation (CurrentQuadToken) ;
3132 handlerBlock := NIL ;
3133 tryBlock := BuildTryBegin (location)
3134 END CodeTry ;
3135
3136
3137 (*
3138 CodeThrow - builds a GCC 'throw' node.
3139 *)
3140
3141 PROCEDURE CodeThrow (value: CARDINAL) ;
3142 VAR
3143 location: location_t ;
3144 BEGIN
3145 location := TokenToLocation (CurrentQuadToken) ;
3146 IF value = NulSym
3147 THEN
3148 AddStatement (location, BuildThrow (location, tree (NIL)))
3149 ELSE
3150 DeclareConstant (CurrentQuadToken, value) ; (* Checks to see whether it is a constant and declares it. *)
3151 AddStatement (location, BuildThrow (location, BuildConvert (location,
3152 GetIntegerType (),
3153 Mod2Gcc (value), FALSE)))
3154 END
3155 END CodeThrow ;
3156
3157
3158 PROCEDURE CodeRetry (destQuad: CARDINAL) ;
3159 VAR
3160 location: location_t ;
3161 BEGIN
3162 location := TokenToLocation (CurrentQuadToken) ;
3163 BuildGoto (location, string (CreateLabelName (destQuad)))
3164 END CodeRetry ;
3165
3166
3167 PROCEDURE CodeCatchBegin ;
3168 VAR
3169 location: location_t ;
3170 BEGIN
3171 location := TokenToLocation (CurrentQuadToken) ;
3172 BuildTryEnd (tryBlock) ;
3173 handlerBlock := BuildCatchBegin (location)
3174 END CodeCatchBegin ;
3175
3176
3177 PROCEDURE CodeCatchEnd ;
3178 VAR
3179 location: location_t ;
3180 BEGIN
3181 location := TokenToLocation (CurrentQuadToken) ;
3182 tryBlock := BuildCatchEnd (location, handlerBlock, tryBlock) ;
3183 AddStatement (location, tryBlock)
3184 END CodeCatchEnd ;
3185
3186
3187 (*
3188 DescribeTypeError -
3189 *)
3190
3191 PROCEDURE DescribeTypeError (token: CARDINAL;
3192 op1, op2: CARDINAL) ;
3193 BEGIN
3194 MetaErrorT2(token, 'incompatible set types in assignment, assignment between {%1ERad} and {%2ad}', op1, op2) ;
3195 MetaError2('set types are {%1CDtsad} and {%2Dtsad}', op1, op2)
3196 END DescribeTypeError ;
3197
3198
3199 (*
3200 DefaultConvertGM2 - provides a simple mapping between
3201 front end data types and GCC equivalents.
3202 This is only used to aid assignment of
3203 typed constants.
3204 *)
3205
3206 PROCEDURE DefaultConvertGM2 (sym: CARDINAL) : tree ;
3207 BEGIN
3208 sym := SkipType (sym) ;
3209 IF sym=Bitset
3210 THEN
3211 RETURN( GetWordType() )
3212 ELSE
3213 RETURN( Mod2Gcc(sym) )
3214 END
3215 END DefaultConvertGM2 ;
3216
3217
3218 (*
3219 FoldConstBecomes - returns a Tree containing op3.
3220 The tree will have been folded and
3221 type converted if necessary.
3222 *)
3223
3224 PROCEDURE FoldConstBecomes (tokenno: CARDINAL;
3225 op1, op3: CARDINAL) : tree ;
3226 VAR
3227 t, type : tree ;
3228 location: location_t ;
3229 BEGIN
3230 IF IsConstSet(op3) OR ((SkipType(GetType(op3))#NulSym) AND
3231 IsSet(SkipType(GetType(op3))))
3232 THEN
3233 IF SkipType(GetTypeMode(op1))#SkipType(GetTypeMode(op3))
3234 THEN
3235 DescribeTypeError (tokenno, op1, op3) ;
3236 (* Assigning an errant op3 might ICE, therefore it is safer to return op1. *)
3237 RETURN( Mod2Gcc (op1) )
3238 END
3239 END ;
3240 location := TokenToLocation (tokenno) ;
3241 TryDeclareConstant (tokenno, op3) ;
3242 t := Mod2Gcc (op3) ;
3243 Assert (t#NIL) ;
3244 IF IsConstant (op3)
3245 THEN
3246 IF IsProcedure (op3)
3247 THEN
3248 RETURN t
3249 ELSIF (NOT IsConstString (op3)) AND (NOT IsConstSet (op3)) AND
3250 (SkipType (GetType (op3)) # SkipType (GetType (op1)))
3251 THEN
3252 type := DefaultConvertGM2 (GetType(op1)) ; (* do we need this now? --fixme-- *)
3253 t := ConvertConstantAndCheck (location, type, t)
3254 ELSIF GetType (op1) # NulSym
3255 THEN
3256 t := StringToChar (Mod2Gcc (op3), GetType (op1), op3)
3257 END
3258 END ;
3259 RETURN( t )
3260 END FoldConstBecomes ;
3261
3262
3263 (*
3264 PrepareCopyString - returns two trees:
3265 length number of bytes to be copied (including the nul if room)
3266 srcTreeType the new string type (with the extra nul character).
3267
3268 Pre condition: destStrType the dest type string.
3269 src is the original string (without a nul)
3270 to be copied.
3271 Post condition: TRUE or FALSE is returned.
3272 if true length and srcTreeType will be assigned
3273 else length is set to the maximum length to be
3274 copied and srcTree is set to the max length
3275 which fits in dest.
3276 *)
3277
3278 PROCEDURE PrepareCopyString (tokenno: CARDINAL; VAR length, srcTree: tree;
3279 src, destStrType: CARDINAL) : BOOLEAN ;
3280 VAR
3281 location : location_t ;
3282 intLength: INTEGER ;
3283 BEGIN
3284 location := TokenToLocation (tokenno) ;
3285 Assert (IsArray (SkipType (destStrType))) ;
3286 (* Handle string assignments:
3287 VAR
3288 str: ARRAY [0..10] OF CHAR ;
3289 ch : CHAR ;
3290
3291 str := 'abcde' but not ch := 'a'
3292 *)
3293 IF GetType (src) = Char
3294 THEN
3295 (*
3296 * Create string from char and add nul to the end, nul is
3297 * added by BuildStringConstant. In modula-2 an array must
3298 * have at least one element.
3299 *)
3300 length := GetIntegerOne (location) ;
3301 PushIntegerTree (FindSize (tokenno, src)) ;
3302 PushIntegerTree (FindSize (tokenno, destStrType)) ;
3303 IF Less (tokenno)
3304 THEN
3305 (* There is room for the extra <nul> character. *)
3306 length := BuildAdd (location, length,
3307 GetIntegerOne (location), FALSE)
3308 END
3309 ELSE
3310 PushIntegerTree (FindSize (tokenno, src)) ;
3311 PushIntegerTree (FindSize (tokenno, destStrType)) ;
3312 IF Less (tokenno)
3313 THEN
3314 (* There is room for the extra <nul> character. *)
3315 length := BuildAdd (location, FindSize (tokenno, src),
3316 GetIntegerOne (location), FALSE) ;
3317 srcTree := Mod2Gcc (src)
3318 ELSE
3319 (* We need to truncate the <nul> at least. *)
3320 length := FindSize (tokenno, destStrType) ;
3321 PushIntegerTree (FindSize (tokenno, src)) ;
3322 PushIntegerTree (length) ;
3323 (* Greater or Equal so return max characters in the array. *)
3324 IF Gre (tokenno)
3325 THEN
3326 (* Create a new string without non nul characters to be gimple safe.
3327 But return FALSE indicating an overflow. *)
3328 intLength := GetCstInteger (length) ;
3329 srcTree := BuildStringConstant (KeyToCharStar (GetString (src)), intLength) ;
3330 srcTree := ConvertString (Mod2Gcc (destStrType), srcTree) ;
3331 RETURN FALSE
3332 END
3333 END
3334 END ;
3335 intLength := GetCstInteger (length) ;
3336 srcTree := BuildStringConstant (KeyToCharStar (GetString (src)), intLength) ;
3337 srcTree := ConvertString (Mod2Gcc (destStrType), srcTree) ;
3338 RETURN TRUE
3339 END PrepareCopyString ;
3340
3341
3342 (*
3343 checkArrayElements - return TRUE if des or expr are not arrays.
3344 If they are arrays and have different number of
3345 elements return FALSE, otherwise TRUE.
3346 *)
3347
3348 PROCEDURE checkArrayElements (des, expr: CARDINAL; virtpos, despos, exprpos: CARDINAL) : BOOLEAN ;
3349 VAR
3350 e1, e3: tree ;
3351 t1, t3: CARDINAL ;
3352 BEGIN
3353 t1 := GetType (des) ;
3354 t3 := GetType (expr) ;
3355 IF (t1 # NulSym) AND (t3 # NulSym) AND
3356 IsArray (SkipType (GetType (expr))) AND IsArray (SkipType (GetType (des)))
3357 THEN
3358 (* both arrays continue checking *)
3359 e1 := GetArrayNoOfElements (TokenToLocation (despos),
3360 Mod2Gcc (SkipType (GetType (des)))) ;
3361 e3 := GetArrayNoOfElements (TokenToLocation (exprpos),
3362 Mod2Gcc (SkipType (GetType (expr)))) ;
3363 IF CompareTrees (e1, e3) # 0
3364 THEN
3365 MetaErrorT2 (virtpos,
3366 'not allowed to assign array {%2Ead} to {%1ad} as they have a different number of elements',
3367 des, expr) ;
3368 RETURN( FALSE )
3369 END
3370 END ;
3371 RETURN( TRUE )
3372 END checkArrayElements ;
3373
3374
3375 (*
3376 CodeInitAddress -
3377 *)
3378
3379 PROCEDURE CodeInitAddress (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
3380 VAR
3381 location: location_t ;
3382 BEGIN
3383 DeclareConstant (CurrentQuadToken, op3) ; (* checks to see whether it is a constant and declares it *)
3384 DeclareConstructor (CurrentQuadToken, quad, op3) ;
3385
3386 location := TokenToLocation (CurrentQuadToken) ;
3387
3388 Assert (op2 = NulSym) ;
3389 Assert (GetMode (op1) = LeftValue) ;
3390 BuildAssignmentStatement (location,
3391 Mod2Gcc (op1),
3392 BuildConvert (location, GetPointerType (), Mod2Gcc (op3), FALSE))
3393 END CodeInitAddress ;
3394
3395
3396 (*
3397 checkRecordTypes - returns TRUE if des is not a record or if the record
3398 is the same type as expr.
3399 *)
3400
3401 PROCEDURE checkRecordTypes (des, expr: CARDINAL; virtpos: CARDINAL) : BOOLEAN ;
3402 VAR
3403 t1, t2: CARDINAL ;
3404 BEGIN
3405 IF (GetType (des) = NulSym) OR (GetMode (des) = LeftValue)
3406 THEN
3407 RETURN( TRUE )
3408 ELSE
3409 t1 := SkipType (GetType (des)) ;
3410 IF IsRecord (t1)
3411 THEN
3412 IF GetType (expr) = NulSym
3413 THEN
3414 MetaErrorT2 (virtpos,
3415 'cannot assign an operand of type {%1Ets} to a record type {%2tsa}',
3416 expr, des) ;
3417 RETURN( FALSE )
3418 ELSE
3419 t2 := SkipType (GetType (expr)) ;
3420 IF t1 = t2
3421 THEN
3422 RETURN( TRUE )
3423 ELSE
3424 MetaErrorT2 (virtpos,
3425 'cannot assign an operand of type {%1ts} to a record type {%2tsa}',
3426 expr, des) ;
3427 RETURN( FALSE )
3428 END
3429 END
3430 END
3431 END ;
3432 RETURN( TRUE )
3433 END checkRecordTypes ;
3434
3435
3436 (*
3437 checkIncorrectMeta - checks to see if des and expr are assignment compatible is allows
3438 generic system types to be assigned.
3439 *)
3440
3441 PROCEDURE checkIncorrectMeta (des, expr: CARDINAL; virtpos: CARDINAL) : BOOLEAN ;
3442 VAR
3443 t1, t2: CARDINAL ;
3444 BEGIN
3445 t1 := SkipType (GetType (des)) ;
3446 t2 := SkipType (GetType (expr)) ;
3447 IF (t1 = NulSym) OR (GetMode(des) = LeftValue) OR
3448 (t2 = NulSym) OR (GetMode(expr) = LeftValue)
3449 THEN
3450 RETURN( TRUE )
3451 ELSIF (t1 # t2) AND (NOT IsGenericSystemType (t1)) AND (NOT IsGenericSystemType (t2))
3452 THEN
3453 IF IsArray (t1) OR IsSet (t1) OR IsRecord (t1)
3454 THEN
3455 IF NOT IsAssignmentCompatible (t1, t2)
3456 THEN
3457 ErrorMessageDecl (virtpos,
3458 'illegal assignment error between {%1Etad} and {%2tad}',
3459 des, expr, TRUE) ;
3460 RETURN( FALSE )
3461 END
3462 END
3463 END ;
3464 RETURN( TRUE )
3465 END checkIncorrectMeta ;
3466
3467
3468 (*
3469 checkBecomes - returns TRUE if the checks pass.
3470 *)
3471
3472 PROCEDURE checkBecomes (des, expr: CARDINAL; virtpos, despos, exprpos: CARDINAL) : BOOLEAN ;
3473 BEGIN
3474 IF (NOT checkArrayElements (des, expr, virtpos, despos, exprpos)) OR
3475 (NOT checkRecordTypes (des, expr, virtpos)) OR
3476 (NOT checkIncorrectMeta (des, expr, virtpos))
3477 THEN
3478 RETURN FALSE
3479 END ;
3480 RETURN TRUE
3481 END checkBecomes ;
3482
3483
3484 (*
3485 checkDeclare - checks to see if sym is declared and if it is not then declare it.
3486 *)
3487
3488 PROCEDURE checkDeclare (sym: CARDINAL) ;
3489 BEGIN
3490 IF IsTemporary (sym) AND IsVariableSSA (sym) AND (NOT GccKnowsAbout (sym))
3491 THEN
3492 DeclareLocalVariable (sym)
3493 END
3494 END checkDeclare ;
3495
3496
3497 (*
3498 PerformCodeBecomes -
3499 *)
3500
3501 PROCEDURE PerformCodeBecomes (location: location_t;
3502 virtpos: CARDINAL; des, expr: CARDINAL) ;
3503 VAR
3504 destree, exprtree: tree ;
3505 BEGIN
3506 destree := Mod2Gcc (des) ;
3507 exprtree := FoldConstBecomes (virtpos, des, expr) ;
3508 IF IsVar (des) AND IsVariableSSA (des)
3509 THEN
3510 Replace (des, exprtree)
3511 ELSIF IsGccStrictTypeEquivalent (destree, exprtree)
3512 THEN
3513 BuildAssignmentStatement (location, destree, exprtree)
3514 ELSE
3515 CopyByField (location, destree, exprtree)
3516 END
3517 END PerformCodeBecomes ;
3518
3519
3520 (*
3521 ------------------------------------------------------------------------------
3522 := Operator
3523 ------------------------------------------------------------------------------
3524 Sym1<I> := Sym3<I> := produces a constant
3525 Sym1<O> := Sym3<O> := has the effect Mem[Sym1<I>] := Mem[Sym3<I>]
3526 *)
3527
3528 PROCEDURE CodeBecomes (quad: CARDINAL) ;
3529 VAR
3530 constExpr,
3531 overflowChecking: BOOLEAN ;
3532 op : QuadOperator ;
3533 des, op2, expr : CARDINAL ;
3534 virtpos,
3535 becomespos,
3536 despos,
3537 op2pos,
3538 exprpos : CARDINAL ;
3539 length,
3540 exprt : tree ;
3541 location : location_t ;
3542 BEGIN
3543 GetQuadOtok (quad, becomespos, op, des, op2, expr,
3544 overflowChecking, constExpr,
3545 despos, op2pos, exprpos) ;
3546 Assert (op2pos = UnknownTokenNo) ;
3547 DeclareConstant (exprpos, expr) ; (* Check to see whether expr is a constant and declare it. *)
3548 DeclareConstructor (exprpos, quad, expr) ;
3549 virtpos := MakeVirtualTok (becomespos, despos, exprpos) ;
3550 location := TokenToLocation (virtpos) ;
3551
3552 IF StrictTypeChecking AND
3553 (NOT AssignmentTypeCompatible (virtpos, "", des, expr, TRUE))
3554 THEN
3555 ErrorMessageDecl (virtpos,
3556 'assignment check caught mismatch between {%1Ead} and {%2ad}',
3557 des, expr, TRUE)
3558 END ;
3559 IF IsConstString (expr) AND (NOT IsConstStringKnown (expr))
3560 THEN
3561 MetaErrorT2 (virtpos,
3562 'internal error: CodeBecomes {%1Aad} in quad {%2n}', des, quad)
3563 END ;
3564 IF IsConst (des) AND (NOT GccKnowsAbout (des))
3565 THEN
3566 ConstantKnownAndUsed (des, CheckConstant (virtpos, des, expr))
3567 ELSIF IsConstString (expr) AND (SkipTypeAndSubrange (GetType (des)) # Char)
3568 THEN
3569 checkDeclare (des) ;
3570 IF NOT PrepareCopyString (becomespos, length, exprt, expr, SkipType (GetType (des)))
3571 THEN
3572 ErrorMessageDecl (virtpos,
3573 'string constant {%1Ea} is too large to be assigned to the array {%2ad}',
3574 expr, des, TRUE)
3575 END ;
3576 AddStatement (location,
3577 MaybeDebugBuiltinMemcpy (location,
3578 BuildAddr (location, Mod2Gcc (des), FALSE),
3579 BuildAddr (location, exprt, FALSE),
3580 length))
3581 ELSE
3582 IF ((IsGenericSystemType(SkipType(GetType(des))) #
3583 IsGenericSystemType(SkipType(GetType(expr)))) OR
3584 (IsUnbounded(SkipType(GetType(des))) AND
3585 IsUnbounded(SkipType(GetType(expr))) AND
3586 (IsGenericSystemType(SkipType(GetType(GetType(des)))) #
3587 IsGenericSystemType(SkipType(GetType(GetType(expr))))))) AND
3588 (NOT IsConstant(expr))
3589 THEN
3590 checkDeclare (des) ;
3591 AddStatement (location,
3592 MaybeDebugBuiltinMemcpy (location,
3593 BuildAddr(location, Mod2Gcc (des), FALSE),
3594 BuildAddr(location, Mod2Gcc (expr), FALSE),
3595 BuildSize(location, Mod2Gcc (des), FALSE)))
3596 ELSE
3597 IF checkBecomes (des, expr, virtpos, despos, exprpos)
3598 THEN
3599 PerformCodeBecomes (location, virtpos, des, expr)
3600 ELSE
3601 SubQuad (quad) (* We don't want multiple errors for the quad. *)
3602 END
3603 END
3604 END
3605 END CodeBecomes ;
3606
3607
3608 (*
3609 LValueToGenericPtr - returns a Tree representing symbol, sym.
3610 It coerces a lvalue into an internal pointer type
3611 *)
3612
3613 PROCEDURE LValueToGenericPtr (location: location_t; sym: CARDINAL) : tree ;
3614 VAR
3615 t: tree ;
3616 BEGIN
3617 t := Mod2Gcc (sym) ;
3618 IF t = NIL
3619 THEN
3620 InternalError ('expecting symbol to be resolved')
3621 END ;
3622 IF GetMode (sym) = LeftValue
3623 THEN
3624 t := BuildConvert (location, GetPointerType (), t, FALSE)
3625 END ;
3626 RETURN t
3627 END LValueToGenericPtr ;
3628
3629
3630 (*
3631 LValueToGenericPtrOrConvert - if sym is an lvalue then convert to pointer type
3632 else convert to type, type. Return the converted tree.
3633 *)
3634
3635 PROCEDURE LValueToGenericPtrOrConvert (sym: CARDINAL; type: tree) : tree ;
3636 VAR
3637 n : tree ;
3638 location: location_t ;
3639 BEGIN
3640 n := Mod2Gcc (sym) ;
3641 location := TokenToLocation (GetDeclaredMod (sym)) ;
3642 IF n = NIL
3643 THEN
3644 InternalError ('expecting symbol to be resolved')
3645 END ;
3646 IF GetMode (sym) = LeftValue
3647 THEN
3648 n := BuildConvert (location, GetPointerType (), n, FALSE)
3649 ELSE
3650 n := BuildConvert (location, type, n, FALSE)
3651 END ;
3652 RETURN n
3653 END LValueToGenericPtrOrConvert ;
3654
3655
3656 (*
3657 ZConstToTypedConst - checks whether op1 and op2 are constants and
3658 coerces, t, appropriately.
3659 *)
3660
3661 PROCEDURE ZConstToTypedConst (t: tree; op1, op2: CARDINAL) : tree ;
3662 VAR
3663 location: location_t ;
3664 BEGIN
3665 location := TokenToLocation(GetDeclaredMod(op2)) ;
3666 IF IsConst(op1) AND IsConst(op2)
3667 THEN
3668 (* leave, Z type, alone *)
3669 RETURN( t )
3670 ELSIF IsConst(op1)
3671 THEN
3672 IF GetMode(op2)=LeftValue
3673 THEN
3674 (* convert, Z type const into type of non constant operand *)
3675 RETURN( BuildConvert(location, GetPointerType(), t, FALSE) )
3676 ELSE
3677 (* convert, Z type const into type of non constant operand *)
3678 RETURN( BuildConvert(location, Mod2Gcc(FindType(op2)), t, FALSE) )
3679 END
3680 ELSIF IsConst(op2)
3681 THEN
3682 IF GetMode(op1)=LeftValue
3683 THEN
3684 (* convert, Z type const into type of non constant operand *)
3685 RETURN( BuildConvert(location, GetPointerType(), t, FALSE) )
3686 ELSE
3687 (* convert, Z type const into type of non constant operand *)
3688 RETURN( BuildConvert(location, Mod2Gcc(FindType(op1)), t, FALSE) )
3689 END
3690 ELSE
3691 (* neither operands are constants, leave alone *)
3692 RETURN( t )
3693 END
3694 END ZConstToTypedConst ;
3695
3696
3697 (*
3698 FoldBinary - check whether we can fold the binop operation.
3699 *)
3700
3701 PROCEDURE FoldBinary (tokenno: CARDINAL; p: WalkAction; binop: BuildBinProcedure;
3702 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
3703 VAR
3704 tl, tr, tv, resType: tree ;
3705 location : location_t ;
3706 BEGIN
3707 (* firstly ensure that constant literals are declared *)
3708 TryDeclareConstant(tokenno, op3) ;
3709 TryDeclareConstant(tokenno, op2) ;
3710 location := TokenToLocation(tokenno) ;
3711 IF IsConst(op2) AND IsConst(op3)
3712 THEN
3713 IF GccKnowsAbout(op2) AND GccKnowsAbout(op3)
3714 THEN
3715 (* fine, we can take advantage of this and fold constants *)
3716 IF IsConst(op1)
3717 THEN
3718 Assert(MixTypes(FindType(op3), FindType(op2), tokenno)#NulSym) ;
3719 PutConst(op1, MixTypes(FindType(op3), FindType(op2), tokenno)) ;
3720
3721 tl := LValueToGenericPtr(location, op2) ;
3722 tr := LValueToGenericPtr(location, op3) ;
3723
3724 IF GetType(op1)=NulSym
3725 THEN
3726 resType := GetM2ZType()
3727 ELSE
3728 resType := Mod2Gcc(GetType(op1))
3729 END ;
3730
3731 tl := BuildConvert(location, resType, tl, FALSE) ;
3732 tr := BuildConvert(location, resType, tr, FALSE) ;
3733
3734 tv := binop(location, tl, tr, TRUE) ;
3735 CheckOrResetOverflow(tokenno, tv, MustCheckOverflow(quad)) ;
3736
3737 AddModGcc(op1, DeclareKnownConstant(location, resType, tv)) ;
3738
3739 p(op1) ;
3740 NoChange := FALSE ;
3741 SubQuad(quad)
3742 ELSE
3743 (* we can still fold the expression, but not the assignment,
3744 however, we will not do this here but in CodeBinary
3745 *)
3746 END
3747 END
3748 END
3749 END FoldBinary ;
3750
3751
3752 (*
3753 ConvertBinaryOperands -
3754 *)
3755
3756 PROCEDURE ConvertBinaryOperands (location: location_t; VAR tl, tr: tree; type, op2, op3: CARDINAL) ;
3757 BEGIN
3758 tl := NIL ;
3759 tr := NIL ;
3760 IF GetMode(op2)=LeftValue
3761 THEN
3762 tl := LValueToGenericPtr(location, op2) ;
3763 type := Address
3764 END ;
3765 IF GetMode(op3)=LeftValue
3766 THEN
3767 tr := LValueToGenericPtr(location, op3) ;
3768 type := Address
3769 END ;
3770 IF (tl=NIL) AND (tr=NIL)
3771 THEN
3772 tl := BuildConvert(location, Mod2Gcc(type), Mod2Gcc(op2), FALSE) ;
3773 tr := BuildConvert(location, Mod2Gcc(type), Mod2Gcc(op3), FALSE)
3774 ELSIF tl=NIL
3775 THEN
3776 tl := BuildConvert(location, Mod2Gcc(type), Mod2Gcc(op2), FALSE)
3777 ELSIF tr=NIL
3778 THEN
3779 tr := BuildConvert(location, Mod2Gcc(type), Mod2Gcc(op3), FALSE)
3780 END
3781 END ConvertBinaryOperands ;
3782
3783
3784 (*
3785 CodeBinaryCheck - encode a binary arithmetic operation.
3786 *)
3787
3788 PROCEDURE CodeBinaryCheck (binop: BuildBinCheckProcedure; quad: CARDINAL) ;
3789 VAR
3790 op : QuadOperator ;
3791 op1, op2,
3792 op3 : CARDINAL ;
3793 op1pos,
3794 op2pos,
3795 op3pos,
3796 lowestType,
3797 type : CARDINAL ;
3798 min, max,
3799 lowest,
3800 tv,
3801 tl, tr : tree ;
3802 location : location_t ;
3803 BEGIN
3804 (* firstly ensure that constant literals are declared. *)
3805 GetQuadtok (quad, op, op1, op2, op3, op1pos, op2pos, op3pos) ;
3806 DeclareConstant (op3pos, op3) ;
3807 DeclareConstant (op2pos, op2) ;
3808 location := TokenToLocation (op1pos) ;
3809
3810 type := MixTypesBinary (op2, op3, op1pos, MustCheckOverflow (quad)) ;
3811 ConvertBinaryOperands (location, tl, tr, type, op2, op3) ;
3812
3813 lowestType := GetLType (op1) ;
3814 lowest := Mod2Gcc (lowestType) ;
3815 IF GetMinMax (CurrentQuadToken, lowestType, min, max)
3816 THEN
3817 tv := binop (location, tl, tr, lowest, min, max)
3818 ELSE
3819 tv := binop (location, tl, tr, NIL, NIL, NIL)
3820 END ;
3821 CheckOrResetOverflow (op1pos, tv, MustCheckOverflow (quad)) ;
3822 IF IsConst (op1)
3823 THEN
3824 (* still have a constant which was not resolved, pass it to gcc. *)
3825 Assert (MixTypes (FindType (op3), FindType (op2), op3pos) # NulSym) ;
3826
3827 PutConst (op1, MixTypes (FindType (op3), FindType (op2), op3pos)) ;
3828 ConstantKnownAndUsed (op1, DeclareKnownConstant (location, Mod2Gcc (GetType (op3)), tv))
3829 ELSE
3830 IF EnableSSA AND IsVariableSSA (op1)
3831 THEN
3832 Replace (op1, tv)
3833 ELSE
3834 BuildAssignmentStatement (location, Mod2Gcc (op1), tv)
3835 END
3836 END
3837 END CodeBinaryCheck ;
3838
3839
3840 (*
3841 MixTypesBinary - depending upon overflowCheck do not check pointer arithmetic.
3842 *)
3843
3844 PROCEDURE MixTypesBinary (left, right: CARDINAL;
3845 tokpos: CARDINAL; overflowCheck: BOOLEAN) : CARDINAL ;
3846 BEGIN
3847 IF (NOT overflowCheck) AND
3848 (IsPointer (GetTypeMode (left)) OR IsPointer (GetTypeMode (right)))
3849 THEN
3850 RETURN Address
3851 ELSE
3852 RETURN MixTypesDecl (left, right, FindType (left), FindType (right), tokpos)
3853 END
3854 END MixTypesBinary ;
3855
3856
3857 (*
3858 CodeBinary - encode a binary arithmetic operation.
3859 *)
3860
3861 PROCEDURE CodeBinary (binop: BuildBinProcedure; quad: CARDINAL) ;
3862 VAR
3863 op : QuadOperator ;
3864 op1, op2,
3865 op3 : CARDINAL ;
3866 op1pos,
3867 op2pos,
3868 op3pos,
3869 type : CARDINAL ;
3870 tv,
3871 tl, tr : tree ;
3872 location: location_t ;
3873 BEGIN
3874 (* firstly ensure that constant literals are declared *)
3875 GetQuadtok (quad, op, op1, op2, op3, op1pos, op2pos, op3pos) ;
3876 DeclareConstant (op3pos, op3) ;
3877 DeclareConstant (op2pos, op2) ;
3878 location := TokenToLocation (op1pos) ;
3879
3880 type := MixTypesBinary (op2, op3, op1pos, MustCheckOverflow (quad)) ;
3881 ConvertBinaryOperands (location, tl, tr, type, op2, op3) ;
3882
3883 tv := binop (location, tl, tr, FALSE) ;
3884 CheckOrResetOverflow (op1pos, tv, MustCheckOverflow(quad)) ;
3885 IF IsConst (op1)
3886 THEN
3887 (* still have a constant which was not resolved, pass it to gcc *)
3888 Assert(MixTypes(FindType(op3), FindType(op2), op1pos)#NulSym) ;
3889
3890 PutConst (op1, MixTypes (FindType (op3), FindType (op2), op1pos)) ;
3891 ConstantKnownAndUsed (op1, DeclareKnownConstant (location, Mod2Gcc(GetType(op3)), tv))
3892 ELSE
3893 IF EnableSSA AND IsVariableSSA (op1)
3894 THEN
3895 Replace (op1, tv)
3896 ELSE
3897 BuildAssignmentStatement (location, Mod2Gcc (op1), tv)
3898 END
3899 END
3900 END CodeBinary ;
3901
3902
3903 (*
3904 NoWalkProcedure -
3905 *)
3906
3907 PROCEDURE NoWalkProcedure (param: CARDINAL <* unused *>) ;
3908 BEGIN
3909 END NoWalkProcedure ;
3910
3911
3912 (*
3913 CheckBinaryExpressionTypes - returns TRUE if all expression checks pass.
3914 If the expression check fails quad is removed,
3915 the walk procedure (des) is called and NoChange is
3916 set to FALSE.
3917 *)
3918
3919 PROCEDURE CheckBinaryExpressionTypes (quad: CARDINAL; p: WalkAction) : BOOLEAN ;
3920 VAR
3921 des, left, right: CARDINAL ;
3922 typeChecking,
3923 constExpr,
3924 overflowChecking: BOOLEAN ;
3925 despos, leftpos,
3926 rightpos,
3927 operatorpos,
3928 subexprpos : CARDINAL ;
3929 op : QuadOperator ;
3930 BEGIN
3931 GetQuadOTypetok (quad, operatorpos, op,
3932 des, left, right,
3933 overflowChecking, typeChecking, constExpr,
3934 despos, leftpos, rightpos) ;
3935 IF typeChecking AND (op # LogicalRotateOp) AND (op # LogicalShiftOp)
3936 THEN
3937 subexprpos := MakeVirtualTok (operatorpos, leftpos, rightpos) ;
3938 IF StrictTypeChecking AND
3939 (NOT ExpressionTypeCompatible (subexprpos, "", left, right,
3940 StrictTypeChecking, FALSE))
3941 THEN
3942 MetaErrorT2 (subexprpos,
3943 'expression mismatch between {%1Etad} and {%2tad}',
3944 left, right) ;
3945 NoChange := FALSE ;
3946 SubQuad (quad) ;
3947 p (des) ;
3948 RETURN FALSE
3949 END
3950 END ;
3951 RETURN TRUE
3952 END CheckBinaryExpressionTypes ;
3953
3954
3955 (*
3956 CheckElementSetTypes - returns TRUE if all expression checks pass.
3957 If the expression check fails quad is removed,
3958 the walk procedure (des) is called and NoChange is
3959 set to FALSE.
3960 *)
3961
3962 PROCEDURE CheckElementSetTypes (quad: CARDINAL) : BOOLEAN ;
3963 VAR
3964 righttype,
3965 ignore, left, right: CARDINAL ;
3966 constExpr,
3967 overflowChecking: BOOLEAN ;
3968 ignorepos,
3969 leftpos,
3970 rightpos,
3971 operatorpos,
3972 subexprpos : CARDINAL ;
3973 op : QuadOperator ;
3974 BEGIN
3975 GetQuadOtok (quad, operatorpos, op,
3976 left, right, ignore,
3977 overflowChecking, constExpr,
3978 leftpos, rightpos, ignorepos) ;
3979 subexprpos := MakeVirtualTok (operatorpos, leftpos, rightpos) ;
3980 righttype := GetType (right) ;
3981 IF StrictTypeChecking AND
3982 (NOT ExpressionTypeCompatible (subexprpos, "", left, right,
3983 StrictTypeChecking, TRUE))
3984 THEN
3985 MetaErrorT2 (subexprpos,
3986 'the types used in expression {%1Etad} {%kIN} {%2tad} are incompatible',
3987 left, right) ;
3988 NoChange := FALSE ;
3989 SubQuad (quad) ;
3990 RETURN FALSE
3991 END ;
3992 IF (righttype = NulSym) OR (NOT IsSet (SkipType (righttype)))
3993 THEN
3994 MetaErrorT1 (rightpos,
3995 'an {%kIN} expression is expecting {%1Etad} to be a {%kSET} type',
3996 right) ;
3997 NoChange := FALSE ;
3998 SubQuad (quad) ;
3999 RETURN FALSE
4000 END ;
4001 RETURN TRUE
4002 END CheckElementSetTypes ;
4003
4004
4005 (*
4006 CodeBinarySet - encode a binary set arithmetic operation.
4007 Set operands may be longer than a word.
4008 *)
4009
4010 PROCEDURE CodeBinarySet (binop: BuildBinProcedure; doOp: DoProcedure;
4011 quad: CARDINAL) ;
4012 VAR
4013 location : location_t ;
4014 constExpr,
4015 overflowChecking: BOOLEAN ;
4016 op : QuadOperator ;
4017 virttoken,
4018 virtexpr,
4019 des,
4020 left,
4021 right,
4022 despos,
4023 leftpos,
4024 rightpos,
4025 operatorpos : CARDINAL ;
4026 BEGIN
4027 GetQuadOtok (quad, operatorpos, op, des, left, right,
4028 overflowChecking, constExpr,
4029 despos, leftpos, rightpos) ;
4030
4031 (* Firstly ensure that constant literals are declared. *)
4032 DeclareConstant (rightpos, right) ;
4033 DeclareConstant (leftpos, left) ;
4034 DeclareConstructor (rightpos, quad, right) ;
4035 DeclareConstructor (leftpos, quad, left) ;
4036
4037 virttoken := MakeVirtualTok (operatorpos, despos, rightpos) ;
4038 location := TokenToLocation (virttoken) ;
4039 IF CheckBinaryExpressionTypes (quad, NoWalkProcedure)
4040 THEN
4041 IF IsConst (des)
4042 THEN
4043 virtexpr := MakeVirtualTok (operatorpos, leftpos, rightpos) ;
4044 IF IsValueSolved (left) AND IsValueSolved (right)
4045 THEN
4046 Assert (MixTypes (FindType (right), FindType (left), virtexpr) # NulSym) ;
4047 PutConst (des, FindType (right)) ;
4048 PushValue (left) ;
4049 PushValue (right) ;
4050 doOp (virttoken) ;
4051 PopValue (des) ;
4052 PutConstSet (des)
4053 ELSE
4054 MetaErrorT0 (virtexpr, '{%E}constant expression cannot be evaluated')
4055 END
4056 ELSE
4057 checkDeclare (des) ;
4058 BuildBinaryForeachWordDo (location,
4059 Mod2Gcc (SkipType (GetType (des))),
4060 Mod2Gcc (des), Mod2Gcc (left), Mod2Gcc (right), binop,
4061 GetMode (des) = LeftValue,
4062 GetMode (left) = LeftValue,
4063 GetMode (right) = LeftValue,
4064 IsConst (des),
4065 IsConst (left),
4066 IsConst (right))
4067 END
4068 END
4069 END CodeBinarySet ;
4070
4071
4072 (*
4073 CheckUnaryOperand - checks to see whether operand is using a generic type.
4074 *)
4075
4076 PROCEDURE CheckUnaryOperand (quad: CARDINAL; operand: CARDINAL) : BOOLEAN ;
4077 VAR
4078 type : CARDINAL ;
4079 s, op : String ;
4080 BEGIN
4081 type := SkipType (GetType (operand)) ;
4082 IF (Word=type) OR IsWordN (type) OR (Byte=type) OR (Loc=type)
4083 THEN
4084 op := GetM2OperatorDesc (GetQuadOp (quad)) ;
4085 s := InitString ('operand of type {%1Ets} is not allowed in an unary expression') ;
4086 IF op # NIL
4087 THEN
4088 s := ConCatChar (s, ' ') ;
4089 s := ConCat (s, Mark (op))
4090 END ;
4091 MetaErrorStringT1 (CurrentQuadToken, s, operand) ;
4092 RETURN FALSE
4093 END ;
4094 RETURN TRUE
4095 END CheckUnaryOperand ;
4096
4097
4098 (*
4099 UnaryOperand - returns TRUE if operand is acceptable for
4100 unary operator: + -. If FALSE
4101 is returned, an error message will be generated
4102 and the quad is deleted.
4103 *)
4104
4105 PROCEDURE UnaryOperand (quad: CARDINAL; operand: CARDINAL) : BOOLEAN ;
4106 BEGIN
4107 IF NOT CheckUnaryOperand (quad, operand)
4108 THEN
4109 SubQuad (quad) ; (* We do not want multiple copies of the same error. *)
4110 RETURN FALSE
4111 END ;
4112 RETURN TRUE
4113 END UnaryOperand ;
4114
4115
4116 (*
4117 CheckBinaryOperand - checks to see whether operand is using a generic type.
4118 *)
4119
4120 PROCEDURE CheckBinaryOperand (quad: CARDINAL; isleft: BOOLEAN;
4121 operand: CARDINAL; result: BOOLEAN) : BOOLEAN ;
4122 VAR
4123 type : CARDINAL ;
4124 qop : QuadOperator ;
4125 op1,
4126 op2,
4127 op3,
4128 op1pos,
4129 op2pos,
4130 op3pos: CARDINAL ;
4131 s, op : String ;
4132 BEGIN
4133 type := SkipType (GetType (operand)) ;
4134 IF (Word=type) OR IsWordN (type) OR (Byte=type) OR (Loc=type)
4135 THEN
4136 GetQuadtok (quad, qop, op1, op2, op3,
4137 op1pos, op2pos, op3pos) ;
4138 op := GetM2OperatorDesc (GetQuadOp (quad)) ;
4139 IF isleft
4140 THEN
4141 s := InitString ('left operand {%1Ea} of type {%1Ets} is not allowed in binary expression')
4142 ELSE
4143 s := InitString ('right operand {%1Ea} of type {%1Ets} is not allowed in binary expression')
4144 END ;
4145 IF op # NIL
4146 THEN
4147 s := ConCatChar (s, ' ') ;
4148 s := ConCat (s, Mark (op))
4149 END ;
4150 MetaErrorStringT1 (op1pos, s, operand) ;
4151 RETURN FALSE
4152 END ;
4153 RETURN result
4154 END CheckBinaryOperand ;
4155
4156
4157 (*
4158 BinaryOperands - returns TRUE if, l, and, r, are acceptable for
4159 binary operator: + - / * and friends. If FALSE
4160 is returned, an error message will be generated
4161 and the, quad, is deleted.
4162 *)
4163
4164 PROCEDURE BinaryOperands (quad: CARDINAL; l, r: CARDINAL) : BOOLEAN ;
4165 VAR
4166 result: BOOLEAN ;
4167 BEGIN
4168 result := CheckBinaryOperand (quad, TRUE, l, TRUE) ;
4169 result := CheckBinaryOperand (quad, FALSE, r, result) ;
4170 IF NOT result
4171 THEN
4172 SubQuad (quad) (* We do not want multiple copies of the same error. *)
4173 END ;
4174 RETURN result
4175 END BinaryOperands ;
4176
4177
4178 (*
4179 IsConstStr - returns TRUE if sym is a constant string or a char constant.
4180 *)
4181
4182 PROCEDURE IsConstStr (sym: CARDINAL) : BOOLEAN ;
4183 BEGIN
4184 RETURN IsConstString (sym) OR (IsConst (sym) AND (GetSType (sym) = Char))
4185 END IsConstStr ;
4186
4187
4188 (*
4189 IsConstStrKnown - returns TRUE if sym is a constant string or a char constant
4190 which is known.
4191 *)
4192
4193 PROCEDURE IsConstStrKnown (sym: CARDINAL) : BOOLEAN ;
4194 BEGIN
4195 RETURN (IsConstString (sym) AND IsConstStringKnown (sym)) OR
4196 (IsConst (sym) AND (GetSType (sym) = Char))
4197 END IsConstStrKnown ;
4198
4199
4200 (*
4201 GetStr - return a string containing a constant string value associated with sym.
4202 A nul char constant will return an empty string.
4203 *)
4204
4205 PROCEDURE GetStr (tokenno: CARDINAL; sym: CARDINAL) : String ;
4206 VAR
4207 ch: CHAR ;
4208 BEGIN
4209 Assert (IsConst (sym)) ;
4210 IF IsConstString (sym)
4211 THEN
4212 RETURN InitStringCharStar (KeyToCharStar (GetString (sym)))
4213 ELSE
4214 Assert (GetSType (sym) = Char) ;
4215 PushValue (sym) ;
4216 ch := PopChar (tokenno) ;
4217 RETURN InitStringChar (ch)
4218 END
4219 END GetStr ;
4220
4221
4222 (*
4223 FoldAdd - check addition for constant folding. It checks for conststrings
4224 overloading the +.
4225 *)
4226
4227 PROCEDURE FoldAdd (tokenno: CARDINAL; p: WalkAction;
4228 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4229 VAR
4230 s: String ;
4231 BEGIN
4232 IF IsConstStr (op2) AND IsConstStr (op3)
4233 THEN
4234 IF IsConstStrKnown (op2) AND IsConstStrKnown (op3)
4235 THEN
4236 (* Handle special addition for constant strings. *)
4237 s := Dup (GetStr (tokenno, op2)) ;
4238 s := ConCat (s, GetStr (tokenno, op3)) ;
4239 PutConstStringKnown (tokenno, op1, makekey (string (s)), FALSE, TRUE) ;
4240 TryDeclareConstant (tokenno, op1) ;
4241 p (op1) ;
4242 NoChange := FALSE ;
4243 SubQuad (quad) ;
4244 s := KillString (s)
4245 END
4246 ELSE
4247 FoldArithAdd (tokenno, p, quad, op1, op2, op3)
4248 END
4249 END FoldAdd ;
4250
4251
4252 (*
4253 FoldArithAdd - check arithmetic addition for constant folding.
4254 *)
4255
4256 PROCEDURE FoldArithAdd (tokenno: CARDINAL; p: WalkAction;
4257 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4258 BEGIN
4259 IF BinaryOperands (quad, op2, op3)
4260 THEN
4261 FoldBinary (tokenno, p, BuildAdd, quad, op1, op2, op3)
4262 END
4263 END FoldArithAdd ;
4264
4265
4266 (*
4267 CodeAddChecked - code an addition instruction, determine whether checking
4268 is required.
4269 *)
4270
4271 PROCEDURE CodeAddChecked (quad: CARDINAL; left, right: CARDINAL) ;
4272 BEGIN
4273 IF MustCheckOverflow (quad)
4274 THEN
4275 CodeAddCheck (quad, left, right)
4276 ELSE
4277 CodeAdd (quad, left, right)
4278 END
4279 END CodeAddChecked ;
4280
4281
4282 (*
4283 CodeAddCheck - encode addition but check for overflow.
4284 *)
4285
4286 PROCEDURE CodeAddCheck (quad, left, right: CARDINAL) ;
4287 BEGIN
4288 IF BinaryOperands (quad, left, right)
4289 THEN
4290 CodeBinaryCheck (BuildAddCheck, quad)
4291 END
4292 END CodeAddCheck ;
4293
4294
4295 (*
4296 CodeAdd - encode addition.
4297 *)
4298
4299 PROCEDURE CodeAdd (quad: CARDINAL; left, right: CARDINAL) ;
4300 BEGIN
4301 IF BinaryOperands (quad, left, right)
4302 THEN
4303 CodeBinary (BuildAdd, quad)
4304 END
4305 END CodeAdd ;
4306
4307
4308 (*
4309 FoldSub - check subtraction for constant folding.
4310 *)
4311
4312 PROCEDURE FoldSub (tokenno: CARDINAL; p: WalkAction;
4313 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4314 BEGIN
4315 IF BinaryOperands (quad, op2, op3)
4316 THEN
4317 FoldBinary(tokenno, p, BuildSub, quad, op1, op2, op3)
4318 END
4319 END FoldSub ;
4320
4321
4322 (*
4323 CodeSubChecked - code a subtract instruction, determine whether checking
4324 is required.
4325 *)
4326
4327 PROCEDURE CodeSubChecked (quad: CARDINAL; left, right: CARDINAL) ;
4328 BEGIN
4329 IF MustCheckOverflow (quad)
4330 THEN
4331 CodeSubCheck (quad, left, right)
4332 ELSE
4333 CodeSub (quad, left, right)
4334 END
4335 END CodeSubChecked ;
4336
4337
4338 (*
4339 CodeSubCheck - encode subtraction but check for overflow.
4340 *)
4341
4342 PROCEDURE CodeSubCheck (quad: CARDINAL; left, right: CARDINAL) ;
4343 BEGIN
4344 IF BinaryOperands (quad, left, right)
4345 THEN
4346 CodeBinaryCheck (BuildSubCheck, quad)
4347 END
4348 END CodeSubCheck ;
4349
4350
4351 (*
4352 CodeSub - encode subtraction.
4353 *)
4354
4355 PROCEDURE CodeSub (quad: CARDINAL; left, right: CARDINAL) ;
4356 BEGIN
4357 IF BinaryOperands (quad, left, right)
4358 THEN
4359 CodeBinary (BuildSub, quad)
4360 END
4361 END CodeSub ;
4362
4363
4364 (*
4365 FoldMult - check multiplication for constant folding.
4366 *)
4367
4368 PROCEDURE FoldMult (tokenno: CARDINAL; p: WalkAction;
4369 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4370 BEGIN
4371 IF BinaryOperands (quad, op2, op3)
4372 THEN
4373 FoldBinary(tokenno, p, BuildMult, quad, op1, op2, op3)
4374 END
4375 END FoldMult ;
4376
4377
4378 (*
4379 CodeMultChecked - code a multiplication instruction, determine whether checking
4380 is required.
4381 *)
4382
4383 PROCEDURE CodeMultChecked (quad: CARDINAL; left, right: CARDINAL) ;
4384 BEGIN
4385 IF MustCheckOverflow (quad)
4386 THEN
4387 CodeMultCheck (quad, left, right)
4388 ELSE
4389 CodeMult (quad, left, right)
4390 END
4391 END CodeMultChecked ;
4392
4393
4394 (*
4395 CodeMultCheck - encode multiplication but check for overflow.
4396 *)
4397
4398 PROCEDURE CodeMultCheck (quad: CARDINAL; left, right: CARDINAL) ;
4399 BEGIN
4400 IF BinaryOperands (quad, left, right)
4401 THEN
4402 CodeBinaryCheck (BuildMultCheck, quad)
4403 END
4404 END CodeMultCheck ;
4405
4406
4407 (*
4408 CodeMult - encode multiplication.
4409 *)
4410
4411 PROCEDURE CodeMult (quad: CARDINAL; left, right: CARDINAL) ;
4412 BEGIN
4413 IF BinaryOperands (quad, left, right)
4414 THEN
4415 CodeBinary (BuildMult, quad)
4416 END
4417 END CodeMult ;
4418
4419
4420 (*
4421 CodeDivM2Checked - code a divide instruction, determine whether checking
4422 is required.
4423 *)
4424
4425 PROCEDURE CodeDivM2Checked (quad: CARDINAL; left, right: CARDINAL) ;
4426 BEGIN
4427 IF MustCheckOverflow (quad)
4428 THEN
4429 CodeDivM2Check (quad, left, right)
4430 ELSE
4431 CodeDivM2 (quad, left, right)
4432 END
4433 END CodeDivM2Checked ;
4434
4435
4436 (*
4437 CodeDivM2Check - encode addition but check for overflow.
4438 *)
4439
4440 PROCEDURE CodeDivM2Check (quad: CARDINAL; left, right: CARDINAL) ;
4441 BEGIN
4442 IF BinaryOperands (quad, left, right)
4443 THEN
4444 CodeBinaryCheck (BuildDivM2Check, quad)
4445 END
4446 END CodeDivM2Check ;
4447
4448
4449 (*
4450 CodeModM2Checked - code a modulus instruction, determine whether checking
4451 is required.
4452 *)
4453
4454 PROCEDURE CodeModM2Checked (quad: CARDINAL; left, right: CARDINAL) ;
4455 BEGIN
4456 IF MustCheckOverflow (quad)
4457 THEN
4458 CodeModM2Check (quad, left, right)
4459 ELSE
4460 CodeModM2 (quad, left, right)
4461 END
4462 END CodeModM2Checked ;
4463
4464
4465 (*
4466 CodeModM2Check - encode addition but check for overflow.
4467 *)
4468
4469 PROCEDURE CodeModM2Check (quad: CARDINAL; left, right: CARDINAL) ;
4470 BEGIN
4471 IF BinaryOperands (quad, left, right)
4472 THEN
4473 CodeBinaryCheck (BuildModM2Check, quad)
4474 END
4475 END CodeModM2Check ;
4476
4477
4478 (*
4479 BinaryOperandRealFamily -
4480 *)
4481
4482 PROCEDURE BinaryOperandRealFamily (op: CARDINAL) : BOOLEAN ;
4483 VAR
4484 t: CARDINAL ;
4485 BEGIN
4486 t := SkipType(GetType(op)) ;
4487 RETURN( IsComplexType(t) OR IsComplexN(t) OR
4488 IsRealType(t) OR IsRealN(t) )
4489 END BinaryOperandRealFamily ;
4490
4491
4492 (*
4493 FoldDivM2 - check division for constant folding.
4494 *)
4495
4496 PROCEDURE FoldDivM2 (tokenno: CARDINAL; p: WalkAction;
4497 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4498 BEGIN
4499 IF BinaryOperands (quad, op2, op3)
4500 THEN
4501 IF BinaryOperandRealFamily(op2) OR BinaryOperandRealFamily(op3)
4502 THEN
4503 FoldBinary(tokenno, p, BuildRDiv, quad, op1, op2, op3)
4504 ELSE
4505 FoldBinary(tokenno, p, BuildDivM2, quad, op1, op2, op3)
4506 END
4507 END
4508 END FoldDivM2 ;
4509
4510
4511 (*
4512 CodeDivM2 - encode division.
4513 *)
4514
4515 PROCEDURE CodeDivM2 (quad: CARDINAL; left, right: CARDINAL) ;
4516 BEGIN
4517 IF BinaryOperands (quad, left, right)
4518 THEN
4519 IF BinaryOperandRealFamily (left) OR BinaryOperandRealFamily (right)
4520 THEN
4521 CodeBinary (BuildRDiv, quad)
4522 ELSE
4523 CodeBinary (BuildDivM2, quad)
4524 END
4525 END
4526 END CodeDivM2 ;
4527
4528
4529 (*
4530 FoldModM2 - check modulus for constant folding.
4531 *)
4532
4533 PROCEDURE FoldModM2 (tokenno: CARDINAL; p: WalkAction;
4534 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4535 BEGIN
4536 IF BinaryOperands (quad, op2, op3)
4537 THEN
4538 FoldBinary(tokenno, p, BuildModM2, quad, op1, op2, op3)
4539 END
4540 END FoldModM2 ;
4541
4542
4543 (*
4544 CodeModM2 - encode modulus.
4545 *)
4546
4547 PROCEDURE CodeModM2 (quad: CARDINAL; left, right: CARDINAL) ;
4548 BEGIN
4549 IF BinaryOperands (quad, left, right)
4550 THEN
4551 CodeBinary (BuildModM2, quad)
4552 END
4553 END CodeModM2 ;
4554
4555
4556 (*
4557 FoldDivTrunc - check division for constant folding.
4558 *)
4559
4560 PROCEDURE FoldDivTrunc (tokenno: CARDINAL; p: WalkAction;
4561 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4562 BEGIN
4563 IF BinaryOperands (quad, op2, op3)
4564 THEN
4565 IF BinaryOperandRealFamily(op2) OR BinaryOperandRealFamily(op3)
4566 THEN
4567 FoldBinary(tokenno, p, BuildRDiv, quad, op1, op2, op3)
4568 ELSE
4569 FoldBinary(tokenno, p, BuildDivTrunc, quad, op1, op2, op3)
4570 END
4571 END
4572 END FoldDivTrunc ;
4573
4574
4575 (*
4576 CodeDivTrunc - encode multiplication.
4577 *)
4578
4579 PROCEDURE CodeDivTrunc (quad: CARDINAL; left, right: CARDINAL) ;
4580 BEGIN
4581 IF BinaryOperands (quad, left, right)
4582 THEN
4583 IF BinaryOperandRealFamily (left) OR BinaryOperandRealFamily (right)
4584 THEN
4585 CodeBinary (BuildRDiv, quad)
4586 ELSE
4587 CodeBinary (BuildDivTrunc, quad)
4588 END
4589 END
4590 END CodeDivTrunc ;
4591
4592
4593 (*
4594 FoldModTrunc - check modulus for constant folding.
4595 *)
4596
4597 PROCEDURE FoldModTrunc (tokenno: CARDINAL; p: WalkAction;
4598 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4599 BEGIN
4600 IF BinaryOperands (quad, op2, op3)
4601 THEN
4602 FoldBinary(tokenno, p, BuildModTrunc, quad, op1, op2, op3)
4603 END
4604 END FoldModTrunc ;
4605
4606
4607 (*
4608 CodeModTrunc - encode modulus.
4609 *)
4610
4611 PROCEDURE CodeModTrunc (quad: CARDINAL; left, right: CARDINAL) ;
4612 BEGIN
4613 IF BinaryOperands (quad, left, right)
4614 THEN
4615 CodeBinary (BuildModTrunc, quad)
4616 END
4617 END CodeModTrunc ;
4618
4619
4620 (*
4621 FoldDivCeil - check division for constant folding.
4622 *)
4623
4624 PROCEDURE FoldDivCeil (tokenno: CARDINAL; p: WalkAction;
4625 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4626 BEGIN
4627 IF BinaryOperands (quad, op2, op3)
4628 THEN
4629 IF BinaryOperandRealFamily(op2) OR BinaryOperandRealFamily(op3)
4630 THEN
4631 FoldBinary(tokenno, p, BuildRDiv, quad, op1, op2, op3)
4632 ELSE
4633 FoldBinary(tokenno, p, BuildDivCeil, quad, op1, op2, op3)
4634 END
4635 END
4636 END FoldDivCeil ;
4637
4638
4639 (*
4640 CodeDivCeil - encode multiplication.
4641 *)
4642
4643 PROCEDURE CodeDivCeil (quad: CARDINAL; left, right: CARDINAL) ;
4644 BEGIN
4645 IF BinaryOperands (quad, left, right)
4646 THEN
4647 IF BinaryOperandRealFamily (left) OR BinaryOperandRealFamily (right)
4648 THEN
4649 CodeBinary (BuildRDiv, quad)
4650 ELSE
4651 CodeBinary (BuildDivCeil, quad)
4652 END
4653 END
4654 END CodeDivCeil ;
4655
4656
4657 (*
4658 FoldModCeil - check modulus for constant folding.
4659 *)
4660
4661 PROCEDURE FoldModCeil (tokenno: CARDINAL; p: WalkAction;
4662 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4663 BEGIN
4664 IF BinaryOperands (quad, op2, op3)
4665 THEN
4666 FoldBinary(tokenno, p, BuildModCeil, quad, op1, op2, op3)
4667 END
4668 END FoldModCeil ;
4669
4670
4671 (*
4672 CodeModCeil - encode multiplication.
4673 *)
4674
4675 PROCEDURE CodeModCeil (quad: CARDINAL; left, right: CARDINAL) ;
4676 BEGIN
4677 IF BinaryOperands (quad, left, right)
4678 THEN
4679 CodeBinary (BuildModCeil, quad)
4680 END
4681 END CodeModCeil ;
4682
4683
4684 (*
4685 FoldDivFloor - check division for constant folding.
4686 *)
4687
4688 PROCEDURE FoldDivFloor (tokenno: CARDINAL; p: WalkAction;
4689 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4690 BEGIN
4691 IF BinaryOperands (quad, op2, op3)
4692 THEN
4693 IF BinaryOperandRealFamily(op2) OR BinaryOperandRealFamily(op3)
4694 THEN
4695 FoldBinary(tokenno, p, BuildRDiv, quad, op1, op2, op3)
4696 ELSE
4697 FoldBinary(tokenno, p, BuildDivFloor, quad, op1, op2, op3)
4698 END
4699 END
4700 END FoldDivFloor ;
4701
4702
4703 (*
4704 CodeDivFloor - encode multiplication.
4705 *)
4706
4707 PROCEDURE CodeDivFloor (quad: CARDINAL; left, right: CARDINAL) ;
4708 BEGIN
4709 IF BinaryOperands (quad, left, right)
4710 THEN
4711 IF BinaryOperandRealFamily (left) OR BinaryOperandRealFamily (right)
4712 THEN
4713 CodeBinary (BuildRDiv, quad)
4714 ELSE
4715 CodeBinary (BuildDivFloor, quad)
4716 END
4717 END
4718 END CodeDivFloor ;
4719
4720
4721 (*
4722 FoldModFloor - check modulus for constant folding.
4723 *)
4724
4725 PROCEDURE FoldModFloor (tokenno: CARDINAL; p: WalkAction;
4726 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4727 BEGIN
4728 IF BinaryOperands (quad, op2, op3)
4729 THEN
4730 FoldBinary(tokenno, p, BuildModFloor, quad, op1, op2, op3)
4731 END
4732 END FoldModFloor ;
4733
4734
4735 (*
4736 CodeModFloor - encode modulus.
4737 *)
4738
4739 PROCEDURE CodeModFloor (quad: CARDINAL; left, right: CARDINAL) ;
4740 BEGIN
4741 IF BinaryOperands (quad, left, right)
4742 THEN
4743 CodeBinary (BuildModFloor, quad)
4744 END
4745 END CodeModFloor ;
4746
4747
4748 (*
4749 FoldBuiltinConst -
4750 *)
4751
4752 PROCEDURE FoldBuiltinConst (tokenno: CARDINAL; p: WalkAction;
4753 quad: CARDINAL; result, constDesc: CARDINAL) ;
4754 VAR
4755 value: tree ;
4756 BEGIN
4757 value := GetBuiltinConst (KeyToCharStar (Name (constDesc))) ;
4758 IF value = NIL
4759 THEN
4760 MetaErrorT1 (tokenno, 'unknown built in constant {%1Ead}', constDesc)
4761 ELSE
4762 AddModGcc (result, value) ;
4763 p (result) ;
4764 NoChange := FALSE ;
4765 SubQuad (quad)
4766 END
4767 END FoldBuiltinConst ;
4768
4769
4770 (*
4771 FoldBuiltinTypeInfo - attempts to fold a builtin attribute value on type op2.
4772 *)
4773
4774 PROCEDURE FoldBuiltinTypeInfo (tokenno: CARDINAL; p: WalkAction;
4775 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
4776 VAR
4777 t : tree ;
4778 location: location_t ;
4779 BEGIN
4780 IF GccKnowsAbout(op2) AND CompletelyResolved(op2)
4781 THEN
4782 location := TokenToLocation(tokenno) ;
4783 t := GetBuiltinTypeInfo(location, Mod2Gcc(op2), KeyToCharStar(Name(op3))) ;
4784 IF t=NIL
4785 THEN
4786 MetaErrorT2 (tokenno, 'unknown built in constant {%1Ead} attribute for type {%2ad}', op3, op2)
4787 ELSE
4788 AddModGcc(op1, t) ;
4789 p(op1) ;
4790 NoChange := FALSE ;
4791 SubQuad(quad)
4792 END
4793 END
4794 END FoldBuiltinTypeInfo ;
4795
4796
4797 (*
4798 FoldTBitsize - attempt to fold the standard function SYSTEM.TBITSIZE
4799 quadruple. If the quadruple is folded it is removed.
4800 *)
4801
4802 PROCEDURE FoldTBitsize (tokenno: CARDINAL; p: WalkAction;
4803 quad: CARDINAL;
4804 res, type: CARDINAL) ;
4805 VAR
4806 location: location_t ;
4807 BEGIN
4808 location := TokenToLocation(tokenno) ;
4809 TryDeclareType (type) ;
4810 type := GetDType (type) ;
4811 IF CompletelyResolved (type)
4812 THEN
4813 AddModGcc (res, BuildSystemTBitSize (location, Mod2Gcc (type))) ;
4814 p (res) ;
4815 NoChange := FALSE ;
4816 SubQuad (quad)
4817 END
4818 END FoldTBitsize ;
4819
4820
4821 (*
4822 FoldStandardFunction - attempts to fold a standard function.
4823 *)
4824
4825 PROCEDURE FoldStandardFunction (tokenno: CARDINAL; p: WalkAction;
4826 quad: CARDINAL;
4827 op1, op2, op3: CARDINAL) ;
4828 VAR
4829 s : String ;
4830 type,
4831 d,
4832 result : CARDINAL ;
4833 location: location_t ;
4834 BEGIN
4835 location := TokenToLocation(tokenno) ;
4836 IF GetSymName(op2)=MakeKey('Length')
4837 THEN
4838 TryDeclareConstant(tokenno, op3) ;
4839 IF IsConst(op3) AND GccKnowsAbout(op3)
4840 THEN
4841 (* fine, we can take advantage of this and fold constants *)
4842 IF IsConst(op1)
4843 THEN
4844 IF IsConstString(op3)
4845 THEN
4846 AddModGcc(op1, FindSize(tokenno, op3)) ;
4847 p(op1) ;
4848 NoChange := FALSE ;
4849 SubQuad(quad)
4850 ELSE
4851 MetaErrorT1 (tokenno, 'parameter to LENGTH must be a string {%1Ead}', op3)
4852 END
4853 ELSE
4854 (* rewrite the quad to use becomes. *)
4855 d := GetStringLength (tokenno, op3) ;
4856 s := Sprintf1 (Mark (InitString ("%d")), d) ;
4857 result := MakeConstLit (tokenno, makekey (string (s)), Cardinal) ;
4858 s := KillString (s) ;
4859 TryDeclareConstant (tokenno, result) ;
4860 PutQuad (quad, BecomesOp, op1, NulSym, result)
4861 END
4862 END
4863 ELSIF GetSymName(op2)=MakeKey('CAP')
4864 THEN
4865 TryDeclareConstant(tokenno, op3) ;
4866 IF IsConst(op3) AND GccKnowsAbout(op3)
4867 THEN
4868 (* fine, we can take advantage of this and fold constants *)
4869 IF IsConst(op1)
4870 THEN
4871 IF (IsConstString(op3) AND (GetStringLength (tokenno, op3) = 1)) OR
4872 (GetType(op3)=Char)
4873 THEN
4874 AddModGcc(op1, BuildCap(location, Mod2Gcc(op3))) ;
4875 p(op1) ;
4876 NoChange := FALSE ;
4877 SubQuad(quad)
4878 ELSE
4879 MetaErrorT1 (tokenno, 'parameter to CAP must be a single character {%1Ead}', op3)
4880 END
4881 END
4882 END
4883 ELSIF GetSymName(op2)=MakeKey('ABS')
4884 THEN
4885 TryDeclareConstant(tokenno, op3) ;
4886 IF IsConst(op3) AND GccKnowsAbout(op3)
4887 THEN
4888 (* fine, we can take advantage of this and fold constants *)
4889 IF IsConst(op1)
4890 THEN
4891 AddModGcc(op1, BuildAbs(location, Mod2Gcc(op3))) ;
4892 p(op1) ;
4893 NoChange := FALSE ;
4894 SubQuad(quad)
4895 END
4896 END
4897 ELSIF op2=Im
4898 THEN
4899 TryDeclareConstant(tokenno, op3) ;
4900 IF IsConst(op3) AND GccKnowsAbout(op3)
4901 THEN
4902 (* fine, we can take advantage of this and fold constants *)
4903 IF IsConst(op1)
4904 THEN
4905 AddModGcc(op1, BuildIm(Mod2Gcc(op3))) ;
4906 p(op1) ;
4907 NoChange := FALSE ;
4908 SubQuad(quad)
4909 END
4910 END
4911 ELSIF op2=Re
4912 THEN
4913 TryDeclareConstant(tokenno, op3) ;
4914 IF IsConst(op3) AND GccKnowsAbout(op3)
4915 THEN
4916 (* fine, we can take advantage of this and fold constants *)
4917 IF IsConst(op1)
4918 THEN
4919 AddModGcc(op1, BuildRe(Mod2Gcc(op3))) ;
4920 p(op1) ;
4921 NoChange := FALSE ;
4922 SubQuad(quad)
4923 END
4924 END
4925 ELSIF op2=Cmplx
4926 THEN
4927 TryDeclareConstant(tokenno, GetNth(op3, 1)) ;
4928 TryDeclareConstant(tokenno, GetNth(op3, 2)) ;
4929 IF IsConst(GetNth(op3, 1)) AND GccKnowsAbout(GetNth(op3, 1)) AND
4930 IsConst(GetNth(op3, 2)) AND GccKnowsAbout(GetNth(op3, 2))
4931 THEN
4932 (* fine, we can take advantage of this and fold constants *)
4933 IF IsConst(op1)
4934 THEN
4935 type := GetCmplxReturnType(GetType(GetNth(op3, 1)), GetType(GetNth(op3, 2))) ;
4936 IF type=NulSym
4937 THEN
4938 MetaErrorT2 (tokenno, 'real {%1Eatd} and imaginary {%2atd} types are incompatible',
4939 GetNth(op3, 1), GetNth(op3, 2))
4940 ELSE
4941 AddModGcc(op1, BuildCmplx(location,
4942 Mod2Gcc(type),
4943 Mod2Gcc(GetNth(op3, 1)),
4944 Mod2Gcc(GetNth(op3, 2)))) ;
4945 p(op1) ;
4946 NoChange := FALSE ;
4947 SubQuad(quad)
4948 END
4949 END
4950 END
4951 ELSIF op2=TBitSize
4952 THEN
4953 FoldTBitsize (tokenno, p, quad, op1, op3)
4954 ELSE
4955 InternalError ('only expecting LENGTH, CAP, ABS, IM, RE')
4956 END
4957 END FoldStandardFunction ;
4958
4959
4960 (*
4961 CodeStandardFunction -
4962 *)
4963
4964 PROCEDURE CodeStandardFunction (quad: CARDINAL; result, function, param: CARDINAL) ;
4965 VAR
4966 type : CARDINAL ;
4967 location: location_t ;
4968 BEGIN
4969 DeclareConstant (CurrentQuadToken, param) ;
4970 DeclareConstructor (CurrentQuadToken, quad, param) ;
4971 location := TokenToLocation (CurrentQuadToken) ;
4972
4973 IF (function # NulSym) AND (GetSymName (function) = MakeKey ('Length'))
4974 THEN
4975 IF IsConst (result)
4976 THEN
4977 InternalError ('LENGTH function should already have been folded')
4978 END
4979 ELSIF (function # NulSym) AND (GetSymName (function) = MakeKey ('CAP'))
4980 THEN
4981 IF IsConst (result)
4982 THEN
4983 InternalError ('CAP function should already have been folded')
4984 ELSE
4985 BuildAssignmentStatement (location, Mod2Gcc (result), BuildCap (location, Mod2Gcc (param)))
4986 END
4987 ELSIF (function # NulSym) AND (GetSymName (function) = MakeKey('ABS'))
4988 THEN
4989 IF IsConst (result)
4990 THEN
4991 InternalError ('ABS function should already have been folded')
4992 ELSE
4993 BuildAssignmentStatement (location, Mod2Gcc (result), BuildAbs (location, Mod2Gcc (param)))
4994 END
4995 ELSIF function = Im
4996 THEN
4997 IF IsConst (result)
4998 THEN
4999 InternalError ('IM function should already have been folded')
5000 ELSE
5001 BuildAssignmentStatement (location, Mod2Gcc (result), BuildIm (Mod2Gcc (param)))
5002 END
5003 ELSIF function = Re
5004 THEN
5005 IF IsConst (result)
5006 THEN
5007 InternalError ('RE function should already have been folded')
5008 ELSE
5009 BuildAssignmentStatement (location, Mod2Gcc (result), BuildRe (Mod2Gcc (param)))
5010 END
5011 ELSIF function = Cmplx
5012 THEN
5013 IF IsConst (result)
5014 THEN
5015 InternalError ('CMPLX function should already have been folded')
5016 ELSE
5017 type := GetCmplxReturnType (GetType (GetNth (param, 1)), GetType (GetNth (param, 2))) ;
5018 IF type = NulSym
5019 THEN
5020 MetaErrorT2 (CurrentQuadToken,
5021 'real {%1Eatd} and imaginary {%2atd} types are incompatible',
5022 GetNth (param, 1), GetNth (param, 2))
5023 ELSE
5024 BuildAssignmentStatement (location, Mod2Gcc (result), BuildCmplx(location,
5025 Mod2Gcc (type),
5026 Mod2Gcc (GetNth (param, 1)),
5027 Mod2Gcc (GetNth (param, 2))))
5028 END
5029 END
5030 ELSIF function = TBitSize
5031 THEN
5032 IF IsConst (result)
5033 THEN
5034 InternalError ('TBITSIZE function should already have been folded')
5035 ELSE
5036 BuildAssignmentStatement (location, Mod2Gcc (result), BuildTBitSize (location, Mod2Gcc (param)))
5037 END
5038 ELSE
5039 InternalError ('expecting LENGTH, CAP, ABS, IM')
5040 END
5041 END CodeStandardFunction ;
5042
5043
5044 (*
5045 CodeSavePriority - checks to see whether op2 is reachable and is directly accessible
5046 externally. If so then it saves the current interrupt priority
5047 in op1 and sets the current priority to that determined by
5048 appropriate module.
5049
5050 op1 := op3(GetModuleScope(op2))
5051 *)
5052
5053 PROCEDURE CodeSavePriority (oldValue, scopeSym, procedureSym: CARDINAL) ;
5054 VAR
5055 funcTree: tree ;
5056 mod : CARDINAL ;
5057 n : Name ;
5058 location: location_t ;
5059 BEGIN
5060 location := TokenToLocation(CurrentQuadToken) ;
5061
5062 IF IsModule (scopeSym) OR IsDefImp (scopeSym) OR
5063 (IsProcedure (scopeSym) AND GetNeedSavePriority (scopeSym))
5064 THEN
5065 IF IsProcedure (scopeSym)
5066 THEN
5067 mod := GetModuleScope (scopeSym) ;
5068 ELSE
5069 Assert (IsModule(scopeSym) OR IsDefImp (scopeSym)) ;
5070 mod := scopeSym
5071 END ;
5072 IF GetPriority (mod) # NulSym
5073 THEN
5074 IF PriorityDebugging
5075 THEN
5076 n := GetSymName (scopeSym) ;
5077 printf1 ('procedure <%a> needs to save interrupts\n', n)
5078 END ;
5079 DeclareConstant (CurrentQuadToken, GetPriority (mod)) ;
5080 BuildParam (location, Mod2Gcc (GetPriority (mod))) ;
5081 funcTree := BuildProcedureCallTree (location, Mod2Gcc (procedureSym), Mod2Gcc (GetType (procedureSym))) ;
5082 funcTree := BuildFunctValue (location, Mod2Gcc (oldValue)) ;
5083 AddStatement (location, funcTree)
5084 END
5085 END
5086 END CodeSavePriority ;
5087
5088
5089 (*
5090 CodeRestorePriority - checks to see whether op2 is reachable and is directly accessible
5091 externally. If so then it restores the previous interrupt priority
5092 held in op1.
5093
5094 op1 := op3(op1)
5095 *)
5096
5097 PROCEDURE CodeRestorePriority (oldValue, scopeSym, procedureSym: CARDINAL) ;
5098 VAR
5099 funcTree: tree ;
5100 mod : CARDINAL ;
5101 n : Name ;
5102 location: location_t ;
5103 BEGIN
5104 location := TokenToLocation (CurrentQuadToken) ;
5105
5106 IF IsModule (scopeSym) OR IsDefImp (scopeSym) OR
5107 (IsProcedure (scopeSym) AND GetNeedSavePriority (scopeSym))
5108 THEN
5109 IF IsProcedure (scopeSym)
5110 THEN
5111 mod := GetModuleScope (scopeSym) ;
5112 ELSE
5113 Assert (IsModule (scopeSym) OR IsDefImp (scopeSym)) ;
5114 mod := scopeSym
5115 END ;
5116 IF GetPriority (mod) # NulSym
5117 THEN
5118 IF PriorityDebugging
5119 THEN
5120 n := GetSymName (scopeSym) ;
5121 printf1 ('procedure <%a> needs to restore interrupts\n', n)
5122 END ;
5123 BuildParam (location, Mod2Gcc (oldValue)) ;
5124 funcTree := BuildProcedureCallTree (location, Mod2Gcc (procedureSym), Mod2Gcc (GetType (procedureSym))) ;
5125 funcTree := BuildFunctValue (location, Mod2Gcc (oldValue)) ;
5126 AddStatement(location, funcTree)
5127 END
5128 END
5129 END CodeRestorePriority ;
5130
5131
5132 (*
5133 FoldBinarySet - attempts to fold set arithmetic it removes the quad if successful.
5134 *)
5135
5136 PROCEDURE FoldBinarySet (tokenno: CARDINAL; p: WalkAction; op: DoProcedure;
5137 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5138 VAR
5139 location: location_t ;
5140 BEGIN
5141 (* firstly try and ensure that constants are declared *)
5142 TryDeclareConstant(tokenno, op2) ;
5143 TryDeclareConstant(tokenno, op3) ;
5144 location := TokenToLocation(tokenno) ;
5145
5146 IF GccKnowsAbout(op2) AND GccKnowsAbout(op3)
5147 THEN
5148 IF CheckBinaryExpressionTypes (quad, p)
5149 THEN
5150 IF IsConst(op2) AND IsConstSet(op2) AND
5151 IsConst(op3) AND IsConstSet(op3) AND
5152 IsConst(op1)
5153 THEN
5154 IF IsValueSolved(op2) AND IsValueSolved(op3)
5155 THEN
5156 Assert(MixTypes(FindType(op3), FindType(op2), tokenno)#NulSym) ;
5157 PutConst(op1, MixTypes(FindType(op3), FindType(op2), tokenno)) ;
5158 PushValue(op2) ;
5159 PushValue(op3) ;
5160 op(tokenno) ;
5161 PopValue(op1) ;
5162 PushValue(op1) ;
5163 PutConstSet(op1) ;
5164 AddModGcc(op1,
5165 DeclareKnownConstant(location,
5166 Mod2Gcc(GetType(op3)),
5167 PopSetTree(tokenno))) ;
5168 p(op1) ;
5169 NoChange := FALSE ;
5170 SubQuad(quad)
5171 END
5172 END
5173 END
5174 END
5175 END FoldBinarySet ;
5176
5177
5178 (*
5179 FoldSetOr - check whether we can fold a set arithmetic or.
5180 *)
5181
5182 PROCEDURE FoldSetOr (tokenno: CARDINAL; p: WalkAction;
5183 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5184 BEGIN
5185 FoldBinarySet (tokenno, p, SetOr, quad, op1, op2, op3)
5186 END FoldSetOr ;
5187
5188
5189 (*
5190 CodeSetOr - encode set arithmetic or.
5191 *)
5192
5193 PROCEDURE CodeSetOr (quad: CARDINAL) ;
5194 BEGIN
5195 CodeBinarySet (BuildLogicalOr, SetOr, quad)
5196 END CodeSetOr ;
5197
5198
5199 (*
5200 FoldSetAnd - check whether we can fold a logical and.
5201 *)
5202
5203 PROCEDURE FoldSetAnd (tokenno: CARDINAL; p: WalkAction;
5204 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5205 BEGIN
5206 FoldBinarySet(tokenno, p, SetAnd, quad, op1, op2, op3)
5207 END FoldSetAnd ;
5208
5209
5210 (*
5211 CodeSetAnd - encode set arithmetic and.
5212 *)
5213
5214 PROCEDURE CodeSetAnd (quad: CARDINAL) ;
5215 BEGIN
5216 CodeBinarySet (BuildLogicalAnd, SetAnd, quad)
5217 END CodeSetAnd ;
5218
5219
5220 (*
5221 CodeBinarySetShift - encode a binary set arithmetic operation.
5222 The set maybe larger than a machine word
5223 and the value of one word may effect the
5224 values of another - ie shift and rotate.
5225 Set sizes of a word or less are evaluated
5226 with binop, whereas multiword sets are
5227 evaluated by M2RTS.
5228 *)
5229
5230 PROCEDURE CodeBinarySetShift (binop: BuildSetProcedure;
5231 doOp : DoProcedure;
5232 var, left, right: Name;
5233 quad: CARDINAL;
5234 op1, op2, op3: CARDINAL) ;
5235 VAR
5236 nBits,
5237 unbounded,
5238 leftproc,
5239 rightproc,
5240 varproc : tree ;
5241 location : location_t ;
5242 BEGIN
5243 (* firstly ensure that constant literals are declared *)
5244 DeclareConstant(CurrentQuadToken, op3) ;
5245 DeclareConstant(CurrentQuadToken, op2) ;
5246 DeclareConstructor(CurrentQuadToken, quad, op3) ;
5247 DeclareConstructor(CurrentQuadToken, quad, op2) ;
5248 location := TokenToLocation(CurrentQuadToken) ;
5249
5250 IF IsConst(op1)
5251 THEN
5252 IF IsValueSolved(op2) AND IsValueSolved(op3)
5253 THEN
5254 Assert(MixTypes(FindType(op3),
5255 FindType(op2), CurrentQuadToken)#NulSym) ;
5256 PutConst(op1, FindType(op3)) ;
5257 PushValue(op2) ;
5258 PushValue(op3) ;
5259 doOp(CurrentQuadToken) ;
5260 PopValue(op1) ;
5261 PutConstSet(op1)
5262 ELSE
5263 MetaErrorT0 (CurrentQuadToken, '{%E}constant expression cannot be evaluated')
5264 END
5265 ELSE
5266 varproc := Mod2Gcc(FromModuleGetSym(CurrentQuadToken, var, System)) ;
5267 leftproc := Mod2Gcc(FromModuleGetSym(CurrentQuadToken, left, System)) ;
5268 rightproc := Mod2Gcc(FromModuleGetSym(CurrentQuadToken, right, System)) ;
5269 unbounded := Mod2Gcc(GetType(GetNthParamAny (FromModuleGetSym(CurrentQuadToken,
5270 var, System), 1))) ;
5271 PushValue(GetTypeMax(SkipType(GetType(op1)))) ;
5272 PushIntegerTree(BuildConvert(location, GetM2ZType(), PopIntegerTree(), FALSE)) ;
5273
5274 PushValue(GetTypeMin(SkipType(GetType(op1)))) ;
5275 PushIntegerTree(BuildConvert(location, GetM2ZType(), PopIntegerTree(), FALSE)) ;
5276 Sub ;
5277 PushCard(1) ;
5278 PushIntegerTree(BuildConvert(location, GetM2ZType(), PopIntegerTree(), FALSE)) ;
5279 Addn ;
5280 nBits := PopIntegerTree() ;
5281 BuildBinarySetDo(location,
5282 Mod2Gcc(SkipType(GetType(op1))),
5283 Mod2Gcc(op1),
5284 Mod2Gcc(op2),
5285 Mod2Gcc(op3),
5286 binop,
5287 GetMode(op1)=LeftValue,
5288 GetMode(op2)=LeftValue,
5289 GetMode(op3)=LeftValue,
5290 nBits,
5291 unbounded,
5292 varproc, leftproc, rightproc)
5293 END
5294 END CodeBinarySetShift ;
5295
5296
5297 (*
5298 FoldSetShift - check whether we can fold a logical shift.
5299 *)
5300
5301 PROCEDURE FoldSetShift (tokenno: CARDINAL; p: WalkAction;
5302 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5303 BEGIN
5304 FoldBinarySet(tokenno, p, SetShift, quad, op1, op2, op3)
5305 END FoldSetShift ;
5306
5307
5308 (*
5309 CodeSetShift - encode set arithmetic shift.
5310 *)
5311
5312 PROCEDURE CodeSetShift (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5313 BEGIN
5314 CodeBinarySetShift (BuildLogicalShift,
5315 SetShift,
5316 MakeKey('ShiftVal'),
5317 MakeKey('ShiftLeft'),
5318 MakeKey('ShiftRight'),
5319 quad, op1, op2, op3)
5320 END CodeSetShift ;
5321
5322
5323 (*
5324 FoldSetRotate - check whether we can fold a logical rotate.
5325 *)
5326
5327 PROCEDURE FoldSetRotate (tokenno: CARDINAL; p: WalkAction;
5328 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5329 BEGIN
5330 FoldBinarySet(tokenno, p, SetRotate, quad, op1, op2, op3)
5331 END FoldSetRotate ;
5332
5333
5334 (*
5335 CodeSetRotate - encode set arithmetic rotate.
5336 *)
5337
5338 PROCEDURE CodeSetRotate (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5339 BEGIN
5340 CodeBinarySetShift (BuildLogicalRotate,
5341 SetRotate,
5342 MakeKey ('RotateVal'),
5343 MakeKey ('RotateLeft'),
5344 MakeKey ('RotateRight'),
5345 quad, op1, op2, op3)
5346 END CodeSetRotate ;
5347
5348
5349 (*
5350 FoldSetLogicalDifference - check whether we can fold a logical difference.
5351 *)
5352
5353 (*
5354 PROCEDURE FoldSetLogicalDifference (tokenno: CARDINAL; p: WalkAction;
5355 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5356 BEGIN
5357 FoldBinarySet(tokenno, p, SetDifference, quad, op1, op2, op3)
5358 END FoldSetLogicalDifference ;
5359 *)
5360
5361
5362 (*
5363 CodeSetLogicalDifference - encode set arithmetic logical difference.
5364 *)
5365
5366 PROCEDURE CodeSetLogicalDifference (quad: CARDINAL) ;
5367 BEGIN
5368 CodeBinarySet (BuildLogicalDifference, SetDifference, quad)
5369 END CodeSetLogicalDifference ;
5370
5371
5372 (*
5373 FoldSymmetricDifference - check whether we can fold a logical difference.
5374 *)
5375
5376 PROCEDURE FoldSymmetricDifference (tokenno: CARDINAL; p: WalkAction;
5377 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
5378 BEGIN
5379 FoldBinarySet (tokenno, p, SetSymmetricDifference, quad, op1, op2, op3)
5380 END FoldSymmetricDifference ;
5381
5382
5383 (*
5384 CodeSetSymmetricDifference - code set difference.
5385 *)
5386
5387 PROCEDURE CodeSetSymmetricDifference (quad: CARDINAL) ;
5388 BEGIN
5389 CodeBinarySet (BuildSymmetricDifference, SetSymmetricDifference, quad)
5390 END CodeSetSymmetricDifference ;
5391
5392
5393 (*
5394 CodeUnarySet - encode a unary set arithmetic operation.
5395 Set operands may be longer than a word.
5396 *)
5397
5398 PROCEDURE CodeUnarySet (unop: BuildUnarySetFunction; constop: DoUnaryProcedure;
5399 quad: CARDINAL; result, expr: CARDINAL) ;
5400 VAR
5401 location: location_t ;
5402 BEGIN
5403 (* firstly ensure that constant literals are declared *)
5404 DeclareConstant (CurrentQuadToken, expr) ;
5405 DeclareConstructor (CurrentQuadToken, quad, expr) ;
5406 location := TokenToLocation (CurrentQuadToken) ;
5407
5408 IF IsConst (result)
5409 THEN
5410 IF IsValueSolved (expr)
5411 THEN
5412 Assert (FindType (expr) # NulSym) ;
5413 PutConst (result, FindType (expr)) ;
5414 PushValue (expr) ;
5415 constop (CurrentQuadToken) ;
5416 PopValue (result) ;
5417 PushValue (result) ;
5418 PutConstSet (result) ;
5419 ConstantKnownAndUsed (result,
5420 DeclareKnownConstant(location,
5421 Mod2Gcc (GetType (expr)),
5422 PopSetTree (CurrentQuadToken)))
5423 ELSE
5424 MetaErrorT0 (CurrentQuadToken,
5425 '{%E}constant expression cannot be evaluated')
5426 END
5427 ELSE
5428 checkDeclare (result) ;
5429 BuildUnaryForeachWordDo (location,
5430 Mod2Gcc (GetType (result)), Mod2Gcc (result), Mod2Gcc (expr), unop,
5431 GetMode(result) = LeftValue, GetMode(expr) = LeftValue,
5432 IsConst (result), IsConst (expr))
5433 END
5434 END CodeUnarySet ;
5435
5436
5437 (*
5438 FoldIncl - check whether we can fold the InclOp.
5439 result := result + (1 << expr)
5440 *)
5441
5442 PROCEDURE FoldIncl (tokenno: CARDINAL; p: WalkAction;
5443 quad: CARDINAL; result, expr: CARDINAL) ;
5444 BEGIN
5445 (* firstly ensure that constant literals are declared *)
5446 TryDeclareConstant (tokenno, expr) ;
5447 IF IsConst (result) AND IsConst (expr)
5448 THEN
5449 IF GccKnowsAbout (expr) AND IsValueSolved (result)
5450 THEN
5451 (* fine, we can take advantage of this and fold constants *)
5452 PushValue (result) ;
5453 AddBit (tokenno, expr) ;
5454 AddModGcc (result, PopSetTree(tokenno)) ;
5455 p (result) ;
5456 NoChange := FALSE ;
5457 SubQuad (quad)
5458 END
5459 END
5460 END FoldIncl ;
5461
5462
5463 (*
5464 FoldIfLess - check to see if it is possible to evaluate
5465 if op1 < op2 then goto op3.
5466 *)
5467
5468 PROCEDURE FoldIfLess (tokenno: CARDINAL;
5469 quad: CARDINAL; left, right, destQuad: CARDINAL) ;
5470 BEGIN
5471 (* Firstly ensure that constant literals are declared. *)
5472 TryDeclareConstant(tokenno, left) ;
5473 TryDeclareConstant(tokenno, right) ;
5474 IF IsConst (left) AND IsConst (right)
5475 THEN
5476 IF IsValueSolved (left) AND IsValueSolved (right)
5477 THEN
5478 (* We can take advantage of the known values and evaluate the condition. *)
5479 PushValue (left) ;
5480 PushValue (right) ;
5481 IF Less (tokenno)
5482 THEN
5483 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5484 ELSE
5485 SubQuad (quad)
5486 END ;
5487 NoChange := FALSE
5488 END
5489 END
5490 END FoldIfLess ;
5491
5492
5493 (*
5494 FoldIfGre - check to see if it is possible to evaluate
5495 if op1 > op2 then goto op3.
5496 *)
5497
5498 PROCEDURE FoldIfGre (tokenno: CARDINAL;
5499 quad: CARDINAL;
5500 left, right, destQuad: CARDINAL) ;
5501 BEGIN
5502 (* Firstly ensure that constant literals are declared. *)
5503 TryDeclareConstant(tokenno, left) ;
5504 TryDeclareConstant(tokenno, right) ;
5505 IF IsConst (left) AND IsConst (right)
5506 THEN
5507 IF IsValueSolved (left) AND IsValueSolved (right)
5508 THEN
5509 (* We can take advantage of the known values and evaluate the condition. *)
5510 PushValue (left) ;
5511 PushValue (right) ;
5512 IF Gre (tokenno)
5513 THEN
5514 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5515 ELSE
5516 SubQuad (quad)
5517 END ;
5518 NoChange := FALSE
5519 END
5520 END
5521 END FoldIfGre ;
5522
5523
5524 (*
5525 FoldIfLessEqu - check to see if it is possible to evaluate
5526 if op1 <= op2 then goto op3.
5527 *)
5528
5529 PROCEDURE FoldIfLessEqu (tokenno: CARDINAL;
5530 quad: CARDINAL;
5531 left, right, destQuad: CARDINAL) ;
5532 BEGIN
5533 (* Firstly ensure that constant literals are declared. *)
5534 TryDeclareConstant(tokenno, left) ;
5535 TryDeclareConstant(tokenno, right) ;
5536 IF IsConst (left) AND IsConst (right)
5537 THEN
5538 IF IsValueSolved (left) AND IsValueSolved (right)
5539 THEN
5540 (* We can take advantage of the known values and evaluate the condition. *)
5541 PushValue (left) ;
5542 PushValue (right) ;
5543 IF LessEqu (tokenno)
5544 THEN
5545 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5546 ELSE
5547 SubQuad (quad)
5548 END ;
5549 NoChange := FALSE
5550 END
5551 END
5552 END FoldIfLessEqu ;
5553
5554
5555 (*
5556 FoldIfGreEqu - check to see if it is possible to evaluate
5557 if op1 >= op2 then goto op3.
5558 *)
5559
5560 PROCEDURE FoldIfGreEqu (tokenno: CARDINAL;
5561 quad: CARDINAL;
5562 left, right, destQuad: CARDINAL) ;
5563 BEGIN
5564 (* Firstly ensure that constant literals are declared. *)
5565 TryDeclareConstant(tokenno, left) ;
5566 TryDeclareConstant(tokenno, right) ;
5567 IF IsConst (left) AND IsConst (right)
5568 THEN
5569 IF IsValueSolved (left) AND IsValueSolved (right)
5570 THEN
5571 (* We can take advantage of the known values and evaluate the condition. *)
5572 PushValue (left) ;
5573 PushValue (right) ;
5574 IF GreEqu (tokenno)
5575 THEN
5576 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5577 ELSE
5578 SubQuad (quad)
5579 END ;
5580 NoChange := FALSE
5581 END
5582 END
5583 END FoldIfGreEqu ;
5584
5585
5586 (*
5587 FoldIfIn - check whether we can fold the IfInOp
5588 if op1 in op2 then goto op3
5589 *)
5590
5591 PROCEDURE FoldIfIn (tokenno: CARDINAL;
5592 quad: CARDINAL;
5593 left, right, destQuad: CARDINAL) ;
5594 BEGIN
5595 (* Firstly ensure that constant literals are declared. *)
5596 TryDeclareConstant (tokenno, left) ;
5597 TryDeclareConstant (tokenno, right) ;
5598 IF IsConst (left) AND IsConst (right)
5599 THEN
5600 IF IsValueSolved (left) AND IsValueSolved (right)
5601 THEN
5602 IF CheckBinaryExpressionTypes (quad, NoWalkProcedure)
5603 THEN
5604 (* We can take advantage of the known values and evaluate the condition. *)
5605 PushValue (right) ;
5606 IF SetIn (tokenno, left)
5607 THEN
5608 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad) ;
5609 ELSE
5610 SubQuad (quad)
5611 END
5612 ELSE
5613 SubQuad (quad)
5614 END ;
5615 NoChange := FALSE
5616 END
5617 END
5618 END FoldIfIn ;
5619
5620
5621 (*
5622 FoldIfNotIn - check whether we can fold the IfNotInOp
5623 if not (op1 in op2) then goto op3
5624 *)
5625
5626 PROCEDURE FoldIfNotIn (tokenno: CARDINAL;
5627 quad: CARDINAL;
5628 left, right, destQuad: CARDINAL) ;
5629 BEGIN
5630 (* Firstly ensure that constant literals are declared. *)
5631 TryDeclareConstant (tokenno, left) ;
5632 TryDeclareConstant (tokenno, right) ;
5633 IF IsConst (left) AND IsConst (right)
5634 THEN
5635 IF IsValueSolved (left) AND IsValueSolved (right)
5636 THEN
5637 IF CheckBinaryExpressionTypes (quad, NoWalkProcedure)
5638 THEN
5639 (* We can take advantage of the known values and evaluate the
5640 condition. *)
5641 PushValue (right) ;
5642 IF NOT SetIn (tokenno, left)
5643 THEN
5644 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5645 ELSE
5646 SubQuad (quad)
5647 END
5648 ELSE
5649 SubQuad (quad)
5650 END ;
5651 NoChange := FALSE
5652 END
5653 END
5654 END FoldIfNotIn ;
5655
5656
5657 (*
5658 FoldIfEqu - check to see if it is possible to evaluate
5659 if op1 = op2 then goto op3.
5660 *)
5661
5662 PROCEDURE FoldIfEqu (tokenno: CARDINAL;
5663 quad: CARDINAL;
5664 left, right, destQuad: CARDINAL) ;
5665 BEGIN
5666 (* Firstly ensure that constant literals are declared. *)
5667 TryDeclareConstant(tokenno, left) ;
5668 TryDeclareConstant(tokenno, right) ;
5669 IF IsConst (left) AND IsConst (right)
5670 THEN
5671 IF IsValueSolved (left) AND IsValueSolved (right)
5672 THEN
5673 (* We can take advantage of the known values and evaluate the
5674 condition. *)
5675 PushValue (left) ;
5676 PushValue (right) ;
5677 IF Equ (tokenno)
5678 THEN
5679 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5680 ELSE
5681 SubQuad (quad)
5682 END ;
5683 NoChange := FALSE
5684 END
5685 END
5686 END FoldIfEqu ;
5687
5688
5689 (*
5690 FoldIfNotEqu - check to see if it is possible to evaluate
5691 if op1 # op2 then goto op3.
5692 *)
5693
5694 PROCEDURE FoldIfNotEqu (tokenno: CARDINAL;
5695 quad: CARDINAL;
5696 left, right, destQuad: CARDINAL) ;
5697 BEGIN
5698 (* Firstly ensure that constant literals are declared. *)
5699 TryDeclareConstant(tokenno, left) ;
5700 TryDeclareConstant(tokenno, right) ;
5701 IF IsConst (left) AND IsConst (right)
5702 THEN
5703 IF IsValueSolved (left) AND IsValueSolved (right)
5704 THEN
5705 (* We can take advantage of the known values and evaluate the
5706 condition. *)
5707 PushValue (left) ;
5708 PushValue (right) ;
5709 IF NotEqu (tokenno)
5710 THEN
5711 PutQuad (quad, GotoOp, NulSym, NulSym, destQuad)
5712 ELSE
5713 SubQuad (quad)
5714 END ;
5715 NoChange := FALSE
5716 END
5717 END
5718 END FoldIfNotEqu ;
5719
5720
5721 (*
5722 GetSetLimits - assigns low and high to the limits of the declared, set.
5723 *)
5724
5725 PROCEDURE GetSetLimits (set: CARDINAL; VAR low, high: CARDINAL) ;
5726 VAR
5727 type: CARDINAL ;
5728 BEGIN
5729 type := GetType(set) ;
5730 IF IsSubrange(type)
5731 THEN
5732 GetSubrange(type, high, low) ;
5733 ELSE
5734 low := GetTypeMin(type) ;
5735 high := GetTypeMax(type)
5736 END
5737 END GetSetLimits ;
5738
5739
5740 (*
5741 GetFieldNo - returns the field number in the, set, which contains, element.
5742 *)
5743
5744 PROCEDURE GetFieldNo (tokenno: CARDINAL; element: CARDINAL; set: CARDINAL; VAR offset: tree) : INTEGER ;
5745 VAR
5746 low, high, bpw, c: CARDINAL ;
5747 location : location_t ;
5748 BEGIN
5749 location := TokenToLocation(tokenno) ;
5750 bpw := GetBitsPerBitset() ;
5751 GetSetLimits(set, low, high) ;
5752
5753 (* check element is legal *)
5754
5755 PushValue(element) ;
5756 PushValue(low) ;
5757 IF Less(tokenno)
5758 THEN
5759 (* out of range *)
5760 RETURN( -1 )
5761 ELSE
5762 PushValue(element) ;
5763 PushValue(high) ;
5764 IF Gre(tokenno)
5765 THEN
5766 RETURN( -1 )
5767 END
5768 END ;
5769
5770 (* all legal *)
5771
5772 PushValue(low) ;
5773 offset := PopIntegerTree() ;
5774 c := 0 ;
5775 PushValue(element) ;
5776 PushValue(low) ;
5777 PushIntegerTree(ToCardinal(location, PopIntegerTree())) ;
5778 PushCard(bpw) ;
5779 PushIntegerTree(ToCardinal(location, PopIntegerTree())) ;
5780 Addn ;
5781 WHILE GreEqu(tokenno) DO
5782 INC(c) ; (* move onto next field *)
5783 PushValue(element) ;
5784 PushIntegerTree(ToCardinal(location, PopIntegerTree())) ;
5785 PushCard((c+1)*bpw) ;
5786 PushValue(low) ;
5787 PushIntegerTree(ToCardinal(location, PopIntegerTree())) ;
5788 Addn ;
5789 PushIntegerTree(offset) ;
5790 PushIntegerTree(ToCardinal(location, PopIntegerTree())) ;
5791 PushCard(bpw) ;
5792 PushIntegerTree(ToCardinal(location, PopIntegerTree())) ;
5793 Addn ;
5794 offset := PopIntegerTree()
5795 END ;
5796 RETURN( VAL(INTEGER, c) )
5797 END GetFieldNo ;
5798
5799
5800 (*
5801 CodeIncl - encode an InclOp:
5802 result := result + (1 << expr)
5803 *)
5804
5805 PROCEDURE CodeIncl (result, expr: CARDINAL) ;
5806 VAR
5807 low,
5808 high : CARDINAL ;
5809 offset : tree ;
5810 fieldno : INTEGER ;
5811 location: location_t ;
5812 BEGIN
5813 (* firstly ensure that constant literals are declared *)
5814 DeclareConstant (CurrentQuadToken, expr) ;
5815 location := TokenToLocation (CurrentQuadToken) ;
5816
5817 IF IsConst (result)
5818 THEN
5819 IF IsConst (expr)
5820 THEN
5821 InternalError ('this quadruple should have been removed by FoldIncl')
5822 ELSE
5823 InternalError ('should not get to here (why are we generating <incl const, var> ?)')
5824 END
5825 ELSE
5826 IF IsConst (expr)
5827 THEN
5828 fieldno := GetFieldNo (CurrentQuadToken, expr, GetType (result), offset) ;
5829 IF fieldno >= 0
5830 THEN
5831 PushValue (expr) ;
5832 PushIntegerTree (offset) ;
5833 Sub ;
5834 BuildIncludeVarConst (location,
5835 Mod2Gcc (GetType (result)),
5836 Mod2Gcc (result),
5837 PopIntegerTree (),
5838 GetMode (result) = LeftValue, fieldno)
5839 ELSE
5840 MetaErrorT1 (CurrentQuadToken, 'bit exceeded the range of set {%1Eatd}', result)
5841 END
5842 ELSE
5843 GetSetLimits (GetType (result), low, high) ;
5844 BuildIncludeVarVar (location,
5845 Mod2Gcc (GetType(result)),
5846 Mod2Gcc (result), Mod2Gcc(expr), GetMode(result) = LeftValue, Mod2Gcc (low))
5847 END
5848 END
5849 END CodeIncl ;
5850
5851
5852 (*
5853 FoldExcl - check whether we can fold the InclOp.
5854 op1 := op1 - (1 << op3)
5855 *)
5856
5857 PROCEDURE FoldExcl (tokenno: CARDINAL; p: WalkAction;
5858 quad: CARDINAL; result, expr: CARDINAL) ;
5859 BEGIN
5860 (* firstly ensure that constant literals are declared *)
5861 TryDeclareConstant (tokenno, expr) ;
5862 IF IsConst (result) AND IsConst (expr)
5863 THEN
5864 IF GccKnowsAbout (expr) AND IsValueSolved (result)
5865 THEN
5866 PushValue (result) ;
5867 SubBit (tokenno, expr) ;
5868 AddModGcc (result, PopSetTree (tokenno)) ;
5869 p (result) ;
5870 NoChange := FALSE ;
5871 SubQuad(quad)
5872 END
5873 END
5874 END FoldExcl ;
5875
5876
5877 (*
5878 CodeExcl - encode an ExclOp:
5879 result := result - (1 << expr)
5880 *)
5881
5882 PROCEDURE CodeExcl (result, expr: CARDINAL) ;
5883 VAR
5884 low,
5885 high : CARDINAL ;
5886 offset : tree ;
5887 fieldno : INTEGER ;
5888 location: location_t ;
5889 BEGIN
5890 (* firstly ensure that constant literals are declared *)
5891 DeclareConstant (CurrentQuadToken, expr) ;
5892 location := TokenToLocation(CurrentQuadToken) ;
5893
5894 IF IsConst (result)
5895 THEN
5896 InternalError ('should not get to here (if we do we should consider calling FoldInclOp)')
5897 ELSE
5898 IF IsConst (expr)
5899 THEN
5900 fieldno := GetFieldNo (CurrentQuadToken, expr, GetType (result), offset) ;
5901 IF fieldno >= 0
5902 THEN
5903 PushValue (expr) ;
5904 PushIntegerTree (offset) ;
5905 Sub ;
5906 BuildExcludeVarConst (location,
5907 Mod2Gcc (GetType (result)),
5908 Mod2Gcc (result), PopIntegerTree (),
5909 GetMode (result)=LeftValue, fieldno)
5910 ELSE
5911 MetaErrorT1 (CurrentQuadToken, 'bit exceeded the range of set {%1Eatd}', result)
5912 END
5913 ELSE
5914 GetSetLimits (GetType (result), low, high) ;
5915 BuildExcludeVarVar (location,
5916 Mod2Gcc (GetType(result)),
5917 Mod2Gcc (result), Mod2Gcc(expr), GetMode(result) = LeftValue, Mod2Gcc (low))
5918 END
5919 END
5920 END CodeExcl ;
5921
5922
5923 (*
5924 FoldUnary - check whether we can fold the unop operation.
5925 *)
5926
5927 PROCEDURE FoldUnary (tokenno: CARDINAL; p: WalkAction;
5928 unop: BuildUnaryProcedure; ZConstToTypedConst: tree;
5929 quad: CARDINAL; result, expr: CARDINAL) ;
5930 VAR
5931 tv : tree ;
5932 location: location_t ;
5933 BEGIN
5934 (* firstly ensure that any constant literal is declared *)
5935 TryDeclareConstant (tokenno, expr) ;
5936 location := TokenToLocation (tokenno) ;
5937
5938 IF IsConst (expr)
5939 THEN
5940 IF GccKnowsAbout (expr)
5941 THEN
5942 (* fine, we can take advantage of this and fold constants *)
5943 IF IsConst (result)
5944 THEN
5945 IF ZConstToTypedConst = tree(NIL)
5946 THEN
5947 IF (GetType (expr) = NulSym) OR IsOrdinalType (SkipType (GetType (expr)))
5948 THEN
5949 ZConstToTypedConst := GetM2ZType ()
5950 ELSIF IsRealType (SkipType (GetType (expr))) OR IsRealN (SkipType (GetType (expr)))
5951 THEN
5952 ZConstToTypedConst := GetM2RType ()
5953 ELSIF IsComplexType (SkipType (GetType (expr))) OR
5954 IsComplexN (SkipType (GetType (expr)))
5955 THEN
5956 ZConstToTypedConst := GetM2CType ()
5957 END
5958 END ;
5959 IF GetType(result) = NulSym
5960 THEN
5961 PutConst (result, NegateType (GetType (expr) (* , tokenno *) ))
5962 END ;
5963 tv := unop (location, LValueToGenericPtrOrConvert (expr, ZConstToTypedConst), FALSE) ;
5964 CheckOrResetOverflow (tokenno, tv, MustCheckOverflow (quad)) ;
5965
5966 AddModGcc (result, DeclareKnownConstant (location, ZConstToTypedConst, tv)) ;
5967 p (result) ;
5968 NoChange := FALSE ;
5969 SubQuad (quad)
5970 ELSE
5971 (* we can still fold the expression, but not the assignment, however, we will
5972 not do this here but in CodeUnary
5973 *)
5974 END
5975 END
5976 END
5977 END FoldUnary ;
5978
5979
5980 (*
5981 FoldUnarySet - check whether we can fold the doOp operation.
5982 *)
5983
5984 PROCEDURE FoldUnarySet (tokenno: CARDINAL; p: WalkAction; doOp: DoUnaryProcedure;
5985 quad: CARDINAL; result, expr: CARDINAL) ;
5986 VAR
5987 location: location_t ;
5988 BEGIN
5989 (* firstly try and ensure that constants are declared *)
5990 TryDeclareConstant (tokenno, expr) ;
5991 location := TokenToLocation (tokenno) ;
5992
5993 IF IsConst (expr) AND IsConstSet (expr) AND
5994 IsConst (result)
5995 THEN
5996 IF IsValueSolved (expr) AND (GetType (expr) # NulSym)
5997 THEN
5998 PutConst (result, FindType (expr)) ;
5999 PushValue (expr) ;
6000 doOp (tokenno) ;
6001 PopValue (result) ;
6002 PushValue (result) ;
6003 PutConstSet (result) ;
6004 AddModGcc (result,
6005 DeclareKnownConstant (location,
6006 Mod2Gcc (GetType (expr)),
6007 PopSetTree (tokenno))) ;
6008 p (result) ;
6009 NoChange := FALSE ;
6010 SubQuad (quad)
6011 END
6012 END
6013 END FoldUnarySet ;
6014
6015
6016 (*
6017 CodeUnaryCheck - encode a unary arithmetic operation.
6018 *)
6019
6020 PROCEDURE CodeUnaryCheck (unop: BuildUnaryCheckProcedure; ZConstToTypedConst: tree;
6021 quad: CARDINAL; result, expr: CARDINAL) ;
6022 VAR
6023 lowestType: CARDINAL ;
6024 min, max,
6025 lowest,
6026 tv : tree ;
6027 location : location_t ;
6028 BEGIN
6029 (* firstly ensure that any constant literal is declared *)
6030 DeclareConstant(CurrentQuadToken, expr) ;
6031 DeclareConstructor(CurrentQuadToken, quad, expr) ;
6032 location := TokenToLocation(CurrentQuadToken) ;
6033
6034 lowestType := GetLType (result) ;
6035 IF lowestType=NulSym
6036 THEN
6037 lowest := NIL ;
6038 ELSE
6039 lowest := Mod2Gcc (lowestType)
6040 END ;
6041 IF GetMinMax (CurrentQuadToken, lowestType, min, max)
6042 THEN
6043 tv := unop (location, LValueToGenericPtr (location, expr), lowest, min, max)
6044 ELSE
6045 tv := unop (location, LValueToGenericPtr (location, expr), NIL, NIL, NIL)
6046 END ;
6047 CheckOrResetOverflow (CurrentQuadToken, tv, MustCheckOverflow(quad)) ;
6048 IF IsConst (result)
6049 THEN
6050 IF ZConstToTypedConst = tree (NIL)
6051 THEN
6052 ZConstToTypedConst := tree (Mod2Gcc( GetType (expr)))
6053 END ;
6054 (* still have a constant which was not resolved, pass it to gcc *)
6055 PutConst (result, FindType (expr)) ;
6056 ConstantKnownAndUsed (result, DeclareKnownConstant (location, ZConstToTypedConst, tv))
6057 ELSE
6058 IF EnableSSA AND IsVariableSSA (result)
6059 THEN
6060 Replace (result, tv)
6061 ELSE
6062 BuildAssignmentStatement (location, Mod2Gcc (result), tv)
6063 END
6064 END
6065 END CodeUnaryCheck ;
6066
6067
6068 (*
6069 CodeUnary - encode a unary arithmetic operation.
6070 *)
6071
6072 PROCEDURE CodeUnary (unop: BuildUnaryProcedure; ZConstToTypedConst: tree;
6073 quad: CARDINAL; result, expr: CARDINAL) ;
6074 VAR
6075 tv : tree ;
6076 location: location_t ;
6077 BEGIN
6078 (* firstly ensure that any constant literal is declared *)
6079 DeclareConstant (CurrentQuadToken, expr) ;
6080 DeclareConstructor (CurrentQuadToken, quad, expr) ;
6081 location := TokenToLocation (CurrentQuadToken) ;
6082
6083 tv := unop(location, LValueToGenericPtr (location, expr), FALSE) ;
6084 CheckOrResetOverflow (CurrentQuadToken, tv, MustCheckOverflow (quad)) ;
6085 IF IsConst(result)
6086 THEN
6087 IF ZConstToTypedConst=tree(NIL)
6088 THEN
6089 ZConstToTypedConst := tree(Mod2Gcc(GetType(expr)))
6090 END ;
6091 (* still have a constant which was not resolved, pass it to gcc *)
6092 PutConst (result, FindType (expr)) ;
6093 ConstantKnownAndUsed (result, DeclareKnownConstant (location, ZConstToTypedConst, tv))
6094 ELSE
6095 IF EnableSSA AND IsVariableSSA (result)
6096 THEN
6097 Replace (result, tv)
6098 ELSE
6099 BuildAssignmentStatement (location, Mod2Gcc (result), tv)
6100 END
6101 END
6102 END CodeUnary ;
6103
6104
6105 (*
6106 FoldNegate - check unary negate for constant folding.
6107 *)
6108
6109 PROCEDURE FoldNegate (tokenno: CARDINAL; p: WalkAction;
6110 quad: CARDINAL; result, expr: CARDINAL) ;
6111 BEGIN
6112 IF IsConstSet (expr)
6113 THEN
6114 FoldUnarySet (tokenno, p, SetNegate, quad, result, expr)
6115 ELSE
6116 FoldUnary (tokenno, p, BuildNegate, NIL, quad, result, expr)
6117 END
6118 END FoldNegate ;
6119
6120
6121 (*
6122 CodeNegateChecked - code a negate instruction, determine whether checking
6123 is required.
6124 *)
6125
6126 PROCEDURE CodeNegateChecked (quad: CARDINAL; op1, op3: CARDINAL) ;
6127 BEGIN
6128 IF IsConstSet (op3) OR IsSet (GetType (op3))
6129 THEN
6130 CodeUnarySet (BuildSetNegate, SetNegate, quad, op1, op3)
6131 ELSIF UnaryOperand (quad, op3)
6132 THEN
6133 IF MustCheckOverflow (quad)
6134 THEN
6135 CodeUnaryCheck (BuildNegateCheck, NIL, quad, op1, op3)
6136 ELSE
6137 CodeUnary (BuildNegate, NIL, quad, op1, op3)
6138 END
6139 END
6140 END CodeNegateChecked ;
6141
6142
6143 (*
6144 FoldSize - check unary SIZE for constant folding.
6145 *)
6146
6147 PROCEDURE FoldSize (tokenno: CARDINAL; p: WalkAction;
6148 quad: CARDINAL; op1, op2, op3: CARDINAL) ;
6149 VAR
6150 t : tree ;
6151 location: location_t ;
6152 BEGIN
6153 location := TokenToLocation(tokenno) ;
6154 IF IsConst(op1) AND CompletelyResolved(op3)
6155 THEN
6156 IF op2=NulSym
6157 THEN
6158 t := BuildSize(location, Mod2Gcc(op3), FALSE) ;
6159 PushIntegerTree(t) ;
6160 PopValue(op1) ;
6161 PutConst(op1, Cardinal) ;
6162 p(op1) ;
6163 NoChange := FALSE ;
6164 SubQuad(quad) ;
6165 t := RememberConstant(t)
6166 ELSIF GccKnowsAbout(op2)
6167 THEN
6168 (* ignore the chosen varients as we implement it as a C union *)
6169 t := BuildSize(location, Mod2Gcc(op3), FALSE) ;
6170 PushIntegerTree(t) ;
6171 PopValue(op1) ;
6172 PutConst(op1, Cardinal) ;
6173 p(op1) ;
6174 NoChange := FALSE ;
6175 SubQuad(quad) ;
6176 t := RememberConstant(t)
6177 END
6178 END
6179 END FoldSize ;
6180
6181
6182 (*
6183 CodeSize - encode the inbuilt SIZE function.
6184 *)
6185
6186 PROCEDURE CodeSize (result, sym: CARDINAL) ;
6187 VAR
6188 location: location_t ;
6189 BEGIN
6190 location := TokenToLocation(CurrentQuadToken) ;
6191
6192 PushIntegerTree (BuildSize (location, Mod2Gcc (sym), FALSE)) ;
6193 IF IsConst (result)
6194 THEN
6195 PopValue (result) ;
6196 PutConst (result, Cardinal) ;
6197 PushValue (result) ;
6198 ConstantKnownAndUsed (result,
6199 DeclareKnownConstant (location,
6200 GetIntegerType (),
6201 PopIntegerTree ()))
6202 ELSE
6203 BuildAssignmentStatement (location, Mod2Gcc (result), PopIntegerTree ())
6204 END
6205 END CodeSize ;
6206
6207
6208 (*
6209 FoldRecordField - check whether we can fold an RecordFieldOp quadruple.
6210 Very similar to FoldBinary, except that we need to
6211 hard code a few parameters to the gcc backend.
6212 *)
6213
6214 PROCEDURE FoldRecordField (tokenno: CARDINAL; p: WalkAction;
6215 quad: CARDINAL; result, record, field: CARDINAL) ;
6216 VAR
6217 recordType,
6218 fieldType : CARDINAL ;
6219 ptr : tree ;
6220 location : location_t ;
6221 BEGIN
6222 RETURN ; (* this procedure should no longer be called *)
6223
6224 location := TokenToLocation(tokenno) ;
6225 (* firstly ensure that any constant literal is declared *)
6226 TryDeclareConstant(tokenno, record) ;
6227 IF IsRecordField(record) OR IsFieldVarient(record)
6228 THEN
6229 recordType := GetType (record) ;
6230 fieldType := GetType (field) ;
6231 IF GccKnowsAbout (record) AND GccKnowsAbout (field) AND
6232 GccKnowsAbout (recordType) AND GccKnowsAbout (fieldType) AND
6233 CompletelyResolved (recordType) AND CompletelyResolved (fieldType)
6234 THEN
6235 (* fine, we can take advantage of this and fold constants *)
6236 IF IsConst (result)
6237 THEN
6238 ptr := BuildComponentRef (location, Mod2Gcc (record), Mod2Gcc (field)) ;
6239 IF NOT IsValueSolved (result)
6240 THEN
6241 PushIntegerTree (ptr) ;
6242 PopValue (result)
6243 END ;
6244 PutConst (result, fieldType) ;
6245 AddModGcc (result, DeclareKnownConstant (location, Mod2Gcc (fieldType), ptr)) ;
6246 p (result) ;
6247 NoChange := FALSE ;
6248 SubQuad (quad)
6249 ELSE
6250 (* we can still fold the expression, but not the assignment, however, we will
6251 not do this here but in CodeOffset
6252 *)
6253 END
6254 END
6255 END
6256 END FoldRecordField ;
6257
6258
6259 (*
6260 CodeRecordField - encode a reference to a field within a record.
6261 *)
6262
6263 PROCEDURE CodeRecordField (result, record, field: CARDINAL) ;
6264 VAR
6265 recordType,
6266 fieldType : CARDINAL ;
6267 ptr : tree ;
6268 location : location_t ;
6269 BEGIN
6270 location := TokenToLocation (CurrentQuadToken) ;
6271 (* firstly ensure that any constant literal is declared *)
6272 IF IsRecordField (field) OR IsFieldVarient (field)
6273 THEN
6274 recordType := GetType (record) ;
6275 fieldType := GetType (field) ;
6276 IF GccKnowsAbout (record) AND GccKnowsAbout (field) AND
6277 GccKnowsAbout (recordType) AND GccKnowsAbout (fieldType) AND
6278 CompletelyResolved (recordType) AND CompletelyResolved (fieldType)
6279 THEN
6280 IF GetMode(record)=LeftValue
6281 THEN
6282 ptr := BuildComponentRef (location,
6283 BuildIndirect (location, Mod2Gcc (record), Mod2Gcc (recordType)),
6284 Mod2Gcc (field))
6285 ELSE
6286 ptr := BuildComponentRef (location, Mod2Gcc (record), Mod2Gcc (field))
6287 END ;
6288 AddModGcc (result, ptr)
6289 ELSE
6290 InternalError ('symbol type should have been declared by now')
6291 END
6292 ELSE
6293 InternalError ('not expecting this type of symbol')
6294 END
6295 END CodeRecordField ;
6296
6297
6298 (*
6299 BuildHighFromChar -
6300 *)
6301
6302 PROCEDURE BuildHighFromChar (operand: CARDINAL) : tree ;
6303 VAR
6304 location: location_t ;
6305 BEGIN
6306 location := TokenToLocation(GetDeclaredMod(operand)) ;
6307 IF IsConstString (operand) AND
6308 (IsConstStringM2nul (operand) OR IsConstStringCnul (operand))
6309 THEN
6310 RETURN GetCardinalOne (location)
6311 END ;
6312 RETURN GetCardinalZero (location)
6313 END BuildHighFromChar ;
6314
6315
6316 (*
6317 SkipToArray -
6318 *)
6319
6320 PROCEDURE SkipToArray (operand, dim: CARDINAL) : CARDINAL ;
6321 VAR
6322 type: CARDINAL ;
6323 BEGIN
6324 WHILE dim>1 DO
6325 type := SkipType(GetType(operand)) ;
6326 IF IsArray(type)
6327 THEN
6328 operand := type
6329 END ;
6330 DEC(dim)
6331 END ;
6332 RETURN( operand )
6333 END SkipToArray ;
6334
6335
6336 (*
6337 BuildHighFromArray -
6338 *)
6339
6340 PROCEDURE BuildHighFromArray (tokenno: CARDINAL; dim, operand: CARDINAL) : tree ;
6341 VAR
6342 Type : CARDINAL ;
6343 location: location_t ;
6344 BEGIN
6345 location := TokenToLocation(tokenno) ;
6346 Type := SkipType (GetType (SkipToArray (operand, dim))) ;
6347 RETURN BuildHighFromStaticArray (location, (* dim, *) Type)
6348 END BuildHighFromArray ;
6349
6350
6351 (*
6352 BuildHighFromStaticArray -
6353 *)
6354
6355 PROCEDURE BuildHighFromStaticArray (location: location_t; (* dim, *) Type: CARDINAL) : tree ;
6356 VAR
6357 High, Low: CARDINAL ;
6358 Subscript,
6359 Subrange : CARDINAL ;
6360 BEGIN
6361 Assert (IsArray (Type)) ;
6362 Subscript := GetArraySubscript (Type) ;
6363 Subrange := SkipType (GetType (Subscript)) ;
6364 IF IsEnumeration (Subrange)
6365 THEN
6366 GetBaseTypeMinMax (Subrange, Low, High) ;
6367 IF GccKnowsAbout (High)
6368 THEN
6369 RETURN tree (Mod2Gcc (High))
6370 END
6371 ELSIF IsSubrange(Subrange)
6372 THEN
6373 GetSubrange (Subrange, High, Low) ;
6374 IF GccKnowsAbout (Low) AND GccKnowsAbout (High)
6375 THEN
6376 RETURN BuildSub (location, Mod2Gcc (High), Mod2Gcc (Low), TRUE)
6377 END
6378 ELSE
6379 MetaError1 ('array subscript {%1EDad:for} must be a subrange or enumeration type', Type) ;
6380 RETURN tree(NIL)
6381 END ;
6382 IF GccKnowsAbout (High)
6383 THEN
6384 RETURN tree (Mod2Gcc (High))
6385 ELSE
6386 RETURN tree (NIL)
6387 END
6388 END BuildHighFromStaticArray ;
6389
6390
6391 (*
6392 BuildHighFromString -
6393 *)
6394
6395 PROCEDURE BuildHighFromString (operand: CARDINAL) : tree ;
6396 VAR
6397 location: location_t ;
6398 BEGIN
6399 location := TokenToLocation (GetDeclaredMod (operand)) ;
6400 IF GccKnowsAbout (operand) AND (StringLength (Mod2Gcc (operand)) > 0)
6401 THEN
6402 RETURN( BuildIntegerConstant (StringLength (Mod2Gcc (operand))-1) )
6403 ELSE
6404 RETURN( GetIntegerZero (location) )
6405 END
6406 END BuildHighFromString ;
6407
6408
6409 (*
6410 ResolveHigh - given an Modula-2 operand, it resolves the HIGH(operand)
6411 and returns a GCC constant symbol containing the value of
6412 HIGH(operand).
6413 *)
6414
6415 PROCEDURE ResolveHigh (tokenno: CARDINAL; dim, operand: CARDINAL) : tree ;
6416 VAR
6417 Type : CARDINAL ;
6418 location: location_t ;
6419 BEGIN
6420 Type := SkipType(GetType(operand)) ;
6421 location := TokenToLocation(tokenno) ;
6422
6423 IF (Type=Char) AND (dim=1)
6424 THEN
6425 RETURN( BuildHighFromChar(operand) )
6426 ELSIF IsConstString(operand) AND (dim=1)
6427 THEN
6428 RETURN( BuildHighFromString(operand) )
6429 ELSIF IsArray(Type)
6430 THEN
6431 RETURN( BuildHighFromArray(tokenno, dim, operand) )
6432 ELSIF IsUnbounded(Type)
6433 THEN
6434 RETURN( GetHighFromUnbounded(location, dim, operand) )
6435 ELSE
6436 MetaErrorT1 (tokenno,
6437 'base procedure HIGH expects a variable of type array or a constant string or CHAR as its parameter, rather than {%1Etad}',
6438 operand) ;
6439 RETURN( GetIntegerZero(location) )
6440 END
6441 END ResolveHigh ;
6442
6443
6444 (*
6445 FoldHigh - if the array is not dynamic then we should be able to
6446 remove the HighOp quadruple and assign op1 with
6447 the known compile time HIGH(op3).
6448 *)
6449
6450 PROCEDURE FoldHigh (tokenno: CARDINAL; p: WalkAction;
6451 quad: CARDINAL; op1, dim, op3: CARDINAL) ;
6452 VAR
6453 t : tree ;
6454 location: location_t ;
6455 BEGIN
6456 (* firstly ensure that any constant literal is declared *)
6457 TryDeclareConstant(tokenno, op3) ;
6458 location := TokenToLocation(tokenno) ;
6459 IF GccKnowsAbout(op3) AND CompletelyResolved(op3)
6460 THEN
6461 t := ResolveHigh(tokenno, dim, op3) ;
6462 (* fine, we can take advantage of this and fold constants *)
6463 IF IsConst(op1) AND (t#tree(NIL))
6464 THEN
6465 PutConst(op1, Cardinal) ;
6466 AddModGcc(op1,
6467 DeclareKnownConstant(location, GetCardinalType(),
6468 ToCardinal(location, t))) ;
6469 p(op1) ;
6470 NoChange := FALSE ;
6471 SubQuad(quad)
6472 ELSE
6473 (* we can still fold the expression, but not the assignment, however, we will
6474 not do this here but in CodeHigh
6475 *)
6476 END
6477 END
6478 END FoldHigh ;
6479
6480
6481 (*
6482 CodeHigh - encode a unary arithmetic operation.
6483 *)
6484
6485 PROCEDURE CodeHigh (result, dim, array: CARDINAL) ;
6486 VAR
6487 location: location_t ;
6488 BEGIN
6489 location := TokenToLocation (CurrentQuadToken) ;
6490
6491 (* firstly ensure that any constant literal is declared *)
6492 DeclareConstant (CurrentQuadToken, array) ;
6493 IF IsConst (result)
6494 THEN
6495 (* still have a constant which was not resolved, pass it to gcc *)
6496 ConstantKnownAndUsed (result,
6497 DeclareKnownConstant(location,
6498 GetM2ZType (),
6499 ResolveHigh (CurrentQuadToken, dim, array)))
6500 ELSE
6501 BuildAssignmentStatement (location,
6502 Mod2Gcc (result),
6503 BuildConvert (location,
6504 Mod2Gcc (GetType (result)),
6505 ResolveHigh (CurrentQuadToken, dim, array),
6506 FALSE))
6507 END
6508 END CodeHigh ;
6509
6510
6511 (*
6512 CodeUnbounded - codes the creation of an unbounded parameter variable.
6513 places the address of op3 into *op1
6514 *)
6515
6516 PROCEDURE CodeUnbounded (result, array: CARDINAL) ;
6517 VAR
6518 Addr : tree ;
6519 location: location_t ;
6520 BEGIN
6521 location := TokenToLocation (CurrentQuadToken) ;
6522
6523 DeclareConstant (CurrentQuadToken, array) ;
6524 IF IsConstString (array) OR (IsConst (array) AND (GetSType (array) = Char))
6525 THEN
6526 BuildAssignmentStatement (location, Mod2Gcc (result), BuildAddr (location, PromoteToString (CurrentQuadToken, array), FALSE))
6527 ELSIF IsConstructor (array)
6528 THEN
6529 BuildAssignmentStatement (location, Mod2Gcc (result), BuildAddr (location, Mod2Gcc (array), TRUE))
6530 ELSIF IsUnbounded (GetType (array))
6531 THEN
6532 IF GetMode(array) = LeftValue
6533 THEN
6534 Addr := BuildConvert (location, Mod2Gcc (GetType (result)), Mod2Gcc (array), FALSE)
6535 ELSE
6536 Addr := BuildComponentRef (location, Mod2Gcc (array), Mod2Gcc (GetUnboundedAddressOffset (GetType (array))))
6537 END ;
6538 BuildAssignmentStatement (location, Mod2Gcc (result), Addr)
6539 ELSIF GetMode(array) = RightValue
6540 THEN
6541 BuildAssignmentStatement (location, Mod2Gcc (result), BuildAddr (location, Mod2Gcc (array), FALSE))
6542 ELSE
6543 BuildAssignmentStatement (location, Mod2Gcc (result), Mod2Gcc (array))
6544 END
6545 END CodeUnbounded ;
6546
6547
6548 (*
6549 AreSubrangesKnown - returns TRUE if the subranges values used within, array, are known.
6550 *)
6551
6552 PROCEDURE AreSubrangesKnown (array: CARDINAL) : BOOLEAN ;
6553 VAR
6554 type,
6555 subscript,
6556 low, high: CARDINAL ;
6557 BEGIN
6558 IF GccKnowsAbout(array)
6559 THEN
6560 subscript := GetArraySubscript(array) ;
6561 IF subscript=NulSym
6562 THEN
6563 InternalError ('not expecting a NulSym as a subscript')
6564 ELSE
6565 type := SkipType(GetType(subscript)) ;
6566 low := GetTypeMin(type) ;
6567 high := GetTypeMax(type) ;
6568 RETURN( GccKnowsAbout(low) AND GccKnowsAbout(high) )
6569 END
6570 ELSE
6571 RETURN( FALSE )
6572 END
6573 END AreSubrangesKnown ;
6574
6575
6576 (*
6577 CodeArray - res is an lvalue which will point to the array element.
6578 *)
6579
6580 PROCEDURE CodeArray (res, index, array: CARDINAL) ;
6581 VAR
6582 resType,
6583 arrayDecl,
6584 type,
6585 low,
6586 subscript : CARDINAL ;
6587 a, ta,
6588 ti, tl : tree ;
6589 location : location_t ;
6590 BEGIN
6591 location := TokenToLocation (CurrentQuadToken) ;
6592
6593 arrayDecl := SkipType (GetType (array)) ;
6594 IF AreSubrangesKnown (arrayDecl)
6595 THEN
6596 subscript := GetArraySubscript (arrayDecl) ;
6597 type := SkipType (GetType (subscript)) ;
6598 low := GetTypeMin (type) ;
6599 resType := GetVarBackEndType(res) ;
6600 IF resType=NulSym
6601 THEN
6602 resType := SkipType(GetType(res))
6603 END ;
6604 ta := Mod2Gcc(SkipType(GetType(arrayDecl))) ;
6605 IF GetMode(array)=LeftValue
6606 THEN
6607 a := BuildIndirect(location, Mod2Gcc(array), Mod2Gcc(SkipType(GetType(array))))
6608 ELSE
6609 a := Mod2Gcc(array)
6610 END ;
6611 IF IsArrayLarge(arrayDecl)
6612 THEN
6613 tl := BuildConvert(location, Mod2Gcc(type), Mod2Gcc(low), FALSE) ;
6614 ti := BuildConvert(location, Mod2Gcc(type), Mod2Gcc(index), FALSE) ;
6615 ti := BuildConvert(location, GetIntegerType(), BuildSub(location, ti, tl, FALSE), FALSE) ;
6616 tl := GetIntegerZero(location)
6617 ELSE
6618 tl := BuildConvert(location, GetIntegerType(), Mod2Gcc(low), FALSE) ;
6619 ti := BuildConvert(location, GetIntegerType(), Mod2Gcc(index), FALSE)
6620 END ;
6621 (* ti := BuildConvert(location, GetIntegerType(), Mod2Gcc(high), FALSE) ; *)
6622 BuildAssignmentStatement (location,
6623 Mod2Gcc (res),
6624 BuildConvert (location,
6625 Mod2Gcc (resType),
6626 BuildAddr (location, BuildArray (location,
6627 ta, a, ti, tl),
6628 FALSE),
6629 FALSE))
6630 ELSE
6631 InternalError ('subranges not yet resolved')
6632 END
6633 END CodeArray ;
6634
6635
6636 (*
6637 FoldElementSizeForArray - attempts to calculate the Subscript
6638 multiplier for the index op3.
6639 *)
6640
6641 PROCEDURE FoldElementSizeForArray (tokenno: CARDINAL; quad: CARDINAL;
6642 p: WalkAction;
6643 result, type: CARDINAL) ;
6644 VAR
6645 Subscript: CARDINAL ;
6646 location : location_t ;
6647 BEGIN
6648 location := TokenToLocation (tokenno) ;
6649
6650 IF IsConst (result) AND (NOT GccKnowsAbout (result))
6651 THEN
6652 Subscript := GetArraySubscript (type) ;
6653 IF IsSizeSolved (Subscript)
6654 THEN
6655 PutConst (result, Integer) ;
6656 PushSize (Subscript) ;
6657 AddModGcc (result,
6658 DeclareKnownConstant (location,
6659 GetCardinalType (),
6660 BuildConvert (location,
6661 GetCardinalType (),
6662 PopIntegerTree (),
6663 TRUE))) ;
6664 p (result) ;
6665 NoChange := FALSE ;
6666 SubQuad (quad)
6667 END
6668 END
6669 END FoldElementSizeForArray ;
6670
6671
6672 (*
6673 FoldElementSizeForUnbounded - Unbounded arrays only have one index,
6674 therefore element size will be the
6675 TSIZE(Type) where Type is defined as:
6676 ARRAY OF Type.
6677 *)
6678
6679 PROCEDURE FoldElementSizeForUnbounded (tokenno: CARDINAL; quad: CARDINAL;
6680 p: WalkAction;
6681 result, ArrayType: CARDINAL) ;
6682 VAR
6683 Type : CARDINAL ;
6684 location: location_t ;
6685 BEGIN
6686 location := TokenToLocation (tokenno) ;
6687
6688 IF IsConst (result)
6689 THEN
6690 IF GccKnowsAbout (result)
6691 THEN
6692 InternalError ('cannot assign a value twice to a constant')
6693 ELSE
6694 Assert (IsUnbounded (ArrayType)) ;
6695 Type := GetType (ArrayType) ;
6696 IF GccKnowsAbout (Type)
6697 THEN
6698 PutConst (result, Cardinal) ;
6699 AddModGcc (result,
6700 DeclareKnownConstant (location,
6701 GetCardinalType (),
6702 BuildConvert (location,
6703 GetCardinalType (),
6704 FindSize (tokenno, Type),
6705 TRUE))) ;
6706 p (result) ;
6707 NoChange := FALSE ;
6708 SubQuad (quad)
6709 END
6710 END
6711 END
6712 END FoldElementSizeForUnbounded ;
6713
6714
6715 (*
6716 FoldElementSize - folds the element size for an ArraySym or UnboundedSym.
6717 ElementSize returns a constant which defines the
6718 multiplier to be multiplied by this element index.
6719 *)
6720
6721 PROCEDURE FoldElementSize (tokenno: CARDINAL; p: WalkAction;
6722 quad: CARDINAL; result, type: CARDINAL) ;
6723 BEGIN
6724 IF IsUnbounded (type)
6725 THEN
6726 FoldElementSizeForUnbounded (tokenno, quad, p, result, type)
6727 ELSIF IsArray (type)
6728 THEN
6729 FoldElementSizeForArray (tokenno, quad, p, result, type)
6730 ELSE
6731 InternalError ('expecting UnboundedSym or ArraySym')
6732 END
6733 END FoldElementSize ;
6734
6735
6736 (*
6737 PopKindTree - returns a Tree from M2ALU of the type implied by, op.
6738 *)
6739
6740 PROCEDURE PopKindTree (op: CARDINAL; tokenno: CARDINAL) : tree ;
6741 VAR
6742 type: CARDINAL ;
6743 BEGIN
6744 IF IsConst (op) AND IsConstString (op)
6745 THEN
6746 (* Converting a nul char or char for example. *)
6747 RETURN PopIntegerTree ()
6748 ELSE
6749 type := SkipType (GetType (op)) ;
6750 IF IsSet (type)
6751 THEN
6752 RETURN( PopSetTree (tokenno) )
6753 ELSIF IsRealType (type)
6754 THEN
6755 RETURN( PopRealTree () )
6756 ELSE
6757 RETURN( PopIntegerTree () )
6758 END
6759 END
6760 END PopKindTree ;
6761
6762
6763 (*
6764 FoldConvert - attempts to fold expr to type into result
6765 providing that result and expr are constants.
6766 If required convert will alter the machine representation
6767 of expr to comply with type.
6768 *)
6769
6770 PROCEDURE FoldConvert (tokenno: CARDINAL; p: WalkAction;
6771 quad: CARDINAL; result, type, expr: CARDINAL) ;
6772
6773 VAR
6774 tl : tree ;
6775 location: location_t ;
6776 BEGIN
6777 location := TokenToLocation (tokenno) ;
6778 (* First ensure that constant literals are declared. *)
6779 TryDeclareConstant (tokenno, expr) ;
6780 IF IsConstant (expr)
6781 THEN
6782 IF GccKnowsAbout (type) AND
6783 (IsProcedure (expr) OR IsValueSolved (expr)) AND
6784 GccKnowsAbout (SkipType (type))
6785 THEN
6786 (* The type is known and expr is resolved so fold the convert. *)
6787 IF IsConst (result)
6788 THEN
6789 PutConst (result, type) ; (* Change result type just in case. *)
6790 tl := Mod2Gcc (SkipType (type)) ;
6791 IF IsProcedure (expr)
6792 THEN
6793 AddModGcc (result, BuildConvert (location, tl, Mod2Gcc (expr), TRUE))
6794 ELSE
6795 PushValue (expr) ;
6796 IF IsConstSet (expr)
6797 THEN
6798 IF IsSet (SkipType (type))
6799 THEN
6800 WriteFormat0 ('cannot convert values between sets')
6801 ELSE
6802 PushIntegerTree (FoldAndStrip (BuildConvert (location, tl, PopSetTree (tokenno), TRUE))) ;
6803 PopValue (result) ;
6804 PushValue (result) ;
6805 AddModGcc (result, PopIntegerTree())
6806 END
6807 ELSE
6808 IF IsSet (SkipType (type))
6809 THEN
6810 PushSetTree (tokenno,
6811 FoldAndStrip (BuildConvert (location, tl, PopKindTree (expr, tokenno),
6812 TRUE)), SkipType (type)) ;
6813 PopValue (result) ;
6814 PutConstSet (result) ;
6815 PushValue (result) ;
6816 AddModGcc (result, PopSetTree (tokenno))
6817 ELSIF IsRealType (SkipType (type))
6818 THEN
6819 PushRealTree (FoldAndStrip (BuildConvert (location, tl, PopKindTree (expr, tokenno),
6820 TRUE))) ;
6821 PopValue (result) ;
6822 PushValue (result) ;
6823 AddModGcc (result, PopKindTree (result, tokenno))
6824 ELSE
6825 (* Let CheckOverflow catch a potential overflow rather than BuildConvert. *)
6826 PushIntegerTree (FoldAndStrip (BuildConvert (location, tl,
6827 PopKindTree (expr, tokenno),
6828 FALSE))) ;
6829 PopValue (result) ;
6830 PushValue (result) ;
6831 CheckOrResetOverflow (tokenno, PopKindTree (result, tokenno), MustCheckOverflow (quad)) ;
6832 PushValue (result) ;
6833 AddModGcc (result, PopKindTree (result, tokenno))
6834 END
6835 END
6836 END ;
6837 p (result) ;
6838 NoChange := FALSE ;
6839 SubQuad (quad)
6840 END
6841 END
6842 END
6843 END FoldConvert ;
6844
6845
6846 (*
6847 CodeConvert - Converts, rhs, to, type, placing the result into lhs.
6848 Convert will, if need be, alter the machine representation
6849 of op3 to comply with TYPE op2.
6850 *)
6851
6852 PROCEDURE CodeConvert (quad: CARDINAL; lhs, type, rhs: CARDINAL) ;
6853 VAR
6854 tl, tr : tree ;
6855 location: location_t ;
6856 BEGIN
6857 CheckStop(quad) ;
6858
6859 (* firstly ensure that constant literals are declared *)
6860 DeclareConstant(CurrentQuadToken, rhs) ;
6861 DeclareConstructor(CurrentQuadToken, quad, rhs) ;
6862 location := TokenToLocation(CurrentQuadToken) ;
6863
6864 tl := LValueToGenericPtr(location, type) ;
6865 IF IsProcedure(rhs)
6866 THEN
6867 tr := BuildAddr(location, Mod2Gcc(rhs), FALSE)
6868 ELSE
6869 tr := LValueToGenericPtr(location, rhs) ;
6870 tr := ConvertRHS(tr, type, rhs)
6871 END ;
6872 IF IsConst(lhs)
6873 THEN
6874 (* fine, we can take advantage of this and fold constant *)
6875 PutConst(lhs, type) ;
6876 tl := Mod2Gcc(SkipType(type)) ;
6877 ConstantKnownAndUsed (lhs,
6878 BuildConvert (location, tl, Mod2Gcc (rhs), TRUE))
6879 ELSE
6880 BuildAssignmentStatement (location, Mod2Gcc (lhs), BuildConvert (location, tl, tr, TRUE)) ;
6881 END
6882 END CodeConvert ;
6883
6884
6885 (*
6886 CodeCoerce - Coerce op3 to type op2 placing the result into
6887 op1.
6888 Coerce will NOT alter the machine representation
6889 of op3 to comply with TYPE op2.
6890 Therefore it _insists_ that under all circumstances that the
6891 type sizes of op1 and op3 are the same.
6892 CONVERT will perform machine manipulation to change variable
6893 types, coerce does no such thing.
6894 *)
6895
6896 PROCEDURE CodeCoerce (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
6897 VAR
6898 location: location_t ;
6899 BEGIN
6900 DeclareConstant(CurrentQuadToken, op3) ; (* checks to see whether it is a constant literal and declares it *)
6901 DeclareConstructor(CurrentQuadToken, quad, op3) ;
6902 location := TokenToLocation(CurrentQuadToken) ;
6903
6904 IF IsProcedure(op3)
6905 THEN
6906 IF AreConstantsEqual(FindSize(CurrentQuadToken, op1), FindSize(CurrentQuadToken, Address))
6907 THEN
6908 IF IsConst(op1)
6909 THEN
6910 ConstantKnownAndUsed(op1, CheckConstant(CurrentQuadToken, op1, op3))
6911 ELSE
6912 BuildAssignmentStatement (location, Mod2Gcc (op1), Mod2Gcc (op3))
6913 END
6914 ELSE
6915 MetaErrorT0 (CurrentQuadToken,
6916 '{%E}procedure address can only be stored in an address sized operand')
6917 END
6918 ELSIF IsConst(op3) OR AreConstantsEqual(FindSize(CurrentQuadToken, op1), FindSize(CurrentQuadToken, op3))
6919 THEN
6920 IF IsConst(op1)
6921 THEN
6922 ConstantKnownAndUsed(op1,
6923 DeclareKnownConstant(location,
6924 Mod2Gcc(GetType(op1)),
6925 Mod2Gcc(op3)))
6926 ELSE
6927 Assert(GccKnowsAbout(op2)) ;
6928 IF IsConst(op3)
6929 THEN
6930 BuildAssignmentStatement (location, Mod2Gcc(op1), Mod2Gcc(op3))
6931 ELSE
6932 (* does not work t := BuildCoerce(Mod2Gcc(op1), Mod2Gcc(op2), Mod2Gcc(op3)) *)
6933 checkDeclare (op1) ;
6934 AddStatement (location,
6935 MaybeDebugBuiltinMemcpy(location,
6936 BuildAddr(location, Mod2Gcc(op1), FALSE),
6937 BuildAddr(location, Mod2Gcc(op3), FALSE),
6938 FindSize(CurrentQuadToken, op2)))
6939 END
6940 END
6941 ELSE
6942 MetaErrorT0 (CurrentQuadToken,
6943 'can only {%kCAST} objects of the same size')
6944 END
6945 END CodeCoerce ;
6946
6947
6948 (*
6949 FoldCoerce -
6950 *)
6951
6952 PROCEDURE FoldCoerce (tokenno: CARDINAL; p: WalkAction;
6953 quad, op1, op2, op3: CARDINAL) ;
6954 VAR
6955 location: location_t ;
6956 BEGIN
6957 TryDeclareConstant(tokenno, op3) ; (* checks to see whether it is a constant literal and declares it *)
6958 location := TokenToLocation(tokenno) ;
6959
6960 IF GccKnowsAbout(op2) AND GccKnowsAbout(op3)
6961 THEN
6962 IF IsProcedure(op3)
6963 THEN
6964 IF AreConstantsEqual(FindSize(tokenno, op1), FindSize(tokenno, Address))
6965 THEN
6966 IF IsConst(op1)
6967 THEN
6968 AddModGcc(op1,
6969 DeclareKnownConstant(location,
6970 Mod2Gcc(GetType(op1)),
6971 Mod2Gcc(op3))) ;
6972 p(op1) ;
6973 NoChange := FALSE ;
6974 SubQuad(quad)
6975 END
6976 ELSE
6977 MetaErrorT0 (CurrentQuadToken,
6978 '{%E}procedure address can only be stored in a address sized operand')
6979 END
6980 ELSIF IsConst(op3)
6981 THEN
6982 IF IsConst(op1)
6983 THEN
6984 AddModGcc(op1,
6985 DeclareKnownConstant(location,
6986 Mod2Gcc(GetType(op1)),
6987 Mod2Gcc(op3))) ;
6988 p(op1) ;
6989 NoChange := FALSE ;
6990 SubQuad(quad)
6991 END
6992 END
6993 END
6994 END FoldCoerce ;
6995
6996
6997 (*
6998 CanConvert - returns TRUE if we can convert variable, var, to a, type.
6999 *)
7000
7001 PROCEDURE CanConvert (type, var: CARDINAL) : BOOLEAN ;
7002 VAR
7003 svar,
7004 stype: CARDINAL ;
7005 BEGIN
7006 stype := SkipType(type) ;
7007 svar := SkipType(GetType(var)) ;
7008 RETURN (IsBaseType(stype) OR IsOrdinalType(stype) OR IsSystemType(stype)) AND
7009 (IsBaseType(svar) OR IsOrdinalType(svar) OR IsSystemType(stype))
7010 END CanConvert ;
7011
7012
7013 (*
7014 CodeCast - Cast op3 to type op2 placing the result into op1.
7015 Cast will NOT alter the machine representation
7016 of op3 to comply with TYPE op2 as long as SIZE(op3)=SIZE(op2).
7017 If the sizes differ then Convert is called.
7018 *)
7019
7020 PROCEDURE CodeCast (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
7021 VAR
7022 location: location_t ;
7023 BEGIN
7024 DeclareConstant(CurrentQuadToken, op3) ; (* checks to see whether it is a constant literal and declares it *)
7025 DeclareConstructor(CurrentQuadToken, quad, op3) ;
7026 location := TokenToLocation(CurrentQuadToken) ;
7027
7028 IF IsProcedure(op3)
7029 THEN
7030 IF AreConstantsEqual(FindSize(CurrentQuadToken, op1), FindSize(CurrentQuadToken, Address))
7031 THEN
7032 IF IsConst(op1)
7033 THEN
7034 ConstantKnownAndUsed(op1, CheckConstant(CurrentQuadToken, op1, op3))
7035 ELSE
7036 BuildAssignmentStatement (location, Mod2Gcc(op1), Mod2Gcc(op3))
7037 END
7038 ELSE
7039 MetaErrorT0 (CurrentQuadToken,
7040 '{%E}procedure address can only be stored in an address sized operand')
7041 END
7042 ELSIF IsConst(op3) OR AreConstantsEqual(FindSize(CurrentQuadToken, op1), FindSize(CurrentQuadToken, op3))
7043 THEN
7044 CodeCoerce(quad, op1, op2, op3)
7045 ELSE
7046 IF PedanticCast AND (NOT CanConvert(op2, op3))
7047 THEN
7048 MetaError2 ('{%WkCAST} cannot copy a variable src {%2Dad} to a destination {%1Dad} as they are of different sizes and are not ordinal or real types',
7049 op1, op3)
7050 END ;
7051 CodeConvert(quad, op1, op2, op3)
7052 END
7053 END CodeCast ;
7054
7055
7056 (*
7057 FoldCoerce -
7058 *)
7059
7060 PROCEDURE FoldCast (tokenno: CARDINAL; p: WalkAction;
7061 quad, op1, op2, op3: CARDINAL) ;
7062 BEGIN
7063 TryDeclareConstant(tokenno, op3) ; (* checks to see whether it is a constant literal and declares it *)
7064 IF GccKnowsAbout(op2) AND GccKnowsAbout(op3)
7065 THEN
7066 IF IsProcedure(op3)
7067 THEN
7068 IF AreConstantsEqual(FindSize(tokenno, op1), FindSize(tokenno, Address))
7069 THEN
7070 FoldCoerce(tokenno, p, quad, op1, op2, op3)
7071 ELSE
7072 MetaErrorT0 (tokenno,
7073 '{%E}procedure address can only be stored in an address sized operand')
7074 END
7075 ELSIF IsConst(op3)
7076 THEN
7077 FoldCoerce(tokenno, p, quad, op1, op2, op3)
7078 END
7079 END
7080 END FoldCast ;
7081
7082
7083 (*
7084 CreateLabelProcedureN - creates a label using procedure name and
7085 an integer.
7086 *)
7087
7088 PROCEDURE CreateLabelProcedureN (proc: CARDINAL; leader: ARRAY OF CHAR;
7089 unboundedCount, n: CARDINAL) : String ;
7090 VAR
7091 n1, n2: String ;
7092 BEGIN
7093 n1 := Mark(InitStringCharStar(KeyToCharStar(GetSymName(proc)))) ;
7094 n2 := Mark(InitString(leader)) ;
7095 (* prefixed by .L unboundedCount and n to ensure that no Modula-2 identifiers clash *)
7096 RETURN( Sprintf4(Mark(InitString('.L%d.%d.unbounded.%s.%s')), unboundedCount, n, n1, n2) )
7097 END CreateLabelProcedureN ;
7098
7099
7100 (*
7101 CreateLabelName - creates a namekey from quadruple, q.
7102 *)
7103
7104 PROCEDURE CreateLabelName (q: CARDINAL) : String ;
7105 BEGIN
7106 (* prefixed by . to ensure that no Modula-2 identifiers clash *)
7107 RETURN( Sprintf1(Mark(InitString('.L%d')), q) )
7108 END CreateLabelName ;
7109
7110
7111 (*
7112 CodeGoto - creates a jump to a labeled quadruple.
7113 *)
7114
7115 PROCEDURE CodeGoto (destquad: CARDINAL) ;
7116 VAR
7117 location: location_t ;
7118 BEGIN
7119 location := TokenToLocation (CurrentQuadToken) ;
7120 BuildGoto (location, string (CreateLabelName (destquad)))
7121 END CodeGoto ;
7122
7123
7124 (*
7125 CheckReferenced - checks to see whether this quadruple requires a label.
7126 *)
7127
7128 PROCEDURE CheckReferenced (quad: CARDINAL; op: QuadOperator) ;
7129 VAR
7130 location: location_t ;
7131 BEGIN
7132 location := TokenToLocation(CurrentQuadToken) ;
7133
7134 (* we do not create labels for procedure entries *)
7135 IF (op#ProcedureScopeOp) AND (op#NewLocalVarOp) AND IsReferenced(quad)
7136 THEN
7137 DeclareLabel(location, string(CreateLabelName(quad)))
7138 END
7139 END CheckReferenced ;
7140
7141
7142 (*
7143 CodeIfSetLess -
7144 *)
7145
7146 PROCEDURE CodeIfSetLess (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
7147 VAR
7148 settype : CARDINAL ;
7149 falselabel: ADDRESS ;
7150 location : location_t ;
7151 BEGIN
7152 location := TokenToLocation(CurrentQuadToken) ;
7153
7154 IF IsConst(op1) AND IsConst(op2)
7155 THEN
7156 InternalError ('this should have been folded in the calling procedure')
7157 ELSIF IsConst(op1)
7158 THEN
7159 settype := SkipType(GetType(op2))
7160 ELSE
7161 settype := SkipType(GetType(op1))
7162 END ;
7163 IF CompareTrees(FindSize(CurrentQuadToken, settype), FindSize(CurrentQuadToken, Word)) <= 0
7164 THEN
7165 (* word size sets *)
7166 DoJump(location,
7167 BuildIsNotSuperset(location,
7168 BuildConvert(location, GetWordType(), Mod2Gcc(op1), FALSE),
7169 BuildConvert(location, GetWordType(), Mod2Gcc(op2), FALSE)),
7170 NIL, string(CreateLabelName(op3)))
7171 ELSE
7172 falselabel := string(Sprintf1(Mark(InitString('.Lset%dcomp')), quad)) ;
7173
7174 BuildForeachWordInSetDoIfExpr(location,
7175 Mod2Gcc(settype),
7176 Mod2Gcc(op1), Mod2Gcc(op2),
7177 GetMode(op1)=LeftValue,
7178 GetMode(op2)=LeftValue,
7179 IsConst(op1), IsConst(op2),
7180 BuildIsSuperset,
7181 falselabel) ;
7182
7183 BuildGoto(location, string(CreateLabelName(op3))) ;
7184 DeclareLabel(location, falselabel)
7185 END
7186 END CodeIfSetLess ;
7187
7188
7189 (*
7190 PerformCodeIfLess - codes the quadruple if op1 < op2 then goto op3
7191 *)
7192
7193 PROCEDURE PerformCodeIfLess (quad: CARDINAL) ;
7194 VAR
7195 tl, tr : tree ;
7196 location : location_t ;
7197 left, right, dest, combined,
7198 leftpos, rightpos, destpos : CARDINAL ;
7199 constExpr, overflow : BOOLEAN ;
7200 op : QuadOperator ;
7201 BEGIN
7202 GetQuadOtok (quad, combined, op,
7203 left, right, dest, overflow,
7204 constExpr,
7205 leftpos, rightpos, destpos) ;
7206 location := TokenToLocation (combined) ;
7207
7208 IF IsConst(left) AND IsConst(right)
7209 THEN
7210 PushValue(left) ;
7211 PushValue(right) ;
7212 IF Less(CurrentQuadToken)
7213 THEN
7214 BuildGoto(location, string(CreateLabelName(dest)))
7215 ELSE
7216 (* Fall through. *)
7217 END
7218 ELSIF IsConstSet(left) OR (IsVar(left) AND IsSet(SkipType(GetType(left)))) OR
7219 IsConstSet(right) OR (IsVar(right) AND IsSet(SkipType(GetType(right))))
7220 THEN
7221 CodeIfSetLess(quad, left, right, dest)
7222 ELSE
7223 IF IsComposite(GetType(left)) OR IsComposite(GetType(right))
7224 THEN
7225 MetaErrorT2 (combined,
7226 'comparison tests between composite types not allowed {%1Eatd} and {%2atd}',
7227 left, right)
7228 ELSE
7229 ConvertBinaryOperands (location,
7230 tl, tr,
7231 ComparisonMixTypes (left, right,
7232 SkipType (GetType (left)),
7233 SkipType (GetType (right)),
7234 combined),
7235 left, right) ;
7236 DoJump (location,
7237 BuildLessThan (location, tl, tr), NIL, string (CreateLabelName (dest)))
7238 END
7239 END
7240 END PerformCodeIfLess ;
7241
7242
7243 (*
7244 CodeIfLess - codes the quadruple if op1 < op2 then goto op3
7245 *)
7246
7247 PROCEDURE CodeIfLess (quad: CARDINAL) ;
7248 BEGIN
7249 IF IsValidExpressionRelOp (quad, FALSE)
7250 THEN
7251 PerformCodeIfLess (quad)
7252 END
7253 END CodeIfLess ;
7254
7255
7256 (*
7257 CodeIfSetGre -
7258 *)
7259
7260 PROCEDURE CodeIfSetGre (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
7261 VAR
7262 settype : CARDINAL ;
7263 falselabel: ADDRESS ;
7264 location : location_t ;
7265 BEGIN
7266 location := TokenToLocation(CurrentQuadToken) ;
7267
7268 IF IsConst(op1) AND IsConst(op2)
7269 THEN
7270 InternalError ('this should have been folded in the calling procedure')
7271 ELSIF IsConst(op1)
7272 THEN
7273 settype := SkipType(GetType(op2))
7274 ELSE
7275 settype := SkipType(GetType(op1))
7276 END ;
7277 IF CompareTrees(FindSize(CurrentQuadToken, settype), FindSize(CurrentQuadToken, Word)) <= 0
7278 THEN
7279 (* word size sets *)
7280 DoJump(location,
7281 BuildIsNotSubset(location,
7282 BuildConvert(location, GetWordType(), Mod2Gcc(op1), FALSE),
7283 BuildConvert(location, GetWordType(), Mod2Gcc(op2), FALSE)),
7284 NIL, string(CreateLabelName(op3)))
7285 ELSE
7286 falselabel := string(Sprintf1(Mark(InitString('.Lset%dcomp')), quad)) ;
7287
7288 BuildForeachWordInSetDoIfExpr(location,
7289 Mod2Gcc(settype),
7290 Mod2Gcc(op1), Mod2Gcc(op2),
7291 GetMode(op1)=LeftValue,
7292 GetMode(op2)=LeftValue,
7293 IsConst(op1), IsConst(op2),
7294 BuildIsSubset,
7295 falselabel) ;
7296
7297 BuildGoto(location, string(CreateLabelName(op3))) ;
7298 DeclareLabel(location, falselabel)
7299 END
7300 END CodeIfSetGre ;
7301
7302
7303 (*
7304 PerformCodeIfGre - codes the quadruple if op1 > op2 then goto op3
7305 *)
7306
7307 PROCEDURE PerformCodeIfGre (quad: CARDINAL) ;
7308 VAR
7309 tl, tr : tree ;
7310 location : location_t ;
7311 left, right, dest, combined,
7312 leftpos, rightpos, destpos : CARDINAL ;
7313 constExpr, overflow : BOOLEAN ;
7314 op : QuadOperator ;
7315 BEGIN
7316 GetQuadOtok (quad, combined, op,
7317 left, right, dest, overflow, constExpr,
7318 leftpos, rightpos, destpos) ;
7319 location := TokenToLocation (combined) ;
7320 IF IsConst(left) AND IsConst(right)
7321 THEN
7322 PushValue(left) ;
7323 PushValue(right) ;
7324 IF Gre(combined)
7325 THEN
7326 BuildGoto(location, string(CreateLabelName(dest)))
7327 ELSE
7328 (* fall through *)
7329 END
7330 ELSIF IsConstSet(left) OR (IsVar(left) AND IsSet(SkipType(GetType(left)))) OR
7331 IsConstSet(right) OR (IsVar(right) AND IsSet(SkipType(GetType(right))))
7332 THEN
7333 CodeIfSetGre(quad, left, right, dest)
7334 ELSE
7335 IF IsComposite(GetType(left)) OR IsComposite(GetType(right))
7336 THEN
7337 MetaErrorT2 (combined,
7338 'comparison tests between composite types not allowed {%1Eatd} and {%2atd}',
7339 left, right)
7340 ELSE
7341 ConvertBinaryOperands(location,
7342 tl, tr,
7343 ComparisonMixTypes (left, right,
7344 SkipType (GetType (left)),
7345 SkipType (GetType (right)),
7346 combined),
7347 left, right) ;
7348 DoJump(location, BuildGreaterThan(location, tl, tr), NIL, string(CreateLabelName(dest)))
7349 END
7350 END
7351 END PerformCodeIfGre ;
7352
7353
7354 (*
7355 CodeIfGre - codes the quadruple if op1 > op2 then goto op3
7356 *)
7357
7358 PROCEDURE CodeIfGre (quad: CARDINAL) ;
7359 BEGIN
7360 IF IsValidExpressionRelOp (quad, FALSE)
7361 THEN
7362 PerformCodeIfGre (quad)
7363 END
7364 END CodeIfGre ;
7365
7366
7367 (*
7368 CodeIfSetLessEqu -
7369 *)
7370
7371 PROCEDURE CodeIfSetLessEqu (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
7372 VAR
7373 settype : CARDINAL ;
7374 falselabel: ADDRESS ;
7375 location : location_t ;
7376 BEGIN
7377 location := TokenToLocation(CurrentQuadToken) ;
7378
7379 IF IsConst(op1) AND IsConst(op2)
7380 THEN
7381 InternalError ('this should have been folded in the calling procedure')
7382 ELSIF IsConst(op1)
7383 THEN
7384 settype := SkipType(GetType(op2))
7385 ELSE
7386 settype := SkipType(GetType(op1))
7387 END ;
7388 IF CompareTrees(FindSize(CurrentQuadToken, settype), FindSize(CurrentQuadToken, Word)) <= 0
7389 THEN
7390 (* word size sets *)
7391 DoJump(location,
7392 BuildIsSubset(location,
7393 BuildConvert(location, GetWordType(), Mod2Gcc(op1), FALSE),
7394 BuildConvert(location, GetWordType(), Mod2Gcc(op2), FALSE)),
7395 NIL, string(CreateLabelName(op3)))
7396 ELSE
7397 falselabel := string(Sprintf1(Mark(InitString('.Lset%dcomp')), quad)) ;
7398
7399 BuildForeachWordInSetDoIfExpr(location,
7400 Mod2Gcc(settype),
7401 Mod2Gcc(op1), Mod2Gcc(op2),
7402 GetMode(op1)=LeftValue,
7403 GetMode(op2)=LeftValue,
7404 IsConst(op1), IsConst(op2),
7405 BuildIsNotSubset,
7406 falselabel) ;
7407
7408 BuildGoto(location, string(CreateLabelName(op3))) ;
7409 DeclareLabel(location, falselabel)
7410 END
7411 END CodeIfSetLessEqu ;
7412
7413
7414 (*
7415 PerformCodeIfLessEqu - codes the quadruple if op1 <= op2 then goto op3
7416 *)
7417
7418 PROCEDURE PerformCodeIfLessEqu (quad: CARDINAL) ;
7419 VAR
7420 tl, tr : tree ;
7421 location : location_t ;
7422 left, right, dest, combined,
7423 leftpos, rightpos, destpos : CARDINAL ;
7424 constExpr, overflow : BOOLEAN ;
7425 op : QuadOperator ;
7426 BEGIN
7427 GetQuadOtok (quad, combined, op,
7428 left, right, dest,
7429 overflow, constExpr,
7430 leftpos, rightpos, destpos) ;
7431 location := TokenToLocation (combined) ;
7432 IF IsConst(left) AND IsConst(right)
7433 THEN
7434 PushValue(left) ;
7435 PushValue(right) ;
7436 IF LessEqu(combined)
7437 THEN
7438 BuildGoto(location, string(CreateLabelName(dest)))
7439 ELSE
7440 (* fall through *)
7441 END
7442 ELSIF IsConstSet (left) OR (IsVar (left) AND IsSet (SkipType (GetType (left)))) OR
7443 IsConstSet (right) OR (IsVar (right) AND IsSet (SkipType (GetType (right))))
7444 THEN
7445 CodeIfSetLessEqu (quad, left, right, dest)
7446 ELSE
7447 IF IsComposite (GetType (left)) OR IsComposite (GetType (right))
7448 THEN
7449 MetaErrorT2 (combined,
7450 'comparison tests between composite types not allowed {%1Eatd} and {%2atd}',
7451 left, right)
7452 ELSE
7453 ConvertBinaryOperands (location,
7454 tl, tr,
7455 ComparisonMixTypes (left, right,
7456 SkipType (GetType (left)),
7457 SkipType (GetType (right)),
7458 combined),
7459 left, right) ;
7460 DoJump (location, BuildLessThanOrEqual (location, tl, tr),
7461 NIL, string (CreateLabelName (dest)))
7462 END
7463 END
7464 END PerformCodeIfLessEqu ;
7465
7466
7467 (*
7468 CodeIfLessEqu - codes the quadruple if op1 <= op2 then goto op3
7469 *)
7470
7471 PROCEDURE CodeIfLessEqu (quad: CARDINAL) ;
7472 BEGIN
7473 IF IsValidExpressionRelOp (quad, FALSE)
7474 THEN
7475 PerformCodeIfLessEqu (quad)
7476 END
7477 END CodeIfLessEqu ;
7478
7479
7480 (*
7481 CodeIfSetGreEqu -
7482 *)
7483
7484 PROCEDURE CodeIfSetGreEqu (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
7485 VAR
7486 settype : CARDINAL ;
7487 falselabel: ADDRESS ;
7488 location: location_t ;
7489 BEGIN
7490 location := TokenToLocation(CurrentQuadToken) ;
7491
7492 IF IsConst(op1) AND IsConst(op2)
7493 THEN
7494 InternalError ('this should have been folded in the calling procedure')
7495 ELSIF IsConst(op1)
7496 THEN
7497 settype := SkipType(GetType(op2))
7498 ELSE
7499 settype := SkipType(GetType(op1))
7500 END ;
7501 IF CompareTrees(FindSize(CurrentQuadToken, settype), FindSize(CurrentQuadToken, Word)) <= 0
7502 THEN
7503 (* word size sets *)
7504 DoJump(location,
7505 BuildIsSuperset(location,
7506 BuildConvert(location, GetWordType(), Mod2Gcc(op1), FALSE),
7507 BuildConvert(location, GetWordType(), Mod2Gcc(op2), FALSE)),
7508 NIL, string(CreateLabelName(op3)))
7509 ELSE
7510 falselabel := string(Sprintf1(Mark(InitString('.Lset%dcomp')), quad)) ;
7511
7512 BuildForeachWordInSetDoIfExpr(location,
7513 Mod2Gcc(settype),
7514 Mod2Gcc(op1), Mod2Gcc(op2),
7515 GetMode(op1)=LeftValue,
7516 GetMode(op2)=LeftValue,
7517 IsConst(op1), IsConst(op2),
7518 BuildIsNotSuperset,
7519 falselabel) ;
7520
7521 BuildGoto(location, string(CreateLabelName(op3))) ;
7522 DeclareLabel(location, falselabel)
7523 END
7524 END CodeIfSetGreEqu ;
7525
7526
7527 (*
7528 PerformCodeIfGreEqu - codes the quadruple if op1 >= op2 then goto op3
7529 *)
7530
7531 PROCEDURE PerformCodeIfGreEqu (quad: CARDINAL) ;
7532 VAR
7533 tl, tr: tree ;
7534 location : location_t ;
7535 left, right, dest, combined,
7536 leftpos, rightpos, destpos : CARDINAL ;
7537 constExpr, overflow : BOOLEAN ;
7538 op : QuadOperator ;
7539 BEGIN
7540 GetQuadOtok (quad, combined, op,
7541 left, right, dest,
7542 overflow, constExpr,
7543 leftpos, rightpos, destpos) ;
7544 location := TokenToLocation (combined) ;
7545 IF IsConst(left) AND IsConst(right)
7546 THEN
7547 PushValue(left) ;
7548 PushValue(right) ;
7549 IF GreEqu(combined)
7550 THEN
7551 BuildGoto(location, string(CreateLabelName(dest)))
7552 ELSE
7553 (* fall through *)
7554 END
7555 ELSIF IsConstSet(left) OR (IsVar(left) AND IsSet(SkipType(GetType(left)))) OR
7556 IsConstSet(right) OR (IsVar(right) AND IsSet(SkipType(GetType(right))))
7557 THEN
7558 CodeIfSetGreEqu(quad, left, right, dest)
7559 ELSE
7560 IF IsComposite(GetType(left)) OR IsComposite(GetType(right))
7561 THEN
7562 MetaErrorT2 (combined,
7563 'comparison tests between composite types not allowed {%1Eatd} and {%2atd}',
7564 left, right)
7565 ELSE
7566 ConvertBinaryOperands(location,
7567 tl, tr,
7568 ComparisonMixTypes (left, right,
7569 SkipType (GetType (left)),
7570 SkipType (GetType (right)),
7571 combined),
7572 left, right) ;
7573 DoJump(location, BuildGreaterThanOrEqual(location, tl, tr), NIL, string(CreateLabelName(dest)))
7574 END
7575 END
7576 END PerformCodeIfGreEqu ;
7577
7578
7579 (*
7580 CodeIfGreEqu - codes the quadruple if op1 >= op2 then goto op3
7581 *)
7582
7583 PROCEDURE CodeIfGreEqu (quad: CARDINAL) ;
7584 BEGIN
7585 IF IsValidExpressionRelOp (quad, FALSE)
7586 THEN
7587 PerformCodeIfGreEqu (quad)
7588 END
7589 END CodeIfGreEqu ;
7590
7591
7592 (*
7593 CodeIfSetEqu - codes if op1 = op2 then goto op3
7594 Note that if op1 and op2 are not both constants
7595 since this will have been evaluated in CodeIfEqu.
7596 *)
7597
7598 PROCEDURE CodeIfSetEqu (quad: CARDINAL; op1, op2, op3: CARDINAL) ;
7599 VAR
7600 settype : CARDINAL ;
7601 falselabel: ADDRESS ;
7602 location : location_t ;
7603 BEGIN
7604 location := TokenToLocation(CurrentQuadToken) ;
7605
7606 IF IsConst(op1) AND IsConst(op2)
7607 THEN
7608 InternalError ('this should have been folded in the calling procedure')
7609 ELSIF IsConst(op1)
7610 THEN
7611 settype := SkipType(GetType(op2))
7612 ELSE
7613 settype := SkipType(GetType(op1))
7614 END ;
7615 IF CompareTrees(FindSize(CurrentQuadToken, settype), FindSize(CurrentQuadToken, Word)) <= 0
7616 THEN
7617 (* word size sets *)
7618 DoJump(location,
7619 BuildEqualTo(location,
7620 BuildConvert(location, GetWordType(), Mod2Gcc(op1), FALSE),
7621 BuildConvert(location, GetWordType(), Mod2Gcc(op2), FALSE)),
7622 NIL, string(CreateLabelName(op3)))
7623 ELSIF GetSType(op1)=GetSType(op2)
7624 THEN
7625 falselabel := string(Sprintf1(Mark(InitString('.Lset%dcomp')), quad)) ;
7626
7627 BuildForeachWordInSetDoIfExpr(location,
7628 Mod2Gcc(settype),
7629 Mod2Gcc(op1), Mod2Gcc(op2),
7630 GetMode(op1)=LeftValue,
7631 GetMode(op2)=LeftValue,
7632 IsConst(op1), IsConst(op2),
7633 BuildNotEqualTo,
7634 falselabel) ;
7635
7636 BuildGoto(location, string(CreateLabelName(op3))) ;
7637 DeclareLabel(location, falselabel)
7638 ELSE
7639 MetaErrorT2 (CurrentQuadToken,
7640 'set comparison is only allowed between the same set type, the set types used by {%1Eatd} and {%2atd} are different',
7641 op1, op2)
7642 END
7643 END CodeIfSetEqu ;
7644
7645
7646 (*
7647 CodeIfSetNotEqu - codes if op1 # op2 then goto op3
7648 Note that if op1 and op2 are not both constants
7649 since this will have been evaluated in CodeIfNotEqu.
7650 *)
7651
7652 PROCEDURE CodeIfSetNotEqu (left, right, destQuad: CARDINAL) ;
7653 VAR
7654 settype : CARDINAL ;
7655 truelabel: ADDRESS ;
7656 location : location_t ;
7657 BEGIN
7658 location := TokenToLocation(CurrentQuadToken) ;
7659
7660 IF IsConst (left) AND IsConst (right)
7661 THEN
7662 InternalError ('this should have been folded in the calling procedure')
7663 ELSIF IsConst (left)
7664 THEN
7665 settype := SkipType (GetType (right))
7666 ELSE
7667 settype := SkipType (GetType (left))
7668 END ;
7669 IF CompareTrees (FindSize (CurrentQuadToken, settype), FindSize (CurrentQuadToken, Word)) <= 0
7670 THEN
7671 (* word size sets *)
7672 DoJump (location,
7673 BuildNotEqualTo(location,
7674 BuildConvert (location, GetWordType (), Mod2Gcc (left), FALSE),
7675 BuildConvert (location, GetWordType (), Mod2Gcc (right), FALSE)),
7676 NIL, string (CreateLabelName (destQuad)))
7677 ELSIF GetSType (left) = GetSType (right)
7678 THEN
7679 truelabel := string (CreateLabelName (destQuad)) ;
7680
7681 BuildForeachWordInSetDoIfExpr (location,
7682 Mod2Gcc (settype),
7683 Mod2Gcc (left), Mod2Gcc (right),
7684 GetMode (left) = LeftValue,
7685 GetMode (right) = LeftValue,
7686 IsConst (left), IsConst (right),
7687 BuildNotEqualTo,
7688 truelabel)
7689 ELSE
7690 MetaErrorT2 (CurrentQuadToken,
7691 'set comparison is only allowed between the same set type, the set types used by {%1Eatd} and {%2atd} are different',
7692 left, right)
7693 END
7694 END CodeIfSetNotEqu ;
7695
7696
7697 (*
7698 ComparisonMixTypes -
7699 *)
7700
7701 PROCEDURE ComparisonMixTypes (varleft, varright, left, right: CARDINAL; tokpos: CARDINAL) : CARDINAL ;
7702 BEGIN
7703 IF IsGenericSystemType (left)
7704 THEN
7705 RETURN left
7706 ELSIF IsGenericSystemType (right)
7707 THEN
7708 RETURN right
7709 ELSE
7710 RETURN MixTypesDecl (varleft, varright, left, right, tokpos)
7711 END
7712 END ComparisonMixTypes ;
7713
7714
7715 (*
7716 PerformCodeIfEqu -
7717 *)
7718
7719 PROCEDURE PerformCodeIfEqu (quad: CARDINAL) ;
7720 VAR
7721 tl, tr : tree ;
7722 location : location_t ;
7723 left, right, dest, combined,
7724 leftpos, rightpos, destpos : CARDINAL ;
7725 constExpr, overflow : BOOLEAN ;
7726 op : QuadOperator ;
7727 BEGIN
7728 GetQuadOtok (quad, combined, op,
7729 left, right, dest,
7730 overflow, constExpr,
7731 leftpos, rightpos, destpos) ;
7732 location := TokenToLocation (combined) ;
7733 IF IsConst (left) AND IsConst (right)
7734 THEN
7735 PushValue (left) ;
7736 PushValue (right) ;
7737 IF Equ (combined)
7738 THEN
7739 BuildGoto (location, string (CreateLabelName (dest)))
7740 ELSE
7741 (* Fall through. *)
7742 END
7743 ELSIF IsConstSet (left) OR (IsVar (left) AND IsSet (SkipType (GetType (left)))) OR
7744 IsConstSet (right) OR (IsVar (right) AND IsSet (SkipType (GetType (right))))
7745 THEN
7746 CodeIfSetEqu (quad, left, right, dest)
7747 ELSE
7748 IF IsComposite (GetType (left)) OR IsComposite (GetType (right))
7749 THEN
7750 MetaErrorT2 (combined,
7751 'equality tests between composite types not allowed {%1Eatd} and {%2atd}',
7752 left, right)
7753 ELSE
7754 ConvertBinaryOperands (location,
7755 tl, tr,
7756 ComparisonMixTypes (left, right,
7757 SkipType (GetType (left)),
7758 SkipType (GetType (right)),
7759 combined),
7760 left, right) ;
7761 DoJump (location, BuildEqualTo (location, tl, tr), NIL,
7762 string (CreateLabelName (dest)))
7763 END
7764 END
7765 END PerformCodeIfEqu ;
7766
7767
7768 (*
7769 PerformCodeIfNotEqu -
7770 *)
7771
7772 PROCEDURE PerformCodeIfNotEqu (quad: CARDINAL) ;
7773 VAR
7774 tl, tr : tree ;
7775 location : location_t ;
7776 left, right, dest, combined,
7777 leftpos, rightpos, destpos : CARDINAL ;
7778 constExpr, overflow : BOOLEAN ;
7779 op : QuadOperator ;
7780 BEGIN
7781 (* Ensure that any remaining undeclared constant literal is declared. *)
7782 GetQuadOtok (quad, combined, op,
7783 left, right, dest,
7784 constExpr, overflow,
7785 leftpos, rightpos, destpos) ;
7786 location := TokenToLocation (combined) ;
7787 IF IsConst (left) AND IsConst (right)
7788 THEN
7789 PushValue (left) ;
7790 PushValue (right) ;
7791 IF NotEqu (combined)
7792 THEN
7793 BuildGoto (location, string (CreateLabelName (dest)))
7794 ELSE
7795 (* Fall through. *)
7796 END
7797 ELSIF IsConstSet (left) OR (IsVar (left) AND IsSet (SkipType (GetType (left)))) OR
7798 IsConstSet (right) OR (IsVar (right) AND IsSet (SkipType (GetType (right))))
7799 THEN
7800 CodeIfSetNotEqu (left, right, dest)
7801 ELSE
7802 IF IsComposite (GetType (left)) OR IsComposite (GetType (right))
7803 THEN
7804 MetaErrorT2 (combined,
7805 'inequality tests between composite types not allowed {%1Eatd} and {%2atd}',
7806 left, right)
7807 ELSE
7808 ConvertBinaryOperands (location,
7809 tl, tr,
7810 ComparisonMixTypes (left, right,
7811 SkipType (GetType (left)),
7812 SkipType (GetType (right)),
7813 combined),
7814 left, right) ;
7815 DoJump (location, BuildNotEqualTo (location, tl, tr), NIL,
7816 string (CreateLabelName (dest)))
7817 END
7818 END
7819 END PerformCodeIfNotEqu ;
7820
7821
7822 (*
7823 IsValidExpressionRelOp - declare left and right constants (if they are not already declared).
7824 Check whether left and right are expression compatible.
7825 *)
7826
7827 PROCEDURE IsValidExpressionRelOp (quad: CARDINAL; isin: BOOLEAN) : BOOLEAN ;
7828 CONST
7829 Verbose = FALSE ;
7830 VAR
7831 left, right, dest, combined,
7832 leftpos, rightpos, destpos : CARDINAL ;
7833 constExpr, overflow : BOOLEAN ;
7834 op : QuadOperator ;
7835 BEGIN
7836 (* Ensure that any remaining undeclared constant literal is declared. *)
7837 GetQuadOtok (quad, combined, op,
7838 left, right, dest,
7839 constExpr, overflow,
7840 leftpos, rightpos, destpos) ;
7841 DeclareConstant (leftpos, left) ;
7842 DeclareConstant (rightpos, right) ;
7843 DeclareConstructor (leftpos, quad, left) ;
7844 DeclareConstructor (rightpos, quad, right) ;
7845 IF ExpressionTypeCompatible (combined, "", left, right,
7846 StrictTypeChecking, isin)
7847 THEN
7848 RETURN TRUE
7849 ELSE
7850 IF Verbose
7851 THEN
7852 MetaErrorT2 (combined,
7853 'expression mismatch between {%1Etad} and {%2tad} seen during comparison',
7854 left, right)
7855 END ;
7856 RETURN FALSE
7857 END
7858 END IsValidExpressionRelOp ;
7859
7860
7861 (*
7862 CodeIfEqu - codes the quadruple if op1 = op2 then goto op3
7863 *)
7864
7865 PROCEDURE CodeIfEqu (quad: CARDINAL) ;
7866 BEGIN
7867 IF IsValidExpressionRelOp (quad, FALSE)
7868 THEN
7869 PerformCodeIfEqu (quad)
7870 END
7871 END CodeIfEqu ;
7872
7873
7874 (*
7875 CodeIfNotEqu - codes the quadruple if op1 # op2 then goto op3
7876 *)
7877
7878 PROCEDURE CodeIfNotEqu (quad: CARDINAL) ;
7879 BEGIN
7880 IF IsValidExpressionRelOp (quad, FALSE)
7881 THEN
7882 PerformCodeIfNotEqu (quad)
7883 END
7884 END CodeIfNotEqu ;
7885
7886
7887 (*
7888 MixTypes3 - returns a type compatible from, low, high, var.
7889 *)
7890
7891 PROCEDURE MixTypes3 (low, high, var: CARDINAL; tokenno: CARDINAL) : CARDINAL ;
7892 VAR
7893 type: CARDINAL ;
7894 BEGIN
7895 type := MixTypes(SkipType(GetType(low)), SkipType(GetType(high)), tokenno) ;
7896 type := MixTypes(type, SkipType(GetType(var)), tokenno) ;
7897 RETURN( type )
7898 END MixTypes3 ;
7899
7900
7901 (*
7902 BuildIfVarInConstValue - if var in constsetvalue then goto trueexit
7903 *)
7904
7905 PROCEDURE BuildIfVarInConstValue (location: location_t; tokenno: CARDINAL;
7906 constsetvalue: PtrToValue; var, trueexit: CARDINAL) ;
7907 VAR
7908 vt, lt, ht : tree ;
7909 type,
7910 low, high, n: CARDINAL ;
7911 truelabel : String ;
7912 BEGIN
7913 n := 1 ;
7914 truelabel := string(CreateLabelName(trueexit)) ;
7915 WHILE GetRange(constsetvalue, n, low, high) DO
7916 type := MixTypes3(low, high, var, tokenno) ;
7917 ConvertBinaryOperands(location, vt, lt, type, var, low) ;
7918 ConvertBinaryOperands(location, ht, lt, type, high, low) ;
7919 BuildIfInRangeGoto(location, vt, lt, ht, truelabel) ;
7920 INC(n)
7921 END
7922 END BuildIfVarInConstValue ;
7923
7924
7925 (*
7926 BuildIfNotVarInConstValue - if not (var in constsetvalue) then goto trueexit
7927 *)
7928
7929 PROCEDURE BuildIfNotVarInConstValue (quad: CARDINAL; constsetvalue: PtrToValue; var, trueexit: CARDINAL) ;
7930 VAR
7931 vt, lt, ht : tree ;
7932 type,
7933 low, high, n: CARDINAL ;
7934 falselabel,
7935 truelabel : String ;
7936 location : location_t ;
7937 BEGIN
7938 location := TokenToLocation(CurrentQuadToken) ;
7939
7940 truelabel := string(CreateLabelName(trueexit)) ;
7941 n := 1 ;
7942 WHILE GetRange(constsetvalue, n, low, high) DO
7943 INC(n)
7944 END ;
7945 IF n=2
7946 THEN
7947 (* actually only one set range, so we invert it *)
7948 type := MixTypes3(low, high, var, CurrentQuadToken) ;
7949 ConvertBinaryOperands(location, vt, lt, type, var, low) ;
7950 ConvertBinaryOperands(location, ht, lt, type, high, low) ;
7951 BuildIfNotInRangeGoto(location, vt, lt, ht, truelabel)
7952 ELSE
7953 n := 1 ;
7954 falselabel := string(Sprintf1(Mark(InitString('.Lset%d')), quad)) ;
7955 WHILE GetRange(constsetvalue, n, low, high) DO
7956 type := MixTypes3(low, high, var, CurrentQuadToken) ;
7957 ConvertBinaryOperands(location, vt, lt, type, var, low) ;
7958 ConvertBinaryOperands(location, ht, lt, type, high, low) ;
7959 BuildIfInRangeGoto(location, vt, lt, ht, falselabel) ;
7960 INC(n)
7961 END ;
7962 BuildGoto(location, truelabel) ;
7963 DeclareLabel(location, falselabel)
7964 END
7965 END BuildIfNotVarInConstValue ;
7966
7967
7968 (*
7969 PerformCodeIfIn - code the quadruple: if op1 in op2 then goto op3
7970 *)
7971
7972 PROCEDURE PerformCodeIfIn (quad: CARDINAL) ;
7973 VAR
7974 low,
7975 high : CARDINAL ;
7976 lowtree,
7977 hightree,
7978 offset : tree ;
7979 fieldno : INTEGER ;
7980 location : location_t ;
7981 left, right, dest, combined,
7982 leftpos, rightpos, destpos : CARDINAL ;
7983 constExpr, overflow : BOOLEAN ;
7984 op : QuadOperator ;
7985 BEGIN
7986 (* Ensure that any remaining undeclared constant literal is declared. *)
7987 GetQuadOtok (quad, combined, op,
7988 left, right, dest,
7989 constExpr, overflow,
7990 leftpos, rightpos, destpos) ;
7991 location := TokenToLocation (combined) ;
7992 IF IsConst(left) AND IsConst(right)
7993 THEN
7994 InternalError ('should not get to here (if we do we should consider calling FoldIfIn)')
7995 ELSIF CheckElementSetTypes (quad)
7996 THEN
7997 IF IsConst(left)
7998 THEN
7999 fieldno := GetFieldNo(combined, left, GetType(right), offset) ;
8000 IF fieldno>=0
8001 THEN
8002 PushValue(left) ;
8003 PushIntegerTree(offset) ;
8004 ConvertToType(GetType(left)) ;
8005 Sub ;
8006 BuildIfConstInVar(location,
8007 Mod2Gcc(SkipType(GetType(right))),
8008 Mod2Gcc(right), PopIntegerTree(),
8009 GetMode(right)=LeftValue, fieldno,
8010 string(CreateLabelName(dest)))
8011 ELSE
8012 MetaErrorT1 (combined, 'bit exceeded the range of set {%1Eatd}', left)
8013 END
8014 ELSIF IsConst(right)
8015 THEN
8016 (* builds a cascaded list of if statements *)
8017 PushValue(right) ;
8018 BuildIfVarInConstValue(location, combined, GetValue(combined), left, dest)
8019 ELSE
8020 GetSetLimits(SkipType(GetType(right)), low, high) ;
8021
8022 PushValue(low) ;
8023 lowtree := PopIntegerTree() ;
8024 PushValue(high) ;
8025 hightree := PopIntegerTree() ;
8026
8027 BuildIfVarInVar(location,
8028 Mod2Gcc(SkipType(GetType(right))),
8029 Mod2Gcc(right), Mod2Gcc(left),
8030 GetMode(right)=LeftValue,
8031 lowtree, hightree,
8032 string(CreateLabelName(dest)))
8033 END
8034 END
8035 END PerformCodeIfIn ;
8036
8037
8038 (*
8039 PerformCodeIfNotIn - code the quadruple: if not (op1 in op2) then goto op3
8040 *)
8041
8042 PROCEDURE PerformCodeIfNotIn (quad: CARDINAL) ;
8043 VAR
8044 low,
8045 high : CARDINAL ;
8046 lowtree,
8047 hightree,
8048 offset : tree ;
8049 fieldno : INTEGER ;
8050 location : location_t ;
8051 left, right, dest, combined,
8052 leftpos, rightpos, destpos : CARDINAL ;
8053 constExpr, overflow : BOOLEAN ;
8054 op : QuadOperator ;
8055 BEGIN
8056 (* Ensure that any remaining undeclared constant literal is declared. *)
8057 GetQuadOtok (quad, combined, op,
8058 left, right, dest,
8059 overflow, constExpr,
8060 leftpos, rightpos, destpos) ;
8061 location := TokenToLocation (combined) ;
8062 IF IsConst(left) AND IsConst(right)
8063 THEN
8064 InternalError ('should not get to here (if we do we should consider calling FoldIfIn)')
8065 ELSIF CheckElementSetTypes (quad)
8066 THEN
8067 IF IsConst(left)
8068 THEN
8069 fieldno := GetFieldNo(combined, left, SkipType(GetType(right)), offset) ;
8070 IF fieldno>=0
8071 THEN
8072 PushValue(left) ;
8073 PushIntegerTree(offset) ;
8074 ConvertToType(GetType(left)) ;
8075 Sub ;
8076 BuildIfNotConstInVar(location,
8077 Mod2Gcc(SkipType(GetType(right))),
8078 Mod2Gcc(right), PopIntegerTree(),
8079 GetMode(right)=LeftValue, fieldno,
8080 string(CreateLabelName(dest)))
8081 ELSE
8082 MetaErrorT1 (combined, 'bit exceeded the range of set {%1Eatd}', right)
8083 END
8084 ELSIF IsConst(right)
8085 THEN
8086 (* builds a cascaded list of if statements *)
8087 PushValue(right) ;
8088 BuildIfNotVarInConstValue(quad, GetValue(combined), left, dest)
8089 ELSE
8090 GetSetLimits(SkipType(GetType(right)), low, high) ;
8091
8092 PushValue(low) ;
8093 lowtree := PopIntegerTree() ;
8094 PushValue(high) ;
8095 hightree := PopIntegerTree() ;
8096
8097 BuildIfNotVarInVar(location,
8098 Mod2Gcc(SkipType(GetType(right))),
8099 Mod2Gcc(right), Mod2Gcc(left),
8100 GetMode(right)=LeftValue,
8101 lowtree, hightree,
8102 string(CreateLabelName(dest)))
8103 END
8104 END
8105 END PerformCodeIfNotIn ;
8106
8107
8108 (*
8109 CodeIfIn - code the quadruple: if op1 in op2 then goto op3
8110 *)
8111
8112 PROCEDURE CodeIfIn (quad: CARDINAL) ;
8113 BEGIN
8114 IF IsValidExpressionRelOp (quad, TRUE)
8115 THEN
8116 PerformCodeIfIn (quad)
8117 END
8118 END CodeIfIn ;
8119
8120
8121 (*
8122 CodeIfNotIn - code the quadruple: if not (op1 in op2) then goto op3
8123 *)
8124
8125 PROCEDURE CodeIfNotIn (quad: CARDINAL) ;
8126 BEGIN
8127 IF IsValidExpressionRelOp (quad, TRUE)
8128 THEN
8129 PerformCodeIfNotIn (quad)
8130 END
8131 END CodeIfNotIn ;
8132
8133
8134 (*
8135 ------------------------------------------------------------------------------
8136 IndrX Operator a = *b
8137 ------------------------------------------------------------------------------
8138 Sym1<X> IndrX Sym2<I> Meaning Mem[Sym1<I>] := Mem[constant]
8139 Sym1<X> IndrX Sym2<X> Meaning Mem[Sym1<I>] := Mem[Mem[Sym3<I>]]
8140
8141 (op2 is the type of the data being indirectly copied)
8142 *)
8143
8144 PROCEDURE CodeIndrX (quad: CARDINAL) ;
8145 VAR
8146 constExpr,
8147 overflowChecking: BOOLEAN ;
8148 op : QuadOperator ;
8149 tokenno,
8150 left,
8151 type,
8152 right,
8153 leftpos,
8154 rightpos,
8155 typepos,
8156 indrxpos : CARDINAL ;
8157 length,
8158 newstr : tree ;
8159 location : location_t ;
8160 BEGIN
8161 GetQuadOtok (quad, indrxpos, op, left, type, right,
8162 overflowChecking, constExpr,
8163 leftpos, typepos, rightpos) ;
8164 tokenno := MakeVirtualTok (indrxpos, leftpos, rightpos) ;
8165 location := TokenToLocation (tokenno) ;
8166
8167 (*
8168 Follow the Quadruple rules:
8169 *)
8170 DeclareConstant (rightpos, right) ; (* Checks to see whether it is a constant
8171 and if necessary declare it. *)
8172 DeclareConstructor (rightpos, quad, right) ;
8173 IF IsConstString (right)
8174 THEN
8175 InternalError ('not expecting to index through a constant string')
8176 ELSIF StrictTypeChecking AND
8177 (NOT AssignmentTypeCompatible (indrxpos, "", left, GetType (right), TRUE))
8178 THEN
8179 MetaErrorT2 (tokenno,
8180 'assignment check caught mismatch between {%1Ead} and {%2ad}',
8181 left, right) ;
8182 SubQuad (quad)
8183 ELSE
8184
8185 (*
8186 Mem[op1] := Mem[Mem[op3]]
8187 *)
8188 BuildAssignmentStatement (location, Mod2Gcc (left),
8189 BuildIndirect (location, Mod2Gcc (right), Mod2Gcc (type)))
8190 END
8191 END CodeIndrX ;
8192
8193
8194 (*
8195 CodeXIndr - operands for XIndrOp are: left type right.
8196 *left = right. The second operand is the type of the data being
8197 indirectly copied.
8198 *)
8199
8200 PROCEDURE CodeXIndr (quad: CARDINAL) ;
8201 VAR
8202 constExpr,
8203 overflowChecking: BOOLEAN ;
8204 op : QuadOperator ;
8205 tokenno,
8206 left,
8207 type,
8208 right,
8209 leftpos,
8210 rightpos,
8211 typepos,
8212 xindrpos : CARDINAL ;
8213 length,
8214 newstr : tree ;
8215 location : location_t ;
8216 BEGIN
8217 GetQuadOtok (quad, xindrpos, op, left, type, right,
8218 overflowChecking, constExpr,
8219 leftpos, typepos, rightpos) ;
8220 tokenno := MakeVirtualTok (xindrpos, leftpos, rightpos) ;
8221 location := TokenToLocation (tokenno) ;
8222
8223 type := SkipType (type) ;
8224 DeclareConstant (rightpos, right) ;
8225 DeclareConstructor (rightpos, quad, right) ;
8226 IF StrictTypeChecking AND
8227 (NOT AssignmentTypeCompatible (xindrpos, "", GetType (left), right, TRUE))
8228 THEN
8229 MetaErrorT2 (tokenno,
8230 'assignment check caught mismatch between {%1Ead} and {%2ad}',
8231 left, right) ;
8232 SubQuad (quad)
8233 END ;
8234 IF IsProcType(SkipType(type))
8235 THEN
8236 BuildAssignmentStatement (location, BuildIndirect (location, Mod2Gcc (left), GetPointerType ()), Mod2Gcc (right))
8237 ELSIF IsConstString (right) AND (GetStringLength (rightpos, right) = 0) AND (GetMode (left) = LeftValue)
8238 THEN
8239 (*
8240 no need to check for type errors,
8241 but we handle nul string as a special case as back end
8242 complains if we pass through a "" and ask it to copy the
8243 contents.
8244 *)
8245 BuildAssignmentStatement (location,
8246 BuildIndirect (location, LValueToGenericPtr (location, left), Mod2Gcc (Char)),
8247 StringToChar (Mod2Gcc (right), Char, right))
8248 ELSIF IsConstString (right) AND (SkipTypeAndSubrange (GetType (left)) # Char)
8249 THEN
8250 IF NOT PrepareCopyString (tokenno, length, newstr, right, type)
8251 THEN
8252 MetaErrorT2 (MakeVirtualTok (xindrpos, leftpos, rightpos),
8253 'string constant {%1Ea} is too large to be assigned to the array {%2ad}',
8254 right, left)
8255 END ;
8256 AddStatement (location,
8257 MaybeDebugBuiltinMemcpy (location,
8258 Mod2Gcc (left),
8259 BuildAddr (location, newstr, FALSE),
8260 length))
8261 ELSE
8262 BuildAssignmentStatement (location,
8263 BuildIndirect (location, Mod2Gcc (left), Mod2Gcc (type)),
8264 ConvertRHS (Mod2Gcc (right), type, right))
8265 END
8266 END CodeXIndr ;
8267
8268
8269 (*
8270 InitBuiltinSyms -
8271 *)
8272
8273 PROCEDURE InitBuiltinSyms (tok: CARDINAL) ;
8274 BEGIN
8275 IF Memset = NulSym
8276 THEN
8277 Memset := FromModuleGetSym (tok, MakeKey ('memset'), MakeDefinitionSource (tok, MakeKey ('Builtins')))
8278 END ;
8279 IF Memcpy = NulSym
8280 THEN
8281 Memcpy := FromModuleGetSym (tok, MakeKey ('memcpy'), MakeDefinitionSource (tok, MakeKey ('Builtins')))
8282 END ;
8283 END InitBuiltinSyms ;
8284
8285
8286 BEGIN
8287 Memset := NulSym ;
8288 Memcpy := NulSym ;
8289 UnboundedLabelNo := 0 ;
8290 CurrentQuadToken := 0 ;
8291 ScopeStack := InitStackWord ()
8292 END M2GenGCC.