Generic.Modules.Mod 32 KB

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