DEADSOFTWARE

5773b4ebcdedcbc3fe3124c6bb9fe428d8043569
[cpc.git] / src / native / posix / generic / System / Mod / Kernel.cp
1 MODULE Kernel;
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;
8 (* init fpu? *)
9 (* add signal blocking to avoid race conditions in Try/Trap/TrapHandler *)
10 (* add BeepHook for Beep *)
11 (* implement Call using libffi *)
13 CONST
14 nameLen* = 256;
16 littleEndian* = TRUE;
17 timeResolution* = 1000; (* ticks per second *)
19 processor* = 1; (* generic c *)
21 objType* = "ocf"; (* file types *)
22 symType* = "osf";
23 docType* = "odc";
25 (* loader constants *)
26 done* = 0;
27 fileNotFound* = 1;
28 syntaxError* = 2;
29 objNotFound* = 3;
30 illegalFPrint* = 4;
31 cyclicImport* = 5;
32 noMem* = 6;
33 commNotFound* = 7;
34 commSyntaxError* = 8;
35 moduleNotFound* = 9;
37 any = 1000000;
39 CX = 1;
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;
53 TYPE
54 Name* = ARRAY nameLen OF CHAR;
55 Utf8Name* = ARRAY nameLen OF SHORTCHAR;
56 Command* = PROCEDURE;
58 Module* = POINTER TO RECORD [untagged]
59 next-: Module;
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) *)
73 name-: Utf8Name
74 END;
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 *)
79 mod-: Module;
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 *)
84 END;
86 Object* = POINTER TO ObjDesc;
88 ObjDesc* = RECORD [untagged]
89 fprint-: INTEGER;
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 *)
93 END;
95 Directory* = POINTER TO RECORD [untagged]
96 num-: INTEGER; (* number of entries *)
97 obj-: ARRAY any OF ObjDesc (* array of any length *)
98 END;
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 *)
106 END
107 END;
109 Handler* = PROCEDURE;
111 Reducer* = POINTER TO ABSTRACT RECORD
112 next: Reducer
113 END;
115 Identifier* = ABSTRACT RECORD
116 typ*: INTEGER;
117 obj-: ANYPTR
118 END;
120 TrapCleaner* = POINTER TO ABSTRACT RECORD
121 next: TrapCleaner
122 END;
124 TryHandler* = PROCEDURE (a, b, c: INTEGER);
126 (* meta extension suport *)
128 ItemExt* = POINTER TO ABSTRACT RECORD END;
130 ItemAttr* = RECORD
131 obj*, vis*, typ*, adr*: INTEGER;
132 mod*: Module;
133 desc*: Type;
134 ptr*: S.PTR;
135 ext*: ItemExt
136 END;
138 Hook* = POINTER TO ABSTRACT RECORD END;
140 LoaderHook* = POINTER TO ABSTRACT RECORD (Hook)
141 res*: INTEGER;
142 importing*, imported*, object*: ARRAY 256 OF CHAR
143 END;
145 Block = POINTER TO RECORD [untagged]
146 tag: Type;
147 last: INTEGER; (* arrays: last element *)
148 actual: INTEGER; (* arrays: used during mark phase *)
149 first: INTEGER (* arrays: first element *)
150 END;
152 FreeBlock = POINTER TO FreeDesc;
154 FreeDesc = RECORD [untagged]
155 tag: Type; (* f.tag = ADR(f.size) *)
156 size: INTEGER;
157 next: FreeBlock
158 END;
160 Cluster = POINTER TO RECORD [untagged]
161 size: INTEGER; (* total size *)
162 next: Cluster;
163 max: INTEGER (* exe: reserved size, dll: original address *)
164 (* start of first block *)
165 END;
167 FList = POINTER TO RECORD
168 next: FList;
169 blk: Block;
170 iptr, aiptr: BOOLEAN
171 END;
173 CList = POINTER TO RECORD
174 next: CList;
175 do: Command;
176 trapped: BOOLEAN
177 END;
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;
199 VAR
200 baseStack: INTEGER;
201 root: Cluster;
202 modList-: Module;
203 trapCount-: INTEGER;
204 err-, pc-, sp-, fp-, stack-, val-: INTEGER;
206 isTry: BOOLEAN;
207 startEnv: setjmp.sigjmp_buf;
208 tryEnv: setjmp.jmp_buf;
210 argc-: INTEGER;
211 argv-: ArrStrPtr;
212 pagesize: unistd.long;
214 free: ARRAY N OF FreeBlock; (* free list *)
215 sentinelBlock: FreeDesc;
216 sentinel: FreeBlock;
217 candidates: ARRAY 1024 OF INTEGER;
218 nofcand: 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 *)
222 finalizers: FList;
223 hotFinalizers: FList;
224 cleaners: CList;
225 reducers: Reducer;
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;
233 restart: Command;
235 loader: LoaderHook;
236 loadres: INTEGER;
238 wouldFinalize: BOOLEAN;
240 watcher*: PROCEDURE (event: INTEGER); (* for debugging *)
242 PROCEDURE Erase (adr, words: INTEGER);
243 BEGIN
244 ASSERT(words >= 0, 20);
245 WHILE words > 0 DO
246 S.PUT(adr, 0);
247 INC(adr, 4);
248 DEC(words)
249 END
250 END Erase;
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;
293 BEGIN
294 RETURN wctype.iswupper(ORD(ch)) # 0
295 END IsUpper;
297 PROCEDURE Upper* (ch: CHAR): CHAR;
298 BEGIN
299 RETURN CHR(wctype.towupper(ORD(ch)))
300 END Upper;
302 PROCEDURE IsLower* (ch: CHAR): BOOLEAN;
303 BEGIN
304 RETURN wctype.iswlower(ORD(ch)) # 0
305 END IsLower;
307 PROCEDURE Lower* (ch: CHAR): CHAR;
308 BEGIN
309 RETURN CHR(wctype.towlower(ORD(ch)))
310 END Lower;
312 PROCEDURE IsAlpha* (ch: CHAR): BOOLEAN;
313 BEGIN
314 RETURN wctype.iswalpha(ORD(ch)) # 0
315 END IsAlpha;
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*)
322 END FormatError;
324 BEGIN
325 ch := in[0]; i := 1; j := 0; max := LEN(out) - 1;
326 WHILE (ch # 0X) & (j < max) DO
327 IF ch < 80X THEN
328 out[j] := ch; INC(j)
329 ELSIF ch < 0E0X THEN
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)
335 ELSIF ch < 0F0X THEN
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)
342 ELSE
343 FormatError; RETURN
344 END ;
345 ch := in[i]; INC(i)
346 END;
347 out[j] := 0X;
348 IF ch = 0X THEN res := 0 (*ok*) ELSE res := 1 (*truncated*) END
349 END Utf8ToString;
351 PROCEDURE StringToUtf8* (IN in: ARRAY OF CHAR; OUT out: ARRAY OF SHORTCHAR; OUT res: INTEGER);
352 VAR i, j, val, max: INTEGER;
353 BEGIN
354 i := 0; j := 0; max := LEN(out) - 3;
355 WHILE (in[i] # 0X) & (j < max) DO
356 val := ORD(in[i]); INC(i);
357 IF val < 128 THEN
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)
362 ELSE
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)
366 END;
367 END;
368 out[j] := 0X;
369 IF in[i] = 0X THEN res := 0 (*ok*) ELSE res := 1 (*truncated*) END
370 END StringToUtf8;
372 PROCEDURE SplitName* (name: ARRAY OF CHAR; VAR head, tail: ARRAY OF CHAR);
373 (* portable *)
374 VAR i, j: INTEGER; ch, lch: CHAR;
375 BEGIN
376 i := 0; ch := name[0];
377 IF ch # 0X THEN
378 REPEAT
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;
384 tail[j] := 0X;
385 IF tail = "" THEN tail := head$; head := "" END
386 ELSE head := ""; tail := ""
387 END
388 END SplitName;
390 PROCEDURE MakeFileName* (VAR name: ARRAY OF CHAR; type: ARRAY OF CHAR);
391 VAR i, j: INTEGER; ext: ARRAY 8 OF CHAR; ch: CHAR;
392 BEGIN
393 i := 0;
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
397 ELSE
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];
401 WHILE ch # 0X DO
402 name[i] := Lower(ch); INC(i); INC(j); ch := ext[j]
403 END;
404 name[i] := 0X
405 END
406 END
407 END MakeFileName;
409 PROCEDURE Time* (): LONGINT;
410 VAR res: time.int; tp: time.struct_timespec;
411 BEGIN
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)
417 END Time;
419 PROCEDURE Beep*;
420 (* !!! *)
421 END Beep;
423 PROCEDURE SearchProcVar* (var: INTEGER; VAR m: Module; VAR adr: INTEGER);
424 BEGIN
425 adr := var; m := NIL;
426 IF var # 0 THEN
427 m := modList;
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
430 END
431 END SearchProcVar;
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;
437 BEGIN
438 max := (size + pagesize - 1) DIV pagesize * pagesize;
439 fd := fcntl.open("/dev/zero", fcntl.O_RDWR, 0);
440 IF fd # -1 THEN
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);
445 ASSERT(res = 0, 100)
446 ELSE
447 ptr := 0
448 END;
449 RETURN ptr
450 END AllocMem;
452 PROCEDURE FreeMem (adr: ADDRESS; size: sysmman.size_t);
453 VAR res: types.int;
454 BEGIN
455 size := (size + pagesize - 1) DIV pagesize * pagesize;
456 res := sysmman.munmap(adr, size);
457 ASSERT(res = 0, 100)
458 END FreeMem;
460 PROCEDURE AllocHeapMem (size: INTEGER; VAR c: Cluster);
461 CONST N = 65536; (* cluster size for dll *)
462 VAR adr, allocated, newsize: INTEGER;
463 BEGIN
464 INC(size, 16);
465 ASSERT(size > 0, 100); adr := 0;
466 IF size < N THEN
467 adr := AllocMem(N, newsize);
468 allocated := newsize
469 END;
470 IF adr = 0 THEN
471 adr := AllocMem(size, newsize);
472 allocated := newsize
473 END;
474 IF adr = 0 THEN c := NIL
475 ELSE
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)
479 END;
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) *)
482 END AllocHeapMem;
484 PROCEDURE FreeHeapMem (c: Cluster);
485 BEGIN
486 DEC(used, c.size); DEC(total, c.size);
487 FreeMem(S.VAL(ADDRESS, c.max), c.size)
488 END FreeHeapMem;
490 PROCEDURE HeapFull (size: INTEGER): BOOLEAN;
491 BEGIN
492 RETURN TRUE
493 END HeapFull;
495 PROCEDURE AllocModMem* (descSize, modSize: INTEGER; VAR descAdr, modAdr: INTEGER);
496 BEGIN
497 descAdr := 0; modAdr := 0;
498 descAdr := AllocMem(descSize, descSize);
499 IF descAdr # 0 THEN
500 modAdr := AllocMem(modSize, modSize);
501 IF modAdr = 0 THEN
502 FreeMem(descAdr, descSize)
503 ELSE
504 INC(used, descSize + modSize)
505 END
506 END
507 END AllocModMem;
509 PROCEDURE DeallocModMem* (descSize, modSize, descAdr, modAdr: INTEGER);
510 BEGIN
511 FreeMem(descAdr, descSize);
512 FreeMem(modAdr, modSize);
513 DEC(used, descSize + modSize)
514 END DeallocModMem;
516 PROCEDURE InvalModMem (modSize, modAdr: INTEGER);
517 BEGIN
518 FreeMem(modAdr, modSize)
519 END InvalModMem;
521 PROCEDURE TryRead (from, to, c: INTEGER);
522 VAR i: INTEGER; x: BYTE;
523 BEGIN
524 IF from <= to THEN
525 FOR i := from TO to DO
526 S.GET(i, x)
527 END
528 ELSE
529 FOR i := to TO from BY -1 DO
530 S.GET(i, x)
531 END
532 END;
533 END TryRead;
535 PROCEDURE^ Try* (h: TryHandler; a, b, c: INTEGER);
537 PROCEDURE IsReadable* (from, to: INTEGER): BOOLEAN;
538 VAR i: INTEGER;
539 BEGIN
540 i := trapCount;
541 Try(TryRead, from, to, 0);
542 RETURN trapCount = i
543 END IsReadable;
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;
551 BEGIN
552 IF ~ODD(typ) THEN
553 tag := S.VAL(Type, typ);
554 b := NewBlock(tag.size);
555 IF b # NIL THEN
556 b.tag := tag;
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
562 END;
563 adr := S.ADR(b.last)
564 ELSE
565 adr := 0
566 END
567 ELSE
568 HALT(100) (* COM interface pointers not supported *)
569 END;
570 RETURN adr
571 END NewRec;
573 PROCEDURE NewArr* (eltyp, nofelem, nofdim: INTEGER): INTEGER; (* impl. of NEW(ptr, dim0, dim1, ...) *)
574 VAR b: Block; size, headSize: INTEGER; t: Type;
575 BEGIN
576 CASE eltyp OF
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 *)
591 ELSE
592 ASSERT(~ODD(eltyp), 102) (* COM interface pointers not supported *)
593 END;
594 t := S.VAL(Type, eltyp);
595 headSize := 4 * nofdim + 12;
596 size := headSize + nofelem * t.size;
597 b := NewBlock(size);
598 IF b # NIL THEN
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 *)
602 RETURN S.ADR(b.last)
603 ELSE
604 RETURN 0
605 END;
606 END NewArr;
608 (* -------------------- handler installation (portable) --------------------- *)
610 PROCEDURE ThisFinObj* (VAR id: Identifier): ANYPTR;
611 VAR l: FList;
612 BEGIN
613 ASSERT(id.typ # 0, 100);
614 l := finalizers;
615 WHILE l # NIL DO
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
619 END;
620 l := l.next
621 END;
622 RETURN NIL
623 END ThisFinObj;
625 PROCEDURE InstallReducer* (r: Reducer);
626 BEGIN
627 r.next := reducers; reducers := r
628 END InstallReducer;
630 PROCEDURE InstallTrapViewer* (h: Handler);
631 BEGIN
632 trapViewer := h
633 END InstallTrapViewer;
635 PROCEDURE InstallTrapChecker* (h: Handler);
636 BEGIN
637 trapChecker := h
638 END InstallTrapChecker;
640 PROCEDURE PushTrapCleaner* (c: TrapCleaner);
641 VAR t: TrapCleaner;
642 BEGIN
643 t := trapStack; WHILE (t # NIL) & (t # c) DO t := t.next END;
644 ASSERT(t = NIL, 20);
645 c.next := trapStack; trapStack := c
646 END PushTrapCleaner;
648 PROCEDURE PopTrapCleaner* (c: TrapCleaner);
649 VAR t: TrapCleaner;
650 BEGIN
651 t := NIL;
652 WHILE (trapStack # NIL) & (t # c) DO
653 t := trapStack; trapStack := trapStack.next
654 END
655 END PopTrapCleaner;
657 PROCEDURE InstallCleaner* (p: Command);
658 VAR c: CList;
659 BEGIN
660 c := S.VAL(CList, NewRec(S.TYP(CList))); (* NEW(c) *)
661 c.do := p; c.trapped := FALSE; c.next := cleaners; cleaners := c
662 END InstallCleaner;
664 PROCEDURE RemoveCleaner* (p: Command);
665 VAR c0, c: CList;
666 BEGIN
667 c := cleaners; c0 := NIL;
668 WHILE (c # NIL) & (c.do # p) DO c0 := c; c := c.next END;
669 IF c # NIL THEN
670 IF c0 = NIL THEN cleaners := cleaners.next ELSE c0.next := c.next END
671 END
672 END RemoveCleaner;
674 PROCEDURE Cleanup*;
675 VAR c, c0: CList;
676 BEGIN
677 c := cleaners; c0 := NIL;
678 WHILE c # NIL DO
679 IF ~c.trapped THEN
680 c.trapped := TRUE; c.do; c.trapped := FALSE; c0 := c
681 ELSE
682 IF c0 = NIL THEN cleaners := cleaners.next
683 ELSE c0.next := c.next
684 END
685 END;
686 c := c.next
687 END
688 END Cleanup;
690 (* -------------------- meta information (portable) --------------------- *)
692 PROCEDURE (h: LoaderHook) ThisMod* (IN name: ARRAY OF CHAR): Module, NEW, ABSTRACT;
694 PROCEDURE SetLoaderHook*(h: LoaderHook);
695 BEGIN
696 loader := h
697 END SetLoaderHook;
699 PROCEDURE InitModule (mod: Module); (* initialize linked modules *)
700 VAR body: Command;
701 BEGIN
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);
706 actual := mod;
707 body(); actual := NIL
708 END
709 END InitModule;
711 PROCEDURE ThisLoadedMod* (IN name: ARRAY OF CHAR): Module; (* loaded modules only *)
712 VAR m: Module; res: INTEGER; n: Utf8Name;
713 BEGIN
714 StringToUtf8(name, n, res); ASSERT(res = 0);
715 loadres := done;
716 m := modList;
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;
720 RETURN m
721 END ThisLoadedMod;
723 PROCEDURE ThisMod* (IN name: ARRAY OF CHAR): Module;
724 BEGIN
725 IF loader # NIL THEN
726 loader.res := done;
727 RETURN loader.ThisMod(name)
728 ELSE
729 RETURN ThisLoadedMod(name)
730 END
731 END ThisMod;
733 PROCEDURE LoadMod* (IN name: ARRAY OF CHAR);
734 VAR m: Module;
735 BEGIN
736 m := ThisMod(name)
737 END LoadMod;
739 PROCEDURE GetLoaderResult* (OUT res: INTEGER; OUT importing, imported, object: ARRAY OF CHAR);
740 BEGIN
741 IF loader # NIL THEN
742 res := loader.res;
743 importing := loader.importing$;
744 imported := loader.imported$;
745 object := loader.object$
746 ELSE
747 res := loadres;
748 importing := "";
749 imported := "";
750 object := ""
751 END
752 END GetLoaderResult;
754 PROCEDURE ThisObject* (mod: Module; IN name: ARRAY OF CHAR): Object;
755 VAR l, r, m, res: INTEGER; p: StrPtr; n: Utf8Name;
756 BEGIN
757 StringToUtf8(name, n, res); ASSERT(res = 0);
758 l := 0; r := mod.export.num;
759 WHILE l < r DO (* binary search *)
760 m := (l + r) DIV 2;
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
764 END;
765 RETURN NIL
766 END ThisObject;
768 PROCEDURE ThisDesc* (mod: Module; fprint: INTEGER): Object;
769 VAR i, n: INTEGER;
770 BEGIN
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;
774 INC(i)
775 END;
776 RETURN NIL
777 END ThisDesc;
779 PROCEDURE ThisField* (rec: Type; IN name: ARRAY OF CHAR): Object;
780 VAR n, res: INTEGER; p: StrPtr; obj: Object; m: Module; nn: Utf8Name;
781 BEGIN
782 StringToUtf8(name, nn, res); ASSERT(res = 0);
783 m := rec.mod;
784 obj := S.VAL(Object, S.ADR(rec.fields.obj[0])); n := rec.fields.num;
785 WHILE n > 0 DO
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)
789 END;
790 RETURN NIL
791 END ThisField;
793 PROCEDURE ThisCommand* (mod: Module; IN name: ARRAY OF CHAR): Command;
794 VAR x: Object; sig: Signature;
795 BEGIN
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
800 END;
801 RETURN NIL
802 END ThisCommand;
804 PROCEDURE ThisType* (mod: Module; IN name: ARRAY OF CHAR): Type;
805 VAR x: Object;
806 BEGIN
807 x := ThisObject(mod, name);
808 IF (x # NIL) & (x.id MOD 16 = mTyp) & (S.VAL(INTEGER, x.struct) DIV 256 # 0) THEN
809 RETURN x.struct
810 ELSE
811 RETURN NIL
812 END
813 END ThisType;
815 PROCEDURE TypeOf* (IN rec: ANYREC): Type;
816 BEGIN
817 RETURN S.VAL(Type, S.TYP(rec))
818 END TypeOf;
820 PROCEDURE LevelOf* (t: Type): SHORTINT;
821 BEGIN
822 RETURN SHORT(t.id DIV 16 MOD 16)
823 END LevelOf;
825 PROCEDURE NewObj* (VAR o: S.PTR; t: Type);
826 VAR i: INTEGER;
827 BEGIN
828 IF t.size = -1 THEN o := NIL
829 ELSE
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 *)
833 END
834 END NewObj;
836 PROCEDURE GetModName* (mod: Module; OUT name: Name);
837 VAR res: INTEGER;
838 BEGIN
839 Utf8ToString(mod.name, name, res); ASSERT(res = 0)
840 END GetModName;
842 PROCEDURE GetObjName* (mod: Module; obj: Object; OUT name: Name);
843 VAR p: StrPtr; res: INTEGER;
844 BEGIN
845 p := S.VAL(StrPtr, S.ADR(mod.names[obj.id DIV 256]));
846 Utf8ToString(p^$, name, res); ASSERT(res = 0)
847 END GetObjName;
849 PROCEDURE GetTypeName* (t: Type; OUT name: Name);
850 VAR p: StrPtr; res: INTEGER;
851 BEGIN
852 p := S.VAL(StrPtr, S.ADR(t.mod.names[t.id DIV 256]));
853 Utf8ToString(p^$, name, res); ASSERT(res = 0)
854 END GetTypeName;
856 PROCEDURE RegisterMod* (mod: Module);
857 VAR i: INTEGER; epoch: time.time_t; tm: time.struct_tm; ptm: time.Pstruct_tm;
858 BEGIN
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;
862 INC(i)
863 END;
864 epoch := time.time(NIL);
865 ptm := time.localtime_r(epoch, tm);
866 IF ptm # NIL THEN
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)
873 ELSE
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;
879 mod.loadTime[5] := 0
880 END;
881 IF ~(init IN mod.opts) THEN InitModule(mod) END
882 END RegisterMod;
884 PROCEDURE^ Collect*;
886 PROCEDURE UnloadMod* (mod: Module);
887 VAR i: INTEGER; t: Command;
888 BEGIN
889 IF mod.refcnt = 0 THEN
890 t := mod.term; mod.term := NIL;
891 IF t # NIL THEN t() END; (* terminate module *)
892 i := 0;
893 WHILE i < mod.nofptrs DO (* release global pointers *)
894 S.PUT(mod.varBase + mod.ptrs[i], 0); INC(i)
895 END;
896 Collect; (* call finalizers *)
897 i := 0;
898 WHILE i < mod.nofimps DO (* release imported modules *)
899 IF mod.imports[i] # NIL THEN DEC(mod.imports[i].refcnt) END;
900 INC(i)
901 END;
902 mod.refcnt := -1;
903 IF dyn IN mod.opts THEN (* release memory *)
904 InvalModMem(mod.data + mod.dsize - mod.refs, mod.refs)
905 END
906 END
907 END UnloadMod;
909 (* -------------------- dynamic procedure call --------------------- *)
911 PROCEDURE Call* (adr: INTEGER; sig: Signature; IN par: ARRAY OF INTEGER; n: INTEGER): LONGINT;
912 BEGIN
913 HALT(126); (* !!! *)
914 RETURN 0
915 END Call;
917 (* -------------------- reference information (portable) --------------------- *)
919 PROCEDURE RefCh (VAR ref: INTEGER; OUT ch: SHORTCHAR);
920 BEGIN
921 S.GET(ref, ch); INC(ref)
922 END RefCh;
924 PROCEDURE RefNum (VAR ref: INTEGER; OUT x: INTEGER);
925 VAR s, n: INTEGER; ch: SHORTCHAR;
926 BEGIN
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)
930 END RefNum;
932 PROCEDURE RefName (VAR ref: INTEGER; OUT n: Utf8Name);
933 VAR i: INTEGER; ch: SHORTCHAR;
934 BEGIN
935 i := 0; RefCh(ref, ch);
936 WHILE ch # 0X DO n[i] := ch; INC(i); RefCh(ref, ch) END;
937 n[i] := 0X
938 END RefName;
940 PROCEDURE GetRefProc* (VAR ref: INTEGER; OUT adr: INTEGER; OUT name: Utf8Name);
941 VAR ch: SHORTCHAR;
942 BEGIN
943 S.GET(ref, ch);
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)
948 END;
949 WHILE (ch > 0X) & (ch < 0FCX) DO (* skip source refs *)
950 INC(ref); RefNum(ref, adr); S.GET(ref, ch)
951 END;
952 IF ch = 0FCX THEN INC(ref); RefNum(ref, adr); RefName(ref, name)
953 ELSE adr := 0
954 END
955 END GetRefProc;
957 PROCEDURE GetRefVar* (VAR ref: INTEGER; OUT mode, form: SHORTCHAR; OUT desc: Type; OUT adr: INTEGER; OUT name: Utf8Name);
958 BEGIN
959 S.GET(ref, mode); desc := NIL;
960 IF mode >= 0FDX THEN
961 mode := SHORT(CHR(ORD(mode) - 0FCH));
962 INC(ref); RefCh(ref, form);
963 IF form = 10X THEN
964 S.GET(ref, desc); INC(ref, 4); form := SHORT(CHR(16 + desc.id MOD 4))
965 END;
966 RefNum(ref, adr); RefName(ref, name)
967 ELSE
968 mode := 0X; form := 0X; adr := 0
969 END
970 END GetRefVar;
972 PROCEDURE SourcePos* (mod: Module; codePos: INTEGER): INTEGER;
973 VAR ref, pos, ad, d: INTEGER; ch: SHORTCHAR; name: Utf8Name;
974 BEGIN
975 IF mod # NIL THEN (* mf, 12.02.04 *)
976 ref := mod.refs; pos := 0; ad := 0; S.GET(ref, ch);
977 WHILE ch # 0X DO
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)
982 END;
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
986 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)
991 END
992 END;
993 END;
994 RETURN -1
995 END SourcePos;
997 PROCEDURE LoadDll* (IN name: ARRAY OF CHAR; VAR ok: BOOLEAN);
998 VAR h: ADDRESS; file: Utf8Name; res: INTEGER;
999 BEGIN
1000 StringToUtf8(name, file, res);
1001 IF res = 0 THEN
1002 h := dlfcn.dlopen(file, dlfcn.RTLD_LAZY + dlfcn.RTLD_GLOBAL);
1003 ok := h # 0
1004 ELSE
1005 ok := FALSE
1006 END
1007 END LoadDll;
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;
1011 BEGIN
1012 StringToUtf8(dll, file, res);
1013 IF res = 0 THEN
1014 h := dlfcn.dlopen(file, dlfcn.RTLD_LAZY + dlfcn.RTLD_GLOBAL);
1015 IF h # 0 THEN
1016 StringToUtf8(name, sym, res);
1017 IF res = 0 THEN
1018 p := dlfcn.dlsym(h, sym)
1019 ELSE
1020 p := 0
1021 END;
1022 err := dlfcn.dlclose(h);
1023 ASSERT(err = 0, 100)
1024 ELSE
1025 p := 0
1026 END
1027 ELSE
1028 p := 0
1029 END;
1030 RETURN p
1031 END ThisDllObj;
1033 (* -------------------- garbage collector (portable) --------------------- *)
1035 PROCEDURE Mark (this: Block);
1036 VAR father, son: Block; tag: Type; flag, offset, actual: INTEGER;
1037 BEGIN
1038 IF ~ODD(S.VAL(INTEGER, this.tag)) THEN
1039 father := NIL;
1040 LOOP
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)
1046 END;
1047 LOOP
1048 offset := tag.ptroffs[0];
1049 IF offset < 0 THEN
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
1053 ELSE (* up *)
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)
1063 END
1064 ELSE
1065 S.GET(actual + offset, son);
1066 IF son # NIL THEN
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;
1071 EXIT
1072 END
1073 END;
1074 INC(S.VAL(INTEGER, tag), 4)
1075 END
1076 END
1077 END
1078 END
1079 END Mark;
1081 PROCEDURE MarkGlobals;
1082 VAR m: Module; i, p: INTEGER;
1083 BEGIN
1084 m := modList;
1085 WHILE m # NIL DO
1086 IF m.refcnt >= 0 THEN
1087 i := 0;
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
1091 END
1092 END;
1093 m := m.next
1094 END
1095 END MarkGlobals;
1097 PROCEDURE Next (b: Block): Block; (* next block in same cluster *)
1098 VAR size: INTEGER;
1099 BEGIN
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)
1103 END Next;
1105 PROCEDURE CheckCandidates;
1106 (* pre: nofcand > 0 *)
1107 VAR i, j, h, p, end: INTEGER; c: Cluster; blk, next: Block;
1108 BEGIN
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
1115 END;
1116 candidates[j] := p; INC(i)
1117 END
1118 UNTIL h = 1;
1119 (* sweep *)
1120 c := root; i := 0;
1121 WHILE c # NIL DO
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
1125 INC(i);
1126 IF i = nofcand THEN RETURN END
1127 END;
1128 WHILE S.VAL(INTEGER, blk) < end DO
1129 next := Next(blk);
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
1133 Mark(blk)
1134 END;
1135 REPEAT
1136 INC(i);
1137 IF i = nofcand THEN RETURN END
1138 UNTIL candidates[i] >= S.VAL(INTEGER, next)
1139 END;
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 *)
1142 Mark(blk)
1143 END;
1144 blk := next
1145 END;
1146 c := c.next
1147 END
1148 END CheckCandidates;
1150 PROCEDURE MarkLocals;
1151 VAR sp, p, min, max: INTEGER; c: Cluster;
1152 BEGIN
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
1157 S.GET(sp, p);
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
1161 END;
1162 INC(sp, 4)
1163 END;
1164 candidates[nofcand] := max; INC(nofcand); (* ensure complete scan for interface mark*)
1165 IF nofcand > 0 THEN CheckCandidates END
1166 END MarkLocals;
1168 PROCEDURE MarkFinObj;
1169 VAR f: FList;
1170 BEGIN
1171 wouldFinalize := FALSE;
1172 f := finalizers;
1173 WHILE f # NIL DO
1174 IF ~ODD(S.VAL(INTEGER, f.blk.tag)) THEN wouldFinalize := TRUE END;
1175 Mark(f.blk);
1176 f := f.next
1177 END;
1178 f := hotFinalizers;
1179 WHILE f # NIL DO IF ~ODD(S.VAL(INTEGER, f.blk.tag)) THEN wouldFinalize := TRUE END;
1180 Mark(f.blk);
1181 f := f.next
1182 END
1183 END MarkFinObj;
1185 PROCEDURE CheckFinalizers;
1186 VAR f, g, h, k: FList;
1187 BEGIN
1188 f := finalizers; g := NIL;
1189 IF hotFinalizers = NIL THEN k := NIL
1190 ELSE
1191 k := hotFinalizers;
1192 WHILE k.next # NIL DO k := k.next END
1193 END;
1194 WHILE f # NIL DO
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
1200 ELSE g := h
1201 END
1202 END;
1203 h := hotFinalizers;
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);
1209 BEGIN
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;
1213 END ExecFinalizer;
1215 PROCEDURE^ Try* (h: TryHandler; a, b, c: INTEGER); (* COMPILER DEPENDENT *)
1217 PROCEDURE CallFinalizers;
1218 VAR f: FList;
1219 BEGIN
1220 WHILE hotFinalizers # NIL DO
1221 f := hotFinalizers; hotFinalizers := hotFinalizers.next;
1222 Try(ExecFinalizer, S.VAL(INTEGER, f), 0, 0)
1223 END;
1224 wouldFinalize := FALSE
1225 END CallFinalizers;
1227 PROCEDURE Insert (blk: FreeBlock; size: INTEGER); (* insert block in free list *)
1228 VAR i: INTEGER;
1229 BEGIN
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
1233 END Insert;
1235 PROCEDURE Sweep (dealloc: BOOLEAN);
1236 VAR cluster, last, c: Cluster; blk, next: Block; fblk, b, t: FreeBlock; end, i: INTEGER;
1237 BEGIN
1238 cluster := root; last := NIL; allocated := 0;
1239 i := N;
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;
1244 fblk := NIL;
1245 WHILE S.VAL(INTEGER, blk) < end DO
1246 next := Next(blk);
1247 IF ODD(S.VAL(INTEGER, blk.tag)) THEN
1248 IF fblk # NIL THEN
1249 Insert(fblk, S.VAL(INTEGER, blk) - S.VAL(INTEGER, fblk));
1250 fblk := NIL
1251 END;
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)
1256 END;
1257 blk := next
1258 END;
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;
1262 FreeHeapMem(c)
1263 ELSE
1264 IF fblk # NIL THEN Insert(fblk, end - S.VAL(INTEGER, fblk)) END;
1265 last := cluster; cluster := cluster.next
1266 END
1267 END;
1268 (* reverse free list *)
1269 i := N;
1270 REPEAT
1271 DEC(i);
1272 b := free[i]; fblk := sentinel;
1273 WHILE b # sentinel DO t := b; b := t.next; t.next := fblk; fblk := t END;
1274 free[i] := fblk
1275 UNTIL i = 0
1276 END Sweep;
1278 PROCEDURE Collect*;
1279 BEGIN
1280 IF root # NIL THEN
1281 CallFinalizers; (* trap cleanup *)
1282 MarkGlobals;
1283 MarkLocals;
1284 CheckFinalizers;
1285 Sweep(TRUE);
1286 CallFinalizers
1287 END
1288 END Collect;
1290 PROCEDURE FastCollect*;
1291 BEGIN
1292 IF root # NIL THEN
1293 MarkGlobals;
1294 MarkLocals;
1295 MarkFinObj;
1296 Sweep(FALSE)
1297 END
1298 END FastCollect;
1300 PROCEDURE WouldFinalize* (): BOOLEAN;
1301 BEGIN
1302 RETURN wouldFinalize
1303 END WouldFinalize;
1305 (* --------------------- memory allocation (portable) -------------------- *)
1307 PROCEDURE OldBlock (size: INTEGER): FreeBlock; (* size MOD 16 = 0 *)
1308 VAR b, l: FreeBlock; s, i: INTEGER;
1309 BEGIN
1310 s := size - 4;
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
1317 ELSE b := NIL
1318 END;
1319 RETURN b
1320 END OldBlock;
1322 PROCEDURE LastBlock (limit: INTEGER): FreeBlock; (* size MOD 16 = 0 *)
1323 VAR b, l: FreeBlock; s, i: INTEGER;
1324 BEGIN
1325 s := limit - 4;
1326 i := 0;
1327 REPEAT
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
1332 ELSE b := NIL
1333 END;
1334 INC(i)
1335 UNTIL (b # NIL) OR (i = N);
1336 RETURN b
1337 END LastBlock;
1339 PROCEDURE NewBlock (size: INTEGER): Block;
1340 VAR tsize, a, s: INTEGER; b: FreeBlock; new, c: Cluster; r: Reducer;
1341 BEGIN
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 *)
1346 IF b = NIL THEN
1347 FastCollect; b := OldBlock(tsize); (* 2) collect *)
1348 IF b = NIL THEN
1349 Collect; b := OldBlock(tsize); (* 2a) fully collect *)
1350 END;
1351 IF b = NIL THEN
1352 AllocHeapMem(tsize + 12, new); (* 3) allocate new cluster *)
1353 IF new # NIL THEN
1354 IF (root = NIL) OR (S.VAL(INTEGER, new) < S.VAL(INTEGER, root)) THEN
1355 new.next := root; root := new
1356 ELSE
1357 c := root;
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
1360 END;
1361 b := S.VAL(FreeBlock, S.VAL(INTEGER, new) + 12);
1362 b.size := (new.size - 12) DIV 16 * 16 - 4
1363 ELSE
1364 RETURN NIL (* 4) give up *)
1365 END
1366 END
1367 END;
1368 (* b # NIL *)
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)
1374 END NewBlock;
1376 PROCEDURE Allocated* (): INTEGER;
1377 BEGIN
1378 RETURN allocated
1379 END Allocated;
1381 PROCEDURE Used* (): INTEGER;
1382 BEGIN
1383 RETURN used
1384 END Used;
1386 PROCEDURE Root* (): INTEGER;
1387 BEGIN
1388 RETURN S.VAL(INTEGER, root)
1389 END Root;
1391 (* -------------------- Trap Handling --------------------- *)
1393 PROCEDURE Start* (code: Command);
1394 VAR res: setjmp.int;
1395 BEGIN
1396 restart := code;
1397 S.GETREG(SP, baseStack);
1398 res := setjmp.sigsetjmp(startEnv, 1);
1399 restart
1400 END Start;
1402 PROCEDURE Quit* (exitCode: INTEGER);
1403 VAR m: Module; term: Command; t: BOOLEAN;
1404 BEGIN
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
1411 END;
1412 m := m.next
1413 END;
1414 CallFinalizers;
1415 hotFinalizers := finalizers; finalizers := NIL;
1416 CallFinalizers;
1417 stdlib.exit(exitCode)
1418 END Quit;
1420 PROCEDURE FatalError* (id: INTEGER; str: ARRAY OF CHAR);
1421 VAR res: stdio.int; title: ARRAY 16 OF CHAR; text: ARRAY 256 OF SHORTCHAR;
1422 BEGIN
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);
1427 stdlib.abort
1428 END FatalError;
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);
1435 VAR i: INTEGER;
1436 BEGIN
1437 i := 0;
1438 WHILE (len < LEN(out) - 1) & (s[i] # 0X) DO out[len] := s[i]; INC(i); INC(len) END
1439 END WriteString;
1441 PROCEDURE WriteHex (x, n: INTEGER);
1442 VAR i, y: INTEGER;
1443 BEGIN
1444 IF len + n < LEN(out) THEN
1445 i := len + n - 1;
1446 WHILE i >= len DO
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)
1450 END;
1451 INC(len, n)
1452 END
1453 END WriteHex;
1455 PROCEDURE WriteLn;
1456 BEGIN
1457 IF len < LEN(out) - 1 THEN out[len] := 0AX; INC(len) END
1458 END WriteLn;
1460 BEGIN
1461 len := 0;
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: ");
1482 WriteHex(val, 4)
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)
1496 END;
1497 WriteString(" ======");
1498 a := pc; b := fp; c := 12;
1499 REPEAT
1500 WriteLn; WriteString("- ");
1501 mod := modList;
1502 WHILE (mod # NIL) & ((a < mod.code) OR (a >= mod.code + mod.csize)) DO mod := mod.next END;
1503 IF mod # NIL THEN
1504 DEC(a, mod.code);
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);
1508 IF a < end THEN
1509 Utf8ToString(n, name, res); WriteString("."); WriteString(SHORT(name))
1510 END
1511 ELSE
1512 GetModName(mod, modName); WriteString("("); WriteString(SHORT(modName)); WriteString(")")
1513 END;
1514 WriteString(" ")
1515 END;
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 *)
1521 DEC(c)
1522 ELSE c := 0
1523 END
1524 UNTIL c = 0;
1525 out[len] := 0X;
1526 res := unistd.write(2, S.ADR(out), len)
1527 END DefaultTrapViewer;
1529 PROCEDURE TrapCleanup;
1530 VAR t: TrapCleaner;
1531 BEGIN
1532 WHILE trapStack # NIL DO
1533 t := trapStack; trapStack := trapStack.next; t.Cleanup
1534 END;
1535 IF (trapChecker # NIL) & (err # 128) THEN trapChecker END
1536 END TrapCleanup;
1538 PROCEDURE SetTrapGuard* (on: BOOLEAN);
1539 BEGIN
1540 guarded := on
1541 END SetTrapGuard;
1543 PROCEDURE Try* (h: TryHandler; a, b, c: INTEGER);
1544 VAR oldIsTry: BOOLEAN; oldTryEnv: setjmp.jmp_buf; res: setjmp.int;
1545 BEGIN
1546 oldIsTry := isTry; oldTryEnv := tryEnv;
1547 isTry := TRUE;
1548 res := setjmp._setjmp(tryEnv);
1549 IF res = 0 THEN h(a, b, c) END;
1550 isTry := oldIsTry; tryEnv := oldTryEnv
1551 END Try;
1553 PROCEDURE Trap* (n: INTEGER);
1554 BEGIN
1555 IF trapped THEN
1556 DefaultTrapViewer;
1557 IF ~secondTrap THEN trapped := FALSE; secondTrap := TRUE END
1558 END;
1559 IF n >= 0 THEN err := n
1560 ELSE err := -n + 128
1561 END;
1562 pc := 0; sp := 0; fp := 0; stack := 0; val := 0;
1563 INC(trapCount);
1564 (* !!! InitFPU *)
1565 TrapCleanup;
1566 IF isTry THEN
1567 setjmp._longjmp(tryEnv, 1)
1568 END;
1569 IF err = 128 THEN (* do nothing *)
1570 ELSIF (trapViewer # NIL) & (restart # NIL) & ~trapped & ~guarded THEN
1571 trapped := TRUE; trapViewer()
1572 ELSE DefaultTrapViewer
1573 END;
1574 trapped := FALSE; secondTrap := FALSE;
1575 IF restart # NIL THEN
1576 setjmp.siglongjmp(startEnv, 1)
1577 END;
1578 stdlib.abort
1579 END Trap;
1581 PROCEDURE [ccall] TrapHandler (signo: signal.int; IN info: signal.siginfo_t; context: ADDRESS);
1582 VAR res: signal.int;
1583 BEGIN
1584 IF trapped THEN
1585 DefaultTrapViewer;
1586 IF ~secondTrap THEN trapped := FALSE; secondTrap := TRUE END
1587 END;
1588 err := -signo; pc := 0; sp := 0; fp := 0; stack := baseStack; val := 0;
1589 CASE signo OF
1590 | signal.SIGFPE:
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: *) *)
1602 ELSE (* unknown *)
1603 END
1604 | signal.SIGINT:
1605 val := info.si_code;
1606 err := 200 (* keyboard interrupt *)
1607 | signal.SIGSEGV:
1608 val := info.info.sigsegv.si_addr;
1609 err := 203 (* illigal read *)
1610 | signal.SIGBUS:
1611 val := info.info.sigbus.si_addr;
1612 err := 10001H (* bus error *)
1613 | signal.SIGILL:
1614 pc := info.info.sigill.si_addr;
1615 err := 202; (* illigal instruction *)
1616 IF IsReadable(pc, pc + 4) THEN
1617 S.GET(pc, val)
1618 (* !!! err := halt code *)
1619 END;
1620 ELSE (* unknown *)
1621 END;
1622 INC(trapCount);
1623 (* !!! InitFPU *)
1624 TrapCleanup;
1625 IF isTry THEN
1626 setjmp._longjmp(tryEnv, 1)
1627 END;
1628 IF err = 128 THEN (* do nothing *)
1629 ELSIF (trapViewer # NIL) & (restart # NIL) & ~trapped & ~guarded THEN
1630 trapped := TRUE; trapViewer()
1631 ELSE DefaultTrapViewer
1632 END;
1633 trapped := FALSE; secondTrap := FALSE;
1634 IF restart # NIL THEN
1635 setjmp.siglongjmp(startEnv, 1)
1636 END;
1637 stdlib.abort
1638 END TrapHandler;
1640 (* -------------------- Initialization --------------------- *)
1642 PROCEDURE InstallTrap (signo: signal.int);
1643 VAR act: signal.struct_sigaction; res: signal.int;
1644 BEGIN
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);
1650 END InstallTrap;
1652 PROCEDURE InstallTrapVectors;
1653 BEGIN
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;
1664 PROCEDURE Init;
1665 VAR i: INTEGER;
1666 BEGIN
1667 pagesize := unistd.sysconf(unistd._SC_PAGESIZE);
1669 (* init heap *)
1670 allocated := 0; total := 0; used := 0;
1671 sentinelBlock.size := MAX(INTEGER);
1672 sentinel := S.ADR(sentinelBlock);
1673 i := N;
1674 REPEAT DEC(i); free[i] := sentinel UNTIL i = 0;
1676 IF ~inDll THEN
1677 InstallTrapVectors
1678 END;
1680 (* !!! InitFPU *)
1681 IF ~static THEN
1682 InitModule(modList);
1683 IF ~inDll THEN Quit(1) END
1684 END
1685 END Init;
1687 BEGIN
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;
1695 Init
1696 END
1697 CLOSE
1698 IF ~terminating THEN
1699 terminating := TRUE;
1700 Quit(0)
1701 END
1702 END Kernel.