Generic.Modules.Mod 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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 *)
  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. CONST ModuleInitTimeout = HUGEINT(3000000000); (* Timeout for waiting until a module get initialized, 3 seconds for 1 GHz CPU *)
  455. (* Retrieve a procedure given a module name, the procedure name and some type information (kernel call) *)
  456. PROCEDURE GetProcedure*(CONST moduleName, procedureName : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  457. VAR module : Module; ignoreMsg : ARRAY 32 OF CHAR; i, res : LONGINT; t: HUGEINT;
  458. BEGIN
  459. module := ThisModule(moduleName, res, ignoreMsg);
  460. IF (res = Ok) THEN
  461. (*!
  462. module body must have been called (see note at the end of this module);
  463. return NIL if the module does not get initialized within the specified timeout
  464. *)
  465. IF ~module.init THEN
  466. t := Machine.GetTimer();
  467. WHILE ~module.init & (Machine.GetTimer() - t < ModuleInitTimeout) DO END;
  468. IF ~module.init THEN (* timeout has expired *)
  469. RETURN;
  470. END;
  471. END;
  472. Machine.Acquire(Machine.Modules);
  473. i := 0; entryAdr := Heaps.NilVal;
  474. WHILE (entryAdr = Heaps.NilVal) & (i # LEN(module.command^)) DO
  475. IF (module.command[i].name = procedureName) & (module.command[i].argTdAdr = argTdAdr) & (module.command[i].retTdAdr = retTdAdr) THEN
  476. entryAdr := module.command[i].entryAdr;
  477. END;
  478. INC(i)
  479. END;
  480. Machine.Release(Machine.Modules);
  481. END;
  482. END GetProcedure;
  483. (** Return the named type *)
  484. PROCEDURE ThisType*(m: Module; CONST name: ARRAY OF CHAR): TypeDesc;
  485. VAR i: LONGINT; type: TypeDesc;
  486. BEGIN
  487. Machine.Acquire(Machine.Modules);
  488. i := 0;
  489. WHILE (i < LEN(m.typeInfo)) & (m.typeInfo[i].name # name) DO INC(i) END;
  490. IF i = LEN(m.typeInfo) THEN
  491. type := NIL
  492. ELSE
  493. type := m.typeInfo[i]
  494. END;
  495. Machine.Release(Machine.Modules);
  496. RETURN type
  497. END ThisType;
  498. PROCEDURE ThisTypeByAdr*(adr: ADDRESS; VAR m: Module; VAR t: TypeDesc);
  499. BEGIN
  500. IF adr # 0 THEN
  501. Machine.Acquire(Machine.Modules);
  502. SYSTEM.GET (adr + Heaps.TypeDescOffset, adr);
  503. t := SYSTEM.VAL(TypeDesc, adr);
  504. m := t.mod;
  505. Machine.Release(Machine.Modules)
  506. ELSE
  507. m := NIL; t := NIL
  508. END
  509. END ThisTypeByAdr;
  510. (** create a new object given its type descriptor *)
  511. PROCEDURE NewObj*(t : TypeDesc; isRealtime: BOOLEAN) : ANY;
  512. VAR x : ANY;
  513. BEGIN
  514. Heaps.NewRec(x, SYSTEM.VAL (ADDRESS, t.tag), isRealtime);
  515. RETURN x;
  516. END NewObj;
  517. (** return the type descriptor of an object *)
  518. PROCEDURE TypeOf*(obj : ANY): TypeDesc;
  519. VAR
  520. m : Module;
  521. t : TypeDesc;
  522. adr : ADDRESS;
  523. BEGIN
  524. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.TypeDescOffset, adr);
  525. ThisTypeByAdr(adr, m, t);
  526. RETURN t;
  527. END TypeOf;
  528. PROCEDURE FindPos(key: ADDRESS; VAR pos: LONGINT): BOOLEAN;
  529. VAR l, r, x: LONGINT; isHit: BOOLEAN;
  530. BEGIN
  531. IF numProcs > 0 THEN
  532. l := 0; r := numProcs - 1;
  533. REPEAT
  534. x := (l + r) DIV 2;
  535. IF key < procOffsets[x].data.pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  536. isHit := ((procOffsets[x].data.pcFrom <= key) & (key < procOffsets[x].data.pcLimit));
  537. UNTIL isHit OR (l > r);
  538. IF isHit THEN
  539. pos := x;
  540. RETURN TRUE
  541. END;
  542. END;
  543. RETURN FALSE
  544. END FindPos;
  545. (** searches for the given pc in the global ProcKeyTable, if found it returns the corresponding data element *)
  546. PROCEDURE FindProc*(pc: ADDRESS; VAR data: ProcTableEntry; VAR index: LONGINT; VAR success: BOOLEAN);
  547. VAR x: LONGINT;
  548. BEGIN
  549. success := FindPos(pc, x);
  550. IF success THEN
  551. data := procOffsets[x].data;
  552. index := procOffsets[x].startIndex
  553. END
  554. END FindProc;
  555. PROCEDURE FindInsertionPos(VAR entry: ProcTableEntry; VAR pos: LONGINT): BOOLEAN;
  556. VAR l, r, x: LONGINT; success, isHit: BOOLEAN;
  557. BEGIN
  558. pos := -1;
  559. success := FALSE;
  560. IF numProcs = 0 THEN (* empty table *)
  561. pos := 0; success := TRUE
  562. ELSE
  563. l := 0; r := numProcs - 1;
  564. REPEAT
  565. x := (l + r) DIV 2;
  566. IF entry.pcLimit < procOffsets[x].data.pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  567. isHit := ((x = 0) OR (procOffsets[x - 1].data.pcLimit <= entry.pcFrom)) & (entry.pcLimit <= procOffsets[x].data.pcFrom);
  568. UNTIL isHit OR (l > r);
  569. IF isHit THEN
  570. pos := x; success := TRUE
  571. ELSE
  572. IF (x = numProcs - 1) & (procOffsets[x].data.pcLimit <= entry.pcFrom) THEN
  573. pos := x + 1; success := TRUE
  574. END
  575. END
  576. END;
  577. RETURN success
  578. END FindInsertionPos;
  579. PROCEDURE NumTotalPtrs(procTable: ProcTable): LONGINT;
  580. VAR i, num: LONGINT;
  581. BEGIN
  582. num := 0;
  583. IF procTable # NIL THEN
  584. FOR i := 0 TO LEN(procTable) - 1 DO
  585. num := num + procTable[i].noPtr
  586. END;
  587. END;
  588. RETURN num
  589. END NumTotalPtrs;
  590. (* insert the procedure code offsets and pointer offsets of a single module into the global table *)
  591. PROCEDURE InsertProcOffsets(procTable: ProcTable; ptrTable: PtrTable; maxPtr: LONGINT);
  592. VAR success: BOOLEAN; i, j, pos, poslast, newLen, num,numberPointer: LONGINT;
  593. temp: ADDRESS;
  594. newProcOffsets: ProcOffsetTable; newPtrOffsets: PtrTable;
  595. ptrOfsLen,procOfsLen: LONGINT;
  596. BEGIN
  597. (* this procedure is called by procedure Publish only and is protected by the Machine.Modules lock *)
  598. IF procTable=NIL THEN RETURN END;
  599. IF ptrTable=NIL THEN RETURN END;
  600. IF LEN(procTable) > 0 THEN
  601. IF procOffsets = NIL THEN procOfsLen := 0 ELSE procOfsLen := LEN(procOffsets) END;
  602. IF numProcs + LEN(procTable) > procOfsLen THEN
  603. newLen := procOfsLen + InitTableLen;
  604. WHILE numProcs + LEN(procTable) > newLen DO newLen := newLen + InitTableLen END;
  605. NEW(newProcOffsets, newLen);
  606. FOR i := 0 TO numProcs - 1 DO
  607. newProcOffsets[i] := procOffsets[i]
  608. END;
  609. procOffsets := newProcOffsets
  610. END;
  611. num := NumTotalPtrs(procTable);
  612. IF ptrOffsets = NIL THEN ptrOfsLen := 0 ELSE ptrOfsLen := LEN(ptrOffsets) END;
  613. IF numPtrs + num > ptrOfsLen THEN
  614. newLen := ptrOfsLen + InitPtrTableLen;
  615. WHILE numPtrs + num > newLen DO newLen := newLen + InitPtrTableLen END;
  616. NEW(newPtrOffsets, newLen);
  617. FOR i := 0 TO numPtrs - 1 DO
  618. newPtrOffsets[i] := ptrOffsets[i]
  619. END;
  620. ptrOffsets := newPtrOffsets
  621. END;
  622. success := FindInsertionPos(procTable[0], pos); success := success & FindInsertionPos(procTable[LEN(procTable) - 1], poslast);
  623. IF (~success) OR (pos # poslast) THEN Machine.Release(Machine.Modules); HALT(2001) END;
  624. FOR i := numProcs - 1 TO pos BY -1 DO procOffsets[i + LEN(procTable)] := procOffsets[i] END;
  625. numberPointer := 0;
  626. FOR i := 0 TO LEN(procTable) - 1 DO
  627. procOffsets[pos + i].data := procTable[i];
  628. 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 *)
  629. FOR j := 0 TO procTable[i].noPtr - 1 DO
  630. (*
  631. temp := ptrTable[numberPointer]; INC(numberPointer);
  632. *)
  633. temp := ptrTable[i * maxPtr + j];
  634. ptrOffsets[numPtrs + j] := temp;
  635. END;
  636. numPtrs := numPtrs + procTable[i].noPtr;
  637. END;
  638. numProcs := numProcs + LEN(procTable);
  639. END
  640. END InsertProcOffsets;
  641. (** deletes a sequence of entries given in procTable from the global procOffsets table - the table remains sorted,
  642. this procedure is called within AosLocks.AosModules, so no lock is taken here. *)
  643. PROCEDURE DeleteProcOffsets(firstProcPC: ADDRESS; noProcsInMod: LONGINT);
  644. VAR pos, i, noPtrsInMod, oldIndex: LONGINT; success: BOOLEAN;
  645. BEGIN
  646. IF noProcsInMod > 0 THEN
  647. success := FindPos(firstProcPC, pos);
  648. IF success THEN
  649. (* delete entries in ptrOffsets first *)
  650. noPtrsInMod := 0;
  651. FOR i := pos TO pos + noProcsInMod - 1 DO
  652. noPtrsInMod := noPtrsInMod + procOffsets[i].data.noPtr
  653. END;
  654. oldIndex := procOffsets[pos].startIndex;
  655. FOR i := procOffsets[pos].startIndex + noPtrsInMod TO numPtrs - 1 DO
  656. ptrOffsets[i - noPtrsInMod] := ptrOffsets[i]
  657. END;
  658. numPtrs := numPtrs - noPtrsInMod;
  659. (* delete entries in procOffsets *)
  660. FOR i := pos + noProcsInMod TO numProcs - 1 DO
  661. procOffsets[i - noProcsInMod] := procOffsets[i]
  662. END;
  663. numProcs := numProcs - noProcsInMod;
  664. (* adjust startIndex of procOffsets entries greater than those that have been deleted *)
  665. FOR i := 0 TO numProcs - 1 DO
  666. IF procOffsets[i].startIndex > oldIndex THEN
  667. procOffsets[i].startIndex := procOffsets[i].startIndex - noPtrsInMod
  668. END
  669. END;
  670. ELSE
  671. Trace.String("corrupt global procOffsets table"); Trace.Ln;
  672. Machine.Release(Machine.Modules);
  673. HALT(2000)
  674. END
  675. END
  676. END DeleteProcOffsets;
  677. (** 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. *)
  678. PROCEDURE InstallTermHandler*(h: TerminationHandler);
  679. VAR m: Module;
  680. BEGIN
  681. m := ThisModuleByAdr(SYSTEM.VAL (ADDRESS, h));
  682. IF m # NIL THEN
  683. m.term := h (* overwrite existing handler, if any *)
  684. END
  685. END InstallTermHandler;
  686. (** 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. *)
  687. PROCEDURE FreeModule*(CONST name: ARRAY OF CHAR; VAR res: LONGINT; VAR msg: ARRAY OF CHAR);
  688. VAR p, m: Module; term: TerminationHandler; i: LONGINT;
  689. BEGIN
  690. m := ModuleByName(name);
  691. IF (m # NIL) & (m.refcnt = 0) THEN (* will be freed below *)
  692. IF m.term # NIL THEN (* call termination handler *)
  693. term := m.term; m.term := NIL; term (* may trap *)
  694. END;
  695. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  696. END;
  697. res := Ok; msg[0] := 0X;
  698. Machine.Acquire(Machine.Modules);
  699. Trace.String("Acquired Machine.Modules x"); Trace.Ln;
  700. p := NIL; m := root;
  701. WHILE (m # NIL) & (m.name # name) DO p := m; m := m.next END;
  702. Trace.String("Acquired Machine.Modules y"); Trace.Ln;
  703. IF m # NIL THEN
  704. Trace.String("found module"); Trace.Ln;
  705. IF m.refcnt = 0 THEN (* free the module *)
  706. FOR i := 0 TO LEN(m.module)-1 DO DEC(m.module[i].refcnt) END;
  707. m.init := FALSE; (* disallow ThisCommand *)
  708. Append("?", m.name);
  709. (* move module to free list *)
  710. IF p = NIL THEN root := root.next ELSE p.next := m.next END;
  711. m.next := freeRoot; freeRoot := m;
  712. (* clear global pointers and code *)
  713. IF m.ptrAdr # NIL THEN
  714. Trace.String("ptradr del"); Trace.Ln;
  715. FOR i := 0 TO LEN(m.ptrAdr)-1 DO SYSTEM.PUT (m.ptrAdr[i], NIL) END;
  716. END;
  717. IF ClearCode & (m.code # NIL) THEN
  718. Trace.String("clear code"); Trace.Ln;
  719. FOR i := 0 TO LEN(m.code)-1 DO m.code[i] := 0CCX END
  720. END;
  721. Trace.String("clear code f"); Trace.Ln;
  722. (* remove references to module data *)
  723. m.published := FALSE;
  724. m.entry := NIL; m.command := NIL; m.ptrAdr := NIL;
  725. (* do not clear m.type or m.module, as old heap block tags might reference type descs indirectly. *) (* m.staticTypeDescs, m.typeInfo ??? *)
  726. (* do not clear m.data or m.code, as they are used in ThisModuleByAdr (for debugging). *)
  727. (* do not clear m.refs, as they are used in Traps (for debugging). *)
  728. m.export.dsc := NIL; m.exTable := NIL;
  729. (*Trace.String("delete proc offsets"); Trace.Ln;
  730. DeleteProcOffsets(m.firstProc, m.noProcs);
  731. *)
  732. ELSE
  733. res := 1901; (* can not free module in use *)
  734. COPY(name, msg); Append(" reference count not zero", msg)
  735. END
  736. ELSE
  737. res := 1902; (* module not found *)
  738. COPY(name, msg); Append(" not found", msg)
  739. END;
  740. Machine.Release(Machine.Modules)
  741. END FreeModule;
  742. (** Shut down all modules by calling their termination handlers and then call Machine.Shutdown. *)
  743. PROCEDURE Shutdown*(code: LONGINT);
  744. VAR m: Module; term: TerminationHandler;
  745. BEGIN
  746. IF code # None THEN
  747. LOOP
  748. Machine.Acquire(Machine.Modules);
  749. m := root; WHILE (m # NIL) & (m.term = NIL) DO m := m.next END;
  750. IF m # NIL THEN term := m.term; m.term := NIL END;
  751. Machine.Release(Machine.Modules);
  752. IF m = NIL THEN EXIT END;
  753. IF trace THEN
  754. Machine.Acquire (Machine.TraceOutput);
  755. Trace.String("TermHandler "); Trace.StringLn (m.name);
  756. Machine.Release (Machine.TraceOutput);
  757. END;
  758. term (* if this causes exception or hangs, another shutdown call will retry *)
  759. END;
  760. (* clean up finalizers *)
  761. m := root;
  762. WHILE m # NIL DO
  763. IF LEN(m.code)>0 THEN
  764. Heaps.CleanupModuleFinalizers(ADDRESSOF(m.code[0]), LEN(m.code), m.name)
  765. END;
  766. m := m.next
  767. END;
  768. IF trace THEN
  769. Machine.Acquire (Machine.TraceOutput);
  770. Trace.StringLn ("Modules.Shutdown finished");
  771. Machine.Release (Machine.TraceOutput);
  772. END;
  773. Machine.Shutdown(code = Reboot) (* does not return *)
  774. END
  775. END Shutdown;
  776. (* Is this PC handled in the corresponding module. deep = scan the whole stack. *)
  777. PROCEDURE IsExceptionHandled*(VAR pc, fp: ADDRESS; deep: BOOLEAN): BOOLEAN;
  778. VAR
  779. handler: ADDRESS;
  780. BEGIN
  781. IF deep THEN
  782. handler := GetExceptionHandler(pc);
  783. IF handler # -1 THEN (* Handler in the current PAF *)
  784. RETURN TRUE
  785. ELSE
  786. WHILE (fp # 0) & (handler = -1) DO
  787. SYSTEM.GET (fp + 4, pc);
  788. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  789. handler := GetExceptionHandler(pc);
  790. SYSTEM.GET (fp, fp) (* Unwind PAF *)
  791. END;
  792. IF handler = -1 THEN RETURN FALSE ELSE pc := handler; RETURN TRUE END
  793. END
  794. ELSE
  795. RETURN GetExceptionHandler(pc) # -1
  796. END
  797. END IsExceptionHandled;
  798. (* Is this PC handled in the corresponding module. If the PC is handled the PC of the
  799. handler is return else -1 is return. There is no problem concurrently accessing this
  800. procedure, there is only reading work. *)
  801. PROCEDURE GetExceptionHandler*(pc: ADDRESS): ADDRESS;
  802. VAR
  803. m: Module;
  804. PROCEDURE BinSearch(exTable: ExceptionTable; key: ADDRESS): ADDRESS;
  805. VAR
  806. x, l, r: LONGINT;
  807. BEGIN
  808. l := 0; r:=LEN(exTable) - 1;
  809. REPEAT
  810. x := (l + r) DIV 2;
  811. IF key < exTable[x].pcFrom THEN r := x - 1 ELSE l := x + 1 END;
  812. UNTIL ((key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) ) OR (l > r);
  813. IF (key >= exTable[x].pcFrom) & (key < exTable[x].pcTo) THEN
  814. RETURN exTable[x].pcHandler;
  815. ELSE
  816. RETURN -1;
  817. END
  818. END BinSearch;
  819. BEGIN
  820. m := ThisModuleByAdr(pc);
  821. IF (m # NIL) & (m.exTable # NIL) & (LEN(m.exTable) > 0) THEN
  822. RETURN BinSearch(m.exTable, pc);
  823. END;
  824. RETURN -1;
  825. END GetExceptionHandler;
  826. (** fof: to make custom solutions to the race process, described below, possible. This is not a solution to the generic problem !! *)
  827. PROCEDURE Initialized*(m: Module): BOOLEAN;
  828. BEGIN
  829. RETURN m.init;
  830. END Initialized;
  831. (** Return the specified kernel procedure address. *)
  832. PROCEDURE GetKernelProc*(num: LONGINT): ADDRESS;
  833. VAR adr: ADDRESS;
  834. BEGIN
  835. adr := kernelProc[253-num];
  836. ASSERT(adr # 0);
  837. RETURN adr
  838. END GetKernelProc;
  839. PROCEDURE Register- (module {UNTRACED}: Module);
  840. BEGIN {UNCOOPERATIVE, UNCHECKED}
  841. IF register.first = NIL THEN
  842. register.first := module;
  843. ELSE
  844. register.last.next := module;
  845. END;
  846. register.last := module;
  847. END Register;
  848. PROCEDURE PublishRegisteredModules;
  849. VAR m {UNTRACED}, prev {UNTRACED}, cur {UNTRACED}: Module; module, import: SIZE;
  850. BEGIN
  851. WHILE register.first # NIL DO
  852. m := register.first;
  853. register.first := m.next;
  854. m.next := NIL;
  855. IF m.module # NIL THEN
  856. FOR import := 0 TO LEN (m.module) - 1 DO
  857. IF ~m.module[import].published THEN
  858. ASSERT(register.first # NIL);
  859. prev := NIL;
  860. cur := register.first;
  861. WHILE (cur # NIL) & (cur # m.module[import]) DO
  862. prev := cur;
  863. cur := cur.next
  864. END;
  865. (*ASSERT(cur = m.module[import]);*)
  866. ASSERT(cur = m.module[import]);
  867. IF prev = NIL THEN
  868. register.first := cur.next
  869. ELSE
  870. prev.next := cur.next;
  871. END;
  872. cur.next := NIL;
  873. Initialize0 (m.module[import]);
  874. END
  875. END;
  876. END;
  877. Initialize0 (m);
  878. END;
  879. END PublishRegisteredModules;
  880. (* procedure that will be called last in a linked kernel *)
  881. PROCEDURE {FINAL} Main;
  882. BEGIN
  883. Machine.Init;
  884. Trace.String("publish registered modules"); Trace.Ln;
  885. PublishRegisteredModules;
  886. END Main;
  887. PROCEDURE Init;
  888. VAR
  889. newArr: PROCEDURE (VAR p: ANY; elemTag: ADDRESS; numElems, numDims: SIZE; isRealtime: BOOLEAN);
  890. newSys: PROCEDURE (VAR p: ANY; size: SIZE; isRealtime: BOOLEAN);
  891. newRec: PROCEDURE (VAR p: ANY; tag: ADDRESS; isRealtime: BOOLEAN);
  892. getProcedure: PROCEDURE(CONST m, p : ARRAY OF CHAR; argTdAdr, retTdAdr : ADDRESS; VAR entryAdr : ADDRESS);
  893. s: ARRAY 4 OF CHAR;
  894. module: Module; new: BOOLEAN; i: LONGINT;
  895. BEGIN
  896. (* root and initBlock are initialized by the linker *)
  897. shutdown := None;
  898. newArr := Heaps.NewArr;
  899. newSys := Heaps.NewSys;
  900. newRec := Heaps.NewRec;
  901. getProcedure := GetProcedure;
  902. kernelProc[0] := SYSTEM.VAL (ADDRESS, newRec); (* 253 *)
  903. kernelProc[1] := SYSTEM.VAL (ADDRESS, newSys); (* 252 *)
  904. kernelProc[2] := SYSTEM.VAL (ADDRESS, newArr); (* 251 *)
  905. kernelProc[3] := 0; (* 250 *)
  906. kernelProc[4] := 0; (* 249 *)
  907. kernelProc[5] := 0; (* 248 *)
  908. kernelProc[6] := 0; (* 247 *)
  909. kernelProc[7] := 0; (* 246 *)
  910. kernelProc[8] := 0; (* 245 *)
  911. kernelProc[9] := 0; (* 244 *)
  912. kernelProc[10] := SYSTEM.VAL(ADDRESS, getProcedure); (* 243 *)
  913. numLoaders := 0;
  914. freeRoot := NIL;
  915. Machine.GetConfig("TraceModules", s);
  916. trace := (s[0] = "1");
  917. (*
  918. FOR i := 0 TO Runtime.modules-1 DO
  919. module := SYSTEM.VAL(Module,Runtime.kernelModule[i]);
  920. IF TraceBoot THEN
  921. Trace.String("publishing module ");
  922. Trace.String(module.name); Trace.Ln;
  923. END;
  924. Publish(module,new);
  925. ASSERT(new,112233);
  926. END;
  927. *)
  928. (*
  929. module := SYSTEM.VAL(Module,SELF);
  930. Publish(module,new);
  931. *)
  932. END Init;
  933. BEGIN
  934. Init;
  935. END Modules.
  936. (*
  937. 19.03.1998 pjm Started
  938. 06.10.1998 pjm FreeModule
  939. Note:
  940. 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).
  941. *)