Generic.Modules.Mod 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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=FALSE;
  19. TYPE
  20. (* definitions for object-model loader support *)
  21. Name* = ARRAY 32 OF CHAR;
  22. (* the correponding name array is protected from being GCed via module's internal pointer arrray
  23. compiler generated!
  24. *)
  25. DynamicName* = POINTER {UNSAFE} TO ARRAY 256 OF CHAR;
  26. Command* = RECORD
  27. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  28. name*: Name; (* name of the procedure *)
  29. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  30. entryAdr* : ADDRESS; (* entry address of procedure *)
  31. END;
  32. ExportDesc* = RECORD
  33. fp*: ADDRESS;
  34. name* {UNTRACED}: DynamicName;
  35. adr*: ADDRESS;
  36. exports*: LONGINT; (* number of exports referenced by ExportArray *)
  37. dsc* {UNTRACED}: ExportArray; (* pointer to memory containing a raw array with "exports" entries *)
  38. END;
  39. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  40. Bytes* = POINTER TO ARRAY OF CHAR;
  41. TerminationHandler* = PROCEDURE;
  42. (* all implicit or explicit pointers in the subsequent data structures are protected with one pointer array
  43. *)
  44. TypeDesc* = POINTER TO RECORD
  45. descSize-: LONGINT;
  46. sentinel-: LONGINT; (* = MPO-4 *)
  47. tag-: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  48. flags-: SET;
  49. mod- {UNTRACED}: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  50. name-: Name;
  51. refsOffset-: SIZE;
  52. END;
  53. ExceptionTableEntry* = RECORD
  54. pcFrom*: ADDRESS;
  55. pcTo*: ADDRESS;
  56. pcHandler*: ADDRESS;
  57. END;
  58. ExceptionTable* = POINTER TO ARRAY OF ExceptionTableEntry;
  59. ProcedureDescPointer* = POINTER TO ProcedureDesc;
  60. ProcedureDesc*= RECORD
  61. pcFrom-, pcLimit-: ADDRESS;
  62. offsets- {UNTRACED}: POINTER TO ARRAY OF ADDRESS;
  63. END;
  64. ProcedureDescs* = POINTER TO ARRAY OF ProcedureDescPointer;
  65. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  66. VAR
  67. next*: Module; (** once a module is published, all fields are read-only *)
  68. name*: Name;
  69. init, published: BOOLEAN;
  70. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  71. sb*: ADDRESS; (* reference address between constants and local variables *)
  72. entry*: POINTER TO ARRAY OF ADDRESS;
  73. command*: POINTER TO ARRAY OF Command;
  74. ptrAdr*: POINTER TO ARRAY OF ADDRESS; (* traced explicitly in FindRoots *)
  75. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  76. module*: POINTER TO ARRAY OF Module; (* imported modules: for reference counting *)
  77. procTable*: ProcedureDescs; (* information inserted by loader, sorted by pc after loading *)
  78. data*, code*, staticTypeDescs* (* ug *), refs*: Bytes;
  79. export*: ExportDesc;
  80. term*: TerminationHandler;
  81. exTable*: ExceptionTable;
  82. (* internal pointer array: to protect internal data structures from being GCed *)
  83. internal-: POINTER TO ARRAY OF ANY;
  84. crc*: LONGINT;
  85. body*: PROCEDURE;
  86. PROCEDURE FindRoots; (* override *)
  87. VAR i: LONGINT; ptr: ANY; false: BOOLEAN;
  88. BEGIN
  89. false := FALSE; IF false THEN BEGIN{EXCLUSIVE} END END; (* trick to make a module a protected record ... *)
  90. IF published THEN (* mark global pointers *)
  91. FOR i := 0 TO LEN(ptrAdr) - 1 DO
  92. SYSTEM.GET (ptrAdr[i], ptr);
  93. IF ptr # NIL THEN Heaps.Mark(ptr) END
  94. END;
  95. Heaps.AddRootObject(next);
  96. (* all other fields are being traversed by Mark of the Garbage Collector *)
  97. END;
  98. END FindRoots;
  99. END Module;
  100. LoaderProc* = PROCEDURE (CONST name, fileName: ARRAY OF CHAR; VAR res: LONGINT;
  101. 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. root-: Module; (** list of modules (read-only) *)
  108. shutdown*: LONGINT; (** None, Reboot, PowerDown *)
  109. trace: BOOLEAN;
  110. register: RECORD
  111. first, last: Module;
  112. END;
  113. (* global sorted table of all procedures , basically for GC *)
  114. procedureDescriptors-: ProcedureDescs;
  115. (** Register a module loader. *)
  116. PROCEDURE AddLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  117. BEGIN
  118. Machine.Acquire(Machine.Modules);
  119. ASSERT(numLoaders < MaxObjFormats);
  120. loader[numLoaders] := proc;
  121. COPY(ext, extension[numLoaders]);
  122. ASSERT(ext = extension[numLoaders]); (* no overflow *)
  123. INC(numLoaders);
  124. Machine.Release(Machine.Modules)
  125. END AddLoader;
  126. (** Remove a module loader. *)
  127. PROCEDURE RemoveLoader*(CONST ext: ARRAY OF CHAR; proc: LoaderProc);
  128. VAR i, j: LONGINT;
  129. BEGIN
  130. Machine.Acquire(Machine.Modules);
  131. i := 0;
  132. WHILE (i # numLoaders) & ((loader[i] # proc) OR (extension[i] # ext)) DO INC(i) END;
  133. IF i # numLoaders THEN
  134. FOR j := i TO numLoaders - 2 DO
  135. loader[j] := loader[j + 1]; extension[j] := extension[j + 1];
  136. END;
  137. loader[numLoaders - 1] := NIL; extension[numLoaders - 1] := "";
  138. DEC(numLoaders)
  139. END;
  140. Machine.Release(Machine.Modules)
  141. END RemoveLoader;
  142. (** Append string from to to, truncating on overflow. *)
  143. PROCEDURE Append*(CONST from: ARRAY OF CHAR; VAR to: ARRAY OF CHAR);
  144. VAR i, j, m: LONGINT;
  145. BEGIN
  146. j := 0; WHILE to[j] # 0X DO INC(j) END;
  147. m := LEN(to)-1;
  148. i := 0; WHILE (from[i] # 0X) & (j # m) DO to[j] := from[i]; INC(i); INC(j) END;
  149. to[j] := 0X
  150. END Append;
  151. (** Add a module to the pool of accessible modules, or return named module. *)
  152. PROCEDURE Publish*(VAR m: Module; VAR new: BOOLEAN);
  153. VAR n: Module; i: LONGINT;
  154. BEGIN
  155. Machine.Acquire(Machine.Modules);
  156. n := root; WHILE (n # NIL) & (n.name # m.name) DO n := n.next END;
  157. IF n # NIL THEN (* module with same name exists, return it and ignore new m *)
  158. m := n; new := FALSE
  159. ELSE
  160. IF TraceBoot OR trace THEN
  161. Machine.Acquire(Machine.TraceOutput);
  162. Trace.String("publish "); Trace.String(m.name);
  163. Trace.Ln;
  164. Machine.Release(Machine.TraceOutput);
  165. END;
  166. Unregister(m);
  167. m.published := TRUE;
  168. m.next := root; root := m;
  169. m.refcnt := 0;
  170. SortExceptionTable(m.exTable);
  171. SortProcedureDescs(m.procTable);
  172. IF m.module # NIL THEN
  173. FOR i := 0 TO LEN(m.module)-1 DO INC(m.module[i].refcnt) END;
  174. END;
  175. new := TRUE;
  176. END;
  177. Machine.Release(Machine.Modules)
  178. END Publish;
  179. PROCEDURE Initialize*(VAR module: Module);
  180. VAR new: BOOLEAN; import: LONGINT;
  181. BEGIN
  182. IF (module = NIL) THEN RETURN END;
  183. Publish (module, new);
  184. IF new THEN
  185. FOR import := 0 TO LEN (module.module) - 1 DO
  186. IF ~module.module[import].published THEN
  187. ASSERT(register.first # NIL);
  188. Initialize(module.module[import]);
  189. END
  190. END;
  191. IF (module.code # NIL) & (LEN(module.code) > 0) THEN
  192. Machine.FlushDCacheRange(ADDRESSOF(module.code[0]), LEN(module.code));
  193. END;
  194. IF module.body # NIL THEN
  195. module.body
  196. END;
  197. module.init := TRUE;
  198. END;
  199. END Initialize;
  200. VAR callagain: BOOLEAN;
  201. PROCEDURE Initialize0*(module: Module);
  202. VAR new: BOOLEAN;
  203. BEGIN
  204. (*TRACE(module.name);*)
  205. (* module MUST have been removed from register list and must not have been initialized yet *)
  206. (* ASSERT(module.next = NIL); *)
  207. Publish (module, new);
  208. callagain := FALSE;
  209. IF new THEN
  210. IF module.name = "Objects" THEN
  211. callagain := TRUE;
  212. module.init := TRUE;
  213. END;
  214. (*
  215. Trace.Memory(SYSTEM.VAL(ADDRESS, module), 256);
  216. TRACE(module, module.name, module.body);
  217. TRACE(module);
  218. TRACE(ADDRESS OF module.next);
  219. TRACE(ADDRESS OF module.name);
  220. TRACE(ADDRESS OF module.init);
  221. TRACE(ADDRESS OF module.published);
  222. TRACE(ADDRESS OF module.body);
  223. TRACE(ADDRESS OF module.refcnt);
  224. TRACE(ADDRESS OF module.sb);
  225. TRACE(ADDRESS OF module.entry);
  226. TRACE(ADDRESS OF module.command);
  227. TRACE(ADDRESS OF module.ptrAdr);
  228. TRACE(ADDRESS OF module.typeInfo);
  229. TRACE(ADDRESS OF module.module);
  230. TRACE(ADDRESS OF module.procTable);
  231. TRACE(ADDRESS OF module.ptrTable);
  232. TRACE(ADDRESS OF module.data);
  233. TRACE(ADDRESS OF module.code);
  234. TRACE(ADDRESS OF module.staticTypeDescs);
  235. TRACE(ADDRESS OF module.refs);
  236. TRACE(ADDRESS OF module.export);
  237. TRACE(ADDRESS OF module.term);
  238. TRACE(ADDRESS OF module.exTable);
  239. TRACE(ADDRESS OF module.noProcs);
  240. TRACE(ADDRESS OF module.firstProc);
  241. TRACE(ADDRESS OF module.maxPtrs);
  242. TRACE(ADDRESS OF module.crc);
  243. TRACE(ADDRESS OF module.body);
  244. *)
  245. IF module.body # NIL THEN module.body END;
  246. IF callagain THEN
  247. 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 *)
  248. ELSE
  249. module.init := TRUE;
  250. END;
  251. END;
  252. END Initialize0;
  253. (** Return the named module or NIL if it is not loaded yet. *)
  254. PROCEDURE ModuleByName*(CONST name: ARRAY OF CHAR): Module;
  255. VAR m: Module;
  256. BEGIN
  257. Machine.Acquire(Machine.Modules);
  258. m := root; WHILE (m # NIL) & (m.name # name) DO m := m.next END;
  259. Machine.Release(Machine.Modules);
  260. RETURN m
  261. END ModuleByName;
  262. PROCEDURE ByName(CONST name: ARRAY OF CHAR; VAR referenced: BOOLEAN): Module;
  263. VAR m: Module;
  264. BEGIN
  265. Machine.Acquire(Machine.Modules);
  266. referenced := FALSE;
  267. m := root; WHILE (m # NIL) & (m.name # name) DO m := m.next END;
  268. IF m = NIL THEN
  269. referenced := TRUE;
  270. m := register.first; WHILE (m#NIL) & (m.name # name) DO m := m.next END;
  271. END;
  272. Machine.Release(Machine.Modules);
  273. RETURN m
  274. END ByName;
  275. (* Generate a module file name. *)
  276. PROCEDURE GetFileName(CONST name, extension: ARRAY OF CHAR; VAR fileName: ARRAY OF CHAR);
  277. VAR i, j: LONGINT;
  278. BEGIN
  279. i := 0; WHILE name[i] # 0X DO fileName[i] := name[i]; INC(i) END;
  280. j := 0; WHILE extension[j] # 0X DO fileName[i] := extension[j]; INC(i); INC(j) END;
  281. fileName[i] := 0X
  282. END GetFileName;
  283. (* sort procedure descriptors by firstPC in ascending order *)
  284. PROCEDURE SortProcedureDescs(p: ProcedureDescs);
  285. PROCEDURE Less(i,j: LONGINT): BOOLEAN;
  286. BEGIN
  287. RETURN p[i].pcFrom < p[j].pcFrom;
  288. END Less;
  289. PROCEDURE Swap(i,j: LONGINT);
  290. VAR tmp: ProcedureDescPointer;
  291. BEGIN
  292. tmp := p[i];
  293. p[i] := p[j];
  294. p[j] := tmp;
  295. END Swap;
  296. PROCEDURE Quick( lo, hi: LONGINT);
  297. VAR i, j, m: LONGINT;
  298. BEGIN
  299. IF lo < hi THEN
  300. i := lo; j := hi; m := (lo + hi) DIV 2;
  301. REPEAT
  302. WHILE Less( i, m ) DO INC( i ) END;
  303. WHILE Less( m, j ) DO DEC( j ) END;
  304. IF i <= j THEN
  305. IF m = i THEN m := j
  306. ELSIF m = j THEN m := i
  307. END;
  308. Swap( i, j ); INC( i ); DEC( j )
  309. END
  310. UNTIL i > j;
  311. Quick( lo, j); Quick( i, hi)
  312. END;
  313. END Quick;
  314. BEGIN
  315. Quick(0, LEN(p)-1);
  316. END SortProcedureDescs;
  317. (* sort procedure descriptors by firstPC in ascending order *)
  318. PROCEDURE SortExceptionTable(p: ExceptionTable);
  319. PROCEDURE Less(i,j: LONGINT): BOOLEAN;
  320. BEGIN
  321. RETURN p[i].pcFrom < p[j].pcFrom;
  322. END Less;
  323. PROCEDURE Swap(i,j: LONGINT);
  324. VAR tmp: ExceptionTableEntry;
  325. BEGIN
  326. tmp := p[i];
  327. p[i] := p[j];
  328. p[j] := tmp;
  329. END Swap;
  330. PROCEDURE Quick( lo, hi: LONGINT);
  331. VAR i, j, m: LONGINT;
  332. BEGIN
  333. IF lo < hi THEN
  334. i := lo; j := hi; m := (lo + hi) DIV 2;
  335. REPEAT
  336. WHILE Less( i, m ) DO INC( i ) END;
  337. WHILE Less( m, j ) DO DEC( j ) END;
  338. IF i <= j THEN
  339. IF m = i THEN m := j
  340. ELSIF m = j THEN m := i
  341. END;
  342. Swap( i, j ); INC( i ); DEC( j )
  343. END
  344. UNTIL i > j;
  345. Quick( lo, j); Quick( i, hi)
  346. END;
  347. END Quick;
  348. BEGIN
  349. Quick(0, LEN(p)-1);
  350. END SortExceptionTable;
  351. (** Load the module if it is not already loaded. *) (* Algorithm J. Templ, ETHZ, 1994 *)
  352. PROCEDURE ThisModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR): Module;
  353. VAR m: Module; fileName: ARRAY 64 OF CHAR; i: LONGINT; registered: BOOLEAN;
  354. BEGIN
  355. res := Ok; msg[0] := 0X; m := ByName(name, registered);
  356. IF (m#NIL) & (registered) THEN
  357. IF trace THEN
  358. Machine.Acquire (Machine.TraceOutput);
  359. Trace.String(">R>"); Trace.StringLn (name);
  360. Machine.Release (Machine.TraceOutput);
  361. END;
  362. IF ~m.published THEN (* no race on m.published, as update is done in Publish *)
  363. Initialize(m);
  364. END;
  365. IF trace THEN
  366. Machine.Acquire (Machine.TraceOutput);
  367. Trace.String("<R<"); Trace.StringLn (name);
  368. Machine.Release (Machine.TraceOutput);
  369. END;
  370. ELSIF m = NIL THEN
  371. IF trace THEN
  372. Machine.Acquire (Machine.TraceOutput);
  373. Trace.String(">L>"); Trace.StringLn (name);
  374. Machine.Release (Machine.TraceOutput);
  375. END;
  376. IF numLoaders = 0 THEN
  377. res := NoLoader; m := NIL;
  378. ELSE
  379. i:= 0;
  380. REPEAT
  381. GetFileName(name, extension[i], fileName);
  382. m := loader[i](name, fileName, res, msg);
  383. INC(i);
  384. UNTIL (m # NIL) OR (i=numLoaders);
  385. END;
  386. IF trace THEN
  387. Machine.Acquire (Machine.TraceOutput);
  388. Trace.String("?"); Trace.StringLn (name);
  389. Machine.Release (Machine.TraceOutput);
  390. END;
  391. IF (m # NIL) & ~m.published THEN (* no race on m.published, as update is done below in Publish *)
  392. Initialize(m);
  393. END;
  394. IF trace THEN
  395. Machine.Acquire (Machine.TraceOutput);
  396. IF m = NIL THEN
  397. Trace.String("could not load "); Trace.StringLn(name)
  398. ELSIF ~m.published THEN
  399. Trace.String("not published "); Trace.StringLn(name)
  400. ELSE
  401. Trace.String("<L<"); Trace.StringLn (name);
  402. END;
  403. Machine.Release (Machine.TraceOutput);
  404. END;
  405. END;
  406. RETURN m
  407. END ThisModule;
  408. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. Non-blocking version for reflection *)
  409. PROCEDURE ThisModuleByAdr0*(pc: ADDRESS): Module;
  410. VAR m: Module; found: BOOLEAN; list: LONGINT;
  411. BEGIN
  412. list := 0; found := FALSE;
  413. REPEAT
  414. CASE list OF
  415. 0: m := root
  416. |1: m := freeRoot
  417. END;
  418. WHILE (m # NIL) & ~found DO
  419. found := FindProc(pc, m.procTable) # NIL;
  420. IF ~found THEN m := m.next END;
  421. END;
  422. INC(list)
  423. UNTIL found OR (list=2);
  424. RETURN m
  425. END ThisModuleByAdr0;
  426. (** Return the module that contains code address pc or NIL if not found. Can also return freed modules. *)
  427. PROCEDURE ThisModuleByAdr*(pc: ADDRESS): Module;
  428. VAR m: Module;
  429. BEGIN
  430. Machine.Acquire(Machine.Modules);
  431. m := ThisModuleByAdr0(pc);
  432. Machine.Release(Machine.Modules);
  433. RETURN m
  434. END ThisModuleByAdr;
  435. CONST ModuleInitTimeout = HUGEINT(3000000000); (* Timeout for waiting until a module get initialized, 3 seconds for 1 GHz CPU *)
  436. (* Retrieve a procedure given a module name, the procedure name and some type information (kernel call) *)
  437. PROCEDURE GetProcedure*(CONST moduleName, procedureName : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  438. VAR module : Module; ignoreMsg : ARRAY 32 OF CHAR; i, res : LONGINT; t: HUGEINT;
  439. BEGIN
  440. module := ThisModule(moduleName, res, ignoreMsg);
  441. IF (res = Ok) THEN
  442. (*!
  443. module body must have been called (see note at the end of this module);
  444. return NIL if the module does not get initialized within the specified timeout
  445. *)
  446. IF ~module.init THEN
  447. t := Machine.GetTimer();
  448. WHILE ~module.init & (Machine.GetTimer() - t < ModuleInitTimeout) DO END;
  449. IF ~module.init THEN (* timeout has expired *)
  450. RETURN;
  451. END;
  452. END;
  453. Machine.Acquire(Machine.Modules);
  454. i := 0; entryAdr := Heaps.NilVal;
  455. WHILE (entryAdr = Heaps.NilVal) & (i # LEN(module.command^)) DO
  456. IF (module.command[i].name = procedureName) & (module.command[i].argTdAdr = argTdAdr) & (module.command[i].retTdAdr = retTdAdr) THEN
  457. entryAdr := module.command[i].entryAdr;
  458. END;
  459. INC(i)
  460. END;
  461. Machine.Release(Machine.Modules);
  462. END;
  463. END GetProcedure;
  464. (** Return the named type *)
  465. PROCEDURE ThisType*(m: Module; CONST name: ARRAY OF CHAR): TypeDesc;
  466. VAR i: LONGINT; type: TypeDesc;
  467. BEGIN
  468. Machine.Acquire(Machine.Modules);
  469. i := 0;
  470. WHILE (i < LEN(m.typeInfo)) & (m.typeInfo[i].name # name) DO INC(i) END;
  471. IF i = LEN(m.typeInfo) THEN
  472. type := NIL
  473. ELSE
  474. type := m.typeInfo[i]
  475. END;
  476. Machine.Release(Machine.Modules);
  477. RETURN type
  478. END ThisType;
  479. PROCEDURE ThisTypeByAdr*(adr: ADDRESS; VAR m: Module; VAR t: TypeDesc);
  480. BEGIN
  481. IF adr # 0 THEN
  482. Machine.Acquire(Machine.Modules);
  483. SYSTEM.GET (adr + Heaps.TypeDescOffset, adr);
  484. t := SYSTEM.VAL(TypeDesc, adr);
  485. m := t.mod;
  486. Machine.Release(Machine.Modules)
  487. ELSE
  488. m := NIL; t := NIL
  489. END
  490. END ThisTypeByAdr;
  491. (** create a new object given its type descriptor *)
  492. PROCEDURE NewObj*(t : TypeDesc; isRealtime: BOOLEAN) : ANY;
  493. VAR x : ANY;
  494. BEGIN
  495. Heaps.NewRec(x, SYSTEM.VAL (ADDRESS, t.tag), isRealtime);
  496. RETURN x;
  497. END NewObj;
  498. (** return the type descriptor of an object *)
  499. PROCEDURE TypeOf*(obj : ANY): TypeDesc;
  500. VAR
  501. m : Module;
  502. t : TypeDesc;
  503. adr : ADDRESS;
  504. BEGIN
  505. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.TypeDescOffset, adr);
  506. ThisTypeByAdr(adr, m, t);
  507. RETURN t;
  508. END TypeOf;
  509. (** searches for the given pc in the global ProcKeyTable, if found it returns the corresponding data element *)
  510. PROCEDURE FindProc*(pc: ADDRESS; p: ProcedureDescs): ProcedureDescPointer;
  511. VAR l,r,x: LONGINT; isHit: BOOLEAN;
  512. BEGIN
  513. IF p # NIL THEN
  514. l := 0; r := LEN(p)-1;
  515. REPEAT
  516. x := (l + r) DIV 2;
  517. IF pc < p[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  518. isHit := ((p[x].pcFrom <= pc) & (pc < p[x].pcLimit));
  519. UNTIL isHit OR (l > r);
  520. IF isHit THEN
  521. RETURN p[x];
  522. END;
  523. END;
  524. RETURN NIL;
  525. END FindProc;
  526. (** 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. *)
  527. PROCEDURE InstallTermHandler*(h: TerminationHandler);
  528. VAR m: Module;
  529. BEGIN
  530. m := ThisModuleByAdr(SYSTEM.VAL (ADDRESS, h));
  531. IF m # NIL THEN
  532. m.term := h (* overwrite existing handler, if any *)
  533. END
  534. END InstallTermHandler;
  535. (** 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. *)
  536. PROCEDURE FreeModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR);
  537. VAR p, m: Module; term: TerminationHandler; i: LONGINT;
  538. BEGIN
  539. m := ModuleByName(name);
  540. IF (m # NIL) & (m.refcnt = 0) THEN (* will be freed below *)
  541. IF m.term # NIL THEN (* call termination handler *)
  542. term := m.term; m.term := NIL; term (* may trap *)
  543. END;
  544. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  545. END;
  546. res := Ok; msg[0] := 0X;
  547. Machine.Acquire(Machine.Modules);
  548. p := NIL; m := root;
  549. WHILE (m # NIL) & (m.name # name) DO p := m; m := m.next END;
  550. IF m # NIL THEN
  551. IF m.refcnt = 0 THEN (* free the module *)
  552. FOR i := 0 TO LEN(m.module)-1 DO DEC(m.module[i].refcnt) END;
  553. m.init := FALSE; (* disallow ThisCommand *)
  554. Append("?", m.name);
  555. (* move module to free list *)
  556. IF p = NIL THEN root := root.next ELSE p.next := m.next END;
  557. m.next := freeRoot; freeRoot := m;
  558. (* clear global pointers and code *)
  559. IF m.ptrAdr # NIL THEN
  560. Trace.String("ptradr del"); Trace.Ln;
  561. FOR i := 0 TO LEN(m.ptrAdr)-1 DO SYSTEM.PUT (m.ptrAdr[i], NIL) END;
  562. END;
  563. IF ClearCode & (m.code # NIL) THEN
  564. Trace.String("clear code"); Trace.Ln;
  565. FOR i := 0 TO LEN(m.code)-1 DO m.code[i] := 0CCX END
  566. END;
  567. Trace.String("clear code f"); Trace.Ln;
  568. (* remove references to module data *)
  569. m.published := FALSE;
  570. m.entry := NIL; m.command := NIL; m.ptrAdr := NIL;
  571. (* do not clear m.type or m.module, as old heap block tags might reference type descs indirectly. *) (* m.staticTypeDescs, m.typeInfo ??? *)
  572. (* do not clear m.data or m.code, as they are used in ThisModuleByAdr (for debugging). *)
  573. (* do not clear m.refs, as they are used in Traps (for debugging). *)
  574. m.export.dsc := NIL; m.exTable := NIL;
  575. ELSE
  576. res := 1901; (* can not free module in use *)
  577. COPY(name, msg); Append(" reference count not zero", msg)
  578. END
  579. ELSE
  580. res := 1902; (* module not found *)
  581. COPY(name, msg); Append(" not found", msg)
  582. END;
  583. Machine.Release(Machine.Modules)
  584. END FreeModule;
  585. (** Shut down all modules by calling their termination handlers and then call Machine.Shutdown. *)
  586. PROCEDURE Shutdown*(code: LONGINT);
  587. VAR m: Module; term: TerminationHandler;
  588. BEGIN
  589. IF code # None THEN
  590. LOOP
  591. Machine.Acquire(Machine.Modules);
  592. m := root; WHILE (m # NIL) & (m.term = NIL) DO m := m.next END;
  593. IF m # NIL THEN term := m.term; m.term := NIL END; (* finalizer only called once *)
  594. Machine.Release(Machine.Modules);
  595. IF m = NIL THEN EXIT END;
  596. IF trace THEN
  597. Machine.Acquire (Machine.TraceOutput);
  598. Trace.String("TermHandler "); Trace.StringLn (m.name);
  599. Machine.Release (Machine.TraceOutput);
  600. END;
  601. term (* if this causes exception or hangs, another shutdown call will retry *)
  602. END;
  603. (* clean up finalizers *)
  604. m := root;
  605. WHILE m # NIL DO
  606. IF LEN(m.code)>0 THEN
  607. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  608. END;
  609. m := m.next
  610. END;
  611. IF trace THEN
  612. Machine.Acquire (Machine.TraceOutput);
  613. Trace.StringLn ("Modules.Shutdown finished");
  614. Machine.Release (Machine.TraceOutput);
  615. END;
  616. Machine.Shutdown(code = Reboot) (* does not return *)
  617. END
  618. END Shutdown;
  619. (* Is this PC handled in the corresponding module. deep = scan the whole stack. *)
  620. PROCEDURE IsExceptionHandled*(VAR pc, fp: ADDRESS; deep: BOOLEAN): BOOLEAN;
  621. VAR
  622. handler: ADDRESS;
  623. BEGIN
  624. IF deep THEN
  625. handler := GetExceptionHandler(pc);
  626. IF handler # -1 THEN (* Handler in the current PAF *)
  627. RETURN TRUE
  628. ELSE
  629. WHILE (fp # 0) & (handler = -1) DO
  630. SYSTEM.GET (fp + 4, pc);
  631. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  632. handler := GetExceptionHandler(pc);
  633. SYSTEM.GET (fp, fp) (* Unwind PAF *)
  634. END;
  635. IF handler = -1 THEN RETURN FALSE ELSE pc := handler; RETURN TRUE END
  636. END
  637. ELSE
  638. RETURN GetExceptionHandler(pc) # -1
  639. END
  640. END IsExceptionHandled;
  641. (* Is this PC handled in the corresponding module. If the PC is handled the PC of the
  642. handler is return else -1 is return. There is no problem concurrently accessing this
  643. procedure, there is only reading work. *)
  644. PROCEDURE GetExceptionHandler*(pc: ADDRESS): ADDRESS;
  645. VAR
  646. m: Module;
  647. PROCEDURE BinSearch(exTable: ExceptionTable; key: ADDRESS): ADDRESS;
  648. VAR
  649. x, l, r: LONGINT;
  650. BEGIN
  651. l := 0; r:=LEN(exTable) - 1;
  652. REPEAT
  653. x := (l + r) DIV 2;
  654. IF key < exTable[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  655. UNTIL ((key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) ) OR (l > r);
  656. IF (key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) THEN
  657. RETURN exTable[x].pcHandler;
  658. ELSE
  659. RETURN -1;
  660. END
  661. END BinSearch;
  662. BEGIN
  663. m := ThisModuleByAdr(pc);
  664. IF (m # NIL) & (m.exTable # NIL) & (LEN(m.exTable) > 0) THEN
  665. RETURN BinSearch(m.exTable, pc);
  666. END;
  667. RETURN -1;
  668. END GetExceptionHandler;
  669. (** fof: to make custom solutions to the race process, described below, possible. This is not a solution to the generic problem !! *)
  670. PROCEDURE Initialized*(m: Module): BOOLEAN;
  671. BEGIN
  672. RETURN m.init;
  673. END Initialized;
  674. PROCEDURE Register- (module {UNTRACED}: Module);
  675. BEGIN {UNCOOPERATIVE, UNCHECKED}
  676. (*TRACE(module.name);*)
  677. IF register.first = NIL THEN
  678. register.first := module;
  679. ELSE
  680. register.last.next := module;
  681. END;
  682. register.last := module;
  683. END Register;
  684. PROCEDURE Unregister(m: Module);
  685. VAR prev: Module;
  686. BEGIN
  687. ASSERT(m#NIL);
  688. IF register.first = NIL THEN RETURN
  689. ELSIF m = register.first THEN
  690. register.first := m.next;
  691. IF register.first = NIL THEN register.last := NIL END;
  692. ELSE
  693. prev := register.first;
  694. WHILE (prev.next # NIL) & (prev.next # m) DO
  695. prev := prev.next;
  696. END;
  697. IF prev.next = m THEN
  698. prev.next := prev.next.next;
  699. IF prev.next = NIL THEN register.last := prev END;
  700. END;
  701. END;
  702. m.next := NIL;
  703. END Unregister;
  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. (*
  710. register.first := m.next;
  711. m.next := NIL;
  712. *)
  713. IF m.module # NIL THEN
  714. FOR import := 0 TO LEN (m.module) - 1 DO
  715. IF ~m.module[import].published THEN
  716. ASSERT(register.first # NIL);
  717. (*prev := NIL;
  718. cur := register.first;
  719. WHILE (cur # NIL) & (cur # m.module[import]) DO
  720. prev := cur;
  721. cur := cur.next
  722. END;
  723. (*ASSERT(cur = m.module[import]);*)
  724. ASSERT(cur = m.module[import]);
  725. IF prev = NIL THEN
  726. register.first := cur.next
  727. ELSE
  728. prev.next := cur.next;
  729. END;
  730. cur.next := NIL;
  731. *)
  732. Initialize0 (m.module[import]);
  733. END
  734. END;
  735. END;
  736. Initialize0 (m);
  737. END;
  738. END PublishRegisteredModules;
  739. (* procedure that will be called last in a linked kernel *)
  740. PROCEDURE {FINAL, NOPAF} Main-;
  741. BEGIN
  742. (*Machine.Init;*)
  743. IF TraceBoot THEN
  744. Trace.String("publish registered modules"); Trace.Ln;
  745. END;
  746. PublishRegisteredModules;
  747. END Main;
  748. PROCEDURE Init;
  749. VAR
  750. s: ARRAY 4 OF CHAR;
  751. BEGIN
  752. (* root is initialized by the linker *)
  753. shutdown := None;
  754. numLoaders := 0;
  755. freeRoot := NIL;
  756. Machine.GetConfig("TraceModules", s);
  757. trace := (s[0] = "1");
  758. END Init;
  759. BEGIN
  760. Init;
  761. END Modules.
  762. (*
  763. 19.03.1998 pjm Started
  764. 06.10.1998 pjm FreeModule
  765. Note:
  766. 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).
  767. *)
  768. StaticLinker.Link --fileFormat=PE32 --fileName=A2M.exe --extension=GofW --displacement=401000H
  769. Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands
  770. FIles WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection
  771. GenericLoader BootConsole
  772. Traps
  773. TrapWriters
  774. SystemVersion
  775. CRC
  776. FileTrapWriter
  777. Display
  778. User32
  779. GDI32
  780. Displays
  781. Plugins
  782. Inputs
  783. Options
  784. WindowManager
  785. WMGraphics
  786. WMRectangles
  787. Raster
  788. CLUTs
  789. UTF8Strings
  790. WMRasterScale
  791. Codecs
  792. SoundDevices
  793. Configuration
  794. XMLObjects
  795. XML
  796. DynamicStrings
  797. XMLScanner
  798. XMLParser
  799. Unzip
  800. Inflate
  801. Texts
  802. WMEvents
  803. Locks
  804. FP1616
  805. Archives
  806. WMMessages
  807. Debugging
  808. WMDefaultWindows
  809. WMWindowManager
  810. WMGraphicUtilities
  811. WMFontManager
  812. WMDefaultFont
  813. WMOTFonts
  814. OpenType
  815. OpenTypeInt
  816. OpenTypeScan
  817. WMCCGFonts
  818. PNGDecoder
  819. Clipboard
  820. TextUtilities
  821. Repositories
  822. Localization
  823. UnicodeProperties
  824. HostClipboard
  825. FSTools
  826. RelativeFileSystem
  827. Autostart
  828. WMTrapWriter
  829. WMUtilities
  830. WMComponents
  831. Events
  832. WMProperties
  833. Models
  834. Types
  835. WMDropTarget
  836. WMDocumentEditor
  837. WMMacros
  838. WMTextView
  839. SyntaxHighlighter
  840. WMStandardComponents
  841. FileHandlers
  842. WMPopups
  843. WMPieMenu
  844. UnicodeBidirectionality
  845. PositionDebugging
  846. ContextualDependency
  847. WMEditors
  848. UndoManager
  849. WMInputMethods
  850. WMSearchComponents
  851. WMDialogs
  852. WMRestorable
  853. UpTime
  854. StartMenu
  855. MainMenu
  856. WMTabComponents
  857. Tar
  858. SkinEngine
  859. SkinLanguage
  860. Pipes
  861. WMFileManager
  862. WMSystemComponents
  863. WMTrees
  864. WMGrids
  865. WMStringGrids
  866. Notepad
  867. WMKernelLog
  868. KernelLogger
  869. WMClock
  870. Math
  871. WMTextTool
  872. PET
  873. CompilerInterface
  874. WhitespaceRemover
  875. WMDiagnostics
  876. WMBitmapFont
  877. PETTrees
  878. WMOberonFonts
  879. WMNavigate
  880. HotKeys
  881. Errors
  882. Zip
  883. Zlib
  884. ZlibReaders
  885. ZlibBuffers
  886. ZlibInflate
  887. ZlibWriters
  888. ZlibDeflate
  889. FoxBasic
  890. FoxA2Interface
  891. FoxScanner
  892. FoxSyntaxTree
  893. FoxGlobal
  894. FoxParser
  895. FoxPrintout
  896. FoxFormats
  897. FoxSemanticChecker
  898. FoxBackend
  899. FoxFrontend
  900. Compiler
  901. ReleaseThreadPool
  902. Release
  903. PETReleaseTree
  904. ModuleParser
  905. PETModuleTree
  906. ProcessInfo0
  907. ProcessInfo
  908. SystemTools
  909. StaticLinker
  910. ~
  911. SystemTools.ListModules -l ~