ActiveCellsRuntime.mod 12 KB

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