Generic.Modules.Mod 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. MODULE Modules; (** AUTHOR "pjm"; PURPOSE "Modules and types"; *)
  2. IMPORT SYSTEM, Trace, Machine, Heaps, Runtime;
  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. InitTableLen = 1024;
  17. InitPtrTableLen = 2048;
  18. DefaultContext* = "A2";
  19. NoLoader=3400;
  20. TraceBoot=TRUE;
  21. TYPE
  22. (* definitions for object-model loader support *)
  23. Name* = ARRAY 32 OF CHAR;
  24. (* the correponding name array is protected from being GCed via module's internal pointer arrray
  25. compiler generated!
  26. *)
  27. DynamicName* = POINTER {UNSAFE} TO ARRAY 256 OF CHAR;
  28. Command* = RECORD
  29. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  30. name*: Name; (* name of the procedure *)
  31. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  32. entryAdr* : ADDRESS; (* entry address of procedure *)
  33. END;
  34. ExportDesc* = RECORD
  35. fp*: LONGINT;
  36. name* {UNTRACED}: DynamicName;
  37. adr*: ADDRESS;
  38. exports*: LONGINT;
  39. dsc* {UNTRACED}: ExportArray
  40. END;
  41. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  42. Bytes* = POINTER TO ARRAY OF CHAR;
  43. TerminationHandler* = PROCEDURE;
  44. (* all implicit or explicit pointers in the subsequent data structures are protected with one pointer array
  45. *)
  46. EntryType*=RECORD
  47. class*: CHAR;
  48. subclass*: CHAR;
  49. (* size in bits *)
  50. size*: INTEGER;
  51. type*: ADDRESS; (* type descriptor or additional information *)
  52. END;
  53. FieldEntry* = RECORD
  54. name*: DynamicName;
  55. offset*: SIZE; (* offset of this type *)
  56. type*: EntryType;
  57. flags*: SET;
  58. END;
  59. FieldEntries*= POINTER TO ARRAY OF FieldEntry;
  60. ProcedureEntries*=POINTER TO ARRAY OF ProcedureEntry;
  61. ProcedureEntry*=RECORD
  62. name*: DynamicName;
  63. address*: ADDRESS;
  64. size*: SIZE;
  65. parameters* {UNTRACED}: FieldEntries;
  66. variables* {UNTRACED}: FieldEntries;
  67. procedures* {UNTRACED}: ProcedureEntries;
  68. returnType*: EntryType;
  69. flags*: SET;
  70. END;
  71. TypeDesc* = POINTER TO RECORD
  72. descSize: LONGINT;
  73. sentinel: LONGINT; (* = MPO-4 *)
  74. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  75. flags*: SET;
  76. mod* {UNTRACED}: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  77. name*: Name;
  78. fields* {UNTRACED}: FieldEntries;
  79. procedures* {UNTRACED}: ProcedureEntries;
  80. END;
  81. ExceptionTableEntry* = RECORD
  82. pcFrom*: ADDRESS;
  83. pcTo*: ADDRESS;
  84. pcHandler*: ADDRESS;
  85. END;
  86. ExceptionTable* = POINTER TO ARRAY OF ExceptionTableEntry;
  87. ProcTableEntry* = RECORD
  88. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  89. noPtr*: LONGINT;
  90. END;
  91. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  92. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  93. ProcOffsetEntry* = RECORD
  94. data*: ProcTableEntry; (* code offsets of procedures *)
  95. startIndex*: LONGINT; (* index into global ptrOffsets table *)
  96. END;
  97. ProcOffsetTable* = POINTER TO ARRAY OF ProcOffsetEntry;
  98. (* don't rename this --> compile relies on this name *)
  99. InternalPtr= RECORD p: ANY END;
  100. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  101. VAR
  102. next*: Module; (** once a module is published, all fields are read-only *)
  103. name*: Name;
  104. init, published: BOOLEAN;
  105. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  106. sb*: ADDRESS; (* reference address between constants and local variables *)
  107. entry*: POINTER TO ARRAY OF ADDRESS;
  108. command*: POINTER TO ARRAY OF Command;
  109. ptrAdr*: POINTER TO ARRAY OF ADDRESS; (* traced explicitly in FindRoots *)
  110. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  111. module*: POINTER TO ARRAY OF Module; (* imported modules: for reference counting *)
  112. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  113. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  114. data*, code*, staticTypeDescs* (* ug *), refs*: Bytes;
  115. export*: ExportDesc;
  116. term*: TerminationHandler;
  117. exTable*: ExceptionTable;
  118. noProcs*: LONGINT; (* used for removing proc offsets when unloading module *)
  119. firstProc*: ADDRESS; (* procedure with lowest PC in module, also used for unloading *)
  120. maxPtrs*: LONGINT;
  121. (* internal pointer array: to protect internal data structures from being GCed *)
  122. internal: POINTER TO ARRAY OF InternalPtr;
  123. crc*: LONGINT;
  124. body*: PROCEDURE;
  125. PROCEDURE FindRoots; (* override *)
  126. VAR i: LONGINT; ptr: ANY; (* moduleName: Name; *) false: BOOLEAN;
  127. BEGIN
  128. false := FALSE; IF false THEN BEGIN{EXCLUSIVE} END END; (* trick to make a module a protected record ... *)
  129. IF published THEN (* mark global pointers *)
  130. (* moduleName := name; *)
  131. FOR i := 0 TO LEN(ptrAdr) - 1 DO
  132. SYSTEM.GET (ptrAdr[i], ptr);
  133. IF ptr # NIL THEN Heaps.Mark(ptr) END
  134. END;
  135. Heaps.AddRootObject(next);
  136. (* all other fields are being traversed by Mark of the Garbage Collector *)
  137. END;
  138. END FindRoots;
  139. END Module;
  140. LoaderProc* = PROCEDURE (CONST name, fileName: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR): Module; (** load an object file *)
  141. VAR
  142. extension-: ARRAY MaxObjFormats, 8 OF CHAR;
  143. loader: ARRAY MaxObjFormats OF LoaderProc;
  144. numLoaders: LONGINT;
  145. kernelProc*: ARRAY 11 OF ADDRESS; (** kernel call addresses for loader *)
  146. freeRoot*: Module; (** list of freed modules (temporary) *)
  147. (* the following two variables are initialized by Linker *)
  148. root-: Module; (** list of modules (read-only) *)
  149. initBlock: ANY; (* placeholder - anchor for module init code (initialized by linker) *)
  150. procOffsets-: ProcOffsetTable; (* global table containing procedure code offsets and pointer offsets, sorted in ascending order of procedure code offsets *)
  151. numProcs: LONGINT; (* number of entries in procOffsets *)
  152. ptrOffsets-: PtrTable;
  153. numPtrs: LONGINT;
  154. shutdown*: LONGINT; (** None, Reboot, PowerDown *)
  155. trace: BOOLEAN;
  156. register: RECORD
  157. first, last: Module;
  158. END;
  159. (** Register a module loader. *)
  160. PROCEDURE AddLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  161. BEGIN
  162. Machine.Acquire(Machine.Modules);
  163. ASSERT(numLoaders < MaxObjFormats);
  164. loader[numLoaders] := proc;
  165. COPY(ext, extension[numLoaders]);
  166. ASSERT(ext = extension[numLoaders]); (* no overflow *)
  167. INC(numLoaders);
  168. Machine.Release(Machine.Modules)
  169. END AddLoader;
  170. (** Remove a module loader. *)
  171. PROCEDURE RemoveLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  172. VAR i, j: LONGINT;
  173. BEGIN
  174. Machine.Acquire(Machine.Modules);
  175. i := 0;
  176. WHILE (i # numLoaders) & ((loader[i] # proc) OR (extension[i] # ext)) DO INC(i) END;
  177. IF i # numLoaders THEN
  178. FOR j := i TO numLoaders - 2 DO
  179. loader[j] := loader[j + 1]; extension[j] := extension[j + 1];
  180. END;
  181. loader[numLoaders - 1] := NIL; extension[numLoaders - 1] := "";
  182. DEC(numLoaders)
  183. END;
  184. Machine.Release(Machine.Modules)
  185. END RemoveLoader;
  186. (** Append string from to to, truncating on overflow. *)
  187. PROCEDURE Append*(CONST from: ARRAY OF CHAR; VAR to: ARRAY OF CHAR);
  188. VAR i, j, m: LONGINT;
  189. BEGIN
  190. j := 0; WHILE to[j] # 0X DO INC(j) END;
  191. m := LEN(to)-1;
  192. i := 0; WHILE (from[i] # 0X) & (j # m) DO to[j] := from[i]; INC(i); INC(j) END;
  193. to[j] := 0X
  194. END Append;
  195. (** Add a module to the pool of accessible modules, or return named module. *)
  196. PROCEDURE Publish*(VAR m: Module; VAR new: BOOLEAN);
  197. VAR n: Module; i: LONGINT; a: ANY;
  198. BEGIN
  199. (*
  200. ASSERT((m.code # NIL) & (LEN(m.code^) > 0));
  201. *)
  202. Machine.Acquire(Machine.Modules);
  203. n := root; WHILE (n # NIL) & (n.name # m.name) DO n := n.next END;
  204. IF n # NIL THEN (* module with same name exists, return it and ignore new m *)
  205. m := n; new := FALSE
  206. ELSE
  207. IF TraceBoot THEN
  208. Machine.Acquire(Machine.TraceOutput);
  209. Trace.String("publish "); Trace.String(m.name);
  210. (*
  211. a := m;
  212. IF a IS Heaps.RootObject THEN Trace.String(" IS RootObj") END;
  213. IF a IS Module THEN Trace.String(" IS Module"); END;
  214. *)
  215. Trace.Ln;
  216. Machine.Release(Machine.TraceOutput);
  217. END;
  218. m.published := TRUE;
  219. m.next := root; root := m;
  220. m.refcnt := 0;
  221. (*! reactivate: does not work with statically linked image
  222. SortProcTable(m);
  223. InsertProcOffsets(m.procTable, m.ptrTable, m.maxPtrs);
  224. (*! yes: used, cf. ThisModuleByAdr *)
  225. m.procTable := NIL; m.ptrTable := NIL; (* not used any more as entered in global variable *)
  226. *)
  227. IF m.module # NIL THEN
  228. FOR i := 0 TO LEN(m.module)-1 DO INC(m.module[i].refcnt) END;
  229. END;
  230. new := TRUE;
  231. END;
  232. Machine.Release(Machine.Modules)
  233. END Publish;
  234. (*
  235. (* runtime call for new compiler -- called by body of loaded module *)
  236. PROCEDURE PublishThis*(m: Module): BOOLEAN;
  237. VAR new: BOOLEAN; i:LONGINT; module: Module;
  238. BEGIN
  239. IF m = SELF THEN
  240. RETURN Runtime.InsertModule(SYSTEM.VAL(ADDRESS,m))
  241. END;
  242. Publish(m,new);
  243. RETURN new
  244. END PublishThis;
  245. *)
  246. PROCEDURE Initialize*(VAR module: Module);
  247. VAR new: BOOLEAN;
  248. BEGIN
  249. IF (module = NIL) THEN RETURN END;
  250. Publish (module, new);
  251. IF new THEN
  252. IF module.body # NIL THEN
  253. Machine.FlushDCacheRange(ADDRESSOF(module.code[0]), LEN(module.code));
  254. module.body
  255. END;
  256. module.init := TRUE;
  257. END;
  258. END Initialize;
  259. VAR callagain: BOOLEAN;
  260. PROCEDURE Initialize0*(module: Module);
  261. VAR new: BOOLEAN;
  262. BEGIN
  263. (*TRACE(module.name);*)
  264. (* module MUST have been removed from register list and must not have been initialized yet *)
  265. ASSERT(module.next = NIL);
  266. Publish (module, new);
  267. callagain := FALSE;
  268. IF new THEN
  269. IF module.name = "Objects" THEN
  270. callagain := TRUE;
  271. module.init := TRUE;
  272. END;
  273. (*
  274. Trace.Memory(SYSTEM.VAL(ADDRESS, module), 256);
  275. TRACE(module, module.name, module.body);
  276. TRACE(module);
  277. TRACE(ADDRESS OF module.next);
  278. TRACE(ADDRESS OF module.name);
  279. TRACE(ADDRESS OF module.init);
  280. TRACE(ADDRESS OF module.published);
  281. TRACE(ADDRESS OF module.body);
  282. TRACE(ADDRESS OF module.refcnt);
  283. TRACE(ADDRESS OF module.sb);
  284. TRACE(ADDRESS OF module.entry);
  285. TRACE(ADDRESS OF module.command);
  286. TRACE(ADDRESS OF module.ptrAdr);
  287. TRACE(ADDRESS OF module.typeInfo);
  288. TRACE(ADDRESS OF module.module);
  289. TRACE(ADDRESS OF module.procTable);
  290. TRACE(ADDRESS OF module.ptrTable);
  291. TRACE(ADDRESS OF module.data);
  292. TRACE(ADDRESS OF module.code);
  293. TRACE(ADDRESS OF module.staticTypeDescs);
  294. TRACE(ADDRESS OF module.refs);
  295. TRACE(ADDRESS OF module.export);
  296. TRACE(ADDRESS OF module.term);
  297. TRACE(ADDRESS OF module.exTable);
  298. TRACE(ADDRESS OF module.noProcs);
  299. TRACE(ADDRESS OF module.firstProc);
  300. TRACE(ADDRESS OF module.maxPtrs);
  301. TRACE(ADDRESS OF module.crc);
  302. TRACE(ADDRESS OF module.body);
  303. *)
  304. IF module.body # NIL THEN module.body END;
  305. IF callagain THEN
  306. 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 *)
  307. ELSE
  308. module.init := TRUE;
  309. END;
  310. END;
  311. END Initialize0;
  312. (** Return the named module or NIL if it is not loaded yet. *)
  313. PROCEDURE ModuleByName*(CONST name: ARRAY OF CHAR): Module;
  314. VAR m: Module;
  315. BEGIN
  316. Machine.Acquire(Machine.Modules);
  317. m := root; WHILE (m # NIL) & (m.name # name) DO m := m.next END;
  318. Machine.Release(Machine.Modules);
  319. RETURN m
  320. END ModuleByName;
  321. (* Generate a module file name. *)
  322. PROCEDURE GetFileName(CONST name, extension: ARRAY OF CHAR; VAR fileName: ARRAY OF CHAR);
  323. VAR i, j: LONGINT;
  324. BEGIN
  325. i := 0; WHILE name[i] # 0X DO fileName[i] := name[i]; INC(i) END;
  326. j := 0; WHILE extension[j] # 0X DO fileName[i] := extension[j]; INC(i); INC(j) END;
  327. fileName[i] := 0X
  328. END GetFileName;
  329. PROCEDURE SortProcTable(m: Module);
  330. VAR i, j, min : LONGINT;
  331. PROCEDURE Max(a,b: LONGINT): LONGINT;
  332. BEGIN
  333. IF a > b THEN RETURN a ELSE RETURN b END;
  334. END Max;
  335. PROCEDURE SwapProcTableEntries(p, q : LONGINT);
  336. VAR procentry : ProcTableEntry;
  337. k, i, basep, baseq: LONGINT; ptr: SIZE;
  338. BEGIN
  339. k := Max(m.procTable[p].noPtr, m.procTable[q].noPtr);
  340. IF k > 0 THEN (* swap entries in ptrTable first *)
  341. basep := p * m.maxPtrs; baseq := q * m.maxPtrs;
  342. FOR i := 0 TO k - 1 DO
  343. ptr := m.ptrTable[basep + i];
  344. m.ptrTable[basep + i] := m.ptrTable[baseq + i];
  345. m.ptrTable[baseq + i] := ptr
  346. END
  347. END;
  348. procentry := m.procTable[p];
  349. m.procTable[p] := m.procTable[q];
  350. m.procTable[q] := procentry
  351. END SwapProcTableEntries;
  352. PROCEDURE NormalizePointerArray;
  353. VAR ptrTable: PtrTable; i,j,k: LONGINT;
  354. BEGIN
  355. NEW(ptrTable, m.maxPtrs*m.noProcs);
  356. k := 0;
  357. FOR i := 0 TO LEN(m.procTable)-1 DO
  358. FOR j := 0 TO m.procTable[i].noPtr-1 DO
  359. ptrTable[i*m.maxPtrs+j] := m.ptrTable[k];
  360. INC(k);
  361. END;
  362. END;
  363. m.ptrTable := ptrTable;
  364. END NormalizePointerArray;
  365. BEGIN
  366. NormalizePointerArray;
  367. FOR i := 0 TO m.noProcs - 2 DO
  368. min := i;
  369. FOR j := i + 1 TO m.noProcs - 1 DO
  370. IF m.procTable[j].pcFrom < m.procTable[min].pcFrom THEN min:= j END
  371. END;
  372. IF min # i THEN SwapProcTableEntries(i, min) END
  373. END
  374. END SortProcTable;
  375. PROCEDURE SelectionSort(exTable: ExceptionTable);
  376. VAR
  377. p, q, min: LONGINT;
  378. entry: ExceptionTableEntry;
  379. BEGIN
  380. FOR p := 0 TO LEN(exTable) - 2 DO
  381. min := p;
  382. FOR q := p + 1 TO LEN(exTable) - 1 DO
  383. IF exTable[min].pcFrom > exTable[q].pcFrom THEN min := q END;
  384. entry := exTable[min]; exTable[min] := exTable[p]; exTable[p] := entry;
  385. END
  386. END
  387. END SelectionSort;
  388. (** Load the module if it is not already loaded. *) (* Algorithm J. Templ, ETHZ, 1994 *)
  389. PROCEDURE ThisModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR): Module;
  390. TYPE Body = PROCEDURE;
  391. VAR m, p: Module; fileName: ARRAY 64 OF CHAR; body: Body; new: BOOLEAN; i: LONGINT;
  392. BEGIN
  393. res := Ok; msg[0] := 0X; m := ModuleByName(name);
  394. IF m = NIL THEN
  395. IF trace THEN
  396. Machine.Acquire (Machine.TraceOutput);
  397. Trace.String(">"); Trace.StringLn (name);
  398. Machine.Release (Machine.TraceOutput);
  399. END;
  400. IF numLoaders = 0 THEN
  401. res := NoLoader; m := NIL;
  402. ELSE
  403. i:= 0;
  404. REPEAT
  405. GetFileName(name, extension[i], fileName);
  406. m := loader[i](name, fileName, res, msg);
  407. INC(i);
  408. UNTIL (m # NIL) OR (i=numLoaders);
  409. END;
  410. IF trace THEN
  411. Machine.Acquire (Machine.TraceOutput);
  412. Trace.String("?"); Trace.StringLn (name);
  413. Machine.Release (Machine.TraceOutput);
  414. END;
  415. p := m;
  416. IF (m # NIL) & ~m.published THEN (* no race on m.published, as update is done below in Publish *)
  417. Initialize(m);
  418. END;
  419. IF trace THEN
  420. Machine.Acquire (Machine.TraceOutput);
  421. IF m = NIL THEN
  422. Trace.String("could not load "); Trace.StringLn(name)
  423. ELSIF ~m.published THEN
  424. Trace.String("not published "); Trace.StringLn(name)
  425. ELSE
  426. Trace.String("<"); Trace.StringLn (name);
  427. END;
  428. Machine.Release (Machine.TraceOutput);
  429. END;
  430. END;
  431. RETURN m
  432. END ThisModule;
  433. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. Non-blocking version for reflection *)
  434. PROCEDURE ThisModuleByAdr0*(pc: ADDRESS): Module;
  435. VAR m: Module; cbase, dbase: ADDRESS; i: LONGINT; found: BOOLEAN; list: LONGINT;
  436. BEGIN
  437. list := 0; found := FALSE;
  438. REPEAT
  439. CASE list OF
  440. 0: m := root
  441. |1: m := freeRoot
  442. END;
  443. WHILE (m # NIL) & ~found DO
  444. IF m.procTable # NIL THEN
  445. i := 0;
  446. WHILE ~found & (i<LEN(m.procTable)) DO
  447. IF (m.procTable[i].pcFrom <= pc) & (pc <m.procTable[i].pcLimit) THEN
  448. found := TRUE;
  449. END;
  450. INC(i);
  451. END;
  452. END;
  453. IF ~found THEN
  454. m := m.next;
  455. END;
  456. END;
  457. INC(list)
  458. UNTIL found OR (list=2);
  459. RETURN m
  460. END ThisModuleByAdr0;
  461. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. *)
  462. PROCEDURE ThisModuleByAdr*(pc: ADDRESS): Module;
  463. VAR m: Module; cbase, dbase: ADDRESS; i: LONGINT; found: BOOLEAN; list: LONGINT;
  464. BEGIN
  465. Machine.Acquire(Machine.Modules);
  466. m := ThisModuleByAdr0(pc);
  467. Machine.Release(Machine.Modules);
  468. RETURN m
  469. END ThisModuleByAdr;
  470. CONST ModuleInitTimeout = HUGEINT(3000000000); (* Timeout for waiting until a module get initialized, 3 seconds for 1 GHz CPU *)
  471. (* Retrieve a procedure given a module name, the procedure name and some type information (kernel call) *)
  472. PROCEDURE GetProcedure*(CONST moduleName, procedureName : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  473. VAR module : Module; ignoreMsg : ARRAY 32 OF CHAR; i, res : LONGINT; t: HUGEINT;
  474. BEGIN
  475. module := ThisModule(moduleName, res, ignoreMsg);
  476. IF (res = Ok) THEN
  477. (*!
  478. module body must have been called (see note at the end of this module);
  479. return NIL if the module does not get initialized within the specified timeout
  480. *)
  481. IF ~module.init THEN
  482. t := Machine.GetTimer();
  483. WHILE ~module.init & (Machine.GetTimer() - t < ModuleInitTimeout) DO END;
  484. IF ~module.init THEN (* timeout has expired *)
  485. RETURN;
  486. END;
  487. END;
  488. Machine.Acquire(Machine.Modules);
  489. i := 0; entryAdr := Heaps.NilVal;
  490. WHILE (entryAdr = Heaps.NilVal) & (i # LEN(module.command^)) DO
  491. IF (module.command[i].name = procedureName) & (module.command[i].argTdAdr = argTdAdr) & (module.command[i].retTdAdr = retTdAdr) THEN
  492. entryAdr := module.command[i].entryAdr;
  493. END;
  494. INC(i)
  495. END;
  496. Machine.Release(Machine.Modules);
  497. END;
  498. END GetProcedure;
  499. (** Return the named type *)
  500. PROCEDURE ThisType*(m: Module; CONST name: ARRAY OF CHAR): TypeDesc;
  501. VAR i: LONGINT; type: TypeDesc;
  502. BEGIN
  503. Machine.Acquire(Machine.Modules);
  504. i := 0;
  505. WHILE (i < LEN(m.typeInfo)) & (m.typeInfo[i].name # name) DO INC(i) END;
  506. IF i = LEN(m.typeInfo) THEN
  507. type := NIL
  508. ELSE
  509. type := m.typeInfo[i]
  510. END;
  511. Machine.Release(Machine.Modules);
  512. RETURN type
  513. END ThisType;
  514. PROCEDURE ThisTypeByAdr*(adr: ADDRESS; VAR m: Module; VAR t: TypeDesc);
  515. BEGIN
  516. IF adr # 0 THEN
  517. Machine.Acquire(Machine.Modules);
  518. SYSTEM.GET (adr + Heaps.TypeDescOffset, adr);
  519. t := SYSTEM.VAL(TypeDesc, adr);
  520. m := t.mod;
  521. Machine.Release(Machine.Modules)
  522. ELSE
  523. m := NIL; t := NIL
  524. END
  525. END ThisTypeByAdr;
  526. (** create a new object given its type descriptor *)
  527. PROCEDURE NewObj*(t : TypeDesc; isRealtime: BOOLEAN) : ANY;
  528. VAR x : ANY;
  529. BEGIN
  530. Heaps.NewRec(x, SYSTEM.VAL (ADDRESS, t.tag), isRealtime);
  531. RETURN x;
  532. END NewObj;
  533. (** return the type descriptor of an object *)
  534. PROCEDURE TypeOf*(obj : ANY): TypeDesc;
  535. VAR
  536. m : Module;
  537. t : TypeDesc;
  538. adr : ADDRESS;
  539. BEGIN
  540. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.TypeDescOffset, adr);
  541. ThisTypeByAdr(adr, m, t);
  542. RETURN t;
  543. END TypeOf;
  544. PROCEDURE FindPos(key: ADDRESS; VAR pos: LONGINT): BOOLEAN;
  545. VAR l, r, x: LONGINT; isHit: BOOLEAN;
  546. BEGIN
  547. IF numProcs > 0 THEN
  548. l := 0; r := numProcs - 1;
  549. REPEAT
  550. x := (l + r) DIV 2;
  551. IF key < procOffsets[x].data.pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  552. isHit := ((procOffsets[x].data.pcFrom <= key) & (key < procOffsets[x].data.pcLimit));
  553. UNTIL isHit OR (l > r);
  554. IF isHit THEN
  555. pos := x;
  556. RETURN TRUE
  557. END;
  558. END;
  559. RETURN FALSE
  560. END FindPos;
  561. (** searches for the given pc in the global ProcKeyTable, if found it returns the corresponding data element *)
  562. PROCEDURE FindProc*(pc: ADDRESS; VAR data: ProcTableEntry; VAR index: LONGINT; VAR success: BOOLEAN);
  563. VAR x: LONGINT;
  564. BEGIN
  565. success := FindPos(pc, x);
  566. IF success THEN
  567. data := procOffsets[x].data;
  568. index := procOffsets[x].startIndex
  569. END
  570. END FindProc;
  571. PROCEDURE FindInsertionPos(VAR entry: ProcTableEntry; VAR pos: LONGINT): BOOLEAN;
  572. VAR l, r, x: LONGINT; success, isHit: BOOLEAN;
  573. BEGIN
  574. pos := -1;
  575. success := FALSE;
  576. IF numProcs = 0 THEN (* empty table *)
  577. pos := 0; success := TRUE
  578. ELSE
  579. l := 0; r := numProcs - 1;
  580. REPEAT
  581. x := (l + r) DIV 2;
  582. IF entry.pcLimit < procOffsets[x].data.pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  583. isHit := ((x = 0) OR (procOffsets[x - 1].data.pcLimit <= entry.pcFrom)) & (entry.pcLimit <= procOffsets[x].data.pcFrom);
  584. UNTIL isHit OR (l > r);
  585. IF isHit THEN
  586. pos := x; success := TRUE
  587. ELSE
  588. IF (x = numProcs - 1) & (procOffsets[x].data.pcLimit <= entry.pcFrom) THEN
  589. pos := x + 1; success := TRUE
  590. END
  591. END
  592. END;
  593. RETURN success
  594. END FindInsertionPos;
  595. PROCEDURE NumTotalPtrs(procTable: ProcTable): LONGINT;
  596. VAR i, num: LONGINT;
  597. BEGIN
  598. num := 0;
  599. IF procTable # NIL THEN
  600. FOR i := 0 TO LEN(procTable) - 1 DO
  601. num := num + procTable[i].noPtr
  602. END;
  603. END;
  604. RETURN num
  605. END NumTotalPtrs;
  606. (* insert the procedure code offsets and pointer offsets of a single module into the global table *)
  607. PROCEDURE InsertProcOffsets(procTable: ProcTable; ptrTable: PtrTable; maxPtr: LONGINT);
  608. VAR success: BOOLEAN; i, j, pos, poslast, newLen, num,numberPointer: LONGINT;
  609. temp: ADDRESS;
  610. newProcOffsets: ProcOffsetTable; newPtrOffsets: PtrTable;
  611. ptrOfsLen,procOfsLen: LONGINT;
  612. BEGIN
  613. (* this procedure is called by procedure Publish only and is protected by the Machine.Modules lock *)
  614. IF procTable=NIL THEN RETURN END;
  615. IF ptrTable=NIL THEN RETURN END;
  616. IF LEN(procTable) > 0 THEN
  617. IF procOffsets = NIL THEN procOfsLen := 0 ELSE procOfsLen := LEN(procOffsets) END;
  618. IF numProcs + LEN(procTable) > procOfsLen THEN
  619. newLen := procOfsLen + InitTableLen;
  620. WHILE numProcs + LEN(procTable) > newLen DO newLen := newLen + InitTableLen END;
  621. NEW(newProcOffsets, newLen);
  622. FOR i := 0 TO numProcs - 1 DO
  623. newProcOffsets[i] := procOffsets[i]
  624. END;
  625. procOffsets := newProcOffsets
  626. END;
  627. num := NumTotalPtrs(procTable);
  628. IF ptrOffsets = NIL THEN ptrOfsLen := 0 ELSE ptrOfsLen := LEN(ptrOffsets) END;
  629. IF numPtrs + num > ptrOfsLen THEN
  630. newLen := ptrOfsLen + InitPtrTableLen;
  631. WHILE numPtrs + num > newLen DO newLen := newLen + InitPtrTableLen END;
  632. NEW(newPtrOffsets, newLen);
  633. FOR i := 0 TO numPtrs - 1 DO
  634. newPtrOffsets[i] := ptrOffsets[i]
  635. END;
  636. ptrOffsets := newPtrOffsets
  637. END;
  638. success := FindInsertionPos(procTable[0], pos); success := success & FindInsertionPos(procTable[LEN(procTable) - 1], poslast);
  639. IF (~success) OR (pos # poslast) THEN Machine.Release(Machine.Modules); HALT(2001) END;
  640. FOR i := numProcs - 1 TO pos BY -1 DO procOffsets[i + LEN(procTable)] := procOffsets[i] END;
  641. numberPointer := 0;
  642. FOR i := 0 TO LEN(procTable) - 1 DO
  643. procOffsets[pos + i].data := procTable[i];
  644. procOffsets[pos + i].startIndex := numPtrs; (* this field is never accessed in case of procTable[i].noPtr = 0, so we may as well put numPtrs in there *)
  645. FOR j := 0 TO procTable[i].noPtr - 1 DO
  646. (*
  647. temp := ptrTable[numberPointer]; INC(numberPointer);
  648. *)
  649. temp := ptrTable[i * maxPtr + j];
  650. ptrOffsets[numPtrs + j] := temp;
  651. END;
  652. numPtrs := numPtrs + procTable[i].noPtr;
  653. END;
  654. numProcs := numProcs + LEN(procTable);
  655. END
  656. END InsertProcOffsets;
  657. (** deletes a sequence of entries given in procTable from the global procOffsets table - the table remains sorted,
  658. this procedure is called within AosLocks.AosModules, so no lock is taken here. *)
  659. PROCEDURE DeleteProcOffsets(firstProcPC: ADDRESS; noProcsInMod: LONGINT);
  660. VAR pos, i, noPtrsInMod, oldIndex: LONGINT; success: BOOLEAN;
  661. BEGIN
  662. IF noProcsInMod > 0 THEN
  663. success := FindPos(firstProcPC, pos);
  664. IF success THEN
  665. (* delete entries in ptrOffsets first *)
  666. noPtrsInMod := 0;
  667. FOR i := pos TO pos + noProcsInMod - 1 DO
  668. noPtrsInMod := noPtrsInMod + procOffsets[i].data.noPtr
  669. END;
  670. oldIndex := procOffsets[pos].startIndex;
  671. FOR i := procOffsets[pos].startIndex + noPtrsInMod TO numPtrs - 1 DO
  672. ptrOffsets[i - noPtrsInMod] := ptrOffsets[i]
  673. END;
  674. numPtrs := numPtrs - noPtrsInMod;
  675. (* delete entries in procOffsets *)
  676. FOR i := pos + noProcsInMod TO numProcs - 1 DO
  677. procOffsets[i - noProcsInMod] := procOffsets[i]
  678. END;
  679. numProcs := numProcs - noProcsInMod;
  680. (* adjust startIndex of procOffsets entries greater than those that have been deleted *)
  681. FOR i := 0 TO numProcs - 1 DO
  682. IF procOffsets[i].startIndex > oldIndex THEN
  683. procOffsets[i].startIndex := procOffsets[i].startIndex - noPtrsInMod
  684. END
  685. END;
  686. ELSE
  687. Trace.String("corrupt global procOffsets table"); Trace.Ln;
  688. Machine.Release(Machine.Modules);
  689. HALT(2000)
  690. END
  691. END
  692. END DeleteProcOffsets;
  693. (** 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. *)
  694. PROCEDURE InstallTermHandler*(h: TerminationHandler);
  695. VAR m: Module;
  696. BEGIN
  697. m := ThisModuleByAdr(SYSTEM.VAL (ADDRESS, h));
  698. IF m # NIL THEN
  699. m.term := h (* overwrite existing handler, if any *)
  700. END
  701. END InstallTermHandler;
  702. (** 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. *)
  703. PROCEDURE FreeModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR);
  704. VAR p, m: Module; term: TerminationHandler; i: LONGINT;
  705. BEGIN
  706. m := ModuleByName(name);
  707. IF (m # NIL) & (m.refcnt = 0) THEN (* will be freed below *)
  708. IF m.term # NIL THEN (* call termination handler *)
  709. term := m.term; m.term := NIL; term (* may trap *)
  710. END;
  711. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  712. END;
  713. res := Ok; msg[0] := 0X;
  714. Machine.Acquire(Machine.Modules);
  715. Trace.String("Acquired Machine.Modules x"); Trace.Ln;
  716. p := NIL; m := root;
  717. WHILE (m # NIL) & (m.name # name) DO p := m; m := m.next END;
  718. Trace.String("Acquired Machine.Modules y"); Trace.Ln;
  719. IF m # NIL THEN
  720. Trace.String("found module"); Trace.Ln;
  721. IF m.refcnt = 0 THEN (* free the module *)
  722. FOR i := 0 TO LEN(m.module)-1 DO DEC(m.module[i].refcnt) END;
  723. m.init := FALSE; (* disallow ThisCommand *)
  724. Append("?", m.name);
  725. (* move module to free list *)
  726. IF p = NIL THEN root := root.next ELSE p.next := m.next END;
  727. m.next := freeRoot; freeRoot := m;
  728. (* clear global pointers and code *)
  729. IF m.ptrAdr # NIL THEN
  730. Trace.String("ptradr del"); Trace.Ln;
  731. FOR i := 0 TO LEN(m.ptrAdr)-1 DO SYSTEM.PUT (m.ptrAdr[i], NIL) END;
  732. END;
  733. IF ClearCode & (m.code # NIL) THEN
  734. Trace.String("clear code"); Trace.Ln;
  735. FOR i := 0 TO LEN(m.code)-1 DO m.code[i] := 0CCX END
  736. END;
  737. Trace.String("clear code f"); Trace.Ln;
  738. (* remove references to module data *)
  739. m.published := FALSE;
  740. m.entry := NIL; m.command := NIL; m.ptrAdr := NIL;
  741. (* do not clear m.type or m.module, as old heap block tags might reference type descs indirectly. *) (* m.staticTypeDescs, m.typeInfo ??? *)
  742. (* do not clear m.data or m.code, as they are used in ThisModuleByAdr (for debugging). *)
  743. (* do not clear m.refs, as they are used in Traps (for debugging). *)
  744. m.export.dsc := NIL; m.exTable := NIL;
  745. (*Trace.String("delete proc offsets"); Trace.Ln;
  746. DeleteProcOffsets(m.firstProc, m.noProcs);
  747. *)
  748. ELSE
  749. res := 1901; (* can not free module in use *)
  750. COPY(name, msg); Append(" reference count not zero", msg)
  751. END
  752. ELSE
  753. res := 1902; (* module not found *)
  754. COPY(name, msg); Append(" not found", msg)
  755. END;
  756. Machine.Release(Machine.Modules)
  757. END FreeModule;
  758. (** Shut down all modules by calling their termination handlers and then call Machine.Shutdown. *)
  759. PROCEDURE Shutdown*(code: LONGINT);
  760. VAR m: Module; term: TerminationHandler;
  761. BEGIN
  762. IF code # None THEN
  763. LOOP
  764. Machine.Acquire(Machine.Modules);
  765. m := root; WHILE (m # NIL) & (m.term = NIL) DO m := m.next END;
  766. IF m # NIL THEN term := m.term; m.term := NIL END;
  767. Machine.Release(Machine.Modules);
  768. IF m = NIL THEN EXIT END;
  769. IF trace THEN
  770. Machine.Acquire (Machine.TraceOutput);
  771. Trace.String("TermHandler "); Trace.StringLn (m.name);
  772. Machine.Release (Machine.TraceOutput);
  773. END;
  774. term (* if this causes exception or hangs, another shutdown call will retry *)
  775. END;
  776. (* clean up finalizers *)
  777. m := root;
  778. WHILE m # NIL DO
  779. IF LEN(m.code)>0 THEN
  780. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  781. END;
  782. m := m.next
  783. END;
  784. IF trace THEN
  785. Machine.Acquire (Machine.TraceOutput);
  786. Trace.StringLn ("Modules.Shutdown finished");
  787. Machine.Release (Machine.TraceOutput);
  788. END;
  789. Machine.Shutdown(code = Reboot) (* does not return *)
  790. END
  791. END Shutdown;
  792. (* Is this PC handled in the corresponding module. deep = scan the whole stack. *)
  793. PROCEDURE IsExceptionHandled*(VAR pc, fp: ADDRESS; deep: BOOLEAN): BOOLEAN;
  794. VAR
  795. handler: ADDRESS;
  796. BEGIN
  797. IF deep THEN
  798. handler := GetExceptionHandler(pc);
  799. IF handler # -1 THEN (* Handler in the current PAF *)
  800. RETURN TRUE
  801. ELSE
  802. WHILE (fp # 0) & (handler = -1) DO
  803. SYSTEM.GET (fp + 4, pc);
  804. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  805. handler := GetExceptionHandler(pc);
  806. SYSTEM.GET (fp, fp) (* Unwind PAF *)
  807. END;
  808. IF handler = -1 THEN RETURN FALSE ELSE pc := handler; RETURN TRUE END
  809. END
  810. ELSE
  811. RETURN GetExceptionHandler(pc) # -1
  812. END
  813. END IsExceptionHandled;
  814. (* Is this PC handled in the corresponding module. If the PC is handled the PC of the
  815. handler is return else -1 is return. There is no problem concurrently accessing this
  816. procedure, there is only reading work. *)
  817. PROCEDURE GetExceptionHandler*(pc: ADDRESS): ADDRESS;
  818. VAR
  819. m: Module;
  820. PROCEDURE BinSearch(exTable: ExceptionTable; key: ADDRESS): ADDRESS;
  821. VAR
  822. x, l, r: LONGINT;
  823. BEGIN
  824. l := 0; r:=LEN(exTable) - 1;
  825. REPEAT
  826. x := (l + r) DIV 2;
  827. IF key < exTable[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  828. UNTIL ((key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) ) OR (l > r);
  829. IF (key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) THEN
  830. RETURN exTable[x].pcHandler;
  831. ELSE
  832. RETURN -1;
  833. END
  834. END BinSearch;
  835. BEGIN
  836. m := ThisModuleByAdr(pc);
  837. IF (m # NIL) & (m.exTable # NIL) & (LEN(m.exTable) > 0) THEN
  838. RETURN BinSearch(m.exTable, pc);
  839. END;
  840. RETURN -1;
  841. END GetExceptionHandler;
  842. (** fof: to make custom solutions to the race process, described below, possible. This is not a solution to the generic problem !! *)
  843. PROCEDURE Initialized*(m: Module): BOOLEAN;
  844. BEGIN
  845. RETURN m.init;
  846. END Initialized;
  847. (** Return the specified kernel procedure address. *)
  848. PROCEDURE GetKernelProc*(num: LONGINT): ADDRESS;
  849. VAR adr: ADDRESS;
  850. BEGIN
  851. adr := kernelProc[253-num];
  852. ASSERT(adr # 0);
  853. RETURN adr
  854. END GetKernelProc;
  855. PROCEDURE Register- (module {UNTRACED}: Module);
  856. BEGIN {UNCOOPERATIVE, UNCHECKED}
  857. (*TRACE(module.name);*)
  858. IF register.first = NIL THEN
  859. register.first := module;
  860. ELSE
  861. register.last.next := module;
  862. END;
  863. register.last := module;
  864. END Register;
  865. PROCEDURE PublishRegisteredModules;
  866. VAR m {UNTRACED}, prev {UNTRACED}, cur {UNTRACED}: Module; module, import: SIZE;
  867. BEGIN
  868. WHILE register.first # NIL DO
  869. m := register.first;
  870. register.first := m.next;
  871. m.next := NIL;
  872. IF m.module # NIL THEN
  873. FOR import := 0 TO LEN (m.module) - 1 DO
  874. IF ~m.module[import].published THEN
  875. ASSERT(register.first # NIL);
  876. prev := NIL;
  877. cur := register.first;
  878. WHILE (cur # NIL) & (cur # m.module[import]) DO
  879. prev := cur;
  880. cur := cur.next
  881. END;
  882. (*ASSERT(cur = m.module[import]);*)
  883. ASSERT(cur = m.module[import]);
  884. IF prev = NIL THEN
  885. register.first := cur.next
  886. ELSE
  887. prev.next := cur.next;
  888. END;
  889. cur.next := NIL;
  890. Initialize0 (m.module[import]);
  891. END
  892. END;
  893. END;
  894. Initialize0 (m);
  895. END;
  896. END PublishRegisteredModules;
  897. (* procedure that will be called last in a linked kernel *)
  898. PROCEDURE {FINAL, NOPAF} Main;
  899. BEGIN
  900. (*Machine.Init;*)
  901. Trace.String("publish registered modules"); Trace.Ln;
  902. PublishRegisteredModules;
  903. END Main;
  904. PROCEDURE Init;
  905. VAR
  906. newArr: PROCEDURE (VAR p: ANY; elemTag: ADDRESS; numElems, numDims: SIZE; isRealtime: BOOLEAN);
  907. newSys: PROCEDURE (VAR p: ANY; size: SIZE; isRealtime: BOOLEAN);
  908. newRec: PROCEDURE (VAR p: ANY; tag: ADDRESS; isRealtime: BOOLEAN);
  909. getProcedure: PROCEDURE(CONST m, p : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  910. s: ARRAY 4 OF CHAR;
  911. module: Module; new: BOOLEAN; i: LONGINT;
  912. BEGIN
  913. (* root and initBlock are initialized by the linker *)
  914. shutdown := None;
  915. newArr := Heaps.NewArr;
  916. newSys := Heaps.NewSys;
  917. newRec := Heaps.NewRec;
  918. getProcedure := GetProcedure;
  919. kernelProc[0] := SYSTEM.VAL (ADDRESS, newRec); (* 253 *)
  920. kernelProc[1] := SYSTEM.VAL (ADDRESS, newSys); (* 252 *)
  921. kernelProc[2] := SYSTEM.VAL (ADDRESS, newArr); (* 251 *)
  922. kernelProc[3] := 0; (* 250 *)
  923. kernelProc[4] := 0; (* 249 *)
  924. kernelProc[5] := 0; (* 248 *)
  925. kernelProc[6] := 0; (* 247 *)
  926. kernelProc[7] := 0; (* 246 *)
  927. kernelProc[8] := 0; (* 245 *)
  928. kernelProc[9] := 0; (* 244 *)
  929. kernelProc[10] := SYSTEM.VAL(ADDRESS, getProcedure); (* 243 *)
  930. numLoaders := 0;
  931. freeRoot := NIL;
  932. Machine.GetConfig("TraceModules", s);
  933. trace := (s[0] = "1");
  934. (*
  935. FOR i := 0 TO Runtime.modules-1 DO
  936. module := SYSTEM.VAL(Module,Runtime.kernelModule[i]);
  937. IF TraceBoot THEN
  938. Trace.String("publishing module ");
  939. Trace.String(module.name); Trace.Ln;
  940. END;
  941. Publish(module,new);
  942. ASSERT(new,112233);
  943. END;
  944. *)
  945. (*
  946. module := SYSTEM.VAL(Module,SELF);
  947. Publish(module,new);
  948. *)
  949. END Init;
  950. BEGIN
  951. Init;
  952. END Modules.
  953. (*
  954. 19.03.1998 pjm Started
  955. 06.10.1998 pjm FreeModule
  956. Note:
  957. o GetProcedure race: process A calls ThisModule, the module is published, but before its body has finished executing, process B calls GetProcedure, causing the assert (m.init) to fail. Process B should perhaps wait in this case until the body has executed, or GetProcedure should return NIL (but that will just move the race to the user).
  958. *)