ActiveCellsRuntime.mod 14 KB

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