Generic.Modules.Mod 31 KB

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