Coop.Modules.Mod 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. MODULE Modules; (** AUTHOR "pjm"; PURPOSE "Modules and types"; *)
  2. IMPORT SYSTEM, Trace, Machine, Heaps, Environment, Activities, Processors, Queues;
  3. CONST
  4. Ok* = 0;
  5. AddressSize = SIZEOF (ADDRESS); (* architecture dependent size of addresses in bytes *)
  6. MaxTags* = 16; (* in type descriptor *)
  7. (** type descriptor field offsets relative to root (middle) *)
  8. Tag0Ofs* = -AddressSize * 2; (** first tag *)
  9. Mth0Ofs* = Tag0Ofs - AddressSize*MaxTags; (** first method *)
  10. Ptr0Ofs* = AddressSize; (** first pointer offset *)
  11. MaxObjFormats = 5; (* maximum number of object file formats installed *)
  12. (** flags in TypeDesc, RoundUp(log2(MaxTags)) low bits reserved for extLevel *)
  13. ProtTypeBit* = Heaps.ProtTypeBit;
  14. None* = 0; PowerDown* = 1; Reboot* = 2;
  15. ClearCode = TRUE;
  16. DefaultContext* = "A2";
  17. NoLoader=3400;
  18. TraceBoot=TRUE;
  19. TYPE
  20. (* definitions for object-model loader support *)
  21. Name* = ARRAY 32 OF CHAR;
  22. (* the correponding name array is protected from being GCed via module's internal pointer arrray
  23. compiler generated!
  24. *)
  25. DynamicName* = POINTER {UNSAFE} TO ARRAY 256 OF CHAR;
  26. Command* = RECORD
  27. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  28. name*: Name; (* name of the procedure *)
  29. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  30. entryAdr* : ADDRESS; (* entry address of procedure *)
  31. END;
  32. ExportDesc* = RECORD
  33. fp*: ADDRESS;
  34. name* {UNTRACED}: DynamicName;
  35. adr*: ADDRESS;
  36. exports*: LONGINT; (* number of exports referenced by ExportArray *)
  37. dsc* {UNTRACED}: ExportArray; (* pointer to memory containing a raw array with "exports" entries *)
  38. END;
  39. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  40. Bytes* = POINTER TO ARRAY OF CHAR;
  41. TerminationHandler* = PROCEDURE;
  42. (* all implicit or explicit pointers in the subsequent data structures are protected with one pointer array
  43. *)
  44. TypeDesc* = POINTER TO RECORD
  45. descSize-: LONGINT;
  46. sentinel-: LONGINT; (* = MPO-4 *)
  47. tag-: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  48. flags-: SET;
  49. mod- {UNTRACED}: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  50. name-: Name;
  51. refsOffset-: SIZE;
  52. END;
  53. ExceptionTableEntry* = RECORD
  54. pcFrom*: ADDRESS;
  55. pcTo*: ADDRESS;
  56. pcHandler*: ADDRESS;
  57. END;
  58. ExceptionTable* = POINTER TO ARRAY OF ExceptionTableEntry;
  59. ProcedureDescPointer* = POINTER TO ProcedureDesc;
  60. ProcedureDesc*= RECORD
  61. pcFrom-, pcLimit-, pcValid-, pcEnd-: ADDRESS;
  62. refsOffset-: SIZE;
  63. offsets- {UNTRACED}: POINTER TO ARRAY OF ADDRESS;
  64. END;
  65. ProcedureDescs* = POINTER TO ARRAY OF ProcedureDescPointer;
  66. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  67. VAR
  68. next*: Module; (** once a module is published, all fields are read-only *)
  69. name*: Name;
  70. init, published: BOOLEAN;
  71. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  72. sb*: ADDRESS; (* reference address between constants and local variables *)
  73. entry*: POINTER TO ARRAY OF ADDRESS;
  74. command*: POINTER TO ARRAY OF Command;
  75. ptrAdr*: POINTER TO ARRAY OF ADDRESS; (* traced explicitly in FindRoots *)
  76. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  77. module*: POINTER TO ARRAY OF Module; (* imported modules: for reference counting *)
  78. procTable*: ProcedureDescs; (* information inserted by loader, removed after use in Publish *)
  79. data*, code*, staticTypeDescs* (* ug *), refs*: Bytes;
  80. export*: ExportDesc;
  81. term*: TerminationHandler;
  82. exTable*: ExceptionTable;
  83. (* internal pointer array: to protect internal data structures from being GCed *)
  84. internal-: POINTER TO ARRAY OF ANY;
  85. crc*: LONGINT;
  86. body*: PROCEDURE;
  87. END Module;
  88. LoaderProc* = PROCEDURE (CONST name, fileName: ARRAY OF CHAR; VAR res: LONGINT;
  89. VAR msg: ARRAY OF CHAR): Module; (** load an object file *)
  90. VAR
  91. extension-: ARRAY MaxObjFormats, 8 OF CHAR;
  92. loader: ARRAY MaxObjFormats OF LoaderProc;
  93. numLoaders: LONGINT;
  94. freeRoot*: Module; (** list of freed modules (temporary) *)
  95. root-: Module; (** list of modules (read-only) *)
  96. shutdown*: LONGINT; (** None, Reboot, PowerDown *)
  97. trace: BOOLEAN;
  98. register: RECORD
  99. first, last: Module;
  100. END;
  101. (* global sorted table of all procedures , basically for GC *)
  102. procedureDescriptors-: ProcedureDescs;
  103. mayAllocate: BOOLEAN;
  104. (** Register a module loader. *)
  105. PROCEDURE AddLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  106. BEGIN
  107. Machine.Acquire(Machine.Modules);
  108. ASSERT(numLoaders < MaxObjFormats);
  109. loader[numLoaders] := proc;
  110. COPY(ext, extension[numLoaders]);
  111. ASSERT(ext = extension[numLoaders]); (* no overflow *)
  112. INC(numLoaders);
  113. Machine.Release(Machine.Modules)
  114. END AddLoader;
  115. (** Remove a module loader. *)
  116. PROCEDURE RemoveLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  117. VAR i, j: LONGINT;
  118. BEGIN
  119. Machine.Acquire(Machine.Modules);
  120. i := 0;
  121. WHILE (i # numLoaders) & ((loader[i] # proc) OR (extension[i] # ext)) DO INC(i) END;
  122. IF i # numLoaders THEN
  123. FOR j := i TO numLoaders - 2 DO
  124. loader[j] := loader[j + 1]; extension[j] := extension[j + 1];
  125. END;
  126. loader[numLoaders - 1] := NIL; extension[numLoaders - 1] := "";
  127. DEC(numLoaders)
  128. END;
  129. Machine.Release(Machine.Modules)
  130. END RemoveLoader;
  131. (** Append string from to to, truncating on overflow. *)
  132. PROCEDURE Append*(CONST from: ARRAY OF CHAR; VAR to: ARRAY OF CHAR);
  133. VAR i, j, m: LONGINT;
  134. BEGIN
  135. j := 0; WHILE to[j] # 0X DO INC(j) END;
  136. m := LEN(to)-1;
  137. i := 0; WHILE (from[i] # 0X) & (j # m) DO to[j] := from[i]; INC(i); INC(j) END;
  138. to[j] := 0X
  139. END Append;
  140. (** Add a module to the pool of accessible modules, or return named module. *)
  141. PROCEDURE Publish*(VAR m: Module; VAR new: BOOLEAN);
  142. VAR n: Module; i: LONGINT;
  143. BEGIN
  144. Machine.Acquire(Machine.Modules);
  145. n := root; WHILE (n # NIL) & (n.name # m.name) DO n := n.next END;
  146. IF n # NIL THEN (* module with same name exists, return it and ignore new m *)
  147. m := n; new := FALSE;
  148. Machine.Release(Machine.Modules);
  149. ELSE
  150. IF TraceBoot THEN
  151. Machine.Acquire(Machine.TraceOutput);
  152. Trace.String("publish "); Trace.String(m.name);
  153. Trace.Ln;
  154. Machine.Release(Machine.TraceOutput);
  155. END;
  156. m.published := TRUE;
  157. m.next := root; root := m;
  158. m.refcnt := 0;
  159. SortExceptionTable(m.exTable);
  160. SortProcedureDescs(m.procTable);
  161. MergeProcedureDescs(m.procTable);
  162. IF m.module # NIL THEN
  163. FOR i := 0 TO LEN(m.module)-1 DO INC(m.module[i].refcnt) END;
  164. END;
  165. new := TRUE;
  166. Machine.Release(Machine.Modules);
  167. END;
  168. END Publish;
  169. PROCEDURE Initialize*(VAR module: Module);
  170. VAR new: BOOLEAN;
  171. BEGIN
  172. IF (module = NIL) THEN RETURN END;
  173. Publish (module, new);
  174. IF new THEN
  175. IF module.body # NIL THEN module.body END;
  176. module.init := TRUE;
  177. END;
  178. END Initialize;
  179. VAR callagain: BOOLEAN;
  180. PROCEDURE Initialize0*(module: Module);
  181. VAR new: BOOLEAN;
  182. BEGIN
  183. (*TRACE(module.name);*)
  184. (* module MUST have been removed from register list and must not have been initialized yet *)
  185. ASSERT(module.next = NIL);
  186. Publish (module, new);
  187. callagain := FALSE;
  188. IF new THEN
  189. IF module.name = "Objects" THEN
  190. callagain := TRUE;
  191. module.init := TRUE;
  192. END;
  193. (*
  194. Trace.Memory(SYSTEM.VAL(ADDRESS, module), 256);
  195. TRACE(module, module.name, module.body);
  196. TRACE(module);
  197. TRACE(ADDRESS OF module.next);
  198. TRACE(ADDRESS OF module.name);
  199. TRACE(ADDRESS OF module.init);
  200. TRACE(ADDRESS OF module.published);
  201. TRACE(ADDRESS OF module.body);
  202. TRACE(ADDRESS OF module.refcnt);
  203. TRACE(ADDRESS OF module.sb);
  204. TRACE(ADDRESS OF module.entry);
  205. TRACE(ADDRESS OF module.command);
  206. TRACE(ADDRESS OF module.ptrAdr);
  207. TRACE(ADDRESS OF module.typeInfo);
  208. TRACE(ADDRESS OF module.module);
  209. TRACE(ADDRESS OF module.procTable);
  210. TRACE(ADDRESS OF module.ptrTable);
  211. TRACE(ADDRESS OF module.data);
  212. TRACE(ADDRESS OF module.code);
  213. TRACE(ADDRESS OF module.staticTypeDescs);
  214. TRACE(ADDRESS OF module.refs);
  215. TRACE(ADDRESS OF module.export);
  216. TRACE(ADDRESS OF module.term);
  217. TRACE(ADDRESS OF module.exTable);
  218. TRACE(ADDRESS OF module.noProcs);
  219. TRACE(ADDRESS OF module.firstProc);
  220. TRACE(ADDRESS OF module.maxPtrs);
  221. TRACE(ADDRESS OF module.crc);
  222. TRACE(ADDRESS OF module.body);
  223. *)
  224. IF module.body # NIL THEN module.body END;
  225. IF callagain THEN
  226. PublishRegisteredModules (* does not return on intel architecture. Returns on ARM but looses procedure stack frame: we are not allowed to refer to local variables after this *)
  227. ELSE
  228. module.init := TRUE;
  229. END;
  230. END;
  231. END Initialize0;
  232. (** Return the named module or NIL if it is not loaded yet. *)
  233. PROCEDURE ModuleByName*(CONST name: ARRAY OF CHAR): Module;
  234. VAR m: Module;
  235. BEGIN
  236. Machine.Acquire(Machine.Modules);
  237. m := root; WHILE (m # NIL) & (m.name # name) DO m := m.next END;
  238. Machine.Release(Machine.Modules);
  239. RETURN m
  240. END ModuleByName;
  241. (* Generate a module file name. *)
  242. PROCEDURE GetFileName(CONST name, extension: ARRAY OF CHAR; VAR fileName: ARRAY OF CHAR);
  243. VAR i, j: LONGINT;
  244. BEGIN
  245. i := 0; WHILE name[i] # 0X DO fileName[i] := name[i]; INC(i) END;
  246. j := 0; WHILE extension[j] # 0X DO fileName[i] := extension[j]; INC(i); INC(j) END;
  247. fileName[i] := 0X
  248. END GetFileName;
  249. (* sort procedure descriptors by firstPC in ascending order *)
  250. PROCEDURE SortProcedureDescs(p: ProcedureDescs);
  251. PROCEDURE Less(i,j: LONGINT): BOOLEAN;
  252. BEGIN
  253. RETURN p[i].pcFrom < p[j].pcFrom;
  254. END Less;
  255. PROCEDURE Swap(i,j: LONGINT);
  256. VAR tmp: ProcedureDescPointer;
  257. BEGIN
  258. tmp := p[i];
  259. p[i] := p[j];
  260. p[j] := tmp;
  261. END Swap;
  262. PROCEDURE Quick( lo, hi: LONGINT);
  263. VAR i, j, m: LONGINT;
  264. BEGIN
  265. IF lo < hi THEN
  266. i := lo; j := hi; m := (lo + hi) DIV 2;
  267. REPEAT
  268. WHILE Less( i, m ) DO INC( i ) END;
  269. WHILE Less( m, j ) DO DEC( j ) END;
  270. IF i <= j THEN
  271. IF m = i THEN m := j
  272. ELSIF m = j THEN m := i
  273. END;
  274. Swap( i, j ); INC( i ); DEC( j )
  275. END
  276. UNTIL i > j;
  277. Quick( lo, j); Quick( i, hi)
  278. END;
  279. END Quick;
  280. BEGIN
  281. Quick(0, LEN(p)-1);
  282. END SortProcedureDescs;
  283. (* sort procedure descriptors by firstPC in ascending order *)
  284. PROCEDURE SortExceptionTable(p: ExceptionTable);
  285. PROCEDURE Less(i,j: LONGINT): BOOLEAN;
  286. BEGIN
  287. RETURN p[i].pcFrom < p[j].pcFrom;
  288. END Less;
  289. PROCEDURE Swap(i,j: LONGINT);
  290. VAR tmp: ExceptionTableEntry;
  291. BEGIN
  292. tmp := p[i];
  293. p[i] := p[j];
  294. p[j] := tmp;
  295. END Swap;
  296. PROCEDURE Quick( lo, hi: LONGINT);
  297. VAR i, j, m: LONGINT;
  298. BEGIN
  299. IF lo < hi THEN
  300. i := lo; j := hi; m := (lo + hi) DIV 2;
  301. REPEAT
  302. WHILE Less( i, m ) DO INC( i ) END;
  303. WHILE Less( m, j ) DO DEC( j ) END;
  304. IF i <= j THEN
  305. IF m = i THEN m := j
  306. ELSIF m = j THEN m := i
  307. END;
  308. Swap( i, j ); INC( i ); DEC( j )
  309. END
  310. UNTIL i > j;
  311. Quick( lo, j); Quick( i, hi)
  312. END;
  313. END Quick;
  314. BEGIN
  315. Quick(0, LEN(p)-1);
  316. END SortExceptionTable;
  317. (* sort and merge procedure descriptors with the global procedure desc array, replacing the global procedure array *)
  318. PROCEDURE MergeProcedureDescs*(p: ProcedureDescs);
  319. VAR n: ProcedureDescs;
  320. i,j,k: LONGINT;
  321. BEGIN
  322. IF ~mayAllocate THEN RETURN END;
  323. IF (p = NIL) OR (LEN(p) = 0) THEN RETURN END;
  324. IF procedureDescriptors = NIL THEN
  325. procedureDescriptors := p;
  326. ELSE
  327. NEW(n, LEN(procedureDescriptors) + LEN(p));
  328. k := 0; i := 0; j := 0;
  329. FOR k := 0 TO LEN(n)-1 DO
  330. IF (i<LEN(p)) & ((j=LEN(procedureDescriptors)) OR (p[i].pcFrom < procedureDescriptors[j].pcFrom)) THEN
  331. n[k] := p[i]; INC(i);
  332. ELSE
  333. n[k] := procedureDescriptors[j]; INC(j);
  334. END;
  335. END;
  336. procedureDescriptors := n;
  337. END;
  338. END MergeProcedureDescs;
  339. (* remove sorted procedure descriptors from sorted global array *)
  340. PROCEDURE RemoveProcedureDescs*(p: ProcedureDescs);
  341. VAR i,j,k: LONGINT; n: ProcedureDescs;
  342. BEGIN
  343. IF ~mayAllocate THEN RETURN END;
  344. NEW(n, LEN(procedureDescriptors) - LEN(p));
  345. i := 0; j := 0; k := 0;
  346. WHILE i < LEN(procedureDescriptors) DO
  347. IF (j < LEN(p)) & (procedureDescriptors[i] = p[j]) THEN INC(j);
  348. ELSE n[k] := procedureDescriptors[i]; INC(k);
  349. END;
  350. INC(i);
  351. END;
  352. procedureDescriptors := n;
  353. END RemoveProcedureDescs;
  354. (** Load the module if it is not already loaded. *) (* Algorithm J. Templ, ETHZ, 1994 *)
  355. PROCEDURE ThisModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR): Module;
  356. VAR m, p: Module; fileName: ARRAY 64 OF CHAR; i: LONGINT;
  357. BEGIN
  358. res := Ok; msg[0] := 0X; m := ModuleByName(name);
  359. IF m = NIL THEN
  360. IF trace THEN
  361. Machine.Acquire (Machine.TraceOutput);
  362. Trace.String(">"); Trace.StringLn (name);
  363. Machine.Release (Machine.TraceOutput);
  364. END;
  365. IF numLoaders = 0 THEN
  366. res := NoLoader; m := NIL;
  367. ELSE
  368. i:= 0;
  369. REPEAT
  370. GetFileName(name, extension[i], fileName);
  371. m := loader[i](name, fileName, res, msg);
  372. INC(i);
  373. UNTIL (m # NIL) OR (i=numLoaders);
  374. END;
  375. IF trace THEN
  376. Machine.Acquire (Machine.TraceOutput);
  377. Trace.String("?"); Trace.StringLn (name);
  378. Machine.Release (Machine.TraceOutput);
  379. END;
  380. p := m;
  381. IF (m # NIL) & ~m.published THEN (* no race on m.published, as update is done below in Publish *)
  382. Initialize(m);
  383. END;
  384. IF trace THEN
  385. Machine.Acquire (Machine.TraceOutput);
  386. IF m = NIL THEN
  387. Trace.String("could not load "); Trace.StringLn(name)
  388. ELSIF ~m.published THEN
  389. Trace.String("not published "); Trace.StringLn(name)
  390. ELSE
  391. Trace.String("<"); Trace.StringLn (name);
  392. END;
  393. Machine.Release (Machine.TraceOutput);
  394. END;
  395. END;
  396. RETURN m
  397. END ThisModule;
  398. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. Non-blocking version for reflection *)
  399. PROCEDURE ThisModuleByAdr0*(pc: ADDRESS): Module;
  400. VAR m: Module; found: BOOLEAN; list: LONGINT;
  401. BEGIN
  402. list := 0; found := FALSE;
  403. REPEAT
  404. CASE list OF
  405. 0: m := root
  406. |1: m := freeRoot
  407. END;
  408. WHILE (m # NIL) & ~found DO
  409. found := FindProc(pc, m.procTable) # NIL;
  410. IF ~found THEN m := m.next END;
  411. END;
  412. INC(list)
  413. UNTIL found OR (list=2);
  414. RETURN m
  415. END ThisModuleByAdr0;
  416. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. *)
  417. PROCEDURE ThisModuleByAdr*(pc: ADDRESS): Module;
  418. VAR m: Module;
  419. BEGIN
  420. Machine.Acquire(Machine.Modules);
  421. m := ThisModuleByAdr0(pc);
  422. Machine.Release(Machine.Modules);
  423. RETURN m
  424. END ThisModuleByAdr;
  425. (* Retrieve a procedure given a module name, the procedure name and some type information (kernel call) *)
  426. PROCEDURE GetProcedure*(CONST moduleName, procedureName : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  427. VAR module : Module; ignoreMsg : ARRAY 32 OF CHAR; i, res : LONGINT;
  428. BEGIN
  429. module := ThisModule(moduleName, res, ignoreMsg);
  430. IF (res = Ok) THEN
  431. ASSERT(module.init); (* module body must have been called (see note at end of module) *)
  432. IF module.init THEN
  433. Machine.Acquire(Machine.Modules);
  434. i := 0; entryAdr := Heaps.NilVal;
  435. WHILE (entryAdr = Heaps.NilVal) & (i # LEN(module.command^)) DO
  436. IF (module.command[i].name = procedureName) & (module.command[i].argTdAdr = argTdAdr) & (module.command[i].retTdAdr = retTdAdr) THEN
  437. entryAdr := module.command[i].entryAdr;
  438. END;
  439. INC(i)
  440. END;
  441. Machine.Release(Machine.Modules);
  442. END;
  443. END;
  444. END GetProcedure;
  445. (** Return the named type *)
  446. PROCEDURE ThisType*(m: Module; CONST name: ARRAY OF CHAR): TypeDesc;
  447. VAR i: LONGINT; type: TypeDesc;
  448. BEGIN
  449. Machine.Acquire(Machine.Modules);
  450. i := 0;
  451. WHILE (i < LEN(m.typeInfo)) & (m.typeInfo[i].name # name) DO INC(i) END;
  452. IF i = LEN(m.typeInfo) THEN
  453. type := NIL
  454. ELSE
  455. type := m.typeInfo[i]
  456. END;
  457. Machine.Release(Machine.Modules);
  458. RETURN type
  459. END ThisType;
  460. PROCEDURE ThisTypeByAdr*(adr: ADDRESS; VAR m: Module; VAR t: TypeDesc);
  461. BEGIN
  462. IF adr # 0 THEN
  463. Machine.Acquire(Machine.Modules);
  464. SYSTEM.GET (adr + Heaps.TypeDescOffset, adr);
  465. t := SYSTEM.VAL(TypeDesc, adr);
  466. m := t.mod;
  467. Machine.Release(Machine.Modules)
  468. ELSE
  469. m := NIL; t := NIL
  470. END
  471. END ThisTypeByAdr;
  472. (** create a new object given its type descriptor *)
  473. PROCEDURE NewObj*(t : TypeDesc; isRealtime: BOOLEAN) : ANY;
  474. VAR x : ANY;
  475. BEGIN
  476. Heaps.NewRec(x, SYSTEM.VAL (ADDRESS, t.tag), isRealtime);
  477. RETURN x;
  478. END NewObj;
  479. (** return the type descriptor of an object *)
  480. PROCEDURE TypeOf*(obj : ANY): TypeDesc;
  481. VAR
  482. m : Module;
  483. t : TypeDesc;
  484. adr : ADDRESS;
  485. BEGIN
  486. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.TypeDescOffset, adr);
  487. ThisTypeByAdr(adr, m, t);
  488. RETURN t;
  489. END TypeOf;
  490. (** searches for the given pc in the global ProcKeyTable, if found it returns the corresponding data element *)
  491. PROCEDURE FindProc*(pc: ADDRESS; p: ProcedureDescs): ProcedureDescPointer;
  492. VAR l,r,x: LONGINT; isHit: BOOLEAN;
  493. BEGIN
  494. IF p # NIL THEN
  495. l := 0; r := LEN(p)-1;
  496. REPEAT
  497. x := (l + r) DIV 2;
  498. IF pc < p[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  499. isHit := ((p[x].pcFrom <= pc) & (pc < p[x].pcLimit));
  500. UNTIL isHit OR (l > r);
  501. IF isHit THEN
  502. RETURN p[x];
  503. END;
  504. END;
  505. RETURN NIL;
  506. END FindProc;
  507. (** Install procedure to execute when module is freed or shut down. The handler can distinguish the two cases by checking Modules.shutdown. If it is None, the module is being freed, otherwise the system is being shut down or rebooted. Only one handler may be installed per module. The last handler installed is active. *)
  508. PROCEDURE InstallTermHandler*(h: TerminationHandler);
  509. VAR m: Module;
  510. BEGIN
  511. m := ThisModuleByAdr(SYSTEM.VAL (ADDRESS, h));
  512. IF m # NIL THEN
  513. m.term := h (* overwrite existing handler, if any *)
  514. END
  515. END InstallTermHandler;
  516. (** Free a module. The module's termination handler, if any, is called first. Then all objects that have finalizers in this module are finalized (even if they are still reachable). Then the module's data and code are invalidated. *)
  517. PROCEDURE FreeModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR);
  518. VAR p, m: Module; term: TerminationHandler; i: LONGINT;
  519. BEGIN
  520. m := ModuleByName(name);
  521. IF (m # NIL) & (m.refcnt = 0) THEN (* will be freed below *)
  522. IF m.term # NIL THEN (* call termination handler *)
  523. term := m.term; m.term := NIL; term (* may trap *)
  524. END;
  525. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  526. END;
  527. res := Ok; msg[0] := 0X;
  528. Machine.Acquire(Machine.Modules);
  529. p := NIL; m := root;
  530. WHILE (m # NIL) & (m.name # name) DO p := m; m := m.next END;
  531. IF m # NIL THEN
  532. IF m.refcnt = 0 THEN (* free the module *)
  533. FOR i := 0 TO LEN(m.module)-1 DO DEC(m.module[i].refcnt) END;
  534. m.init := FALSE; (* disallow ThisCommand *)
  535. Append("?", m.name);
  536. (* move module to free list *)
  537. IF p = NIL THEN root := root.next ELSE p.next := m.next END;
  538. m.next := freeRoot; freeRoot := m;
  539. (* clear global pointers and code *)
  540. IF m.ptrAdr # NIL THEN
  541. Trace.String("ptradr del"); Trace.Ln;
  542. FOR i := 0 TO LEN(m.ptrAdr)-1 DO SYSTEM.PUT (m.ptrAdr[i], NIL) END;
  543. END;
  544. IF ClearCode & (m.code # NIL) THEN
  545. Trace.String("clear code"); Trace.Ln;
  546. FOR i := 0 TO LEN(m.code)-1 DO m.code[i] := 0CCX END
  547. END;
  548. Trace.String("clear code f"); Trace.Ln;
  549. (* remove references to module data *)
  550. m.published := FALSE;
  551. m.entry := NIL; m.command := NIL; m.ptrAdr := NIL;
  552. (* do not clear m.type or m.module, as old heap block tags might reference type descs indirectly. *) (* m.staticTypeDescs, m.typeInfo ??? *)
  553. (* do not clear m.data or m.code, as they are used in ThisModuleByAdr (for debugging). *)
  554. (* do not clear m.refs, as they are used in Traps (for debugging). *)
  555. m.export.dsc := NIL; m.exTable := NIL;
  556. RemoveProcedureDescs(m.procTable);
  557. ELSE
  558. res := 1901; (* can not free module in use *)
  559. COPY(name, msg); Append(" reference count not zero", msg)
  560. END
  561. ELSE
  562. res := 1902; (* module not found *)
  563. COPY(name, msg); Append(" not found", msg)
  564. END;
  565. Machine.Release(Machine.Modules)
  566. END FreeModule;
  567. (** Shut down all modules by calling their termination handlers and then call Machine.Shutdown. *)
  568. PROCEDURE Shutdown*(code: LONGINT);
  569. VAR m: Module; term: TerminationHandler;
  570. BEGIN
  571. IF code # None THEN
  572. LOOP
  573. Machine.Acquire(Machine.Modules);
  574. m := root; WHILE (m # NIL) & (m.term = NIL) DO m := m.next END;
  575. IF m # NIL THEN term := m.term; m.term := NIL END; (* finalizer only called once *)
  576. Machine.Release(Machine.Modules);
  577. IF m = NIL THEN EXIT END;
  578. IF trace THEN
  579. Machine.Acquire (Machine.TraceOutput);
  580. Trace.String("TermHandler "); Trace.StringLn (m.name);
  581. Machine.Release (Machine.TraceOutput);
  582. END;
  583. term (* if this causes exception or hangs, another shutdown call will retry *)
  584. END;
  585. (* clean up finalizers *)
  586. m := root;
  587. WHILE m # NIL DO
  588. IF LEN(m.code)>0 THEN
  589. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  590. END;
  591. m := m.next
  592. END;
  593. IF trace THEN
  594. Machine.Acquire (Machine.TraceOutput);
  595. Trace.StringLn ("Modules.Shutdown finished");
  596. Machine.Release (Machine.TraceOutput);
  597. END;
  598. Machine.Shutdown(code = Reboot) (* does not return *)
  599. END
  600. END Shutdown;
  601. (* Is this PC handled in the corresponding module. deep = scan the whole stack. *)
  602. PROCEDURE IsExceptionHandled*(VAR pc, fp: ADDRESS; deep: BOOLEAN): BOOLEAN;
  603. VAR
  604. handler: ADDRESS;
  605. BEGIN
  606. IF deep THEN
  607. handler := GetExceptionHandler(pc);
  608. IF handler # -1 THEN (* Handler in the current PAF *)
  609. RETURN TRUE
  610. ELSE
  611. WHILE (fp # 0) & (handler = -1) DO
  612. SYSTEM.GET (fp + 4, pc);
  613. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  614. handler := GetExceptionHandler(pc);
  615. SYSTEM.GET (fp, fp) (* Unwind PAF *)
  616. END;
  617. IF handler = -1 THEN RETURN FALSE ELSE pc := handler; RETURN TRUE END
  618. END
  619. ELSE
  620. RETURN GetExceptionHandler(pc) # -1
  621. END
  622. END IsExceptionHandled;
  623. (* Is this PC handled in the corresponding module. If the PC is handled the PC of the
  624. handler is return else -1 is return. There is no problem concurrently accessing this
  625. procedure, there is only reading work. *)
  626. PROCEDURE GetExceptionHandler*(pc: ADDRESS): ADDRESS;
  627. VAR
  628. m: Module;
  629. PROCEDURE BinSearch(exTable: ExceptionTable; key: ADDRESS): ADDRESS;
  630. VAR
  631. x, l, r: LONGINT;
  632. BEGIN
  633. l := 0; r:=LEN(exTable) - 1;
  634. REPEAT
  635. x := (l + r) DIV 2;
  636. IF key < exTable[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  637. UNTIL ((key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) ) OR (l > r);
  638. IF (key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) THEN
  639. RETURN exTable[x].pcHandler;
  640. ELSE
  641. RETURN -1;
  642. END
  643. END BinSearch;
  644. BEGIN
  645. m := ThisModuleByAdr(pc);
  646. IF (m # NIL) & (m.exTable # NIL) & (LEN(m.exTable) > 0) THEN
  647. RETURN BinSearch(m.exTable, pc);
  648. END;
  649. RETURN -1;
  650. END GetExceptionHandler;
  651. (** fof: to make custom solutions to the race process, described below, possible. This is not a solution to the generic problem !! *)
  652. PROCEDURE Initialized*(m: Module): BOOLEAN;
  653. BEGIN
  654. RETURN m.init;
  655. END Initialized;
  656. PROCEDURE Init;
  657. VAR
  658. s: ARRAY 4 OF CHAR;
  659. BEGIN
  660. (* root and initBlock are initialized by the linker *)
  661. shutdown := None;
  662. numLoaders := 0;
  663. freeRoot := NIL;
  664. Machine.GetConfig("TraceModules", s);
  665. trace := (s[0] = "1");
  666. END Init;
  667. PROCEDURE Register- (module {UNTRACED}: Module);
  668. BEGIN {UNCOOPERATIVE, UNCHECKED}
  669. register[registered] := module;
  670. INC (registered);
  671. END Register;
  672. PROCEDURE PublishRegisteredModules;
  673. VAR m {UNTRACED}: Module; module, import: SIZE;
  674. BEGIN
  675. Activities.Call (Activities.Idle);
  676. FOR module := 0 TO registered - 1 DO
  677. m := register[module];
  678. IF m.module # NIL THEN
  679. FOR import := 0 TO LEN (m.module) - 1 DO
  680. Initialize (m.module[import]);
  681. END;
  682. END;
  683. Initialize (m);
  684. END;
  685. END PublishRegisteredModules;
  686. PROCEDURE FinalizeModules;
  687. VAR module {UNTRACED}: Module;
  688. PROCEDURE Finalize EXTERN "BaseTypes.Object.Finalize" (module {UNTRACED}: Module);
  689. BEGIN {UNCOOPERATIVE, UNCHECKED}
  690. module := root;
  691. WHILE module # NIL DO
  692. Finalize (module);
  693. module := module.next;
  694. END;
  695. END FinalizeModules;
  696. PROCEDURE {FINAL} Main;
  697. PROCEDURE InitializeGC EXTERN "GarbageCollector.Initialize";
  698. PROCEDURE TerminateGC EXTERN "GarbageCollector.Terminate";
  699. BEGIN {UNCOOPERATIVE, UNCHECKED}
  700. SYSTEM.SetActivity (NIL);
  701. SYSTEM.SetFramePointer (NIL);
  702. Environment.Initialize;
  703. InitializeGC;
  704. Processors.Initialize;
  705. Activities.Execute (PublishRegisteredModules);
  706. Processors.Terminate;
  707. FinalizeModules;
  708. Activities.Terminate;
  709. Queues.Terminate;
  710. Environment.Terminate;
  711. TerminateGC;
  712. Environment.Exit (Environment.status);
  713. END Main;
  714. BEGIN
  715. Init
  716. END Modules.