5773b4ebcdedcbc3fe3124c6bb9fe428d8043569
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 *)
11 (* implement Call using libffi *)
17 timeResolution
* = 1000; (* ticks per second *)
19 processor
* = 1; (* generic c *)
21 objType
* = "ocf"; (* file types *)
25 (* loader constants *)
40 SP
= 4; (* register number of stack pointer *)
41 FP
= 5; (* register number of frame pointer *)
42 ML
= 3; (* register which holds the module list at program start *)
44 strictStackSweep
= FALSE
;
45 N
= 128 DIV 16; (* free lists *)
47 (* kernel flags in module desc *)
48 init
= 16; dyn
= 17; dll
= 24; iptrs
= 30;
50 (* meta interface consts *)
51 mConst
= 1; mTyp
= 2; mVar
= 3; mProc
= 4; mField
= 5;
54 Name
* = ARRAY nameLen
OF CHAR;
55 Utf8Name
* = ARRAY nameLen
OF SHORTCHAR
;
58 Module
* = POINTER TO RECORD [untagged
]
60 opts
-: SET; (* 0..15: compiler opts, 16..31: kernel flags *)
61 refcnt
-: INTEGER; (* <0: module invalidated *)
62 compTime
-, loadTime
-: ARRAY 6 OF SHORTINT;
63 ext
-: INTEGER; (* currently not used *)
64 term
-: Command
; (* terminator *)
65 nofimps
-, nofptrs
-: INTEGER;
66 csize
-, dsize
-, rsize
-: INTEGER;
67 code
-, data
-, refs
-: INTEGER;
68 procBase
-, varBase
-: INTEGER; (* meta base addresses *)
69 names
-: POINTER TO ARRAY [untagged
] OF SHORTCHAR
; (* names[0] = 0X *)
70 ptrs
-: POINTER TO ARRAY [untagged
] OF INTEGER;
71 imports
-: POINTER TO ARRAY [untagged
] OF Module
;
72 export
-: Directory
; (* exported objects (name sorted) *)
76 Type
* = POINTER TO RECORD [untagged
]
77 (* record: ptr to method n at offset - 4 * (n+1) *)
78 size
-: INTEGER; (* record: size, array: #elem, dyn array: 0, proc: sigfp *)
80 id
-: INTEGER; (* name idx * 256 + lev * 16 + attr * 4 + form *)
81 base
-: ARRAY 16 OF Type
; (* signature if form = ProcTyp *)
82 fields
-: Directory
; (* new fields (declaration order) *)
83 ptroffs
-: ARRAY any
OF INTEGER (* array of any length *)
86 Object
* = POINTER TO ObjDesc
;
88 ObjDesc
* = RECORD [untagged
]
90 offs
-: INTEGER; (* pvfprint for record types *)
91 id
-: INTEGER; (* name idx * 256 + vis * 16 + mode *)
92 struct
-: Type (* id of basic type or pointer to typedesc/signature *)
95 Directory
* = POINTER TO RECORD [untagged
]
96 num
-: INTEGER; (* number of entries *)
97 obj
-: ARRAY any
OF ObjDesc (* array of any length *)
100 Signature
* = POINTER TO RECORD [untagged
]
101 retStruct
-: Type
; (* id of basic type or pointer to typedesc or 0 *)
102 num
-: INTEGER; (* number of parameters *)
103 par
-: ARRAY any
OF RECORD [untagged
] (* parameters *)
104 id
-: INTEGER; (* name idx * 256 + kind *)
105 struct
-: Type (* id of basic type or pointer to typedesc *)
109 Handler
* = PROCEDURE;
111 Reducer
* = POINTER TO ABSTRACT
RECORD
115 Identifier
* = ABSTRACT
RECORD
120 TrapCleaner
* = POINTER TO ABSTRACT
RECORD
124 TryHandler
* = PROCEDURE (a
, b
, c
: INTEGER);
126 (* meta extension suport *)
128 ItemExt
* = POINTER TO ABSTRACT
RECORD END;
131 obj
*, vis
*, typ
*, adr
*: INTEGER;
138 Hook
* = POINTER TO ABSTRACT
RECORD END;
140 LoaderHook
* = POINTER TO ABSTRACT
RECORD (Hook
)
142 importing
*, imported
*, object
*: ARRAY 256 OF CHAR
145 Block
= POINTER TO RECORD [untagged
]
147 last
: INTEGER; (* arrays: last element *)
148 actual
: INTEGER; (* arrays: used during mark phase *)
149 first
: INTEGER (* arrays: first element *)
152 FreeBlock
= POINTER TO FreeDesc
;
154 FreeDesc
= RECORD [untagged
]
155 tag
: Type
; (* f.tag = ADR(f.size) *)
160 Cluster
= POINTER TO RECORD [untagged
]
161 size
: INTEGER; (* total size *)
163 max
: INTEGER (* exe: reserved size, dll: original address *)
164 (* start of first block *)
167 FList
= POINTER TO RECORD
173 CList
= POINTER TO RECORD
180 PtrType
= RECORD v
: S
.PTR
END; (* used for array of pointer *)
181 Char8Type
= RECORD v
: SHORTCHAR
END;
182 Char16Type
= RECORD v
: CHAR END;
183 Int8Type
= RECORD v
: BYTE END;
184 Int16Type
= RECORD v
: SHORTINT END;
185 Int32Type
= RECORD v
: INTEGER END;
186 Int64Type
= RECORD v
: LONGINT END;
187 BoolType
= RECORD v
: BOOLEAN END;
188 SetType
= RECORD v
: SET END;
189 Real32Type
= RECORD v
: SHORTREAL
END;
190 Real64Type
= RECORD v
: REAL END;
191 ProcType
= RECORD v
: PROCEDURE END;
192 UPtrType
= RECORD v
: INTEGER END;
193 StrPtr
= POINTER TO ARRAY [untagged
] OF SHORTCHAR
;
195 ArrStrPtr
= POINTER TO ARRAY [untagged
] OF StrPtr
;
197 ADDRESS
* = types
.Pvoid
;
204 err
-, pc
-, sp
-, fp
-, stack
-, val
-: INTEGER;
207 startEnv
: setjmp
.sigjmp_buf
;
208 tryEnv
: setjmp
.jmp_buf
;
212 pagesize
: unistd
.long
;
214 free
: ARRAY N
OF FreeBlock
; (* free list *)
215 sentinelBlock
: FreeDesc
;
217 candidates
: ARRAY 1024 OF INTEGER;
219 allocated
: INTEGER; (* bytes allocated on BlackBox heap *)
220 total
: INTEGER; (* current total size of BlackBox heap *)
221 used
: INTEGER; (* bytes allocated on system heap *)
223 hotFinalizers
: FList
;
226 trapStack
: TrapCleaner
;
227 actual
: Module
; (* valid during module initialization *)
229 trapViewer
, trapChecker
: Handler
;
230 trapped
, guarded
, secondTrap
: BOOLEAN;
231 interrupted
: BOOLEAN;
232 static
, inDll
, terminating
: BOOLEAN;
238 wouldFinalize
: BOOLEAN;
240 watcher
*: PROCEDURE (event
: INTEGER); (* for debugging *)
242 PROCEDURE Erase (adr
, words
: INTEGER);
244 ASSERT(words
>= 0, 20);
253 PROCEDURE (VAR id
: Identifier
) Identified
* (): BOOLEAN, NEW, ABSTRACT
;
254 PROCEDURE (r
: Reducer
) Reduce
* (full
: BOOLEAN), NEW, ABSTRACT
;
255 PROCEDURE (c
: TrapCleaner
) Cleanup
*, NEW, EMPTY
;
257 (* meta extension suport *)
259 PROCEDURE (e
: ItemExt
) Lookup
* (name
: ARRAY OF CHAR; VAR i
: ANYREC
), NEW, ABSTRACT
;
260 PROCEDURE (e
: ItemExt
) Index
* (index
: INTEGER; VAR elem
: ANYREC
), NEW, ABSTRACT
;
261 PROCEDURE (e
: ItemExt
) Deref
* (VAR ref
: ANYREC
), NEW, ABSTRACT
;
263 PROCEDURE (e
: ItemExt
) Valid
* (): BOOLEAN, NEW, ABSTRACT
;
264 PROCEDURE (e
: ItemExt
) Size
* (): INTEGER, NEW, ABSTRACT
;
265 PROCEDURE (e
: ItemExt
) BaseTyp
* (): INTEGER, NEW, ABSTRACT
;
266 PROCEDURE (e
: ItemExt
) Len
* (): INTEGER, NEW, ABSTRACT
;
268 PROCEDURE (e
: ItemExt
) Call
* (OUT ok
: BOOLEAN), NEW, ABSTRACT
;
269 PROCEDURE (e
: ItemExt
) BoolVal
* (): BOOLEAN, NEW, ABSTRACT
;
270 PROCEDURE (e
: ItemExt
) PutBoolVal
* (x
: BOOLEAN), NEW, ABSTRACT
;
271 PROCEDURE (e
: ItemExt
) CharVal
* (): CHAR, NEW, ABSTRACT
;
272 PROCEDURE (e
: ItemExt
) PutCharVal
* (x
: CHAR), NEW, ABSTRACT
;
273 PROCEDURE (e
: ItemExt
) IntVal
* (): INTEGER, NEW, ABSTRACT
;
274 PROCEDURE (e
: ItemExt
) PutIntVal
* (x
: INTEGER), NEW, ABSTRACT
;
275 PROCEDURE (e
: ItemExt
) LongVal
* (): LONGINT, NEW, ABSTRACT
;
276 PROCEDURE (e
: ItemExt
) PutLongVal
* (x
: LONGINT), NEW, ABSTRACT
;
277 PROCEDURE (e
: ItemExt
) RealVal
* (): REAL, NEW, ABSTRACT
;
278 PROCEDURE (e
: ItemExt
) PutRealVal
* (x
: REAL), NEW, ABSTRACT
;
279 PROCEDURE (e
: ItemExt
) SetVal
* (): SET, NEW, ABSTRACT
;
280 PROCEDURE (e
: ItemExt
) PutSetVal
* (x
: SET), NEW, ABSTRACT
;
281 PROCEDURE (e
: ItemExt
) PtrVal
* (): ANYPTR
, NEW, ABSTRACT
;
282 PROCEDURE (e
: ItemExt
) PutPtrVal
* (x
: ANYPTR
), NEW, ABSTRACT
;
283 PROCEDURE (e
: ItemExt
) GetSStringVal
* (OUT x
: ARRAY OF SHORTCHAR
;
284 OUT ok
: BOOLEAN), NEW, ABSTRACT
;
285 PROCEDURE (e
: ItemExt
) PutSStringVal
* (IN x
: ARRAY OF SHORTCHAR
;
286 OUT ok
: BOOLEAN), NEW, ABSTRACT
;
287 PROCEDURE (e
: ItemExt
) GetStringVal
* (OUT x
: ARRAY OF CHAR; OUT ok
: BOOLEAN), NEW, ABSTRACT
;
288 PROCEDURE (e
: ItemExt
) PutStringVal
* (IN x
: ARRAY OF CHAR; OUT ok
: BOOLEAN), NEW, ABSTRACT
;
290 (* -------------------- miscellaneous tools -------------------- *)
292 PROCEDURE IsUpper
* (ch
: CHAR): BOOLEAN;
294 RETURN wctype
.iswupper(ORD(ch
)) # 0
297 PROCEDURE Upper
* (ch
: CHAR): CHAR;
299 RETURN CHR(wctype
.towupper(ORD(ch
)))
302 PROCEDURE IsLower
* (ch
: CHAR): BOOLEAN;
304 RETURN wctype
.iswlower(ORD(ch
)) # 0
307 PROCEDURE Lower
* (ch
: CHAR): CHAR;
309 RETURN CHR(wctype
.towlower(ORD(ch
)))
312 PROCEDURE IsAlpha
* (ch
: CHAR): BOOLEAN;
314 RETURN wctype
.iswalpha(ORD(ch
)) # 0
317 PROCEDURE Utf8ToString
* (IN in
: ARRAY OF SHORTCHAR
; OUT out
: ARRAY OF CHAR; OUT res
: INTEGER);
318 VAR i
, j
, val
, max
: INTEGER; ch
: SHORTCHAR
;
320 PROCEDURE FormatError();
321 BEGIN out
:= in$
; res
:= 2 (*format error*)
325 ch
:= in
[0]; i
:= 1; j
:= 0; max
:= LEN(out
) - 1;
326 WHILE (ch
# 0X
) & (j
< max
) DO
330 val
:= ORD(ch
) - 192;
331 IF val
< 0 THEN FormatError
; RETURN END ;
332 ch
:= in
[i
]; INC(i
); val
:= val
* 64 + ORD(ch
) - 128;
333 IF (ch
< 80X
) OR (ch
>= 0E0X
) THEN FormatError
; RETURN END ;
334 out
[j
] := CHR(val
); INC(j
)
336 val
:= ORD(ch
) - 224;
337 ch
:= in
[i
]; INC(i
); val
:= val
* 64 + ORD(ch
) - 128;
338 IF (ch
< 80X
) OR (ch
>= 0E0X
) THEN FormatError
; RETURN END ;
339 ch
:= in
[i
]; INC(i
); val
:= val
* 64 + ORD(ch
) - 128;
340 IF (ch
< 80X
) OR (ch
>= 0E0X
) THEN FormatError
; RETURN END ;
341 out
[j
] := CHR(val
); INC(j
)
348 IF ch
= 0X
THEN res
:= 0 (*ok*) ELSE res
:= 1 (*truncated*) END
351 PROCEDURE StringToUtf8
* (IN in
: ARRAY OF CHAR; OUT out
: ARRAY OF SHORTCHAR
; OUT res
: INTEGER);
352 VAR i
, j
, val
, max
: INTEGER;
354 i
:= 0; j
:= 0; max
:= LEN(out
) - 3;
355 WHILE (in
[i
] # 0X
) & (j
< max
) DO
356 val
:= ORD(in
[i
]); INC(i
);
358 out
[j
] := SHORT(CHR(val
)); INC(j
)
359 ELSIF val
< 2048 THEN
360 out
[j
] := SHORT(CHR(val
DIV 64 + 192)); INC(j
);
361 out
[j
] := SHORT(CHR(val
MOD 64 + 128)); INC(j
)
363 out
[j
] := SHORT(CHR(val
DIV 4096 + 224)); INC(j
);
364 out
[j
] := SHORT(CHR(val
DIV 64 MOD 64 + 128)); INC(j
);
365 out
[j
] := SHORT(CHR(val
MOD 64 + 128)); INC(j
)
369 IF in
[i
] = 0X
THEN res
:= 0 (*ok*) ELSE res
:= 1 (*truncated*) END
372 PROCEDURE SplitName
* (name
: ARRAY OF CHAR; VAR head
, tail
: ARRAY OF CHAR);
374 VAR i
, j
: INTEGER; ch
, lch
: CHAR;
376 i
:= 0; ch
:= name
[0];
379 head
[i
] := ch
; lch
:= ch
; INC(i
); ch
:= name
[i
]
380 UNTIL (ch
= 0X
) OR (ch
= ".") OR IsUpper(ch
) & ~
IsUpper(lch
);
381 IF ch
= "." THEN i
:= 0; ch
:= name
[0] END;
382 head
[i
] := 0X
; j
:= 0;
383 WHILE ch
# 0X
DO tail
[j
] := ch
; INC(i
); INC(j
); ch
:= name
[i
] END;
385 IF tail
= "" THEN tail
:= head$
; head
:= "" END
386 ELSE head
:= ""; tail
:= ""
390 PROCEDURE MakeFileName
* (VAR name
: ARRAY OF CHAR; type
: ARRAY OF CHAR);
391 VAR i
, j
: INTEGER; ext
: ARRAY 8 OF CHAR; ch
: CHAR;
394 WHILE (name
[i
] # 0X
) & (name
[i
] # ".") DO INC(i
) END;
395 IF name
[i
] = "." THEN
396 IF name
[i
+ 1] = 0X
THEN name
[i
] := 0X
END
398 IF type
= "" THEN ext
:= docType
ELSE ext
:= type$
END;
399 IF i
< LEN(name
) - LEN(ext$
) - 1 THEN
400 name
[i
] := "."; INC(i
); j
:= 0; ch
:= ext
[0];
402 name
[i
] := Lower(ch
); INC(i
); INC(j
); ch
:= ext
[j
]
409 PROCEDURE Time
* (): LONGINT;
410 VAR res
: time
.int
; tp
: time
.struct_timespec
;
412 ASSERT(timeResolution
>= 1);
413 ASSERT(timeResolution
<= 1000000000);
414 res
:= time
.clock_gettime(time
.CLOCK_MONOTONIC
, tp
);
415 ASSERT(res
= 0, 100);
416 RETURN tp
.tv_sec
* LONG(timeResolution
) + tp
.tv_nsec
DIV LONG(1000000000 DIV timeResolution
)
423 PROCEDURE SearchProcVar
* (var
: INTEGER; VAR m
: Module
; VAR adr
: INTEGER);
425 adr
:= var
; m
:= NIL;
428 WHILE (m
# NIL) & ((var
< m
.code
) OR (var
>= m
.code
+ m
.csize
)) DO m
:= m
.next
END;
429 IF m
# NIL THEN DEC(adr
, m
.code
) END
433 (* -------------------- system memory management --------------------- *)
435 PROCEDURE AllocMem (size
: sysmman
.size_t
; VAR max
: sysmman
.size_t
): ADDRESS
;
436 VAR fd
, flags
, res
: fcntl
.int
; ptr
: ADDRESS
;
438 max
:= (size
+ pagesize
- 1) DIV pagesize
* pagesize
;
439 fd
:= fcntl
.open("/dev/zero", fcntl
.O_RDWR
, 0);
441 flags
:= sysmman
.PROT_READ
+ sysmman
.PROT_WRITE
;
442 ptr
:= sysmman
.mmap(0, max
, flags
, sysmman
.MAP_PRIVATE
, fd
, 0);
443 IF ptr
= sysmman
.MAP_FAILED
THEN ptr
:= 0 END;
444 res
:= unistd
.close(fd
);
452 PROCEDURE FreeMem (adr
: ADDRESS
; size
: sysmman
.size_t
);
455 size
:= (size
+ pagesize
- 1) DIV pagesize
* pagesize
;
456 res
:= sysmman
.munmap(adr
, size
);
460 PROCEDURE AllocHeapMem (size
: INTEGER; VAR c
: Cluster
);
461 CONST N
= 65536; (* cluster size for dll *)
462 VAR adr
, allocated
, newsize
: INTEGER;
465 ASSERT(size
> 0, 100); adr
:= 0;
467 adr
:= AllocMem(N
, newsize
);
471 adr
:= AllocMem(size
, newsize
);
474 IF adr
= 0 THEN c
:= NIL
476 c
:= S
.VAL(Cluster
, (adr
+ 15) DIV 16 * 16); c
.max
:= adr
;
477 c
.size
:= allocated
- (S
.VAL(INTEGER, c
) - adr
);
478 INC(used
, c
.size
); INC(total
, c
.size
)
480 ASSERT((adr
= 0) OR (adr
MOD 16 = 0) & (c
.size
>= size
), 101);
481 (* post: (c = NIL) OR (c MOD 16 = 0) & (c.size >= size) *)
484 PROCEDURE FreeHeapMem (c
: Cluster
);
486 DEC(used
, c
.size
); DEC(total
, c
.size
);
487 FreeMem(S
.VAL(ADDRESS
, c
.max
), c
.size
)
490 PROCEDURE HeapFull (size
: INTEGER): BOOLEAN;
495 PROCEDURE AllocModMem
* (descSize
, modSize
: INTEGER; VAR descAdr
, modAdr
: INTEGER);
497 descAdr
:= 0; modAdr
:= 0;
498 descAdr
:= AllocMem(descSize
, descSize
);
500 modAdr
:= AllocMem(modSize
, modSize
);
502 FreeMem(descAdr
, descSize
)
504 INC(used
, descSize
+ modSize
)
509 PROCEDURE DeallocModMem
* (descSize
, modSize
, descAdr
, modAdr
: INTEGER);
511 FreeMem(descAdr
, descSize
);
512 FreeMem(modAdr
, modSize
);
513 DEC(used
, descSize
+ modSize
)
516 PROCEDURE InvalModMem (modSize
, modAdr
: INTEGER);
518 FreeMem(modAdr
, modSize
)
521 PROCEDURE TryRead (from
, to
, c
: INTEGER);
522 VAR i
: INTEGER; x
: BYTE;
525 FOR i
:= from
TO to
DO
529 FOR i
:= to
TO from BY
-1 DO
535 PROCEDURE^ Try
* (h
: TryHandler
; a
, b
, c
: INTEGER);
537 PROCEDURE IsReadable
* (from
, to
: INTEGER): BOOLEAN;
541 Try(TryRead
, from
, to
, 0);
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: HALT(101) (* COM interface pointers not supported *)
592 ASSERT(~
ODD(eltyp
), 102) (* 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 Call
* (adr
: INTEGER; sig
: Signature
; IN par
: ARRAY OF INTEGER; n
: INTEGER): LONGINT;
917 (* -------------------- reference information (portable) --------------------- *)
919 PROCEDURE RefCh (VAR ref
: INTEGER; OUT ch
: SHORTCHAR
);
921 S
.GET(ref
, ch
); INC(ref
)
924 PROCEDURE RefNum (VAR ref
: INTEGER; OUT x
: INTEGER);
925 VAR s
, n
: INTEGER; ch
: SHORTCHAR
;
927 s
:= 0; n
:= 0; RefCh(ref
, ch
);
928 WHILE ORD(ch
) >= 128 DO INC(n
, ASH(ORD(ch
) - 128, s
) ); INC(s
, 7); RefCh(ref
, ch
) END;
929 x
:= n
+ ASH(ORD(ch
) MOD 64 - ORD(ch
) DIV 64 * 64, s
)
932 PROCEDURE RefName (VAR ref
: INTEGER; OUT n
: Utf8Name
);
933 VAR i
: INTEGER; ch
: SHORTCHAR
;
935 i
:= 0; RefCh(ref
, ch
);
936 WHILE ch
# 0X
DO n
[i
] := ch
; INC(i
); RefCh(ref
, ch
) END;
940 PROCEDURE GetRefProc
* (VAR ref
: INTEGER; OUT adr
: INTEGER; OUT name
: Utf8Name
);
944 WHILE ch
>= 0FDX
DO (* skip variables *)
945 INC(ref
); RefCh(ref
, ch
);
946 IF ch
= 10X
THEN INC(ref
, 4) END;
947 RefNum(ref
, adr
); RefName(ref
, name
); S
.GET(ref
, ch
)
949 WHILE (ch
> 0X
) & (ch
< 0FCX
) DO (* skip source refs *)
950 INC(ref
); RefNum(ref
, adr
); S
.GET(ref
, ch
)
952 IF ch
= 0FCX
THEN INC(ref
); RefNum(ref
, adr
); RefName(ref
, name
)
957 PROCEDURE GetRefVar
* (VAR ref
: INTEGER; OUT mode
, form
: SHORTCHAR
; OUT desc
: Type
; OUT adr
: INTEGER; OUT name
: Utf8Name
);
959 S
.GET(ref
, mode
); desc
:= NIL;
961 mode
:= SHORT(CHR(ORD(mode
) - 0FCH
));
962 INC(ref
); RefCh(ref
, form
);
964 S
.GET(ref
, desc
); INC(ref
, 4); form
:= SHORT(CHR(16 + desc
.id
MOD 4))
966 RefNum(ref
, adr
); RefName(ref
, name
)
968 mode
:= 0X
; form
:= 0X
; adr
:= 0
972 PROCEDURE SourcePos
* (mod
: Module
; codePos
: INTEGER): INTEGER;
973 VAR ref
, pos
, ad
, d
: INTEGER; ch
: SHORTCHAR
; name
: Utf8Name
;
975 IF mod
# NIL THEN (* mf, 12.02.04 *)
976 ref
:= mod
.refs
; pos
:= 0; ad
:= 0; S
.GET(ref
, ch
);
978 WHILE (ch
> 0X
) & (ch
< 0FCX
) DO (* srcref: {dAdr,dPos} *)
979 INC(ad
, ORD(ch
)); INC(ref
); RefNum(ref
, d
);
980 IF ad
> codePos
THEN RETURN pos
END;
981 INC(pos
, d
); S
.GET(ref
, ch
)
983 IF ch
= 0FCX
THEN (* proc: 0FCX,Adr,Name *)
984 INC(ref
); RefNum(ref
, d
); RefName(ref
, name
); S
.GET(ref
, ch
);
985 IF (d
> codePos
) & (pos
> 0) THEN RETURN pos
END
987 WHILE ch
>= 0FDX
DO (* skip variables: Mode, Form, adr, Name *)
988 INC(ref
); RefCh(ref
, ch
);
989 IF ch
= 10X
THEN INC(ref
, 4) END;
990 RefNum(ref
, d
); RefName(ref
, name
); S
.GET(ref
, ch
)
997 PROCEDURE LoadDll
* (IN name
: ARRAY OF CHAR; VAR ok
: BOOLEAN);
998 VAR h
: ADDRESS
; file
: Utf8Name
; res
: INTEGER;
1000 StringToUtf8(name
, file
, res
);
1002 h
:= dlfcn
.dlopen(file
, dlfcn
.RTLD_LAZY
+ dlfcn
.RTLD_GLOBAL
);
1009 PROCEDURE ThisDllObj
* (mode
, fprint
: INTEGER; IN dll
, name
: ARRAY OF CHAR): INTEGER;
1010 VAR h
, p
: ADDRESS
; file
, sym
: Utf8Name
; res
: INTEGER; err
: dlfcn
.int
;
1012 StringToUtf8(dll
, file
, res
);
1014 h
:= dlfcn
.dlopen(file
, dlfcn
.RTLD_LAZY
+ dlfcn
.RTLD_GLOBAL
);
1016 StringToUtf8(name
, sym
, res
);
1018 p
:= dlfcn
.dlsym(h
, sym
)
1022 err
:= dlfcn
.dlclose(h
);
1023 ASSERT(err
= 0, 100)
1033 (* -------------------- garbage collector (portable) --------------------- *)
1035 PROCEDURE Mark (this
: Block
);
1036 VAR father
, son
: Block
; tag
: Type
; flag
, offset
, actual
: INTEGER;
1038 IF ~
ODD(S
.VAL(INTEGER, this
.tag
)) THEN
1041 INC(S
.VAL(INTEGER, this
.tag
));
1042 flag
:= S
.VAL(INTEGER, this
.tag
) MOD 4;
1043 tag
:= S
.VAL(Type
, S
.VAL(INTEGER, this
.tag
) - flag
);
1044 IF flag
>= 2 THEN actual
:= this
.first
; this
.actual
:= actual
1045 ELSE actual
:= S
.ADR(this
.last
)
1048 offset
:= tag
.ptroffs
[0];
1050 INC(S
.VAL(INTEGER, tag
), offset
+ 4); (* restore tag *)
1051 IF (flag
>= 2) & (actual
< this
.last
) & (offset
< -4) THEN (* next array element *)
1052 INC(actual
, tag
.size
); this
.actual
:= actual
1054 this
.tag
:= S
.VAL(Type
, S
.VAL(INTEGER, tag
) + flag
);
1055 IF father
= NIL THEN RETURN END;
1056 son
:= this
; this
:= father
;
1057 flag
:= S
.VAL(INTEGER, this
.tag
) MOD 4;
1058 tag
:= S
.VAL(Type
, S
.VAL(INTEGER, this
.tag
) - flag
);
1059 offset
:= tag
.ptroffs
[0];
1060 IF flag
>= 2 THEN actual
:= this
.actual
ELSE actual
:= S
.ADR(this
.last
) END;
1061 S
.GET(actual
+ offset
, father
); S
.PUT(actual
+ offset
, S
.ADR(son
.last
));
1062 INC(S
.VAL(INTEGER, tag
), 4)
1065 S
.GET(actual
+ offset
, son
);
1067 DEC(S
.VAL(INTEGER, son
), 4);
1068 IF ~
ODD(S
.VAL(INTEGER, son
.tag
)) THEN (* down *)
1069 this
.tag
:= S
.VAL(Type
, S
.VAL(INTEGER, tag
) + flag
);
1070 S
.PUT(actual
+ offset
, father
); father
:= this
; this
:= son
;
1074 INC(S
.VAL(INTEGER, tag
), 4)
1081 PROCEDURE MarkGlobals
;
1082 VAR m
: Module
; i
, p
: INTEGER;
1086 IF m
.refcnt
>= 0 THEN
1088 WHILE i
< m
.nofptrs
DO
1089 S
.GET(m
.varBase
+ m
.ptrs
[i
], p
); INC(i
);
1090 IF p
# 0 THEN Mark(S
.VAL(Block
, p
- 4)) END
1097 PROCEDURE Next (b
: Block
): Block
; (* next block in same cluster *)
1100 S
.GET(S
.VAL(INTEGER, b
.tag
) DIV 4 * 4, size
);
1101 IF ODD(S
.VAL(INTEGER, b
.tag
) DIV 2) THEN INC(size
, b
.last
- S
.ADR(b
.last
)) END;
1102 RETURN S
.VAL(Block
, S
.VAL(INTEGER, b
) + (size
+ 19) DIV 16 * 16)
1105 PROCEDURE CheckCandidates
;
1106 (* pre: nofcand > 0 *)
1107 VAR i
, j
, h
, p
, end
: INTEGER; c
: Cluster
; blk
, next
: Block
;
1109 (* sort candidates (shellsort) *)
1110 h
:= 1; REPEAT h
:= h
*3 + 1 UNTIL h
> nofcand
;
1111 REPEAT h
:= h
DIV 3; i
:= h
;
1112 WHILE i
< nofcand
DO p
:= candidates
[i
]; j
:= i
;
1113 WHILE (j
>= h
) & (candidates
[j
-h
] > p
) DO
1114 candidates
[j
] := candidates
[j
-h
]; j
:= j
-h
1116 candidates
[j
] := p
; INC(i
)
1122 blk
:= S
.VAL(Block
, S
.VAL(INTEGER, c
) + 12);
1123 end
:= S
.VAL(INTEGER, blk
) + (c
.size
- 12) DIV 16 * 16;
1124 WHILE candidates
[i
] < S
.VAL(INTEGER, blk
) DO
1126 IF i
= nofcand
THEN RETURN END
1128 WHILE S
.VAL(INTEGER, blk
) < end
DO
1130 IF candidates
[i
] < S
.VAL(INTEGER, next
) THEN
1131 IF (S
.VAL(INTEGER, blk
.tag
) # S
.ADR(blk
.last
)) (* not a free block *)
1132 & (~strictStackSweep
OR (candidates
[i
] = S
.ADR(blk
.last
))) THEN
1137 IF i
= nofcand
THEN RETURN END
1138 UNTIL candidates
[i
] >= S
.VAL(INTEGER, next
)
1140 IF (S
.VAL(INTEGER, blk
.tag
) MOD 4 = 0) & (S
.VAL(INTEGER, blk
.tag
) # S
.ADR(blk
.last
))
1141 & (blk
.tag
.base
[0] = NIL) & (blk
.actual
> 0) THEN (* referenced interface record *)
1148 END CheckCandidates
;
1150 PROCEDURE MarkLocals
;
1151 VAR sp
, p
, min
, max
: INTEGER; c
: Cluster
;
1153 sp
:= S
.ADR(sp
); nofcand
:= 0; c
:= root
;
1154 WHILE c
.next
# NIL DO c
:= c
.next
END;
1155 min
:= S
.VAL(INTEGER, root
); max
:= S
.VAL(INTEGER, c
) + c
.size
;
1156 WHILE sp
< baseStack
DO
1158 IF (p
> min
) & (p
< max
) & (~strictStackSweep
OR (p
MOD 16 = 0)) THEN
1159 candidates
[nofcand
] := p
; INC(nofcand
);
1160 IF nofcand
= LEN(candidates
) - 1 THEN CheckCandidates
; nofcand
:= 0 END
1164 candidates
[nofcand
] := max
; INC(nofcand
); (* ensure complete scan for interface mark*)
1165 IF nofcand
> 0 THEN CheckCandidates
END
1168 PROCEDURE MarkFinObj
;
1171 wouldFinalize
:= FALSE
;
1174 IF ~
ODD(S
.VAL(INTEGER, f
.blk
.tag
)) THEN wouldFinalize
:= TRUE
END;
1179 WHILE f
# NIL DO IF ~
ODD(S
.VAL(INTEGER, f
.blk
.tag
)) THEN wouldFinalize
:= TRUE
END;
1185 PROCEDURE CheckFinalizers
;
1186 VAR f
, g
, h
, k
: FList
;
1188 f
:= finalizers
; g
:= NIL;
1189 IF hotFinalizers
= NIL THEN k
:= NIL
1192 WHILE k
.next
# NIL DO k
:= k
.next
END
1195 h
:= f
; f
:= f
.next
;
1196 IF ~
ODD(S
.VAL(INTEGER, h
.blk
.tag
)) THEN
1197 IF g
= NIL THEN finalizers
:= f
ELSE g
.next
:= f
END;
1198 IF k
= NIL THEN hotFinalizers
:= h
ELSE k
.next
:= h
END;
1199 k
:= h
; h
.next
:= NIL
1204 WHILE h
# NIL DO Mark(h
.blk
); h
:= h
.next
END
1205 END CheckFinalizers
;
1207 PROCEDURE ExecFinalizer (a
, b
, c
: INTEGER);
1208 VAR f
: FList
; fin
: PROCEDURE(this
: ANYPTR
);
1210 f
:= S
.VAL(FList
, a
);
1211 S
.GET(S
.VAL(INTEGER, f
.blk
.tag
) - 4, fin
); (* method 0 *)
1212 IF (fin
# NIL) & (f
.blk
.tag
.mod
.refcnt
>= 0) THEN fin(S
.VAL(ANYPTR
, S
.ADR(f
.blk
.last
))) END;
1215 PROCEDURE^ Try
* (h
: TryHandler
; a
, b
, c
: INTEGER); (* COMPILER DEPENDENT *)
1217 PROCEDURE CallFinalizers
;
1220 WHILE hotFinalizers
# NIL DO
1221 f
:= hotFinalizers
; hotFinalizers
:= hotFinalizers
.next
;
1222 Try(ExecFinalizer
, S
.VAL(INTEGER, f
), 0, 0)
1224 wouldFinalize
:= FALSE
1227 PROCEDURE Insert (blk
: FreeBlock
; size
: INTEGER); (* insert block in free list *)
1230 blk
.size
:= size
- 4; blk
.tag
:= S
.VAL(Type
, S
.ADR(blk
.size
));
1231 i
:= MIN(N
- 1, (blk
.size
DIV 16));
1232 blk
.next
:= free
[i
]; free
[i
] := blk
1235 PROCEDURE Sweep (dealloc
: BOOLEAN);
1236 VAR cluster
, last
, c
: Cluster
; blk
, next
: Block
; fblk
, b
, t
: FreeBlock
; end
, i
: INTEGER;
1238 cluster
:= root
; last
:= NIL; allocated
:= 0;
1240 REPEAT DEC(i
); free
[i
] := sentinel
UNTIL i
= 0;
1241 WHILE cluster
# NIL DO
1242 blk
:= S
.VAL(Block
, S
.VAL(INTEGER, cluster
) + 12);
1243 end
:= S
.VAL(INTEGER, blk
) + (cluster
.size
- 12) DIV 16 * 16;
1245 WHILE S
.VAL(INTEGER, blk
) < end
DO
1247 IF ODD(S
.VAL(INTEGER, blk
.tag
)) THEN
1249 Insert(fblk
, S
.VAL(INTEGER, blk
) - S
.VAL(INTEGER, fblk
));
1252 DEC(S
.VAL(INTEGER, blk
.tag
)); (* unmark *)
1253 INC(allocated
, S
.VAL(INTEGER, next
) - S
.VAL(INTEGER, blk
))
1254 ELSIF fblk
= NIL THEN
1255 fblk
:= S
.VAL(FreeBlock
, blk
)
1259 IF dealloc
& (S
.VAL(INTEGER, fblk
) = S
.VAL(INTEGER, cluster
) + 12) THEN (* deallocate cluster *)
1260 c
:= cluster
; cluster
:= cluster
.next
;
1261 IF last
= NIL THEN root
:= cluster
ELSE last
.next
:= cluster
END;
1264 IF fblk
# NIL THEN Insert(fblk
, end
- S
.VAL(INTEGER, fblk
)) END;
1265 last
:= cluster
; cluster
:= cluster
.next
1268 (* reverse free list *)
1272 b
:= free
[i
]; fblk
:= sentinel
;
1273 WHILE b
# sentinel
DO t
:= b
; b
:= t
.next
; t
.next
:= fblk
; fblk
:= t
END;
1281 CallFinalizers
; (* trap cleanup *)
1290 PROCEDURE FastCollect
*;
1300 PROCEDURE WouldFinalize
* (): BOOLEAN;
1302 RETURN wouldFinalize
1305 (* --------------------- memory allocation (portable) -------------------- *)
1307 PROCEDURE OldBlock (size
: INTEGER): FreeBlock
; (* size MOD 16 = 0 *)
1308 VAR b
, l
: FreeBlock
; s
, i
: INTEGER;
1311 i
:= MIN(N
- 1, s
DIV 16);
1312 WHILE (i
# N
- 1) & (free
[i
] = sentinel
) DO INC(i
) END;
1313 b
:= free
[i
]; l
:= NIL;
1314 WHILE b
.size
< s
DO l
:= b
; b
:= b
.next
END;
1315 IF b
# sentinel
THEN
1316 IF l
= NIL THEN free
[i
] := b
.next
ELSE l
.next
:= b
.next
END
1322 PROCEDURE LastBlock (limit
: INTEGER): FreeBlock
; (* size MOD 16 = 0 *)
1323 VAR b
, l
: FreeBlock
; s
, i
: INTEGER;
1328 b
:= free
[i
]; l
:= NIL;
1329 WHILE (b
# sentinel
) & (S
.VAL(INTEGER, b
) + b
.size
# s
) DO l
:= b
; b
:= b
.next
END;
1330 IF b
# sentinel
THEN
1331 IF l
= NIL THEN free
[i
] := b
.next
ELSE l
.next
:= b
.next
END
1335 UNTIL (b
# NIL) OR (i
= N
);
1339 PROCEDURE NewBlock (size
: INTEGER): Block
;
1340 VAR tsize
, a
, s
: INTEGER; b
: FreeBlock
; new
, c
: Cluster
; r
: Reducer
;
1342 ASSERT(size
>= 0, 20);
1343 IF size
> MAX(INTEGER) - 19 THEN RETURN NIL END;
1344 tsize
:= (size
+ 19) DIV 16 * 16;
1345 b
:= OldBlock(tsize
); (* 1) search for free block *)
1347 FastCollect
; b
:= OldBlock(tsize
); (* 2) collect *)
1349 Collect
; b
:= OldBlock(tsize
); (* 2a) fully collect *)
1352 AllocHeapMem(tsize
+ 12, new
); (* 3) allocate new cluster *)
1354 IF (root
= NIL) OR (S
.VAL(INTEGER, new
) < S
.VAL(INTEGER, root
)) THEN
1355 new
.next
:= root
; root
:= new
1358 WHILE (c
.next
# NIL) & (S
.VAL(INTEGER, new
) > S
.VAL(INTEGER, c
.next
)) DO c
:= c
.next
END;
1359 new
.next
:= c
.next
; c
.next
:= new
1361 b
:= S
.VAL(FreeBlock
, S
.VAL(INTEGER, new
) + 12);
1362 b
.size
:= (new
.size
- 12) DIV 16 * 16 - 4
1364 RETURN NIL (* 4) give up *)
1369 a
:= b
.size
+ 4 - tsize
;
1370 IF a
> 0 THEN Insert(S
.VAL(FreeBlock
, S
.VAL(INTEGER, b
) + tsize
), a
) END;
1371 IF size
> 0 THEN Erase(S
.ADR(b
.size
), (size
+ 3) DIV 4) END;
1372 INC(allocated
, tsize
);
1373 RETURN S
.VAL(Block
, b
)
1376 PROCEDURE Allocated
* (): INTEGER;
1381 PROCEDURE Used
* (): INTEGER;
1386 PROCEDURE Root
* (): INTEGER;
1388 RETURN S
.VAL(INTEGER, root
)
1391 (* -------------------- Trap Handling --------------------- *)
1393 PROCEDURE Start
* (code
: Command
);
1394 VAR res
: setjmp
.int
;
1397 S
.GETREG(SP
, baseStack
);
1398 res
:= setjmp
.sigsetjmp(startEnv
, 1);
1402 PROCEDURE Quit
* (exitCode
: INTEGER);
1403 VAR m
: Module
; term
: Command
; t
: BOOLEAN;
1405 trapViewer
:= NIL; trapChecker
:= NIL; restart
:= NIL;
1406 t
:= terminating
; terminating
:= TRUE
; m
:= modList
;
1407 WHILE m
# NIL DO (* call terminators *)
1408 IF ~static
OR ~t
THEN
1409 term
:= m
.term
; m
.term
:= NIL;
1410 IF term
# NIL THEN term() END
1415 hotFinalizers
:= finalizers
; finalizers
:= NIL;
1417 stdlib
.exit(exitCode
)
1420 PROCEDURE FatalError
* (id
: INTEGER; str
: ARRAY OF CHAR);
1421 VAR res
: stdio
.int
; title
: ARRAY 16 OF CHAR; text
: ARRAY 256 OF SHORTCHAR
;
1423 title
:= "Error xy";
1424 title
[6] := CHR(id
DIV 10 + ORD("0"));
1425 title
[7] := CHR(id
MOD 10 + ORD("0"));
1426 res
:= unistd
.write(2, S
.ADR(title
), 8);
1430 PROCEDURE DefaultTrapViewer
;
1431 VAR out
: ARRAY 2048 OF SHORTCHAR
; a
, b
, c
, len
, ref
, end
: INTEGER; mod
: Module
;
1432 modName
, name
: Name
; n
: Utf8Name
; res
: unistd
.int
;
1434 PROCEDURE WriteString (IN s
: ARRAY OF SHORTCHAR
);
1438 WHILE (len
< LEN(out
) - 1) & (s
[i
] # 0X
) DO out
[len
] := s
[i
]; INC(i
); INC(len
) END
1441 PROCEDURE WriteHex (x
, n
: INTEGER);
1444 IF len
+ n
< LEN(out
) THEN
1447 y
:= x
MOD 16; x
:= x
DIV 16;
1448 IF y
> 9 THEN y
:= y
+ (ORD("A") - ORD("0") - 10) END;
1449 out
[i
] := SHORT(CHR(y
+ ORD("0"))); DEC(i
)
1457 IF len
< LEN(out
) - 1 THEN out
[len
] := 0AX
; INC(len
) END
1462 WriteString("====== ");
1463 IF err
= 129 THEN WriteString("invalid with")
1464 ELSIF err
= 130 THEN WriteString("invalid case")
1465 ELSIF err
= 131 THEN WriteString("function without return")
1466 ELSIF err
= 132 THEN WriteString("type guard")
1467 ELSIF err
= 133 THEN WriteString("implied type guard")
1468 ELSIF err
= 134 THEN WriteString("value out of range")
1469 ELSIF err
= 135 THEN WriteString("index out of range")
1470 ELSIF err
= 136 THEN WriteString("string too long")
1471 ELSIF err
= 137 THEN WriteString("stack overflow")
1472 ELSIF err
= 138 THEN WriteString("integer overflow")
1473 ELSIF err
= 139 THEN WriteString("division by zero")
1474 ELSIF err
= 140 THEN WriteString("infinite real result")
1475 ELSIF err
= 141 THEN WriteString("real underflow")
1476 ELSIF err
= 142 THEN WriteString("real overflow")
1477 ELSIF err
= 143 THEN WriteString("undefined real result")
1478 ELSIF err
= 144 THEN WriteString("not a number")
1479 ELSIF err
= 200 THEN WriteString("keyboard interrupt")
1480 ELSIF err
= 201 THEN WriteString("NIL dereference")
1481 ELSIF err
= 202 THEN WriteString("illegal instruction: ");
1483 ELSIF err
= 203 THEN WriteString("illegal memory read [ad = ");
1484 WriteHex(val
, 8); WriteString("]")
1485 ELSIF err
= 204 THEN WriteString("illegal memory write [ad = ");
1486 WriteHex(val
, 8); WriteString("]")
1487 ELSIF err
= 205 THEN WriteString("illegal execution [ad = ");
1488 WriteHex(val
, 8); WriteString("]")
1489 ELSIF err
= 257 THEN WriteString("out of memory")
1490 ELSIF err
= 10001H
THEN WriteString("bus error")
1491 ELSIF err
= 10002H
THEN WriteString("address error")
1492 ELSIF err
= 10007H
THEN WriteString("fpu error")
1493 ELSIF err
< 0 THEN WriteString("exception #"); WriteHex(-err
, 2)
1494 ELSE err
:= err
DIV 100 * 256 + err
DIV 10 MOD 10 * 16 + err
MOD 10;
1495 WriteString("trap #"); WriteHex(err
, 3)
1497 WriteString(" ======");
1498 a
:= pc
; b
:= fp
; c
:= 12;
1500 WriteLn
; WriteString("- ");
1502 WHILE (mod
# NIL) & ((a
< mod
.code
) OR (a
>= mod
.code
+ mod
.csize
)) DO mod
:= mod
.next
END;
1505 IF mod
.refcnt
>= 0 THEN
1506 GetModName(mod
, modName
); WriteString(SHORT(modName
)); ref
:= mod
.refs
;
1507 REPEAT GetRefProc(ref
, end
, n
) UNTIL (end
= 0) OR (a
< end
);
1509 Utf8ToString(n
, name
, res
); WriteString("."); WriteString(SHORT(name
))
1512 GetModName(mod
, modName
); WriteString("("); WriteString(SHORT(modName
)); WriteString(")")
1516 WriteString("(pc="); WriteHex(a
, 8);
1517 WriteString(", fp="); WriteHex(b
, 8); WriteString(")");
1518 IF (b
>= sp
) & (b
< stack
) THEN
1519 S
.GET(b
+4, a
); (* stacked pc *)
1520 S
.GET(b
, b
); (* dynamic link *)
1526 res
:= unistd
.write(2, S
.ADR(out
), len
)
1527 END DefaultTrapViewer
;
1529 PROCEDURE TrapCleanup
;
1532 WHILE trapStack
# NIL DO
1533 t
:= trapStack
; trapStack
:= trapStack
.next
; t
.Cleanup
1535 IF (trapChecker
# NIL) & (err
# 128) THEN trapChecker
END
1538 PROCEDURE SetTrapGuard
* (on
: BOOLEAN);
1543 PROCEDURE Try
* (h
: TryHandler
; a
, b
, c
: INTEGER);
1544 VAR oldIsTry
: BOOLEAN; oldTryEnv
: setjmp
.jmp_buf
; res
: setjmp
.int
;
1546 oldIsTry
:= isTry
; oldTryEnv
:= tryEnv
;
1548 res
:= setjmp
._setjmp(tryEnv
);
1549 IF res
= 0 THEN h(a
, b
, c
) END;
1550 isTry
:= oldIsTry
; tryEnv
:= oldTryEnv
1553 PROCEDURE Trap
* (n
: INTEGER);
1557 IF ~secondTrap
THEN trapped
:= FALSE
; secondTrap
:= TRUE
END
1559 IF n
>= 0 THEN err
:= n
1560 ELSE err
:= -n
+ 128
1562 pc
:= 0; sp
:= 0; fp
:= 0; stack
:= 0; val
:= 0;
1567 setjmp
._longjmp(tryEnv
, 1)
1569 IF err
= 128 THEN (* do nothing *)
1570 ELSIF (trapViewer
# NIL) & (restart
# NIL) & ~trapped
& ~guarded
THEN
1571 trapped
:= TRUE
; trapViewer()
1572 ELSE DefaultTrapViewer
1574 trapped
:= FALSE
; secondTrap
:= FALSE
;
1575 IF restart
# NIL THEN
1576 setjmp
.siglongjmp(startEnv
, 1)
1581 PROCEDURE [ccall
] TrapHandler (signo
: signal
.int
; IN info
: signal
.siginfo_t
; context
: ADDRESS
);
1582 VAR res
: signal
.int
;
1586 IF ~secondTrap
THEN trapped
:= FALSE
; secondTrap
:= TRUE
END
1588 err
:= -signo
; pc
:= 0; sp
:= 0; fp
:= 0; stack
:= baseStack
; val
:= 0;
1591 val
:= info
.si_code
;
1592 pc
:= info
.info
.sigfpe
.si_addr
;
1593 CASE info
.si_code
OF
1594 | signal
.FPE_INTDIV
: err
:= 139 (* division by zero *)
1595 | signal
.FPE_INTOVF
: err
:= 138 (* integer overflow *)
1596 | signal
.FPE_FLTDIV
: err
:= 140 (* fpu: division by zero *)
1597 | signal
.FPE_FLTOVF
: err
:= 142 (* fpu: overflow *)
1598 | signal
.FPE_FLTUND
: err
:= 141 (* fpu: underflow *)
1599 (* !!! | signal.FPE_FLTRES: err := ??? (* fpu: *) *)
1600 | signal
.FPE_FLTINV
: err
:= 143 (* val := opcode *) (* fpu: invalid op *)
1601 (* !!! | signal.FPE_FLTSUB: err := ??? (* fpu: *) *)
1605 val
:= info
.si_code
;
1606 err
:= 200 (* keyboard interrupt *)
1608 val
:= info
.info
.sigsegv
.si_addr
;
1609 err
:= 203 (* illigal read *)
1611 val
:= info
.info
.sigbus
.si_addr
;
1612 err
:= 10001H (* bus error *)
1614 pc
:= info
.info
.sigill
.si_addr
;
1615 err
:= 202; (* illigal instruction *)
1616 IF IsReadable(pc
, pc
+ 4) THEN
1618 (* !!! err := halt code *)
1626 setjmp
._longjmp(tryEnv
, 1)
1628 IF err
= 128 THEN (* do nothing *)
1629 ELSIF (trapViewer
# NIL) & (restart
# NIL) & ~trapped
& ~guarded
THEN
1630 trapped
:= TRUE
; trapViewer()
1631 ELSE DefaultTrapViewer
1633 trapped
:= FALSE
; secondTrap
:= FALSE
;
1634 IF restart
# NIL THEN
1635 setjmp
.siglongjmp(startEnv
, 1)
1640 (* -------------------- Initialization --------------------- *)
1642 PROCEDURE InstallTrap (signo
: signal
.int
);
1643 VAR act
: signal
.struct_sigaction
; res
: signal
.int
;
1645 act
.handler
.sa_handler
:= NIL;
1646 res
:= signal
.sigemptyset(act
.sa_mask
);
1647 act
.sa_flags
:= signal
.SA_NODEFER
+ signal
.SA_SIGINFO
;
1648 act
.handler
.sa_sigaction
:= TrapHandler
;
1649 res
:= signal
.sigaction(signo
, act
, NIL);
1652 PROCEDURE InstallTrapVectors
;
1654 InstallTrap(signal
.SIGFPE
);
1655 InstallTrap(signal
.SIGINT
);
1656 InstallTrap(signal
.SIGSEGV
);
1657 InstallTrap(signal
.SIGBUS
);
1658 InstallTrap(signal
.SIGILL
)
1659 END InstallTrapVectors
;
1661 PROCEDURE RemoveTrapVectors
;
1662 END RemoveTrapVectors
;
1667 pagesize
:= unistd
.sysconf(unistd
._SC_PAGESIZE
);
1670 allocated
:= 0; total
:= 0; used
:= 0;
1671 sentinelBlock
.size
:= MAX(INTEGER);
1672 sentinel
:= S
.ADR(sentinelBlock
);
1674 REPEAT DEC(i
); free
[i
] := sentinel
UNTIL i
= 0;
1682 InitModule(modList
);
1683 IF ~inDll
THEN Quit(1) END
1688 IF modList
= NIL THEN (* only once *)
1689 S
.GETREG(SP
, baseStack
);
1690 S
.GET(baseStack
+ 16, argc
);
1691 argv
:= S
.VAL(ArrStrPtr
, baseStack
+ 20);
1692 S
.GETREG(ML
, modList
); (* linker loads module list to BX *)
1693 static
:= init
IN modList
.opts
;
1694 inDll
:= dll
IN modList
.opts
;
1698 IF ~terminating
THEN
1699 terminating
:= TRUE
;