ActiveCellsRuntime.mod 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. (** Active Cells Runtime Base Code for Variations of ActiveCellsRuntime Implementations
  2. Felix Friedrich, ETH Z 2015
  3. *)
  4. module ActiveCellsRuntime;
  5. import
  6. system, Heaps, Modules, Diagnostics, Strings, Objects, Reflection, Commands;
  7. const
  8. EnableTrace* = false;
  9. type
  10. (* do not inherit from this object -- not supported. This object contains hidden fields instantiated by the compiler that would be lost. *)
  11. Cell* = object (* must be exported for compiler *)
  12. var
  13. c: any;
  14. end Cell;
  15. Context*= object
  16. var
  17. topNet-: any; (** top-level CELLNET object specific to this runtime context *)
  18. finishedAssembly-: boolean; (** assigned to TRUE after the whole architecture has been assembled *)
  19. res*: longint; (** error code, 0 in case of success *)
  20. procedure Allocate*(scope: any; var c: any; t: Modules.TypeDesc; const name: array of char; isCellNet, isEngine: boolean);
  21. end Allocate;
  22. procedure AddPort*(c: any; var p: any; const name: array of char; inout: set; width: longint);
  23. end AddPort;
  24. procedure AddPortArray*(c: any; var ports: any; const name: array of char; inout: set; width: longint; const lens: array of longint);
  25. end AddPortArray;
  26. procedure AddStaticPortArray*(c: any; var ports: array of any; const name: array of char; inout: set; width: longint);
  27. end AddStaticPortArray;
  28. procedure AddPortIntegerProperty*(p: any; const name: array of char; value: longint);
  29. end AddPortIntegerProperty;
  30. procedure AddFlagProperty*(c: any; const name: array of char);
  31. end AddFlagProperty;
  32. procedure AddStringProperty*(c: any; const name: array of char; const value: array of char);
  33. end AddStringProperty;
  34. procedure AddIntegerProperty*(c: any; const name: array of char; value: longint);
  35. end AddIntegerProperty;
  36. procedure AddBooleanProperty*(c: any; const name: array of char; value: boolean);
  37. end AddBooleanProperty;
  38. procedure AddRealProperty*(c: any; const name: array of char; value: longreal);
  39. end AddRealProperty;
  40. procedure AddSetProperty*(c: any; const name: array of char; s: set);
  41. end AddSetProperty;
  42. procedure FinishedProperties*(var c: any);
  43. end FinishedProperties;
  44. procedure Connect*(outPort, inPort: any; depth: longint);
  45. end Connect;
  46. procedure Delegate*(netPort: any; cellPort: any);
  47. end Delegate;
  48. procedure Start*(c: any; proc: procedure{DELEGATE});
  49. end Start;
  50. procedure Send*(p: any; value: longint);
  51. end Send;
  52. procedure BulkSend*(p: any; const value: array of system.byte);
  53. end BulkSend;
  54. procedure SendNonBlocking*(p: any; value: longint): boolean;
  55. end SendNonBlocking;
  56. procedure Receive*(p: any; var value: longint);
  57. end Receive;
  58. procedure BulkReceive*(p: any; var value: array of system.byte);
  59. end BulkReceive;
  60. procedure ReceiveNonBlocking*(p: any; var value: longint): boolean;
  61. end ReceiveNonBlocking;
  62. (* called in Execute after the architecture is fully assembled *)
  63. procedure FinishedAssembly();
  64. begin{EXCLUSIVE}
  65. finishedAssembly := true;
  66. end FinishedAssembly;
  67. procedure WaitUntilFinishedAssembly();
  68. begin{EXCLUSIVE}
  69. await(finishedAssembly or (res # 0));
  70. end WaitUntilFinishedAssembly;
  71. end Context;
  72. Launcher* = object
  73. var
  74. proc: procedure {DELEGATE};
  75. context: Context;
  76. finished, delayedStart: boolean;
  77. error-: boolean;
  78. procedure & Init*(context: Context);
  79. begin
  80. self.context := context;
  81. proc := nil;
  82. finished := false;
  83. end Init;
  84. procedure Start*(p: procedure{DELEGATE}; doWait: boolean);
  85. begin{EXCLUSIVE}
  86. proc := p;
  87. if ~doWait then delayedStart := true; end; (* delay actual start until the whole architecture is fully assembled *)
  88. await(~doWait or finished);
  89. end Start;
  90. begin{ACTIVE}
  91. begin{EXCLUSIVE}
  92. await(proc # nil);
  93. end;
  94. if delayedStart then
  95. context.WaitUntilFinishedAssembly;
  96. end;
  97. if context.res = 0 then
  98. proc;
  99. end;
  100. begin{EXCLUSIVE}
  101. finished := true
  102. end;
  103. finally
  104. begin{EXCLUSIVE}
  105. if ~finished then
  106. error := true;
  107. finished := true;
  108. end;
  109. end;
  110. end Launcher;
  111. procedure GetContext(): Context;
  112. begin
  113. return Objects.ActiveObject()(Launcher).context;
  114. end GetContext;
  115. procedure AllocateOnContext(context: Context;scope: Cell; var c: Cell; tag: address; const name: array of char; isCellnet, isEngine: boolean);
  116. var
  117. a: any;
  118. typeInfo: Modules.TypeDesc;
  119. s, ac: any;
  120. begin
  121. (* allocation of cells must use the tag provided, it contains all internally stored metadata *)
  122. Heaps.NewRec(a, tag, false);
  123. system.get(tag-4,typeInfo);
  124. if EnableTrace then trace(scope, c, typeInfo, name, isCellnet, isEngine); end;
  125. if scope # nil then s := scope.c else s := nil end;
  126. if c # nil then ac := c.c else ac := nil end;
  127. c := a(Cell);
  128. context.Allocate(s, ac, typeInfo, name, isCellnet, isEngine);
  129. c.c := ac;
  130. if scope = nil then context.topNet := ac; end;
  131. end AllocateOnContext;
  132. procedure Allocate*(scope: Cell; var c: Cell; tag: address; const name: array of char; isCellnet, isEngine: boolean);
  133. begin
  134. AllocateOnContext(GetContext(), scope, c, tag, name, isCellnet, isEngine);
  135. end Allocate;
  136. procedure AddPort*(c: Cell; var p: any; const name: array of char; inout: set; width: longint);
  137. begin
  138. if EnableTrace then trace(c,p,name, inout, width); end;
  139. GetContext().AddPort(c.c, p, name, inout, width);
  140. end AddPort;
  141. procedure AddPortArray*(c: Cell; var ports: any; const name: array of char; inout: set; width: longint; const lens: array of longint);
  142. begin
  143. if EnableTrace then trace(name, inout, width, len(lens)); end;
  144. GetContext().AddPortArray(c.c, ports, name, inout, width, lens);
  145. end AddPortArray;
  146. procedure AddStaticPortArray*(c: Cell; var ports: array of any; const name: array of char; inout: set; width: longint);
  147. begin
  148. if EnableTrace then trace(name, inout, width, len(ports)); end;
  149. GetContext().AddStaticPortArray(c.c, ports, name, inout, width);
  150. end AddStaticPortArray;
  151. procedure AddPortIntegerProperty*(p: any; const name: array of char; value: longint);
  152. begin
  153. if EnableTrace then trace(p, name, value); end;
  154. GetContext().AddPortIntegerProperty(p,name,value);
  155. end AddPortIntegerProperty;
  156. procedure AddFlagProperty*(c: Cell; const name: array of char);
  157. begin
  158. if EnableTrace then trace(c, name); end;
  159. GetContext().AddFlagProperty(c.c, name);
  160. end AddFlagProperty;
  161. procedure AddStringProperty*(c: Cell; const name: array of char; var newValue: array of char; const value: array of char);
  162. begin
  163. if EnableTrace then trace(c, name, newValue, value); end;
  164. copy(value, newValue);
  165. GetContext().AddStringProperty(c.c, name, value);
  166. end AddStringProperty;
  167. procedure AddIntegerProperty*(c: Cell; const name: array of char; var newValue: longint; value: longint);
  168. begin
  169. if EnableTrace then trace(c, name, newValue, value); end;
  170. newValue := value;
  171. GetContext().AddIntegerProperty(c.c, name, value);
  172. end AddIntegerProperty;
  173. procedure AddBooleanProperty*(c: Cell; const name: array of char; var newValue: boolean; value: boolean);
  174. begin
  175. if EnableTrace then trace(c, name, newValue, value); end;
  176. newValue := value;
  177. GetContext().AddBooleanProperty(c.c, name, value);
  178. end AddBooleanProperty;
  179. procedure AddRealProperty*(c: Cell; const name: array of char; var newValue: longreal; value: longreal);
  180. begin
  181. if EnableTrace then trace(c, name, newValue, value, entier(value)); end;
  182. newValue := value;
  183. GetContext().AddRealProperty(c.c, name, value);
  184. end AddRealProperty;
  185. procedure AddSetProperty*(c: Cell; const name: array of char; var newValue: set; value: set);
  186. begin
  187. if EnableTrace then trace(c, name, newValue, value); end;
  188. newValue := value;
  189. GetContext().AddSetProperty(c.c, name, value);
  190. end AddSetProperty;
  191. procedure FinishedProperties*(c: Cell);
  192. begin
  193. if EnableTrace then trace(c); end;
  194. GetContext().FinishedProperties(c.c);
  195. end FinishedProperties;
  196. procedure Connect*(outPort, inPort: any; depth: longint);
  197. begin
  198. if EnableTrace then trace(outPort, inPort, outPort, inPort, depth); end;
  199. GetContext().Connect(outPort, inPort, depth);
  200. end Connect;
  201. procedure Delegate*(netPort: any; cellPort: any);
  202. begin
  203. if EnableTrace then trace(netPort, cellPort); end;
  204. GetContext().Delegate(netPort, cellPort);
  205. end Delegate;
  206. procedure Start*(c: Cell; proc: procedure{DELEGATE});
  207. begin
  208. if EnableTrace then trace(c, proc); end;
  209. GetContext().Start(c.c, proc);
  210. end Start;
  211. procedure Send*(p: any; value: longint);
  212. begin
  213. GetContext().Send(p, value);
  214. end Send;
  215. procedure BulkSend*(p: any; const value: array of system.byte);
  216. begin
  217. GetContext().BulkSend(p,value);
  218. end BulkSend;
  219. procedure SendNonBlocking*(p: any; value: longint): boolean;
  220. begin
  221. return GetContext().SendNonBlocking(p, value);
  222. end SendNonBlocking;
  223. procedure Receive*(p: any; var value: longint);
  224. begin
  225. GetContext().Receive(p, value);
  226. end Receive;
  227. procedure BulkReceive*(p: any; var value: array of system.byte);
  228. begin
  229. GetContext().BulkReceive(p,value);
  230. end BulkReceive;
  231. procedure ReceiveNonBlocking*(p: any; var value: longint): boolean;
  232. begin
  233. return GetContext().ReceiveNonBlocking(p, value);
  234. end ReceiveNonBlocking;
  235. type
  236. Module = pointer to record
  237. next: Module;
  238. checked, imports: boolean;
  239. m: Modules.Module
  240. end;
  241. procedure Find(root: Module; m: Modules.Module): Module;
  242. begin
  243. while (root # nil) & (root.m # m) do root := root.next end;
  244. return root
  245. end Find;
  246. procedure Imports(root, m: Module; const name: array of char): boolean;
  247. var i: longint;
  248. begin
  249. if ~m.checked then
  250. if m.m.name # name then
  251. i := 0;
  252. while i # len(m.m.module) do
  253. if (m.m.module[i].name = name) or Imports(root, Find(root, m.m.module[i]), name) then
  254. m.imports := true; i := len(m.m.module)
  255. else
  256. inc(i)
  257. end
  258. end
  259. else
  260. m.imports := true
  261. end;
  262. m.checked := true
  263. end;
  264. return m.imports
  265. end Imports;
  266. (*! caution: this is not thread safe -- must be moved to Modules.Mod *)
  267. procedure CopyModules(): Module;
  268. var firstm, lastm, c: Module; m: Modules.Module;
  269. begin
  270. new(firstm); firstm.next := nil; lastm := firstm;
  271. m := Modules.root;
  272. while m # nil do
  273. new(c); c.checked := false; c.imports := false; c.m := m;
  274. c.next := nil; lastm.next := c; lastm := c;
  275. m := m.next
  276. end;
  277. return firstm.next
  278. end CopyModules;
  279. procedure FreeDownTo(const modulename: array of char): longint;
  280. var
  281. root, m: Module; res: longint;
  282. nbrOfUnloadedModules : longint;
  283. msg: array 32 of char;
  284. begin
  285. nbrOfUnloadedModules := 0;
  286. root := CopyModules();
  287. m := root;
  288. while m # nil do
  289. if Imports(root, m, modulename) then
  290. Modules.FreeModule(m.m.name, res, msg);
  291. if res # 0 then
  292. (*context.error.String(msg);*)
  293. else
  294. inc(nbrOfUnloadedModules);
  295. end
  296. end;
  297. m := m.next
  298. end;
  299. return nbrOfUnloadedModules;
  300. end FreeDownTo;
  301. (**
  302. Execute ActiveCells CELLNET code
  303. cellNet: name of a CELLNET type in the format "ModuleName.TypeName", e.g. TestActiveCells.TestCellNet
  304. context: runtime context used for executing the ActiveCells code
  305. diagnostics: interface for generation of diagnostic messages (see Diagnostics.Mod)
  306. *)
  307. procedure Execute*(const cellNet: array of char; context: Context; diagnostics: Diagnostics.Diagnostics);
  308. type
  309. StartProc = procedure{DELEGATE}();
  310. Starter = object
  311. var
  312. p: StartProc;
  313. c: Cell;
  314. procedure & InitStarter(proc: address; scope: Cell);
  315. var startProcDesc: record proc: address; selfParam: address; end;
  316. begin
  317. startProcDesc.proc := proc;
  318. startProcDesc.selfParam := scope;
  319. system.move(address of startProcDesc, address of p, 2 * size of address);
  320. c := scope;
  321. end InitStarter;
  322. procedure P;
  323. begin
  324. Start(c, p)
  325. end P;
  326. end Starter;
  327. var
  328. moduleName, typeName, name: array 256 of char;
  329. m: Modules.Module;
  330. typeInfo: Modules.TypeDesc;
  331. i, res: longint;
  332. str: array 256 of char;
  333. scope: Cell;
  334. unloaded: longint;
  335. starter: Starter;
  336. launcher: Launcher;
  337. offset: size;
  338. pc: address;
  339. begin
  340. assert(context # nil);
  341. context.topNet := nil;
  342. i := Strings.IndexOfByte2(".",cellNet);
  343. if i = -1 then
  344. diagnostics.Error("",Diagnostics.Invalid,Diagnostics.Invalid, "CELLNET type name is malformed");
  345. return;
  346. end;
  347. Strings.Copy(cellNet,0,i,moduleName);
  348. Strings.Copy(cellNet,i+1,Strings.Length(cellNet)-Strings.Length(moduleName),typeName);
  349. unloaded := FreeDownTo(moduleName);
  350. if unloaded > 0 then
  351. (*param.ctx.Information("", Diagnostics.Invalid,Diagnostics.Invalid,"unloaded " & unloaded & " modules")*)
  352. end;
  353. m := Modules.ThisModule(moduleName,res,str);
  354. if m = nil then
  355. Strings.Concat('failed to load module "',moduleName,str);
  356. Strings.Concat(str,'"',str);
  357. diagnostics.Error("",Diagnostics.Invalid,-1,str);
  358. return;
  359. end;
  360. typeInfo := Modules.ThisType(m,typeName);
  361. if typeInfo = nil then
  362. Strings.Concat('failed to find CELLNET type "',cellNet,str);
  363. Strings.Concat(str,'" in module "',str);
  364. Strings.Concat(str,moduleName,str);
  365. Strings.Concat(str,'"',str);
  366. diagnostics.Error("",Diagnostics.Invalid,-1,str);
  367. return;
  368. end;
  369. copy(typeName, name);
  370. Strings.Append(name, ".@Body");
  371. trace(name);
  372. trace(m.refs);
  373. offset := Reflection.FindByName(m.refs, 0, name, true);
  374. if offset # 0 then
  375. if Reflection.GetChar(m.refs,offset) = Reflection.sfProcedure then
  376. Reflection.SkipSize(offset);
  377. Reflection.SkipString(m.refs,offset);
  378. pc := Reflection.GetAddress(m.refs, offset);
  379. trace(pc);
  380. (*assert(len(typeInfo.procedures) = 1);
  381. assert(typeInfo.procedures[0].name^ = "@Body");
  382. *)
  383. (* allocate the top level cellnet *)
  384. AllocateOnContext(context, nil,scope,typeInfo.tag,typeName,true,false);
  385. assert(scope # nil);
  386. assert(scope.c # nil);
  387. new(starter, pc, scope);
  388. end;
  389. new(launcher, context);
  390. launcher.Start(starter.P, true);
  391. context.FinishedAssembly;
  392. assert(~launcher.error);
  393. else
  394. Reflection.Report(Commands.GetContext().out, m.refs, offset);
  395. end;
  396. end Execute;
  397. type bytearray= array of system.byte;
  398. operator "<<"* (p: port out; const a: bytearray);
  399. begin
  400. if EnableTrace then trace('bulk send',len(a)); end;
  401. BulkSend(system.val(any,p),a);
  402. end "<<";
  403. operator "<<"* (var a: bytearray; p: port in);
  404. begin
  405. if EnableTrace then trace('bulk rec',len(a));end;
  406. BulkReceive(system.val(any,p),a);
  407. end "<<";
  408. (*The extra functions for longint and real were introduced because right now primitive types cannot be passed as byte arrays*)
  409. type longintSpecial= longint;
  410. operator "<<"* (p: port out; a: longintSpecial);
  411. begin
  412. if EnableTrace then trace('longint send');end;
  413. BulkSend(system.val(any,p),a);
  414. end "<<";
  415. operator "<<"* (var a: longintSpecial; p: port in);
  416. begin
  417. if EnableTrace then trace('longint rec');end;
  418. BulkReceive(system.val(any,p),a);
  419. end "<<";
  420. type realSpecial= real;
  421. operator "<<"* (p: port out; a: realSpecial);
  422. begin
  423. if EnableTrace then trace('real send');end;
  424. BulkSend(system.val(any,p),a);
  425. end "<<";
  426. operator "<<"* (var a:realSpecial; p: port in);
  427. begin
  428. if EnableTrace then trace('real rec');end;
  429. BulkReceive(system.val(any,p),a);
  430. end "<<";
  431. type Pin = port in; Pout = port out;
  432. operator ">>"* (pout: Pout; pin: Pin);
  433. begin
  434. Connect(system.val(any, pout), system.val(any, pin), 0);
  435. end ">>";
  436. operator ">>"* (cellPort: Pout; netPort: Pout);
  437. begin
  438. Delegate(system.val(any, netPort), system.val(any, cellPort));
  439. end ">>";
  440. operator ">>"* (netPort: Pin; cellPort: Pin);
  441. begin
  442. Delegate(system.val(any, netPort), system.val(any, cellPort));
  443. end ">>";
  444. end ActiveCellsRuntime.
  445. SystemTools.FreeDownTo FoxSemanticChecker ~