Generic.Modules.Mod 33 KB

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