Generic.Modules.Mod 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. MODULE Modules; (** AUTHOR "pjm"; PURPOSE "Modules and types"; *)
  2. IMPORT SYSTEM, Trace, Machine, Heaps;
  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*: LONGINT;
  34. name* {UNTRACED}: DynamicName;
  35. adr*: ADDRESS;
  36. exports*: LONGINT;
  37. dsc* {UNTRACED}: ExportArray
  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. PROCEDURE FindRoots; (* override *)
  88. VAR i: LONGINT; ptr: ANY; false: BOOLEAN;
  89. BEGIN
  90. false := FALSE; IF false THEN BEGIN{EXCLUSIVE} END END; (* trick to make a module a protected record ... *)
  91. IF published THEN (* mark global pointers *)
  92. FOR i := 0 TO LEN(ptrAdr) - 1 DO
  93. SYSTEM.GET (ptrAdr[i], ptr);
  94. IF ptr # NIL THEN Heaps.Mark(ptr) END
  95. END;
  96. Heaps.AddRootObject(next);
  97. (* all other fields are being traversed by Mark of the Garbage Collector *)
  98. END;
  99. END FindRoots;
  100. END Module;
  101. LoaderProc* = PROCEDURE (CONST name, fileName: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR): Module; (** load an object file *)
  102. VAR
  103. extension-: ARRAY MaxObjFormats, 8 OF CHAR;
  104. loader: ARRAY MaxObjFormats OF LoaderProc;
  105. numLoaders: LONGINT;
  106. freeRoot*: Module; (** list of freed modules (temporary) *)
  107. (* the following two variables are initialized by Linker *)
  108. root-: Module; (** list of modules (read-only) *)
  109. shutdown*: LONGINT; (** None, Reboot, PowerDown *)
  110. trace: BOOLEAN;
  111. register: RECORD
  112. first, last: Module;
  113. END;
  114. (* global sorted table of all procedures , basically for GC *)
  115. procedureDescriptors-: ProcedureDescs;
  116. mayAllocate: BOOLEAN;
  117. (** Register a module loader. *)
  118. PROCEDURE AddLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  119. BEGIN
  120. Machine.Acquire(Machine.Modules);
  121. ASSERT(numLoaders < MaxObjFormats);
  122. loader[numLoaders] := proc;
  123. COPY(ext, extension[numLoaders]);
  124. ASSERT(ext = extension[numLoaders]); (* no overflow *)
  125. INC(numLoaders);
  126. Machine.Release(Machine.Modules)
  127. END AddLoader;
  128. (** Remove a module loader. *)
  129. PROCEDURE RemoveLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  130. VAR i, j: LONGINT;
  131. BEGIN
  132. Machine.Acquire(Machine.Modules);
  133. i := 0;
  134. WHILE (i # numLoaders) & ((loader[i] # proc) OR (extension[i] # ext)) DO INC(i) END;
  135. IF i # numLoaders THEN
  136. FOR j := i TO numLoaders - 2 DO
  137. loader[j] := loader[j + 1]; extension[j] := extension[j + 1];
  138. END;
  139. loader[numLoaders - 1] := NIL; extension[numLoaders - 1] := "";
  140. DEC(numLoaders)
  141. END;
  142. Machine.Release(Machine.Modules)
  143. END RemoveLoader;
  144. (** Append string from to to, truncating on overflow. *)
  145. PROCEDURE Append*(CONST from: ARRAY OF CHAR; VAR to: ARRAY OF CHAR);
  146. VAR i, j, m: LONGINT;
  147. BEGIN
  148. j := 0; WHILE to[j] # 0X DO INC(j) END;
  149. m := LEN(to)-1;
  150. i := 0; WHILE (from[i] # 0X) & (j # m) DO to[j] := from[i]; INC(i); INC(j) END;
  151. to[j] := 0X
  152. END Append;
  153. (** Add a module to the pool of accessible modules, or return named module. *)
  154. PROCEDURE Publish*(VAR m: Module; VAR new: BOOLEAN);
  155. VAR n: Module; i: LONGINT;
  156. BEGIN
  157. (*
  158. ASSERT((m.code # NIL) & (LEN(m.code^) > 0));
  159. *)
  160. Machine.Acquire(Machine.Modules);
  161. n := root; WHILE (n # NIL) & (n.name # m.name) DO n := n.next END;
  162. IF n # NIL THEN (* module with same name exists, return it and ignore new m *)
  163. m := n; new := FALSE
  164. ELSE
  165. IF TraceBoot THEN
  166. Machine.Acquire(Machine.TraceOutput);
  167. Trace.String("publish "); Trace.String(m.name);
  168. (*
  169. a := m;
  170. IF a IS Heaps.RootObject THEN Trace.String(" IS RootObj") END;
  171. IF a IS Module THEN Trace.String(" IS Module"); END;
  172. *)
  173. Trace.Ln;
  174. Machine.Release(Machine.TraceOutput);
  175. END;
  176. m.published := TRUE;
  177. m.next := root; root := m;
  178. m.refcnt := 0;
  179. SortExceptionTable(m.exTable);
  180. SortProcedureDescs(m.procTable);
  181. MergeProcedureDescs(m.procTable);
  182. IF m.module # NIL THEN
  183. FOR i := 0 TO LEN(m.module)-1 DO INC(m.module[i].refcnt) END;
  184. END;
  185. new := TRUE;
  186. END;
  187. Machine.Release(Machine.Modules)
  188. END Publish;
  189. PROCEDURE Initialize*(VAR module: Module);
  190. VAR new: BOOLEAN;
  191. BEGIN
  192. IF (module = NIL) THEN RETURN END;
  193. Publish (module, new);
  194. IF new THEN
  195. IF module.body # NIL THEN
  196. Machine.FlushDCacheRange(ADDRESSOF(module.code[0]), LEN(module.code));
  197. module.body
  198. END;
  199. module.init := TRUE;
  200. END;
  201. END Initialize;
  202. VAR callagain: BOOLEAN;
  203. PROCEDURE Initialize0*(module: Module);
  204. VAR new: BOOLEAN;
  205. BEGIN
  206. (*TRACE(module.name);*)
  207. (* module MUST have been removed from register list and must not have been initialized yet *)
  208. ASSERT(module.next = NIL);
  209. Publish (module, new);
  210. callagain := FALSE;
  211. IF new THEN
  212. IF module.name = "Objects" THEN
  213. callagain := TRUE;
  214. module.init := TRUE;
  215. END;
  216. (*
  217. Trace.Memory(SYSTEM.VAL(ADDRESS, module), 256);
  218. TRACE(module, module.name, module.body);
  219. TRACE(module);
  220. TRACE(ADDRESS OF module.next);
  221. TRACE(ADDRESS OF module.name);
  222. TRACE(ADDRESS OF module.init);
  223. TRACE(ADDRESS OF module.published);
  224. TRACE(ADDRESS OF module.body);
  225. TRACE(ADDRESS OF module.refcnt);
  226. TRACE(ADDRESS OF module.sb);
  227. TRACE(ADDRESS OF module.entry);
  228. TRACE(ADDRESS OF module.command);
  229. TRACE(ADDRESS OF module.ptrAdr);
  230. TRACE(ADDRESS OF module.typeInfo);
  231. TRACE(ADDRESS OF module.module);
  232. TRACE(ADDRESS OF module.procTable);
  233. TRACE(ADDRESS OF module.ptrTable);
  234. TRACE(ADDRESS OF module.data);
  235. TRACE(ADDRESS OF module.code);
  236. TRACE(ADDRESS OF module.staticTypeDescs);
  237. TRACE(ADDRESS OF module.refs);
  238. TRACE(ADDRESS OF module.export);
  239. TRACE(ADDRESS OF module.term);
  240. TRACE(ADDRESS OF module.exTable);
  241. TRACE(ADDRESS OF module.noProcs);
  242. TRACE(ADDRESS OF module.firstProc);
  243. TRACE(ADDRESS OF module.maxPtrs);
  244. TRACE(ADDRESS OF module.crc);
  245. TRACE(ADDRESS OF module.body);
  246. *)
  247. IF module.body # NIL THEN module.body END;
  248. IF callagain THEN
  249. 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 *)
  250. ELSE
  251. module.init := TRUE;
  252. END;
  253. END;
  254. END Initialize0;
  255. (** Return the named module or NIL if it is not loaded yet. *)
  256. PROCEDURE ModuleByName*(CONST name: ARRAY OF CHAR): Module;
  257. VAR m: Module;
  258. BEGIN
  259. Machine.Acquire(Machine.Modules);
  260. m := root; WHILE (m # NIL) & (m.name # name) DO m := m.next END;
  261. Machine.Release(Machine.Modules);
  262. RETURN m
  263. END ModuleByName;
  264. (* Generate a module file name. *)
  265. PROCEDURE GetFileName(CONST name, extension: ARRAY OF CHAR; VAR fileName: ARRAY OF CHAR);
  266. VAR i, j: LONGINT;
  267. BEGIN
  268. i := 0; WHILE name[i] # 0X DO fileName[i] := name[i]; INC(i) END;
  269. j := 0; WHILE extension[j] # 0X DO fileName[i] := extension[j]; INC(i); INC(j) END;
  270. fileName[i] := 0X
  271. END GetFileName;
  272. (* sort procedure descriptors by firstPC in ascending order *)
  273. PROCEDURE SortProcedureDescs(p: ProcedureDescs);
  274. PROCEDURE Less(i,j: LONGINT): BOOLEAN;
  275. BEGIN
  276. RETURN p[i].pcFrom < p[j].pcFrom;
  277. END Less;
  278. PROCEDURE Swap(i,j: LONGINT);
  279. VAR tmp: ProcedureDescPointer;
  280. BEGIN
  281. tmp := p[i];
  282. p[i] := p[j];
  283. p[j] := tmp;
  284. END Swap;
  285. PROCEDURE Quick( lo, hi: LONGINT);
  286. VAR i, j, m: LONGINT;
  287. BEGIN
  288. IF lo < hi THEN
  289. i := lo; j := hi; m := (lo + hi) DIV 2;
  290. REPEAT
  291. WHILE Less( i, m ) DO INC( i ) END;
  292. WHILE Less( m, j ) DO DEC( j ) END;
  293. IF i <= j THEN
  294. IF m = i THEN m := j
  295. ELSIF m = j THEN m := i
  296. END;
  297. Swap( i, j ); INC( i ); DEC( j )
  298. END
  299. UNTIL i > j;
  300. Quick( lo, j); Quick( i, hi)
  301. END;
  302. END Quick;
  303. BEGIN
  304. Quick(0, LEN(p)-1);
  305. END SortProcedureDescs;
  306. (* sort procedure descriptors by firstPC in ascending order *)
  307. PROCEDURE SortExceptionTable(p: ExceptionTable);
  308. PROCEDURE Less(i,j: LONGINT): BOOLEAN;
  309. BEGIN
  310. RETURN p[i].pcFrom < p[j].pcFrom;
  311. END Less;
  312. PROCEDURE Swap(i,j: LONGINT);
  313. VAR tmp: ExceptionTableEntry;
  314. BEGIN
  315. tmp := p[i];
  316. p[i] := p[j];
  317. p[j] := tmp;
  318. END Swap;
  319. PROCEDURE Quick( lo, hi: LONGINT);
  320. VAR i, j, m: LONGINT;
  321. BEGIN
  322. IF lo < hi THEN
  323. i := lo; j := hi; m := (lo + hi) DIV 2;
  324. REPEAT
  325. WHILE Less( i, m ) DO INC( i ) END;
  326. WHILE Less( m, j ) DO DEC( j ) END;
  327. IF i <= j THEN
  328. IF m = i THEN m := j
  329. ELSIF m = j THEN m := i
  330. END;
  331. Swap( i, j ); INC( i ); DEC( j )
  332. END
  333. UNTIL i > j;
  334. Quick( lo, j); Quick( i, hi)
  335. END;
  336. END Quick;
  337. BEGIN
  338. Quick(0, LEN(p)-1);
  339. END SortExceptionTable;
  340. (* sort and merge procedure descriptors with the global procedure desc array, replacing the global procedure array *)
  341. PROCEDURE MergeProcedureDescs*(p: ProcedureDescs);
  342. VAR n: ProcedureDescs;
  343. i,j,k: LONGINT;
  344. BEGIN
  345. IF ~mayAllocate THEN RETURN END;
  346. IF (p = NIL) OR (LEN(p) = 0) THEN RETURN END;
  347. IF procedureDescriptors = NIL THEN
  348. procedureDescriptors := p;
  349. ELSE
  350. NEW(n, LEN(procedureDescriptors) + LEN(p));
  351. k := 0; i := 0; j := 0;
  352. FOR k := 0 TO LEN(n)-1 DO
  353. IF (i<LEN(p)) & ((j=LEN(procedureDescriptors)) OR (p[i].pcFrom < procedureDescriptors[j].pcFrom)) THEN
  354. n[k] := p[i]; INC(i);
  355. ELSE
  356. n[k] := procedureDescriptors[j]; INC(j);
  357. END;
  358. END;
  359. procedureDescriptors := n;
  360. END;
  361. END MergeProcedureDescs;
  362. (* remove sorted procedure descriptors from sorted global array *)
  363. PROCEDURE RemoveProcedureDescs*(p: ProcedureDescs);
  364. VAR i,j,k: LONGINT; n: ProcedureDescs;
  365. BEGIN
  366. IF ~mayAllocate THEN RETURN END;
  367. NEW(n, LEN(procedureDescriptors) - LEN(p));
  368. i := 0; j := 0; k := 0;
  369. WHILE i < LEN(procedureDescriptors) DO
  370. IF (j < LEN(p)) & (procedureDescriptors[i] = p[j]) THEN INC(j);
  371. ELSE n[k] := procedureDescriptors[i]; INC(k);
  372. END;
  373. INC(i);
  374. END;
  375. procedureDescriptors := n;
  376. END RemoveProcedureDescs;
  377. (** Load the module if it is not already loaded. *) (* Algorithm J. Templ, ETHZ, 1994 *)
  378. PROCEDURE ThisModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR): Module;
  379. VAR m, p: Module; fileName: ARRAY 64 OF CHAR; i: LONGINT;
  380. BEGIN
  381. res := Ok; msg[0] := 0X; m := ModuleByName(name);
  382. IF m = NIL THEN
  383. IF trace THEN
  384. Machine.Acquire (Machine.TraceOutput);
  385. Trace.String(">"); Trace.StringLn (name);
  386. Machine.Release (Machine.TraceOutput);
  387. END;
  388. IF numLoaders = 0 THEN
  389. res := NoLoader; m := NIL;
  390. ELSE
  391. i:= 0;
  392. REPEAT
  393. GetFileName(name, extension[i], fileName);
  394. m := loader[i](name, fileName, res, msg);
  395. INC(i);
  396. UNTIL (m # NIL) OR (i=numLoaders);
  397. END;
  398. IF trace THEN
  399. Machine.Acquire (Machine.TraceOutput);
  400. Trace.String("?"); Trace.StringLn (name);
  401. Machine.Release (Machine.TraceOutput);
  402. END;
  403. p := m;
  404. IF (m # NIL) & ~m.published THEN (* no race on m.published, as update is done below in Publish *)
  405. Initialize(m);
  406. END;
  407. IF trace THEN
  408. Machine.Acquire (Machine.TraceOutput);
  409. IF m = NIL THEN
  410. Trace.String("could not load "); Trace.StringLn(name)
  411. ELSIF ~m.published THEN
  412. Trace.String("not published "); Trace.StringLn(name)
  413. ELSE
  414. Trace.String("<"); Trace.StringLn (name);
  415. END;
  416. Machine.Release (Machine.TraceOutput);
  417. END;
  418. END;
  419. RETURN m
  420. END ThisModule;
  421. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. Non-blocking version for reflection *)
  422. PROCEDURE ThisModuleByAdr0*(pc: ADDRESS): Module;
  423. VAR m: Module; found: BOOLEAN; list: LONGINT;
  424. BEGIN
  425. list := 0; found := FALSE;
  426. REPEAT
  427. CASE list OF
  428. 0: m := root
  429. |1: m := freeRoot
  430. END;
  431. WHILE (m # NIL) & ~found DO
  432. found := FindProc(pc, m.procTable) # NIL;
  433. IF ~found THEN m := m.next END;
  434. END;
  435. INC(list)
  436. UNTIL found OR (list=2);
  437. RETURN m
  438. END ThisModuleByAdr0;
  439. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. *)
  440. PROCEDURE ThisModuleByAdr*(pc: ADDRESS): Module;
  441. VAR m: Module;
  442. BEGIN
  443. Machine.Acquire(Machine.Modules);
  444. m := ThisModuleByAdr0(pc);
  445. Machine.Release(Machine.Modules);
  446. RETURN m
  447. END ThisModuleByAdr;
  448. CONST ModuleInitTimeout = HUGEINT(3000000000); (* Timeout for waiting until a module get initialized, 3 seconds for 1 GHz CPU *)
  449. (* Retrieve a procedure given a module name, the procedure name and some type information (kernel call) *)
  450. PROCEDURE GetProcedure*(CONST moduleName, procedureName : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  451. VAR module : Module; ignoreMsg : ARRAY 32 OF CHAR; i, res : LONGINT; t: HUGEINT;
  452. BEGIN
  453. module := ThisModule(moduleName, res, ignoreMsg);
  454. IF (res = Ok) THEN
  455. (*!
  456. module body must have been called (see note at the end of this module);
  457. return NIL if the module does not get initialized within the specified timeout
  458. *)
  459. IF ~module.init THEN
  460. t := Machine.GetTimer();
  461. WHILE ~module.init & (Machine.GetTimer() - t < ModuleInitTimeout) DO END;
  462. IF ~module.init THEN (* timeout has expired *)
  463. RETURN;
  464. END;
  465. END;
  466. Machine.Acquire(Machine.Modules);
  467. i := 0; entryAdr := Heaps.NilVal;
  468. WHILE (entryAdr = Heaps.NilVal) & (i # LEN(module.command^)) DO
  469. IF (module.command[i].name = procedureName) & (module.command[i].argTdAdr = argTdAdr) & (module.command[i].retTdAdr = retTdAdr) THEN
  470. entryAdr := module.command[i].entryAdr;
  471. END;
  472. INC(i)
  473. END;
  474. Machine.Release(Machine.Modules);
  475. END;
  476. END GetProcedure;
  477. (** Return the named type *)
  478. PROCEDURE ThisType*(m: Module; CONST name: ARRAY OF CHAR): TypeDesc;
  479. VAR i: LONGINT; type: TypeDesc;
  480. BEGIN
  481. Machine.Acquire(Machine.Modules);
  482. i := 0;
  483. WHILE (i < LEN(m.typeInfo)) & (m.typeInfo[i].name # name) DO INC(i) END;
  484. IF i = LEN(m.typeInfo) THEN
  485. type := NIL
  486. ELSE
  487. type := m.typeInfo[i]
  488. END;
  489. Machine.Release(Machine.Modules);
  490. RETURN type
  491. END ThisType;
  492. PROCEDURE ThisTypeByAdr*(adr: ADDRESS; VAR m: Module; VAR t: TypeDesc);
  493. BEGIN
  494. IF adr # 0 THEN
  495. Machine.Acquire(Machine.Modules);
  496. SYSTEM.GET (adr + Heaps.TypeDescOffset, adr);
  497. t := SYSTEM.VAL(TypeDesc, adr);
  498. m := t.mod;
  499. Machine.Release(Machine.Modules)
  500. ELSE
  501. m := NIL; t := NIL
  502. END
  503. END ThisTypeByAdr;
  504. (** create a new object given its type descriptor *)
  505. PROCEDURE NewObj*(t : TypeDesc; isRealtime: BOOLEAN) : ANY;
  506. VAR x : ANY;
  507. BEGIN
  508. Heaps.NewRec(x, SYSTEM.VAL (ADDRESS, t.tag), isRealtime);
  509. RETURN x;
  510. END NewObj;
  511. (** return the type descriptor of an object *)
  512. PROCEDURE TypeOf*(obj : ANY): TypeDesc;
  513. VAR
  514. m : Module;
  515. t : TypeDesc;
  516. adr : ADDRESS;
  517. BEGIN
  518. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.TypeDescOffset, adr);
  519. ThisTypeByAdr(adr, m, t);
  520. RETURN t;
  521. END TypeOf;
  522. (** searches for the given pc in the global ProcKeyTable, if found it returns the corresponding data element *)
  523. PROCEDURE FindProc*(pc: ADDRESS; p: ProcedureDescs): ProcedureDescPointer;
  524. VAR l,r,x: LONGINT; isHit: BOOLEAN;
  525. BEGIN
  526. IF p # NIL THEN
  527. l := 0; r := LEN(p)-1;
  528. REPEAT
  529. x := (l + r) DIV 2;
  530. IF pc < p[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  531. isHit := ((p[x].pcFrom <= pc) & (pc < p[x].pcLimit));
  532. UNTIL isHit OR (l > r);
  533. IF isHit THEN
  534. RETURN p[x];
  535. END;
  536. END;
  537. RETURN NIL;
  538. END FindProc;
  539. (** 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. *)
  540. PROCEDURE InstallTermHandler*(h: TerminationHandler);
  541. VAR m: Module;
  542. BEGIN
  543. m := ThisModuleByAdr(SYSTEM.VAL (ADDRESS, h));
  544. IF m # NIL THEN
  545. m.term := h (* overwrite existing handler, if any *)
  546. END
  547. END InstallTermHandler;
  548. (** 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. *)
  549. PROCEDURE FreeModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR);
  550. VAR p, m: Module; term: TerminationHandler; i: LONGINT;
  551. BEGIN
  552. m := ModuleByName(name);
  553. IF (m # NIL) & (m.refcnt = 0) THEN (* will be freed below *)
  554. IF m.term # NIL THEN (* call termination handler *)
  555. term := m.term; m.term := NIL; term (* may trap *)
  556. END;
  557. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  558. END;
  559. res := Ok; msg[0] := 0X;
  560. Machine.Acquire(Machine.Modules);
  561. Trace.String("Acquired Machine.Modules x"); Trace.Ln;
  562. p := NIL; m := root;
  563. WHILE (m # NIL) & (m.name # name) DO p := m; m := m.next END;
  564. Trace.String("Acquired Machine.Modules y"); Trace.Ln;
  565. IF m # NIL THEN
  566. Trace.String("found module"); Trace.Ln;
  567. IF m.refcnt = 0 THEN (* free the module *)
  568. FOR i := 0 TO LEN(m.module)-1 DO DEC(m.module[i].refcnt) END;
  569. m.init := FALSE; (* disallow ThisCommand *)
  570. Append("?", m.name);
  571. (* move module to free list *)
  572. IF p = NIL THEN root := root.next ELSE p.next := m.next END;
  573. m.next := freeRoot; freeRoot := m;
  574. (* clear global pointers and code *)
  575. IF m.ptrAdr # NIL THEN
  576. Trace.String("ptradr del"); Trace.Ln;
  577. FOR i := 0 TO LEN(m.ptrAdr)-1 DO SYSTEM.PUT (m.ptrAdr[i], NIL) END;
  578. END;
  579. IF ClearCode & (m.code # NIL) THEN
  580. Trace.String("clear code"); Trace.Ln;
  581. FOR i := 0 TO LEN(m.code)-1 DO m.code[i] := 0CCX END
  582. END;
  583. Trace.String("clear code f"); Trace.Ln;
  584. (* remove references to module data *)
  585. m.published := FALSE;
  586. m.entry := NIL; m.command := NIL; m.ptrAdr := NIL;
  587. (* do not clear m.type or m.module, as old heap block tags might reference type descs indirectly. *) (* m.staticTypeDescs, m.typeInfo ??? *)
  588. (* do not clear m.data or m.code, as they are used in ThisModuleByAdr (for debugging). *)
  589. (* do not clear m.refs, as they are used in Traps (for debugging). *)
  590. m.export.dsc := NIL; m.exTable := NIL;
  591. (*Trace.String("delete proc offsets"); Trace.Ln;
  592. DeleteProcOffsets(m.firstProc, m.noProcs);
  593. *)
  594. RemoveProcedureDescs(m.procTable);
  595. ELSE
  596. res := 1901; (* can not free module in use *)
  597. COPY(name, msg); Append(" reference count not zero", msg)
  598. END
  599. ELSE
  600. res := 1902; (* module not found *)
  601. COPY(name, msg); Append(" not found", msg)
  602. END;
  603. Machine.Release(Machine.Modules)
  604. END FreeModule;
  605. (** Shut down all modules by calling their termination handlers and then call Machine.Shutdown. *)
  606. PROCEDURE Shutdown*(code: LONGINT);
  607. VAR m: Module; term: TerminationHandler;
  608. BEGIN
  609. IF code # None THEN
  610. LOOP
  611. Machine.Acquire(Machine.Modules);
  612. m := root; WHILE (m # NIL) & (m.term = NIL) DO m := m.next END;
  613. IF m # NIL THEN term := m.term; m.term := NIL END;
  614. Machine.Release(Machine.Modules);
  615. IF m = NIL THEN EXIT END;
  616. IF trace THEN
  617. Machine.Acquire (Machine.TraceOutput);
  618. Trace.String("TermHandler "); Trace.StringLn (m.name);
  619. Machine.Release (Machine.TraceOutput);
  620. END;
  621. term (* if this causes exception or hangs, another shutdown call will retry *)
  622. END;
  623. (* clean up finalizers *)
  624. m := root;
  625. WHILE m # NIL DO
  626. IF LEN(m.code)>0 THEN
  627. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  628. END;
  629. m := m.next
  630. END;
  631. IF trace THEN
  632. Machine.Acquire (Machine.TraceOutput);
  633. Trace.StringLn ("Modules.Shutdown finished");
  634. Machine.Release (Machine.TraceOutput);
  635. END;
  636. Machine.Shutdown(code = Reboot) (* does not return *)
  637. END
  638. END Shutdown;
  639. (* Is this PC handled in the corresponding module. deep = scan the whole stack. *)
  640. PROCEDURE IsExceptionHandled*(VAR pc, fp: ADDRESS; deep: BOOLEAN): BOOLEAN;
  641. VAR
  642. handler: ADDRESS;
  643. BEGIN
  644. IF deep THEN
  645. handler := GetExceptionHandler(pc);
  646. IF handler # -1 THEN (* Handler in the current PAF *)
  647. RETURN TRUE
  648. ELSE
  649. WHILE (fp # 0) & (handler = -1) DO
  650. SYSTEM.GET (fp + 4, pc);
  651. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  652. handler := GetExceptionHandler(pc);
  653. SYSTEM.GET (fp, fp) (* Unwind PAF *)
  654. END;
  655. IF handler = -1 THEN RETURN FALSE ELSE pc := handler; RETURN TRUE END
  656. END
  657. ELSE
  658. RETURN GetExceptionHandler(pc) # -1
  659. END
  660. END IsExceptionHandled;
  661. (* Is this PC handled in the corresponding module. If the PC is handled the PC of the
  662. handler is return else -1 is return. There is no problem concurrently accessing this
  663. procedure, there is only reading work. *)
  664. PROCEDURE GetExceptionHandler*(pc: ADDRESS): ADDRESS;
  665. VAR
  666. m: Module;
  667. PROCEDURE BinSearch(exTable: ExceptionTable; key: ADDRESS): ADDRESS;
  668. VAR
  669. x, l, r: LONGINT;
  670. BEGIN
  671. l := 0; r:=LEN(exTable) - 1;
  672. REPEAT
  673. x := (l + r) DIV 2;
  674. IF key < exTable[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  675. UNTIL ((key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) ) OR (l > r);
  676. IF (key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) THEN
  677. RETURN exTable[x].pcHandler;
  678. ELSE
  679. RETURN -1;
  680. END
  681. END BinSearch;
  682. BEGIN
  683. m := ThisModuleByAdr(pc);
  684. IF (m # NIL) & (m.exTable # NIL) & (LEN(m.exTable) > 0) THEN
  685. RETURN BinSearch(m.exTable, pc);
  686. END;
  687. RETURN -1;
  688. END GetExceptionHandler;
  689. (** fof: to make custom solutions to the race process, described below, possible. This is not a solution to the generic problem !! *)
  690. PROCEDURE Initialized*(m: Module): BOOLEAN;
  691. BEGIN
  692. RETURN m.init;
  693. END Initialized;
  694. PROCEDURE Register- (module {UNTRACED}: Module);
  695. BEGIN {UNCOOPERATIVE, UNCHECKED}
  696. (*TRACE(module.name);*)
  697. IF register.first = NIL THEN
  698. register.first := module;
  699. ELSE
  700. register.last.next := module;
  701. END;
  702. register.last := module;
  703. END Register;
  704. PROCEDURE PublishRegisteredModules;
  705. VAR m {UNTRACED}, prev {UNTRACED}, cur {UNTRACED}: Module; import: SIZE;
  706. BEGIN
  707. WHILE register.first # NIL DO
  708. m := register.first;
  709. register.first := m.next;
  710. m.next := NIL;
  711. IF m.module # NIL THEN
  712. FOR import := 0 TO LEN (m.module) - 1 DO
  713. IF ~m.module[import].published THEN
  714. ASSERT(register.first # NIL);
  715. prev := NIL;
  716. cur := register.first;
  717. WHILE (cur # NIL) & (cur # m.module[import]) DO
  718. prev := cur;
  719. cur := cur.next
  720. END;
  721. (*ASSERT(cur = m.module[import]);*)
  722. ASSERT(cur = m.module[import]);
  723. IF prev = NIL THEN
  724. register.first := cur.next
  725. ELSE
  726. prev.next := cur.next;
  727. END;
  728. cur.next := NIL;
  729. Initialize0 (m.module[import]);
  730. END
  731. END;
  732. END;
  733. Initialize0 (m);
  734. END;
  735. END PublishRegisteredModules;
  736. (* procedure that will be called last in a linked kernel *)
  737. PROCEDURE {FINAL, NOPAF} Main-;
  738. BEGIN
  739. (*Machine.Init;*)
  740. Trace.String("publish registered modules"); Trace.Ln;
  741. PublishRegisteredModules;
  742. END Main;
  743. PROCEDURE Init;
  744. VAR
  745. s: ARRAY 4 OF CHAR;
  746. module: Module;
  747. BEGIN
  748. (* root is initialized by the linker *)
  749. shutdown := None;
  750. numLoaders := 0;
  751. freeRoot := NIL;
  752. Machine.GetConfig("TraceModules", s);
  753. trace := (s[0] = "1");
  754. module := root;
  755. WHILE (module # NIL) DO
  756. MergeProcedureDescs(module.procTable);
  757. module := module.next;
  758. END;
  759. mayAllocate := TRUE;
  760. (*
  761. FOR i := 0 TO Runtime.modules-1 DO
  762. module := SYSTEM.VAL(Module,Runtime.kernelModule[i]);
  763. IF TraceBoot THEN
  764. Trace.String("publishing module ");
  765. Trace.String(module.name); Trace.Ln;
  766. END;
  767. Publish(module,new);
  768. ASSERT(new,112233);
  769. END;
  770. *)
  771. (*
  772. module := SYSTEM.VAL(Module,SELF);
  773. Publish(module,new);
  774. *)
  775. END Init;
  776. BEGIN
  777. Init;
  778. END Modules.
  779. (*
  780. 19.03.1998 pjm Started
  781. 06.10.1998 pjm FreeModule
  782. Note:
  783. 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).
  784. *)