Generic.Modules.Mod 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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*: 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. Trace.String("publish registered modules"); Trace.Ln;
  744. PublishRegisteredModules;
  745. END Main;
  746. PROCEDURE Init;
  747. VAR
  748. s: ARRAY 4 OF CHAR;
  749. BEGIN
  750. (* root is initialized by the linker *)
  751. shutdown := None;
  752. numLoaders := 0;
  753. freeRoot := NIL;
  754. Machine.GetConfig("TraceModules", s);
  755. trace := (s[0] = "1");
  756. END Init;
  757. BEGIN
  758. Init;
  759. END Modules.
  760. (*
  761. 19.03.1998 pjm Started
  762. 06.10.1998 pjm FreeModule
  763. Note:
  764. 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).
  765. *)
  766. StaticLinker.Link --fileFormat=PE32 --fileName=A2M.exe --extension=GofW --displacement=401000H
  767. Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands
  768. FIles WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection
  769. GenericLoader BootConsole
  770. Traps
  771. TrapWriters
  772. SystemVersion
  773. CRC
  774. FileTrapWriter
  775. Display
  776. User32
  777. GDI32
  778. Displays
  779. Plugins
  780. Inputs
  781. Options
  782. WindowManager
  783. WMGraphics
  784. WMRectangles
  785. Raster
  786. CLUTs
  787. UTF8Strings
  788. WMRasterScale
  789. Codecs
  790. SoundDevices
  791. Configuration
  792. XMLObjects
  793. XML
  794. DynamicStrings
  795. XMLScanner
  796. XMLParser
  797. Unzip
  798. Inflate
  799. Texts
  800. WMEvents
  801. Locks
  802. FP1616
  803. Archives
  804. WMMessages
  805. Debugging
  806. WMDefaultWindows
  807. WMWindowManager
  808. WMGraphicUtilities
  809. WMFontManager
  810. WMDefaultFont
  811. WMOTFonts
  812. OpenType
  813. OpenTypeInt
  814. OpenTypeScan
  815. WMCCGFonts
  816. PNGDecoder
  817. Clipboard
  818. TextUtilities
  819. Repositories
  820. Localization
  821. UnicodeProperties
  822. HostClipboard
  823. FSTools
  824. RelativeFileSystem
  825. Autostart
  826. WMTrapWriter
  827. WMUtilities
  828. WMComponents
  829. Events
  830. WMProperties
  831. Models
  832. Types
  833. WMDropTarget
  834. WMDocumentEditor
  835. WMMacros
  836. WMTextView
  837. SyntaxHighlighter
  838. WMStandardComponents
  839. FileHandlers
  840. WMPopups
  841. WMPieMenu
  842. UnicodeBidirectionality
  843. PositionDebugging
  844. ContextualDependency
  845. WMEditors
  846. UndoManager
  847. WMInputMethods
  848. WMSearchComponents
  849. WMDialogs
  850. WMRestorable
  851. UpTime
  852. StartMenu
  853. MainMenu
  854. WMTabComponents
  855. Tar
  856. SkinEngine
  857. SkinLanguage
  858. Pipes
  859. WMFileManager
  860. WMSystemComponents
  861. WMTrees
  862. WMGrids
  863. WMStringGrids
  864. Notepad
  865. WMKernelLog
  866. KernelLogger
  867. WMClock
  868. Math
  869. WMTextTool
  870. PET
  871. CompilerInterface
  872. WhitespaceRemover
  873. WMDiagnostics
  874. WMBitmapFont
  875. PETTrees
  876. WMOberonFonts
  877. WMNavigate
  878. HotKeys
  879. Errors
  880. Zip
  881. Zlib
  882. ZlibReaders
  883. ZlibBuffers
  884. ZlibInflate
  885. ZlibWriters
  886. ZlibDeflate
  887. FoxBasic
  888. FoxA2Interface
  889. FoxScanner
  890. FoxSyntaxTree
  891. FoxGlobal
  892. FoxParser
  893. FoxPrintout
  894. FoxFormats
  895. FoxSemanticChecker
  896. FoxBackend
  897. FoxFrontend
  898. Compiler
  899. ReleaseThreadPool
  900. Release
  901. PETReleaseTree
  902. ModuleParser
  903. PETModuleTree
  904. ProcessInfo0
  905. ProcessInfo
  906. SystemTools
  907. StaticLinker
  908. ~
  909. SystemTools.ListModules -l ~