3 IMPORT S
:= SYSTEM
, stdlib
:= PosixCstdlib
, stdio
:= PosixCstdio
,
4 time
:= PosixCtime
, wctype
:= PosixCwctype
, sysmman
:= PosixCsys_mman
,
5 dlfcn
:= PosixCdlfcn
, types
:= PosixCtypes
, fcntl
:= PosixCfcntl
,
6 unistd
:= PosixCunistd
, signal
:= PosixCsignal
, setjmp
:= PosixCsetjmp
;
9 (* add signal blocking to avoid race conditions in Try/Trap/TrapHandler *)
10 (* add BeepHook for Beep *)
16 timeResolution
* = 1000; (* ticks per second *)
18 processor
* = 10; (* 486 *)
20 objType
* = "ocf"; (* file types *)
24 (* loader constants *)
39 SP
= 4; (* register number of stack pointer *)
40 FP
= 5; (* register number of frame pointer *)
41 ML
= 3; (* register which holds the module list at program start *)
43 strictStackSweep
= FALSE
;
44 N
= 128 DIV 16; (* free lists *)
46 (* kernel flags in module desc *)
47 init
= 16; dyn
= 17; dll
= 24; iptrs
= 30;
49 (* meta interface consts *)
50 mConst
= 1; mTyp
= 2; mVar
= 3; mProc
= 4; mField
= 5;
53 Name
* = ARRAY nameLen
OF CHAR;
54 Utf8Name
* = ARRAY nameLen
OF SHORTCHAR
;
57 Module
* = POINTER TO RECORD [untagged
]
59 opts
-: SET; (* 0..15: compiler opts, 16..31: kernel flags *)
60 refcnt
-: INTEGER; (* <0: module invalidated *)
61 compTime
-, loadTime
-: ARRAY 6 OF SHORTINT;
62 ext
-: INTEGER; (* currently not used *)
63 term
-: Command
; (* terminator *)
64 nofimps
-, nofptrs
-: INTEGER;
65 csize
-, dsize
-, rsize
-: INTEGER;
66 code
-, data
-, refs
-: INTEGER;
67 procBase
-, varBase
-: INTEGER; (* meta base addresses *)
68 names
-: POINTER TO ARRAY [untagged
] OF SHORTCHAR
; (* names[0] = 0X *)
69 ptrs
-: POINTER TO ARRAY [untagged
] OF INTEGER;
70 imports
-: POINTER TO ARRAY [untagged
] OF Module
;
71 export
-: Directory
; (* exported objects (name sorted) *)
75 Type
* = POINTER TO RECORD [untagged
]
76 (* record: ptr to method n at offset - 4 * (n+1) *)
77 size
-: INTEGER; (* record: size, array: #elem, dyn array: 0, proc: sigfp *)
79 id
-: INTEGER; (* name idx * 256 + lev * 16 + attr * 4 + form *)
80 base
-: ARRAY 16 OF Type
; (* signature if form = ProcTyp *)
81 fields
-: Directory
; (* new fields (declaration order) *)
82 ptroffs
-: ARRAY any
OF INTEGER (* array of any length *)
85 Object
* = POINTER TO ObjDesc
;
87 ObjDesc
* = RECORD [untagged
]
89 offs
-: INTEGER; (* pvfprint for record types *)
90 id
-: INTEGER; (* name idx * 256 + vis * 16 + mode *)
91 struct
-: Type (* id of basic type or pointer to typedesc/signature *)
94 Directory
* = POINTER TO RECORD [untagged
]
95 num
-: INTEGER; (* number of entries *)
96 obj
-: ARRAY any
OF ObjDesc (* array of any length *)
99 Signature
* = POINTER TO RECORD [untagged
]
100 retStruct
-: Type
; (* id of basic type or pointer to typedesc or 0 *)
101 num
-: INTEGER; (* number of parameters *)
102 par
-: ARRAY any
OF RECORD [untagged
] (* parameters *)
103 id
-: INTEGER; (* name idx * 256 + kind *)
104 struct
-: Type (* id of basic type or pointer to typedesc *)
108 Handler
* = PROCEDURE;
110 Reducer
* = POINTER TO ABSTRACT
RECORD
114 Identifier
* = ABSTRACT
RECORD
119 TrapCleaner
* = POINTER TO ABSTRACT
RECORD
123 TryHandler
* = PROCEDURE (a
, b
, c
: INTEGER);
125 (* meta extension suport *)
127 ItemExt
* = POINTER TO ABSTRACT
RECORD END;
130 obj
*, vis
*, typ
*, adr
*: INTEGER;
137 Hook
* = POINTER TO ABSTRACT
RECORD END;
139 LoaderHook
* = POINTER TO ABSTRACT
RECORD (Hook
)
141 importing
*, imported
*, object
*: ARRAY 256 OF CHAR
144 Block
= POINTER TO RECORD [untagged
]
146 last
: INTEGER; (* arrays: last element *)
147 actual
: INTEGER; (* arrays: used during mark phase *)
148 first
: INTEGER (* arrays: first element *)
151 FreeBlock
= POINTER TO FreeDesc
;
153 FreeDesc
= RECORD [untagged
]
154 tag
: Type
; (* f.tag = ADR(f.size) *)
159 Cluster
= POINTER TO RECORD [untagged
]
160 size
: INTEGER; (* total size *)
162 max
: INTEGER (* exe: reserved size, dll: original address *)
163 (* start of first block *)
166 FList
= POINTER TO RECORD
172 CList
= POINTER TO RECORD
179 PtrType
= RECORD v
: S
.PTR
END; (* used for array of pointer *)
180 Char8Type
= RECORD v
: SHORTCHAR
END;
181 Char16Type
= RECORD v
: CHAR END;
182 Int8Type
= RECORD v
: BYTE END;
183 Int16Type
= RECORD v
: SHORTINT END;
184 Int32Type
= RECORD v
: INTEGER END;
185 Int64Type
= RECORD v
: LONGINT END;
186 BoolType
= RECORD v
: BOOLEAN END;
187 SetType
= RECORD v
: SET END;
188 Real32Type
= RECORD v
: SHORTREAL
END;
189 Real64Type
= RECORD v
: REAL END;
190 ProcType
= RECORD v
: PROCEDURE END;
191 UPtrType
= RECORD v
: INTEGER END;
192 StrPtr
= POINTER TO ARRAY [untagged
] OF SHORTCHAR
;
194 ArrStrPtr
= POINTER TO ARRAY [untagged
] OF StrPtr
;
196 ADDRESS
* = types
.Pvoid
;
203 err
-, pc
-, sp
-, fp
-, stack
-, val
-: INTEGER;
205 isTry
, checkReadable
: BOOLEAN;
206 startEnv
, checkReadableEnv
: setjmp
.sigjmp_buf
;
207 tryEnv
: setjmp
.jmp_buf
;
211 pagesize
: unistd
.long
;
213 free
: ARRAY N
OF FreeBlock
; (* free list *)
214 sentinelBlock
: FreeDesc
;
216 candidates
: ARRAY 1024 OF INTEGER;
218 allocated
: INTEGER; (* bytes allocated on BlackBox heap *)
219 total
: INTEGER; (* current total size of BlackBox heap *)
220 used
: INTEGER; (* bytes allocated on system heap *)
222 hotFinalizers
: FList
;
225 trapStack
: TrapCleaner
;
226 actual
: Module
; (* valid during module initialization *)
228 trapViewer
, trapChecker
: Handler
;
229 trapped
, guarded
, secondTrap
: BOOLEAN;
230 interrupted
: BOOLEAN;
231 static
, inDll
, terminating
: BOOLEAN;
237 wouldFinalize
: BOOLEAN;
239 watcher
*: PROCEDURE (event
: INTEGER); (* for debugging *)
243 PROCEDURE Erase (adr
, words
: INTEGER);
245 ASSERT(words
>= 0, 20);
254 PROCEDURE (VAR id
: Identifier
) Identified
* (): BOOLEAN, NEW, ABSTRACT
;
255 PROCEDURE (r
: Reducer
) Reduce
* (full
: BOOLEAN), NEW, ABSTRACT
;
256 PROCEDURE (c
: TrapCleaner
) Cleanup
*, NEW, EMPTY
;
258 (* meta extension suport *)
260 PROCEDURE (e
: ItemExt
) Lookup
* (name
: ARRAY OF CHAR; VAR i
: ANYREC
), NEW, ABSTRACT
;
261 PROCEDURE (e
: ItemExt
) Index
* (index
: INTEGER; VAR elem
: ANYREC
), NEW, ABSTRACT
;
262 PROCEDURE (e
: ItemExt
) Deref
* (VAR ref
: ANYREC
), NEW, ABSTRACT
;
264 PROCEDURE (e
: ItemExt
) Valid
* (): BOOLEAN, NEW, ABSTRACT
;
265 PROCEDURE (e
: ItemExt
) Size
* (): INTEGER, NEW, ABSTRACT
;
266 PROCEDURE (e
: ItemExt
) BaseTyp
* (): INTEGER, NEW, ABSTRACT
;
267 PROCEDURE (e
: ItemExt
) Len
* (): INTEGER, NEW, ABSTRACT
;
269 PROCEDURE (e
: ItemExt
) Call
* (OUT ok
: BOOLEAN), NEW, ABSTRACT
;
270 PROCEDURE (e
: ItemExt
) BoolVal
* (): BOOLEAN, NEW, ABSTRACT
;
271 PROCEDURE (e
: ItemExt
) PutBoolVal
* (x
: BOOLEAN), NEW, ABSTRACT
;
272 PROCEDURE (e
: ItemExt
) CharVal
* (): CHAR, NEW, ABSTRACT
;
273 PROCEDURE (e
: ItemExt
) PutCharVal
* (x
: CHAR), NEW, ABSTRACT
;
274 PROCEDURE (e
: ItemExt
) IntVal
* (): INTEGER, NEW, ABSTRACT
;
275 PROCEDURE (e
: ItemExt
) PutIntVal
* (x
: INTEGER), NEW, ABSTRACT
;
276 PROCEDURE (e
: ItemExt
) LongVal
* (): LONGINT, NEW, ABSTRACT
;
277 PROCEDURE (e
: ItemExt
) PutLongVal
* (x
: LONGINT), NEW, ABSTRACT
;
278 PROCEDURE (e
: ItemExt
) RealVal
* (): REAL, NEW, ABSTRACT
;
279 PROCEDURE (e
: ItemExt
) PutRealVal
* (x
: REAL), NEW, ABSTRACT
;
280 PROCEDURE (e
: ItemExt
) SetVal
* (): SET, NEW, ABSTRACT
;
281 PROCEDURE (e
: ItemExt
) PutSetVal
* (x
: SET), NEW, ABSTRACT
;
282 PROCEDURE (e
: ItemExt
) PtrVal
* (): ANYPTR
, NEW, ABSTRACT
;
283 PROCEDURE (e
: ItemExt
) PutPtrVal
* (x
: ANYPTR
), NEW, ABSTRACT
;
284 PROCEDURE (e
: ItemExt
) GetSStringVal
* (OUT x
: ARRAY OF SHORTCHAR
;
285 OUT ok
: BOOLEAN), NEW, ABSTRACT
;
286 PROCEDURE (e
: ItemExt
) PutSStringVal
* (IN x
: ARRAY OF SHORTCHAR
;
287 OUT ok
: BOOLEAN), NEW, ABSTRACT
;
288 PROCEDURE (e
: ItemExt
) GetStringVal
* (OUT x
: ARRAY OF CHAR; OUT ok
: BOOLEAN), NEW, ABSTRACT
;
289 PROCEDURE (e
: ItemExt
) PutStringVal
* (IN x
: ARRAY OF CHAR; OUT ok
: BOOLEAN), NEW, ABSTRACT
;
291 (* -------------------- miscellaneous tools -------------------- *)
293 PROCEDURE IsUpper
* (ch
: CHAR): BOOLEAN;
295 RETURN wctype
.iswupper(ORD(ch
)) # 0
298 PROCEDURE Upper
* (ch
: CHAR): CHAR;
300 RETURN CHR(wctype
.towupper(ORD(ch
)))
303 PROCEDURE IsLower
* (ch
: CHAR): BOOLEAN;
305 RETURN wctype
.iswlower(ORD(ch
)) # 0
308 PROCEDURE Lower
* (ch
: CHAR): CHAR;
310 RETURN CHR(wctype
.towlower(ORD(ch
)))
313 PROCEDURE IsAlpha
* (ch
: CHAR): BOOLEAN;
315 RETURN wctype
.iswalpha(ORD(ch
)) # 0
318 PROCEDURE Utf8ToString
* (IN in
: ARRAY OF SHORTCHAR
; OUT out
: ARRAY OF CHAR; OUT res
: INTEGER);
319 VAR i
, j
, val
, max
: INTEGER; ch
: SHORTCHAR
;
321 PROCEDURE FormatError();
322 BEGIN out
:= in$
; res
:= 2 (*format error*)
326 ch
:= in
[0]; i
:= 1; j
:= 0; max
:= LEN(out
) - 1;
327 WHILE (ch
# 0X
) & (j
< max
) DO
331 val
:= ORD(ch
) - 192;
332 IF val
< 0 THEN FormatError
; RETURN END ;
333 ch
:= in
[i
]; INC(i
); val
:= val
* 64 + ORD(ch
) - 128;
334 IF (ch
< 80X
) OR (ch
>= 0E0X
) THEN FormatError
; RETURN END ;
335 out
[j
] := CHR(val
); INC(j
)
337 val
:= ORD(ch
) - 224;
338 ch
:= in
[i
]; INC(i
); val
:= val
* 64 + ORD(ch
) - 128;
339 IF (ch
< 80X
) OR (ch
>= 0E0X
) THEN FormatError
; RETURN END ;
340 ch
:= in
[i
]; INC(i
); val
:= val
* 64 + ORD(ch
) - 128;
341 IF (ch
< 80X
) OR (ch
>= 0E0X
) THEN FormatError
; RETURN END ;
342 out
[j
] := CHR(val
); INC(j
)
349 IF ch
= 0X
THEN res
:= 0 (*ok*) ELSE res
:= 1 (*truncated*) END
352 PROCEDURE StringToUtf8
* (IN in
: ARRAY OF CHAR; OUT out
: ARRAY OF SHORTCHAR
; OUT res
: INTEGER);
353 VAR i
, j
, val
, max
: INTEGER;
355 i
:= 0; j
:= 0; max
:= LEN(out
) - 3;
356 WHILE (in
[i
] # 0X
) & (j
< max
) DO
357 val
:= ORD(in
[i
]); INC(i
);
359 out
[j
] := SHORT(CHR(val
)); INC(j
)
360 ELSIF val
< 2048 THEN
361 out
[j
] := SHORT(CHR(val
DIV 64 + 192)); INC(j
);
362 out
[j
] := SHORT(CHR(val
MOD 64 + 128)); INC(j
)
364 out
[j
] := SHORT(CHR(val
DIV 4096 + 224)); INC(j
);
365 out
[j
] := SHORT(CHR(val
DIV 64 MOD 64 + 128)); INC(j
);
366 out
[j
] := SHORT(CHR(val
MOD 64 + 128)); INC(j
)
370 IF in
[i
] = 0X
THEN res
:= 0 (*ok*) ELSE res
:= 1 (*truncated*) END
373 PROCEDURE SplitName
* (name
: ARRAY OF CHAR; VAR head
, tail
: ARRAY OF CHAR);
375 VAR i
, j
: INTEGER; ch
, lch
: CHAR;
377 i
:= 0; ch
:= name
[0];
380 head
[i
] := ch
; lch
:= ch
; INC(i
); ch
:= name
[i
]
381 UNTIL (ch
= 0X
) OR (ch
= ".") OR IsUpper(ch
) & ~
IsUpper(lch
);
382 IF ch
= "." THEN i
:= 0; ch
:= name
[0] END;
383 head
[i
] := 0X
; j
:= 0;
384 WHILE ch
# 0X
DO tail
[j
] := ch
; INC(i
); INC(j
); ch
:= name
[i
] END;
386 IF tail
= "" THEN tail
:= head$
; head
:= "" END
387 ELSE head
:= ""; tail
:= ""
391 PROCEDURE MakeFileName
* (VAR name
: ARRAY OF CHAR; type
: ARRAY OF CHAR);
392 VAR i
, j
: INTEGER; ext
: ARRAY 8 OF CHAR; ch
: CHAR;
395 WHILE (name
[i
] # 0X
) & (name
[i
] # ".") DO INC(i
) END;
396 IF name
[i
] = "." THEN
397 IF name
[i
+ 1] = 0X
THEN name
[i
] := 0X
END
399 IF type
= "" THEN ext
:= docType
ELSE ext
:= type$
END;
400 IF i
< LEN(name
) - LEN(ext$
) - 1 THEN
401 name
[i
] := "."; INC(i
); j
:= 0; ch
:= ext
[0];
403 name
[i
] := Lower(ch
); INC(i
); INC(j
); ch
:= ext
[j
]
410 PROCEDURE Time
* (): LONGINT;
411 VAR res
: time
.int
; tp
: time
.struct_timespec
;
413 ASSERT(timeResolution
>= 1);
414 ASSERT(timeResolution
<= 1000000000);
415 res
:= time
.clock_gettime(time
.CLOCK_MONOTONIC
, tp
);
416 ASSERT(res
= 0, 100);
417 RETURN tp
.tv_sec
* LONG(timeResolution
) + tp
.tv_nsec
DIV LONG(1000000000 DIV timeResolution
)
424 PROCEDURE SearchProcVar
* (var
: INTEGER; VAR m
: Module
; VAR adr
: INTEGER);
426 adr
:= var
; m
:= NIL;
429 WHILE (m
# NIL) & ((var
< m
.code
) OR (var
>= m
.code
+ m
.csize
)) DO m
:= m
.next
END;
430 IF m
# NIL THEN DEC(adr
, m
.code
) END
434 (* -------------------- system memory management --------------------- *)
436 PROCEDURE AllocMem (size
: sysmman
.size_t
; VAR max
: sysmman
.size_t
): ADDRESS
;
437 VAR fd
, flags
, res
: fcntl
.int
; ptr
: ADDRESS
;
439 max
:= (size
+ pagesize
- 1) DIV pagesize
* pagesize
;
440 fd
:= fcntl
.open("/dev/zero", fcntl
.O_RDWR
, 0);
442 flags
:= sysmman
.PROT_READ
+ sysmman
.PROT_WRITE
;
443 ptr
:= sysmman
.mmap(0, max
, flags
, sysmman
.MAP_PRIVATE
, fd
, 0);
444 IF ptr
= sysmman
.MAP_FAILED
THEN ptr
:= 0 END;
445 res
:= unistd
.close(fd
);
453 PROCEDURE FreeMem (adr
: ADDRESS
; size
: sysmman
.size_t
);
456 size
:= (size
+ pagesize
- 1) DIV pagesize
* pagesize
;
457 res
:= sysmman
.munmap(adr
, size
);
461 PROCEDURE AllocHeapMem (size
: INTEGER; VAR c
: Cluster
);
462 CONST N
= 65536; (* cluster size for dll *)
463 VAR adr
, allocated
, newsize
: INTEGER;
466 ASSERT(size
> 0, 100); adr
:= 0;
468 adr
:= AllocMem(N
, newsize
);
472 adr
:= AllocMem(size
, newsize
);
475 IF adr
= 0 THEN c
:= NIL
477 c
:= S
.VAL(Cluster
, (adr
+ 15) DIV 16 * 16); c
.max
:= adr
;
478 c
.size
:= allocated
- (S
.VAL(INTEGER, c
) - adr
);
479 INC(used
, c
.size
); INC(total
, c
.size
)
481 ASSERT((adr
= 0) OR (adr
MOD 16 = 0) & (c
.size
>= size
), 101);
482 (* post: (c = NIL) OR (c MOD 16 = 0) & (c.size >= size) *)
485 PROCEDURE FreeHeapMem (c
: Cluster
);
487 DEC(used
, c
.size
); DEC(total
, c
.size
);
488 FreeMem(S
.VAL(ADDRESS
, c
.max
), c
.size
)
491 PROCEDURE HeapFull (size
: INTEGER): BOOLEAN;
496 PROCEDURE AllocModMem
* (descSize
, modSize
: INTEGER; VAR descAdr
, modAdr
: INTEGER);
498 descAdr
:= 0; modAdr
:= 0;
499 descAdr
:= AllocMem(descSize
, descSize
);
501 modAdr
:= AllocMem(modSize
, modSize
);
503 FreeMem(descAdr
, descSize
)
505 INC(used
, descSize
+ modSize
)
510 PROCEDURE DeallocModMem
* (descSize
, modSize
, descAdr
, modAdr
: INTEGER);
512 FreeMem(descAdr
, descSize
);
513 FreeMem(modAdr
, modSize
);
514 DEC(used
, descSize
+ modSize
)
517 PROCEDURE InvalModMem (modSize
, modAdr
: INTEGER);
519 FreeMem(modAdr
, modSize
)
522 PROCEDURE IsReadable
* (from
, to
: INTEGER): BOOLEAN;
523 VAR r
: BOOLEAN; jmp
: setjmp
.sigjmp_buf
; res
: setjmp
.int
; i
: INTEGER; x
: BYTE;
526 jmp
:= checkReadableEnv
;
527 checkReadable
:= TRUE
;
528 res
:= setjmp
.sigsetjmp(checkReadableEnv
, 1);
531 FOR i
:= from
TO to
DO
535 FOR i
:= to
TO from BY
-1 DO
540 checkReadableEnv
:= jmp
;
545 (* --------------------- NEW implementation (portable) -------------------- *)
547 PROCEDURE^
NewBlock (size
: INTEGER): Block
;
549 PROCEDURE NewRec
* (typ
: INTEGER): INTEGER; (* implementation of NEW(ptr) *)
550 VAR size
, adr
: INTEGER; b
: Block
; tag
: Type
; l
: FList
;
553 tag
:= S
.VAL(Type
, typ
);
554 b
:= NewBlock(tag
.size
);
557 S
.GET(typ
- 4, size
);
558 IF size
# 0 THEN (* record uses a finalizer *)
559 l
:= S
.VAL(FList
, S
.ADR(b
.last
)); (* anchor new object! *)
560 l
:= S
.VAL(FList
, NewRec(S
.TYP(FList
))); (* NEW(l) *)
561 l
.blk
:= b
; l
.next
:= finalizers
; finalizers
:= l
568 HALT(100) (* COM interface pointers not supported *)
573 PROCEDURE NewArr
* (eltyp
, nofelem
, nofdim
: INTEGER): INTEGER; (* impl. of NEW(ptr, dim0, dim1, ...) *)
574 VAR b
: Block
; size
, headSize
: INTEGER; t
: Type
;
577 |
-1: HALT(100) (* COM interface pointers not supported *)
578 |
0: eltyp
:= S
.ADR(PtrType
)
579 |
1: eltyp
:= S
.ADR(Char8Type
)
580 |
2: eltyp
:= S
.ADR(Int16Type
)
581 |
3: eltyp
:= S
.ADR(Int8Type
)
582 |
4: eltyp
:= S
.ADR(Int32Type
)
583 |
5: eltyp
:= S
.ADR(BoolType
)
584 |
6: eltyp
:= S
.ADR(SetType
)
585 |
7: eltyp
:= S
.ADR(Real32Type
)
586 |
8: eltyp
:= S
.ADR(Real64Type
)
587 |
9: eltyp
:= S
.ADR(Char16Type
)
588 |
10: eltyp
:= S
.ADR(Int64Type
)
589 |
11: eltyp
:= S
.ADR(ProcType
)
590 |
12: eltyp
:= S
.ADR(UPtrType
)
592 ASSERT(~
ODD(eltyp
), 101) (* COM interface pointers not supported *)
594 t
:= S
.VAL(Type
, eltyp
);
595 headSize
:= 4 * nofdim
+ 12;
596 size
:= headSize
+ nofelem
* t
.size
;
599 b
.tag
:= S
.VAL(Type
, eltyp
+ 2); (* tag + array mark *)
600 b
.last
:= S
.ADR(b
.last
) + size
- t
.size
; (* pointer to last elem *)
601 b
.first
:= S
.ADR(b
.last
) + headSize
; (* pointer to first elem *)
608 (* -------------------- handler installation (portable) --------------------- *)
610 PROCEDURE ThisFinObj
* (VAR id
: Identifier
): ANYPTR
;
613 ASSERT(id
.typ
# 0, 100);
616 IF S
.VAL(INTEGER, l
.blk
.tag
) = id
.typ
THEN
617 id
.obj
:= S
.VAL(ANYPTR
, S
.ADR(l
.blk
.last
));
618 IF id
.Identified() THEN RETURN id
.obj
END
625 PROCEDURE InstallReducer
* (r
: Reducer
);
627 r
.next
:= reducers
; reducers
:= r
630 PROCEDURE InstallTrapViewer
* (h
: Handler
);
633 END InstallTrapViewer
;
635 PROCEDURE InstallTrapChecker
* (h
: Handler
);
638 END InstallTrapChecker
;
640 PROCEDURE PushTrapCleaner
* (c
: TrapCleaner
);
643 t
:= trapStack
; WHILE (t
# NIL) & (t
# c
) DO t
:= t
.next
END;
645 c
.next
:= trapStack
; trapStack
:= c
648 PROCEDURE PopTrapCleaner
* (c
: TrapCleaner
);
652 WHILE (trapStack
# NIL) & (t
# c
) DO
653 t
:= trapStack
; trapStack
:= trapStack
.next
657 PROCEDURE InstallCleaner
* (p
: Command
);
660 c
:= S
.VAL(CList
, NewRec(S
.TYP(CList
))); (* NEW(c) *)
661 c
.do
:= p
; c
.trapped
:= FALSE
; c
.next
:= cleaners
; cleaners
:= c
664 PROCEDURE RemoveCleaner
* (p
: Command
);
667 c
:= cleaners
; c0
:= NIL;
668 WHILE (c
# NIL) & (c
.do
# p
) DO c0
:= c
; c
:= c
.next
END;
670 IF c0
= NIL THEN cleaners
:= cleaners
.next
ELSE c0
.next
:= c
.next
END
677 c
:= cleaners
; c0
:= NIL;
680 c
.trapped
:= TRUE
; c
.do
; c
.trapped
:= FALSE
; c0
:= c
682 IF c0
= NIL THEN cleaners
:= cleaners
.next
683 ELSE c0
.next
:= c
.next
690 (* -------------------- meta information (portable) --------------------- *)
692 PROCEDURE (h
: LoaderHook
) ThisMod
* (IN name
: ARRAY OF CHAR): Module
, NEW, ABSTRACT
;
694 PROCEDURE SetLoaderHook
*(h
: LoaderHook
);
699 PROCEDURE InitModule (mod
: Module
); (* initialize linked modules *)
702 IF ~
(dyn
IN mod
.opts
) & (mod
.next
# NIL) & ~
(init
IN mod
.next
.opts
) THEN InitModule(mod
.next
) END;
703 IF ~
(init
IN mod
.opts
) THEN
704 body
:= S
.VAL(Command
, mod
.code
);
705 INCL(mod
.opts
, init
);
707 body(); actual
:= NIL
711 PROCEDURE ThisLoadedMod
* (IN name
: ARRAY OF CHAR): Module
; (* loaded modules only *)
712 VAR m
: Module
; res
: INTEGER; n
: Utf8Name
;
714 StringToUtf8(name
, n
, res
); ASSERT(res
= 0);
717 WHILE (m
# NIL) & ((m
.name
# n
) OR (m
.refcnt
< 0)) DO m
:= m
.next
END;
718 IF (m
# NIL) & ~
(init
IN m
.opts
) THEN InitModule(m
) END;
719 IF m
= NIL THEN loadres
:= moduleNotFound
END;
723 PROCEDURE ThisMod
* (IN name
: ARRAY OF CHAR): Module
;
727 RETURN loader
.ThisMod(name
)
729 RETURN ThisLoadedMod(name
)
733 PROCEDURE LoadMod
* (IN name
: ARRAY OF CHAR);
739 PROCEDURE GetLoaderResult
* (OUT res
: INTEGER; OUT importing
, imported
, object
: ARRAY OF CHAR);
743 importing
:= loader
.importing$
;
744 imported
:= loader
.imported$
;
745 object
:= loader
.object$
754 PROCEDURE ThisObject
* (mod
: Module
; IN name
: ARRAY OF CHAR): Object
;
755 VAR l
, r
, m
, res
: INTEGER; p
: StrPtr
; n
: Utf8Name
;
757 StringToUtf8(name
, n
, res
); ASSERT(res
= 0);
758 l
:= 0; r
:= mod
.export
.num
;
759 WHILE l
< r
DO (* binary search *)
761 p
:= S
.VAL(StrPtr
, S
.ADR(mod
.names
[mod
.export
.obj
[m
].id
DIV 256]));
762 IF p^
= n
THEN RETURN S
.VAL(Object
, S
.ADR(mod
.export
.obj
[m
])) END;
763 IF p^
< n
THEN l
:= m
+ 1 ELSE r
:= m
END
768 PROCEDURE ThisDesc
* (mod
: Module
; fprint
: INTEGER): Object
;
771 i
:= 0; n
:= mod
.export
.num
;
772 WHILE (i
< n
) & (mod
.export
.obj
[i
].id
DIV 256 = 0) DO
773 IF mod
.export
.obj
[i
].offs
= fprint
THEN RETURN S
.VAL(Object
, S
.ADR(mod
.export
.obj
[i
])) END;
779 PROCEDURE ThisField
* (rec
: Type
; IN name
: ARRAY OF CHAR): Object
;
780 VAR n
, res
: INTEGER; p
: StrPtr
; obj
: Object
; m
: Module
; nn
: Utf8Name
;
782 StringToUtf8(name
, nn
, res
); ASSERT(res
= 0);
784 obj
:= S
.VAL(Object
, S
.ADR(rec
.fields
.obj
[0])); n
:= rec
.fields
.num
;
786 p
:= S
.VAL(StrPtr
, S
.ADR(m
.names
[obj
.id
DIV 256]));
787 IF p^
= nn
THEN RETURN obj
END;
788 DEC(n
); INC(S
.VAL(INTEGER, obj
), 16)
793 PROCEDURE ThisCommand
* (mod
: Module
; IN name
: ARRAY OF CHAR): Command
;
794 VAR x
: Object
; sig
: Signature
;
796 x
:= ThisObject(mod
, name
);
797 IF (x
# NIL) & (x
.id
MOD 16 = mProc
) THEN
798 sig
:= S
.VAL(Signature
, x
.struct
);
799 IF (sig
.retStruct
= NIL) & (sig
.num
= 0) THEN RETURN S
.VAL(Command
, mod
.procBase
+ x
.offs
) END
804 PROCEDURE ThisType
* (mod
: Module
; IN name
: ARRAY OF CHAR): Type
;
807 x
:= ThisObject(mod
, name
);
808 IF (x
# NIL) & (x
.id
MOD 16 = mTyp
) & (S
.VAL(INTEGER, x
.struct
) DIV 256 # 0) THEN
815 PROCEDURE TypeOf
* (IN rec
: ANYREC
): Type
;
817 RETURN S
.VAL(Type
, S
.TYP(rec
))
820 PROCEDURE LevelOf
* (t
: Type
): SHORTINT;
822 RETURN SHORT(t
.id
DIV 16 MOD 16)
825 PROCEDURE NewObj
* (VAR o
: S
.PTR
; t
: Type
);
828 IF t
.size
= -1 THEN o
:= NIL
830 i
:= 0; WHILE t
.ptroffs
[i
] >= 0 DO INC(i
) END;
831 IF t
.ptroffs
[i
+1] >= 0 THEN INC(S
.VAL(INTEGER, t
)) END; (* with interface pointers *)
832 o
:= S
.VAL(S
.PTR
, NewRec(S
.VAL(INTEGER, t
))) (* generic NEW *)
836 PROCEDURE GetModName
* (mod
: Module
; OUT name
: Name
);
839 Utf8ToString(mod
.name
, name
, res
); ASSERT(res
= 0)
842 PROCEDURE GetObjName
* (mod
: Module
; obj
: Object
; OUT name
: Name
);
843 VAR p
: StrPtr
; res
: INTEGER;
845 p
:= S
.VAL(StrPtr
, S
.ADR(mod
.names
[obj
.id
DIV 256]));
846 Utf8ToString(p^$
, name
, res
); ASSERT(res
= 0)
849 PROCEDURE GetTypeName
* (t
: Type
; OUT name
: Name
);
850 VAR p
: StrPtr
; res
: INTEGER;
852 p
:= S
.VAL(StrPtr
, S
.ADR(t
.mod
.names
[t
.id
DIV 256]));
853 Utf8ToString(p^$
, name
, res
); ASSERT(res
= 0)
856 PROCEDURE RegisterMod
* (mod
: Module
);
857 VAR i
: INTEGER; epoch
: time
.time_t
; tm
: time
.struct_tm
; ptm
: time
.Pstruct_tm
;
859 mod
.next
:= modList
; modList
:= mod
; mod
.refcnt
:= 0; INCL(mod
.opts
, dyn
); i
:= 0;
860 WHILE i
< mod
.nofimps
DO
861 IF mod
.imports
[i
] # NIL THEN INC(mod
.imports
[i
].refcnt
) END;
864 epoch
:= time
.time(NIL);
865 ptm
:= time
.localtime_r(epoch
, tm
);
867 mod
.loadTime
[0] := SHORT(tm
.tm_year
+ 1900);
868 mod
.loadTime
[1] := SHORT(tm
.tm_mon
+ 1);
869 mod
.loadTime
[2] := SHORT(tm
.tm_mday
);
870 mod
.loadTime
[3] := SHORT(tm
.tm_hour
);
871 mod
.loadTime
[4] := SHORT(tm
.tm_min
);
872 mod
.loadTime
[5] := SHORT(tm
.tm_sec
)
874 mod
.loadTime
[0] := 0;
875 mod
.loadTime
[1] := 0;
876 mod
.loadTime
[2] := 0;
877 mod
.loadTime
[3] := 0;
878 mod
.loadTime
[4] := 0;
881 IF ~
(init
IN mod
.opts
) THEN InitModule(mod
) END
886 PROCEDURE UnloadMod
* (mod
: Module
);
887 VAR i
: INTEGER; t
: Command
;
889 IF mod
.refcnt
= 0 THEN
890 t
:= mod
.term
; mod
.term
:= NIL;
891 IF t
# NIL THEN t() END; (* terminate module *)
893 WHILE i
< mod
.nofptrs
DO (* release global pointers *)
894 S
.PUT(mod
.varBase
+ mod
.ptrs
[i
], 0); INC(i
)
896 Collect
; (* call finalizers *)
898 WHILE i
< mod
.nofimps
DO (* release imported modules *)
899 IF mod
.imports
[i
] # NIL THEN DEC(mod
.imports
[i
].refcnt
) END;
903 IF dyn
IN mod
.opts
THEN (* release memory *)
904 InvalModMem(mod
.data
+ mod
.dsize
- mod
.refs
, mod
.refs
)
909 (* -------------------- dynamic procedure call --------------------- *)
911 PROCEDURE [code
] PUSH (p
: INTEGER) 050H
; (* push AX *)
912 PROCEDURE [code
] CALL (a
: INTEGER) 0FFH
, 0D0H
; (* call AX *)
913 PROCEDURE [code
] RETI (): LONGINT;
914 PROCEDURE [code
] RETR (): REAL;
923 open array length_N-1 .. length_0
926 PROCEDURE Call
* (adr
: INTEGER; sig
: Signature
; IN par
: ARRAY OF INTEGER; n
: INTEGER): LONGINT;
927 VAR p
, kind
, sp
, size
: INTEGER; typ
: Type
; r
: REAL;
930 WHILE p
> 0 DO (* push parameters from right to left *)
932 typ
:= sig
.par
[p
].struct
;
933 kind
:= sig
.par
[p
].id
MOD 16;
934 IF (S
.VAL(INTEGER, typ
) DIV 256 = 0) OR (typ
.id
MOD 4 IN {0, 3}) THEN (* scalar, ANYREC *)
935 IF (kind
= 10) & ((S
.VAL(INTEGER, typ
) = 8) OR (S
.VAL(INTEGER, typ
) = 10)) THEN (* 64 bit *)
936 DEC(n
); PUSH(par
[n
]) (* push hi word *)
937 ELSIF S
.VAL(INTEGER, typ
) = 11 THEN (* ANYREC *)
938 ASSERT(kind
# 10); (* not a value par, also checked by compiler *)
939 DEC(n
); PUSH(par
[n
]) (* push tag *)
941 DEC(n
); PUSH(par
[n
]) (* push value/address *)
942 ELSIF typ
.id
MOD 4 = 1 THEN (* record *)
943 IF kind
# 10 THEN (* var par *)
944 DEC(n
); PUSH(par
[n
]); (* push tag *)
945 DEC(n
); PUSH(par
[n
]) (* push address *)
947 DEC(n
, 2); (* skip tag *)
948 S
.GETREG(SP
, sp
); sp
:= (sp
- typ
.size
) DIV 4 * 4; S
.PUTREG(SP
, sp
); (* allocate space *)
949 S
.MOVE(par
[n
], sp
, typ
.size
) (* copy to stack *)
951 ELSIF typ
.size
= 0 THEN (* open array *)
952 size
:= typ
.id
DIV 16 MOD 16; (* number of open dimensions *)
954 DEC(size
); DEC(n
); PUSH(par
[n
]) (* push length *)
956 DEC(n
); PUSH(par
[n
]) (* push address *)
958 IF kind
# 10 THEN (* var par *)
959 DEC(n
, 2); PUSH(par
[n
]) (* push address *)
961 DEC(n
); size
:= par
[n
]; DEC(n
);
962 S
.GETREG(SP
, sp
); sp
:= (sp
- size
) DIV 4 * 4; S
.PUTREG(SP
, sp
); (* allocate space *)
963 S
.MOVE(par
[n
], sp
, size
) (* copy to stack *)
968 IF S
.VAL(INTEGER, sig
.retStruct
) = 7 THEN (* shortreal *)
970 RETURN S
.VAL(INTEGER, SHORT(RETR())) (* return value in fpu register *)
971 ELSIF S
.VAL(INTEGER, sig
.retStruct
) = 8 THEN (* real *)
972 CALL(adr
); r
:= RETR();
973 RETURN S
.VAL(LONGINT, r
) (* return value in fpu register *)
976 RETURN RETI() (* return value in integer registers *)
980 (* -------------------- reference information (portable) --------------------- *)
982 PROCEDURE RefCh (VAR ref
: INTEGER; OUT ch
: SHORTCHAR
);
984 S
.GET(ref
, ch
); INC(ref
)
987 PROCEDURE RefNum (VAR ref
: INTEGER; OUT x
: INTEGER);
988 VAR s
, n
: INTEGER; ch
: SHORTCHAR
;
990 s
:= 0; n
:= 0; RefCh(ref
, ch
);
991 WHILE ORD(ch
) >= 128 DO INC(n
, ASH(ORD(ch
) - 128, s
) ); INC(s
, 7); RefCh(ref
, ch
) END;
992 x
:= n
+ ASH(ORD(ch
) MOD 64 - ORD(ch
) DIV 64 * 64, s
)
995 PROCEDURE RefName (VAR ref
: INTEGER; OUT n
: Utf8Name
);
996 VAR i
: INTEGER; ch
: SHORTCHAR
;
998 i
:= 0; RefCh(ref
, ch
);
999 WHILE ch
# 0X
DO n
[i
] := ch
; INC(i
); RefCh(ref
, ch
) END;
1003 PROCEDURE GetRefProc
* (VAR ref
: INTEGER; OUT adr
: INTEGER; OUT name
: Utf8Name
);
1007 WHILE ch
>= 0FDX
DO (* skip variables *)
1008 INC(ref
); RefCh(ref
, ch
);
1009 IF ch
= 10X
THEN INC(ref
, 4) END;
1010 RefNum(ref
, adr
); RefName(ref
, name
); S
.GET(ref
, ch
)
1012 WHILE (ch
> 0X
) & (ch
< 0FCX
) DO (* skip source refs *)
1013 INC(ref
); RefNum(ref
, adr
); S
.GET(ref
, ch
)
1015 IF ch
= 0FCX
THEN INC(ref
); RefNum(ref
, adr
); RefName(ref
, name
)
1020 PROCEDURE GetRefVar
* (VAR ref
: INTEGER; OUT mode
, form
: SHORTCHAR
; OUT desc
: Type
; OUT adr
: INTEGER; OUT name
: Utf8Name
);
1022 S
.GET(ref
, mode
); desc
:= NIL;
1023 IF mode
>= 0FDX
THEN
1024 mode
:= SHORT(CHR(ORD(mode
) - 0FCH
));
1025 INC(ref
); RefCh(ref
, form
);
1027 S
.GET(ref
, desc
); INC(ref
, 4); form
:= SHORT(CHR(16 + desc
.id
MOD 4))
1029 RefNum(ref
, adr
); RefName(ref
, name
)
1031 mode
:= 0X
; form
:= 0X
; adr
:= 0
1035 PROCEDURE SourcePos
* (mod
: Module
; codePos
: INTEGER): INTEGER;
1036 VAR ref
, pos
, ad
, d
: INTEGER; ch
: SHORTCHAR
; name
: Utf8Name
;
1038 IF mod
# NIL THEN (* mf, 12.02.04 *)
1039 ref
:= mod
.refs
; pos
:= 0; ad
:= 0; S
.GET(ref
, ch
);
1041 WHILE (ch
> 0X
) & (ch
< 0FCX
) DO (* srcref: {dAdr,dPos} *)
1042 INC(ad
, ORD(ch
)); INC(ref
); RefNum(ref
, d
);
1043 IF ad
> codePos
THEN RETURN pos
END;
1044 INC(pos
, d
); S
.GET(ref
, ch
)
1046 IF ch
= 0FCX
THEN (* proc: 0FCX,Adr,Name *)
1047 INC(ref
); RefNum(ref
, d
); RefName(ref
, name
); S
.GET(ref
, ch
);
1048 IF (d
> codePos
) & (pos
> 0) THEN RETURN pos
END
1050 WHILE ch
>= 0FDX
DO (* skip variables: Mode, Form, adr, Name *)
1051 INC(ref
); RefCh(ref
, ch
);
1052 IF ch
= 10X
THEN INC(ref
, 4) END;
1053 RefNum(ref
, d
); RefName(ref
, name
); S
.GET(ref
, ch
)
1060 PROCEDURE LoadDll
* (IN name
: ARRAY OF CHAR; VAR ok
: BOOLEAN);
1061 VAR h
: ADDRESS
; file
: Utf8Name
; res
: INTEGER;
1063 StringToUtf8(name
, file
, res
);
1065 h
:= dlfcn
.dlopen(file
, dlfcn
.RTLD_LAZY
+ dlfcn
.RTLD_GLOBAL
);
1072 PROCEDURE ThisDllObj
* (mode
, fprint
: INTEGER; IN dll
, name
: ARRAY OF CHAR): INTEGER;
1073 VAR h
, p
: ADDRESS
; file
, sym
: Utf8Name
; res
: INTEGER; err
: dlfcn
.int
;
1075 StringToUtf8(dll
, file
, res
);
1077 h
:= dlfcn
.dlopen(file
, dlfcn
.RTLD_LAZY
+ dlfcn
.RTLD_GLOBAL
);
1079 StringToUtf8(name
, sym
, res
);
1081 p
:= dlfcn
.dlsym(h
, sym
)
1085 err
:= dlfcn
.dlclose(h
);
1086 ASSERT(err
= 0, 100)
1096 (* -------------------- garbage collector (portable) --------------------- *)
1098 PROCEDURE Mark (this
: Block
);
1099 VAR father
, son
: Block
; tag
: Type
; flag
, offset
, actual
: INTEGER;
1101 IF ~
ODD(S
.VAL(INTEGER, this
.tag
)) THEN
1104 INC(S
.VAL(INTEGER, this
.tag
));
1105 flag
:= S
.VAL(INTEGER, this
.tag
) MOD 4;
1106 tag
:= S
.VAL(Type
, S
.VAL(INTEGER, this
.tag
) - flag
);
1107 IF flag
>= 2 THEN actual
:= this
.first
; this
.actual
:= actual
1108 ELSE actual
:= S
.ADR(this
.last
)
1111 offset
:= tag
.ptroffs
[0];
1113 INC(S
.VAL(INTEGER, tag
), offset
+ 4); (* restore tag *)
1114 IF (flag
>= 2) & (actual
< this
.last
) & (offset
< -4) THEN (* next array element *)
1115 INC(actual
, tag
.size
); this
.actual
:= actual
1117 this
.tag
:= S
.VAL(Type
, S
.VAL(INTEGER, tag
) + flag
);
1118 IF father
= NIL THEN RETURN END;
1119 son
:= this
; this
:= father
;
1120 flag
:= S
.VAL(INTEGER, this
.tag
) MOD 4;
1121 tag
:= S
.VAL(Type
, S
.VAL(INTEGER, this
.tag
) - flag
);
1122 offset
:= tag
.ptroffs
[0];
1123 IF flag
>= 2 THEN actual
:= this
.actual
ELSE actual
:= S
.ADR(this
.last
) END;
1124 S
.GET(actual
+ offset
, father
); S
.PUT(actual
+ offset
, S
.ADR(son
.last
));
1125 INC(S
.VAL(INTEGER, tag
), 4)
1128 S
.GET(actual
+ offset
, son
);
1130 DEC(S
.VAL(INTEGER, son
), 4);
1131 IF ~
ODD(S
.VAL(INTEGER, son
.tag
)) THEN (* down *)
1132 this
.tag
:= S
.VAL(Type
, S
.VAL(INTEGER, tag
) + flag
);
1133 S
.PUT(actual
+ offset
, father
); father
:= this
; this
:= son
;
1137 INC(S
.VAL(INTEGER, tag
), 4)
1144 PROCEDURE MarkGlobals
;
1145 VAR m
: Module
; i
, p
: INTEGER;
1149 IF m
.refcnt
>= 0 THEN
1151 WHILE i
< m
.nofptrs
DO
1152 S
.GET(m
.varBase
+ m
.ptrs
[i
], p
); INC(i
);
1153 IF p
# 0 THEN Mark(S
.VAL(Block
, p
- 4)) END
1160 PROCEDURE Next (b
: Block
): Block
; (* next block in same cluster *)
1163 S
.GET(S
.VAL(INTEGER, b
.tag
) DIV 4 * 4, size
);
1164 IF ODD(S
.VAL(INTEGER, b
.tag
) DIV 2) THEN INC(size
, b
.last
- S
.ADR(b
.last
)) END;
1165 RETURN S
.VAL(Block
, S
.VAL(INTEGER, b
) + (size
+ 19) DIV 16 * 16)
1168 PROCEDURE CheckCandidates
;
1169 (* pre: nofcand > 0 *)
1170 VAR i
, j
, h
, p
, end
: INTEGER; c
: Cluster
; blk
, next
: Block
;
1172 (* sort candidates (shellsort) *)
1173 h
:= 1; REPEAT h
:= h
*3 + 1 UNTIL h
> nofcand
;
1174 REPEAT h
:= h
DIV 3; i
:= h
;
1175 WHILE i
< nofcand
DO p
:= candidates
[i
]; j
:= i
;
1176 WHILE (j
>= h
) & (candidates
[j
-h
] > p
) DO
1177 candidates
[j
] := candidates
[j
-h
]; j
:= j
-h
1179 candidates
[j
] := p
; INC(i
)
1185 blk
:= S
.VAL(Block
, S
.VAL(INTEGER, c
) + 12);
1186 end
:= S
.VAL(INTEGER, blk
) + (c
.size
- 12) DIV 16 * 16;
1187 WHILE candidates
[i
] < S
.VAL(INTEGER, blk
) DO
1189 IF i
= nofcand
THEN RETURN END
1191 WHILE S
.VAL(INTEGER, blk
) < end
DO
1193 IF candidates
[i
] < S
.VAL(INTEGER, next
) THEN
1194 IF (S
.VAL(INTEGER, blk
.tag
) # S
.ADR(blk
.last
)) (* not a free block *)
1195 & (~strictStackSweep
OR (candidates
[i
] = S
.ADR(blk
.last
))) THEN
1200 IF i
= nofcand
THEN RETURN END
1201 UNTIL candidates
[i
] >= S
.VAL(INTEGER, next
)
1203 IF (S
.VAL(INTEGER, blk
.tag
) MOD 4 = 0) & (S
.VAL(INTEGER, blk
.tag
) # S
.ADR(blk
.last
))
1204 & (blk
.tag
.base
[0] = NIL) & (blk
.actual
> 0) THEN (* referenced interface record *)
1211 END CheckCandidates
;
1213 PROCEDURE MarkLocals
;
1214 VAR sp
, p
, min
, max
: INTEGER; c
: Cluster
;
1216 sp
:= S
.ADR(sp
); nofcand
:= 0; c
:= root
;
1217 WHILE c
.next
# NIL DO c
:= c
.next
END;
1218 min
:= S
.VAL(INTEGER, root
); max
:= S
.VAL(INTEGER, c
) + c
.size
;
1219 WHILE sp
< baseStack
DO
1221 IF (p
> min
) & (p
< max
) & (~strictStackSweep
OR (p
MOD 16 = 0)) THEN
1222 candidates
[nofcand
] := p
; INC(nofcand
);
1223 IF nofcand
= LEN(candidates
) - 1 THEN CheckCandidates
; nofcand
:= 0 END
1227 candidates
[nofcand
] := max
; INC(nofcand
); (* ensure complete scan for interface mark*)
1228 IF nofcand
> 0 THEN CheckCandidates
END
1231 PROCEDURE MarkFinObj
;
1234 wouldFinalize
:= FALSE
;
1237 IF ~
ODD(S
.VAL(INTEGER, f
.blk
.tag
)) THEN wouldFinalize
:= TRUE
END;
1242 WHILE f
# NIL DO IF ~
ODD(S
.VAL(INTEGER, f
.blk
.tag
)) THEN wouldFinalize
:= TRUE
END;
1248 PROCEDURE CheckFinalizers
;
1249 VAR f
, g
, h
, k
: FList
;
1251 f
:= finalizers
; g
:= NIL;
1252 IF hotFinalizers
= NIL THEN k
:= NIL
1255 WHILE k
.next
# NIL DO k
:= k
.next
END
1258 h
:= f
; f
:= f
.next
;
1259 IF ~
ODD(S
.VAL(INTEGER, h
.blk
.tag
)) THEN
1260 IF g
= NIL THEN finalizers
:= f
ELSE g
.next
:= f
END;
1261 IF k
= NIL THEN hotFinalizers
:= h
ELSE k
.next
:= h
END;
1262 k
:= h
; h
.next
:= NIL
1267 WHILE h
# NIL DO Mark(h
.blk
); h
:= h
.next
END
1268 END CheckFinalizers
;
1270 PROCEDURE ExecFinalizer (a
, b
, c
: INTEGER);
1271 VAR f
: FList
; fin
: PROCEDURE(this
: ANYPTR
);
1273 f
:= S
.VAL(FList
, a
);
1274 S
.GET(S
.VAL(INTEGER, f
.blk
.tag
) - 4, fin
); (* method 0 *)
1275 IF (fin
# NIL) & (f
.blk
.tag
.mod
.refcnt
>= 0) THEN fin(S
.VAL(ANYPTR
, S
.ADR(f
.blk
.last
))) END;
1278 PROCEDURE^ Try
* (h
: TryHandler
; a
, b
, c
: INTEGER); (* COMPILER DEPENDENT *)
1280 PROCEDURE CallFinalizers
;
1283 WHILE hotFinalizers
# NIL DO
1284 f
:= hotFinalizers
; hotFinalizers
:= hotFinalizers
.next
;
1285 Try(ExecFinalizer
, S
.VAL(INTEGER, f
), 0, 0)
1287 wouldFinalize
:= FALSE
1290 PROCEDURE Insert (blk
: FreeBlock
; size
: INTEGER); (* insert block in free list *)
1293 blk
.size
:= size
- 4; blk
.tag
:= S
.VAL(Type
, S
.ADR(blk
.size
));
1294 i
:= MIN(N
- 1, (blk
.size
DIV 16));
1295 blk
.next
:= free
[i
]; free
[i
] := blk
1298 PROCEDURE Sweep (dealloc
: BOOLEAN);
1299 VAR cluster
, last
, c
: Cluster
; blk
, next
: Block
; fblk
, b
, t
: FreeBlock
; end
, i
: INTEGER;
1301 cluster
:= root
; last
:= NIL; allocated
:= 0;
1303 REPEAT DEC(i
); free
[i
] := sentinel
UNTIL i
= 0;
1304 WHILE cluster
# NIL DO
1305 blk
:= S
.VAL(Block
, S
.VAL(INTEGER, cluster
) + 12);
1306 end
:= S
.VAL(INTEGER, blk
) + (cluster
.size
- 12) DIV 16 * 16;
1308 WHILE S
.VAL(INTEGER, blk
) < end
DO
1310 IF ODD(S
.VAL(INTEGER, blk
.tag
)) THEN
1312 Insert(fblk
, S
.VAL(INTEGER, blk
) - S
.VAL(INTEGER, fblk
));
1315 DEC(S
.VAL(INTEGER, blk
.tag
)); (* unmark *)
1316 INC(allocated
, S
.VAL(INTEGER, next
) - S
.VAL(INTEGER, blk
))
1317 ELSIF fblk
= NIL THEN
1318 fblk
:= S
.VAL(FreeBlock
, blk
)
1322 IF dealloc
& (S
.VAL(INTEGER, fblk
) = S
.VAL(INTEGER, cluster
) + 12) THEN (* deallocate cluster *)
1323 c
:= cluster
; cluster
:= cluster
.next
;
1324 IF last
= NIL THEN root
:= cluster
ELSE last
.next
:= cluster
END;
1327 IF fblk
# NIL THEN Insert(fblk
, end
- S
.VAL(INTEGER, fblk
)) END;
1328 last
:= cluster
; cluster
:= cluster
.next
1331 (* reverse free list *)
1335 b
:= free
[i
]; fblk
:= sentinel
;
1336 WHILE b
# sentinel
DO t
:= b
; b
:= t
.next
; t
.next
:= fblk
; fblk
:= t
END;
1344 CallFinalizers
; (* trap cleanup *)
1353 PROCEDURE FastCollect
*;
1363 PROCEDURE WouldFinalize
* (): BOOLEAN;
1365 RETURN wouldFinalize
1368 (* --------------------- memory allocation (portable) -------------------- *)
1370 PROCEDURE OldBlock (size
: INTEGER): FreeBlock
; (* size MOD 16 = 0 *)
1371 VAR b
, l
: FreeBlock
; s
, i
: INTEGER;
1374 i
:= MIN(N
- 1, s
DIV 16);
1375 WHILE (i
# N
- 1) & (free
[i
] = sentinel
) DO INC(i
) END;
1376 b
:= free
[i
]; l
:= NIL;
1377 WHILE b
.size
< s
DO l
:= b
; b
:= b
.next
END;
1378 IF b
# sentinel
THEN
1379 IF l
= NIL THEN free
[i
] := b
.next
ELSE l
.next
:= b
.next
END
1385 PROCEDURE LastBlock (limit
: INTEGER): FreeBlock
; (* size MOD 16 = 0 *)
1386 VAR b
, l
: FreeBlock
; s
, i
: INTEGER;
1391 b
:= free
[i
]; l
:= NIL;
1392 WHILE (b
# sentinel
) & (S
.VAL(INTEGER, b
) + b
.size
# s
) DO l
:= b
; b
:= b
.next
END;
1393 IF b
# sentinel
THEN
1394 IF l
= NIL THEN free
[i
] := b
.next
ELSE l
.next
:= b
.next
END
1398 UNTIL (b
# NIL) OR (i
= N
);
1402 PROCEDURE NewBlock (size
: INTEGER): Block
;
1403 VAR tsize
, a
, s
: INTEGER; b
: FreeBlock
; new
, c
: Cluster
; r
: Reducer
;
1405 ASSERT(size
>= 0, 20);
1406 IF size
> MAX(INTEGER) - 19 THEN RETURN NIL END;
1407 tsize
:= (size
+ 19) DIV 16 * 16;
1408 b
:= OldBlock(tsize
); (* 1) search for free block *)
1410 FastCollect
; b
:= OldBlock(tsize
); (* 2) collect *)
1412 Collect
; b
:= OldBlock(tsize
); (* 2a) fully collect *)
1415 AllocHeapMem(tsize
+ 12, new
); (* 3) allocate new cluster *)
1417 IF (root
= NIL) OR (S
.VAL(INTEGER, new
) < S
.VAL(INTEGER, root
)) THEN
1418 new
.next
:= root
; root
:= new
1421 WHILE (c
.next
# NIL) & (S
.VAL(INTEGER, new
) > S
.VAL(INTEGER, c
.next
)) DO c
:= c
.next
END;
1422 new
.next
:= c
.next
; c
.next
:= new
1424 b
:= S
.VAL(FreeBlock
, S
.VAL(INTEGER, new
) + 12);
1425 b
.size
:= (new
.size
- 12) DIV 16 * 16 - 4
1427 RETURN NIL (* 4) give up *)
1432 a
:= b
.size
+ 4 - tsize
;
1433 IF a
> 0 THEN Insert(S
.VAL(FreeBlock
, S
.VAL(INTEGER, b
) + tsize
), a
) END;
1434 IF size
> 0 THEN Erase(S
.ADR(b
.size
), (size
+ 3) DIV 4) END;
1435 INC(allocated
, tsize
);
1436 RETURN S
.VAL(Block
, b
)
1439 PROCEDURE Allocated
* (): INTEGER;
1444 PROCEDURE Used
* (): INTEGER;
1449 PROCEDURE Root
* (): INTEGER;
1451 RETURN S
.VAL(INTEGER, root
)
1454 (* -------------------- Trap Handling --------------------- *)
1456 PROCEDURE Start
* (code
: Command
);
1457 VAR res
: setjmp
.int
;
1460 S
.GETREG(SP
, baseStack
);
1461 res
:= setjmp
.sigsetjmp(startEnv
, 1);
1465 PROCEDURE Quit
* (exitCode
: INTEGER);
1466 VAR m
: Module
; term
: Command
; t
: BOOLEAN;
1468 trapViewer
:= NIL; trapChecker
:= NIL; restart
:= NIL;
1469 t
:= terminating
; terminating
:= TRUE
; m
:= modList
;
1470 WHILE m
# NIL DO (* call terminators *)
1471 IF ~static
OR ~t
THEN
1472 term
:= m
.term
; m
.term
:= NIL;
1473 IF term
# NIL THEN term() END
1478 hotFinalizers
:= finalizers
; finalizers
:= NIL;
1480 stdlib
.exit(exitCode
)
1483 PROCEDURE FatalError
* (id
: INTEGER; str
: ARRAY OF CHAR);
1484 VAR res
: stdio
.int
; title
: ARRAY 16 OF CHAR; text
: ARRAY 256 OF SHORTCHAR
;
1486 title
:= "Error xy";
1487 title
[6] := CHR(id
DIV 10 + ORD("0"));
1488 title
[7] := CHR(id
MOD 10 + ORD("0"));
1489 res
:= unistd
.write(2, S
.ADR(title
), 8);
1493 PROCEDURE DefaultTrapViewer
;
1494 VAR out
: ARRAY 2048 OF SHORTCHAR
; a
, b
, c
, len
, ref
, end
: INTEGER; mod
: Module
;
1495 modName
, name
: Name
; n
: Utf8Name
; res
: unistd
.int
;
1497 PROCEDURE WriteString (IN s
: ARRAY OF SHORTCHAR
);
1501 WHILE (len
< LEN(out
) - 1) & (s
[i
] # 0X
) DO out
[len
] := s
[i
]; INC(i
); INC(len
) END
1504 PROCEDURE WriteHex (x
, n
: INTEGER);
1507 IF len
+ n
< LEN(out
) THEN
1510 y
:= x
MOD 16; x
:= x
DIV 16;
1511 IF y
> 9 THEN y
:= y
+ (ORD("A") - ORD("0") - 10) END;
1512 out
[i
] := SHORT(CHR(y
+ ORD("0"))); DEC(i
)
1520 IF len
< LEN(out
) - 1 THEN out
[len
] := 0AX
; INC(len
) END
1525 WriteString("====== ");
1526 IF err
= 129 THEN WriteString("invalid with")
1527 ELSIF err
= 130 THEN WriteString("invalid case")
1528 ELSIF err
= 131 THEN WriteString("function without return")
1529 ELSIF err
= 132 THEN WriteString("type guard")
1530 ELSIF err
= 133 THEN WriteString("implied type guard")
1531 ELSIF err
= 134 THEN WriteString("value out of range")
1532 ELSIF err
= 135 THEN WriteString("index out of range")
1533 ELSIF err
= 136 THEN WriteString("string too long")
1534 ELSIF err
= 137 THEN WriteString("stack overflow")
1535 ELSIF err
= 138 THEN WriteString("integer overflow")
1536 ELSIF err
= 139 THEN WriteString("division by zero")
1537 ELSIF err
= 140 THEN WriteString("infinite real result")
1538 ELSIF err
= 141 THEN WriteString("real underflow")
1539 ELSIF err
= 142 THEN WriteString("real overflow")
1540 ELSIF err
= 143 THEN WriteString("undefined real result")
1541 ELSIF err
= 144 THEN WriteString("not a number")
1542 ELSIF err
= 200 THEN WriteString("keyboard interrupt")
1543 ELSIF err
= 201 THEN WriteString("NIL dereference")
1544 ELSIF err
= 202 THEN WriteString("illegal instruction: ");
1546 ELSIF err
= 203 THEN WriteString("illegal memory read [ad = ");
1547 WriteHex(val
, 8); WriteString("]")
1548 ELSIF err
= 204 THEN WriteString("illegal memory write [ad = ");
1549 WriteHex(val
, 8); WriteString("]")
1550 ELSIF err
= 205 THEN WriteString("illegal execution [ad = ");
1551 WriteHex(val
, 8); WriteString("]")
1552 ELSIF err
= 257 THEN WriteString("out of memory")
1553 ELSIF err
= 10001H
THEN WriteString("bus error")
1554 ELSIF err
= 10002H
THEN WriteString("address error")
1555 ELSIF err
= 10007H
THEN WriteString("fpu error")
1556 ELSIF err
< 0 THEN WriteString("exception #"); WriteHex(-err
, 2)
1557 ELSE err
:= err
DIV 100 * 256 + err
DIV 10 MOD 10 * 16 + err
MOD 10;
1558 WriteString("trap #"); WriteHex(err
, 3)
1560 WriteString(" ======");
1561 a
:= pc
; b
:= fp
; c
:= 12;
1563 WriteLn
; WriteString("- ");
1565 WHILE (mod
# NIL) & ((a
< mod
.code
) OR (a
>= mod
.code
+ mod
.csize
)) DO mod
:= mod
.next
END;
1568 IF mod
.refcnt
>= 0 THEN
1569 GetModName(mod
, modName
); WriteString(SHORT(modName
)); ref
:= mod
.refs
;
1570 REPEAT GetRefProc(ref
, end
, n
) UNTIL (end
= 0) OR (a
< end
);
1572 Utf8ToString(n
, name
, res
); WriteString("."); WriteString(SHORT(name
))
1575 GetModName(mod
, modName
); WriteString("("); WriteString(SHORT(modName
)); WriteString(")")
1579 WriteString("(pc="); WriteHex(a
, 8);
1580 WriteString(", fp="); WriteHex(b
, 8); WriteString(")");
1581 IF (b
>= sp
) & (b
< stack
) THEN
1582 S
.GET(b
+4, a
); (* stacked pc *)
1583 S
.GET(b
, b
); (* dynamic link *)
1589 res
:= unistd
.write(2, S
.ADR(out
), len
)
1590 END DefaultTrapViewer
;
1592 PROCEDURE TrapCleanup
;
1595 WHILE trapStack
# NIL DO
1596 t
:= trapStack
; trapStack
:= trapStack
.next
; t
.Cleanup
1598 IF (trapChecker
# NIL) & (err
# 128) THEN trapChecker
END
1601 PROCEDURE SetTrapGuard
* (on
: BOOLEAN);
1606 PROCEDURE Try
* (h
: TryHandler
; a
, b
, c
: INTEGER);
1607 VAR oldIsTry
: BOOLEAN; oldTryEnv
: setjmp
.jmp_buf
; res
: setjmp
.int
;
1609 oldIsTry
:= isTry
; oldTryEnv
:= tryEnv
;
1611 res
:= setjmp
._setjmp(tryEnv
);
1612 IF res
= 0 THEN h(a
, b
, c
) END;
1613 isTry
:= oldIsTry
; tryEnv
:= oldTryEnv
1616 PROCEDURE Trap
* (n
: INTEGER);
1620 IF ~secondTrap
THEN trapped
:= FALSE
; secondTrap
:= TRUE
END
1622 IF n
>= 0 THEN err
:= n
1623 ELSE err
:= -n
+ 128
1625 pc
:= 0; sp
:= 0; fp
:= 0; stack
:= 0; val
:= 0;
1630 setjmp
._longjmp(tryEnv
, 1)
1632 IF (err
= 128) OR (err
= 200) & ~intTrap
THEN (* do nothing *)
1633 ELSIF (trapViewer
# NIL) & (restart
# NIL) & ~trapped
& ~guarded
THEN
1634 trapped
:= TRUE
; trapViewer()
1635 ELSE DefaultTrapViewer
1637 trapped
:= FALSE
; secondTrap
:= FALSE
;
1638 IF restart
# NIL THEN
1639 setjmp
.siglongjmp(startEnv
, 1)
1644 PROCEDURE [ccall
] TrapHandler (signo
: signal
.int
; IN info
: signal
.siginfo_t
; context
: ADDRESS
);
1645 VAR res
: signal
.int
;
1647 IF checkReadable
THEN
1648 setjmp
.siglongjmp(checkReadableEnv
, 1)
1652 IF ~secondTrap
THEN trapped
:= FALSE
; secondTrap
:= TRUE
END
1654 err
:= -signo
; pc
:= 0; sp
:= 0; fp
:= 0; stack
:= baseStack
; val
:= 0;
1657 val
:= info
.si_code
;
1658 pc
:= info
.info
.sigfpe
.si_addr
;
1659 CASE info
.si_code
OF
1660 | signal
.FPE_INTDIV
: err
:= 139 (* division by zero *)
1661 | signal
.FPE_INTOVF
: err
:= 138 (* integer overflow *)
1662 | signal
.FPE_FLTDIV
: err
:= 140 (* fpu: division by zero *)
1663 | signal
.FPE_FLTOVF
: err
:= 142 (* fpu: overflow *)
1664 | signal
.FPE_FLTUND
: err
:= 141 (* fpu: underflow *)
1665 (* !!! | signal.FPE_FLTRES: err := ??? (* fpu: *) *)
1666 | signal
.FPE_FLTINV
: err
:= 143 (* val := opcode *) (* fpu: invalid op *)
1667 (* !!! | signal.FPE_FLTSUB: err := ??? (* fpu: *) *)
1671 val
:= info
.si_code
;
1672 err
:= 200 (* keyboard interrupt *)
1674 val
:= info
.info
.sigsegv
.si_addr
;
1675 err
:= 203 (* illigal read *)
1677 val
:= info
.info
.sigbus
.si_addr
;
1678 err
:= 10001H (* bus error *)
1680 pc
:= info
.info
.sigill
.si_addr
;
1681 err
:= 202; (* illigal instruction *)
1682 IF IsReadable(pc
, pc
+ 4) THEN
1684 IF val
MOD 100H
= 8DH
THEN (* lea reg,reg *)
1685 IF val
DIV 100H
MOD 100H
= 0F0H
THEN
1686 err
:= val
DIV 10000H
MOD 100H (* trap *)
1687 ELSIF val
DIV 1000H
MOD 10H
= 0EH
THEN
1688 err
:= 128 + val
DIV 100H
MOD 10H (* run time error *)
1698 setjmp
._longjmp(tryEnv
, 1)
1700 IF (err
= 128) OR (err
= 200) & ~intTrap
THEN (* do nothing *)
1701 ELSIF (trapViewer
# NIL) & (restart
# NIL) & ~trapped
& ~guarded
THEN
1702 trapped
:= TRUE
; trapViewer()
1703 ELSE DefaultTrapViewer
1705 trapped
:= FALSE
; secondTrap
:= FALSE
;
1706 IF restart
# NIL THEN
1707 setjmp
.siglongjmp(startEnv
, 1)
1712 (* -------------------- Initialization --------------------- *)
1714 PROCEDURE InstallTrap (signo
: signal
.int
);
1715 VAR act
: signal
.struct_sigaction
; res
: signal
.int
;
1717 act
.handler
.sa_handler
:= NIL;
1718 res
:= signal
.sigemptyset(act
.sa_mask
);
1719 act
.sa_flags
:= signal
.SA_NODEFER
+ signal
.SA_SIGINFO
;
1720 act
.handler
.sa_sigaction
:= TrapHandler
;
1721 res
:= signal
.sigaction(signo
, act
, NIL);
1724 PROCEDURE InstallTrapVectors
;
1726 InstallTrap(signal
.SIGFPE
);
1727 InstallTrap(signal
.SIGINT
);
1728 InstallTrap(signal
.SIGSEGV
);
1729 InstallTrap(signal
.SIGBUS
);
1730 InstallTrap(signal
.SIGILL
)
1731 END InstallTrapVectors
;
1733 PROCEDURE RemoveTrapVectors
;
1734 END RemoveTrapVectors
;
1740 pagesize
:= unistd
.sysconf(unistd
._SC_PAGESIZE
);
1743 allocated
:= 0; total
:= 0; used
:= 0;
1744 sentinelBlock
.size
:= MAX(INTEGER);
1745 sentinel
:= S
.ADR(sentinelBlock
);
1747 REPEAT DEC(i
); free
[i
] := sentinel
UNTIL i
= 0;
1755 InitModule(modList
);
1756 IF ~inDll
THEN Quit(1) END
1761 IF modList
= NIL THEN (* only once *)
1762 S
.GETREG(SP
, baseStack
);
1763 S
.GET(baseStack
+ 16, argc
);
1764 argv
:= S
.VAL(ArrStrPtr
, baseStack
+ 20);
1765 S
.GETREG(ML
, modList
); (* linker loads module list to BX *)
1766 static
:= init
IN modList
.opts
;
1767 inDll
:= dll
IN modList
.opts
;
1771 IF ~terminating
THEN
1772 terminating
:= TRUE
;