2
0

ActiveCellsRuntime.mod 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. (** Active Cells Runtime Base Code for Variations of ActiveCellsRuntime Implementations
  2. Felix Friedrich, ETH Zürich, 2015
  3. *)
  4. module ActiveCellsRuntime;
  5. import
  6. system, Heaps, Modules, Diagnostics, Strings, Objects;
  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. procedure Allocate*(scope: any; var c: any; t: Modules.TypeDesc; const name: array of char; isCellNet, isEngine: boolean);
  17. end Allocate;
  18. procedure AddPort*(c: any; var p: any; const name: array of char; inout: set; width: longint);
  19. end AddPort;
  20. procedure AddPortArray*(c: any; var ports: any; const name: array of char; inout: set; width: longint; const lens: array of longint);
  21. end AddPortArray;
  22. procedure AddStaticPortArray*(c: any; var ports: array of any; const name: array of char; inout: set; width: longint);
  23. end AddStaticPortArray;
  24. procedure AddPortIntegerProperty*(p: any; const name: array of char; value: longint);
  25. end AddPortIntegerProperty;
  26. procedure AddFlagProperty*(c: any; const name: array of char);
  27. end AddFlagProperty;
  28. procedure AddStringProperty*(c: any; const name: array of char; const value: array of char);
  29. end AddStringProperty;
  30. procedure AddIntegerProperty*(c: any; const name: array of char; value: longint);
  31. end AddIntegerProperty;
  32. procedure FinishedProperties*(c: any);
  33. end FinishedProperties;
  34. procedure Connect*(outPort, inPort: any; depth: longint);
  35. end Connect;
  36. procedure Delegate*(netPort: any; cellPort: any);
  37. end Delegate;
  38. procedure Start*(c: any; proc: procedure{DELEGATE});
  39. end Start;
  40. procedure Send*(p: any; value: longint);
  41. end Send;
  42. procedure Receive*(p: any; var value: longint);
  43. end Receive;
  44. end Context;
  45. Launcher* = object
  46. var
  47. proc: procedure {DELEGATE};
  48. context: Context;
  49. finished: boolean;
  50. procedure & Init*(context: Context);
  51. begin
  52. self.context := context;
  53. proc := nil;
  54. finished := false;
  55. end Init;
  56. procedure Start*(p: procedure{DELEGATE}; doWait: boolean);
  57. begin{EXCLUSIVE}
  58. proc := p;
  59. await(~doWait or finished);
  60. end Start;
  61. begin{ACTIVE,EXCLUSIVE}
  62. await(proc # nil);
  63. proc;
  64. finished := true;
  65. end Launcher;
  66. procedure GetContext(): Context;
  67. begin
  68. return Objects.ActiveObject()(Launcher).context;
  69. end GetContext;
  70. procedure AllocateOnContext(context: Context;scope: Cell; var c: Cell; tag: address; const name: array of char; isCellnet, isEngine: boolean);
  71. var
  72. a: any;
  73. typeInfo: Modules.TypeDesc;
  74. s, ac: any;
  75. begin
  76. (* allocation of cells must use the tag provided, it contains all internally stored metadata *)
  77. Heaps.NewRec(a, tag, false);
  78. system.get(tag-4,typeInfo);
  79. if EnableTrace then trace(scope, c, typeInfo, name, isCellnet, isEngine); end;
  80. if scope # nil then s := scope.c else s := nil end;
  81. if c # nil then ac := c.c else ac := nil end;
  82. c := a(Cell);
  83. context.Allocate(s, ac, typeInfo, name, isCellnet, isEngine);
  84. c.c := ac;
  85. end AllocateOnContext;
  86. procedure Allocate*(scope: Cell; var c: Cell; tag: address; const name: array of char; isCellnet, isEngine: boolean);
  87. begin
  88. AllocateOnContext(GetContext(), scope, c, tag, name, isCellnet, isEngine);
  89. end Allocate;
  90. procedure AddPort*(c: Cell; var p: any; const name: array of char; inout: set; width: longint);
  91. begin
  92. if EnableTrace then trace(c,p,name, inout, width); end;
  93. GetContext().AddPort(c.c, p, name, inout, width);
  94. end AddPort;
  95. procedure AddPortArray*(c: Cell; var ports: any; const name: array of char; inout: set; width: longint; const lens: array of longint);
  96. begin
  97. if EnableTrace then trace(name, inout, width, len(lens)); end;
  98. GetContext().AddPortArray(c.c, ports, name, inout, width, lens);
  99. end AddPortArray;
  100. procedure AddStaticPortArray*(c: Cell; var ports: array of any; const name: array of char; inout: set; width: longint);
  101. begin
  102. if EnableTrace then trace(name, inout, width, len(ports)); end;
  103. GetContext().AddStaticPortArray(c.c, ports, name, inout, width);
  104. end AddStaticPortArray;
  105. procedure AddPortIntegerProperty*(p: any; const name: array of char; value: longint);
  106. begin
  107. if EnableTrace then trace(p, name, value); end;
  108. GetContext().AddPortIntegerProperty(p,name,value);
  109. end AddPortIntegerProperty;
  110. procedure AddFlagProperty*(c: Cell; const name: array of char);
  111. begin
  112. if EnableTrace then trace(c, name); end;
  113. GetContext().AddFlagProperty(c.c, name);
  114. end AddFlagProperty;
  115. procedure AddStringProperty*(c: Cell; const name: array of char; var newValue: array of char; const value: array of char);
  116. begin
  117. if EnableTrace then trace(c, name, newValue, value); end;
  118. copy(value, newValue);
  119. GetContext().AddStringProperty(c.c, name, value);
  120. end AddStringProperty;
  121. procedure AddIntegerProperty*(c: Cell; const name: array of char; var newValue: longint; value: longint);
  122. begin
  123. if EnableTrace then trace(c, name, newValue, value); end;
  124. newValue := value;
  125. GetContext().AddIntegerProperty(c.c, name, value);
  126. end AddIntegerProperty;
  127. procedure FinishedProperties*(c: Cell);
  128. begin
  129. if EnableTrace then trace(c); end;
  130. GetContext().FinishedProperties(c.c);
  131. end FinishedProperties;
  132. procedure Connect*(outPort, inPort: any; depth: longint);
  133. begin
  134. if EnableTrace then trace(outPort, inPort, outPort, inPort, depth); end;
  135. GetContext().Connect(outPort, inPort, depth);
  136. end Connect;
  137. procedure Delegate*(netPort: any; cellPort: any);
  138. begin
  139. if EnableTrace then trace(netPort, cellPort); end;
  140. GetContext().Delegate(netPort, cellPort);
  141. end Delegate;
  142. procedure Start*(c: Cell; proc: procedure{DELEGATE});
  143. begin
  144. if EnableTrace then trace(c, proc); end;
  145. GetContext().Start(c.c, proc);
  146. end Start;
  147. procedure Send*(p: any; value: longint);
  148. begin
  149. GetContext().Send(p, value);
  150. end Send;
  151. procedure Receive*(p: any; var value: longint);
  152. begin
  153. GetContext().Receive(p, value);
  154. end Receive;
  155. type
  156. Module = pointer to record
  157. next: Module;
  158. checked, imports: boolean;
  159. m: Modules.Module
  160. end;
  161. procedure Find(root: Module; m: Modules.Module): Module;
  162. begin
  163. while (root # nil) & (root.m # m) do root := root.next end;
  164. return root
  165. end Find;
  166. procedure Imports(root, m: Module; const name: array of char): boolean;
  167. var i: longint;
  168. begin
  169. if ~m.checked then
  170. if m.m.name # name then
  171. i := 0;
  172. while i # len(m.m.module) do
  173. if (m.m.module[i].name = name) or Imports(root, Find(root, m.m.module[i]), name) then
  174. m.imports := true; i := len(m.m.module)
  175. else
  176. inc(i)
  177. end
  178. end
  179. else
  180. m.imports := true
  181. end;
  182. m.checked := true
  183. end;
  184. return m.imports
  185. end Imports;
  186. (*! caution: this is not thread safe -- must be moved to Modules.Mod *)
  187. procedure CopyModules(): Module;
  188. var firstm, lastm, c: Module; m: Modules.Module;
  189. begin
  190. new(firstm); firstm.next := nil; lastm := firstm;
  191. m := Modules.root;
  192. while m # nil do
  193. new(c); c.checked := false; c.imports := false; c.m := m;
  194. c.next := nil; lastm.next := c; lastm := c;
  195. m := m.next
  196. end;
  197. return firstm.next
  198. end CopyModules;
  199. procedure FreeDownTo(const modulename: array of char): longint;
  200. var
  201. root, m: Module; res: longint;
  202. nbrOfUnloadedModules : longint;
  203. msg: array 32 of char;
  204. begin
  205. nbrOfUnloadedModules := 0;
  206. root := CopyModules();
  207. m := root;
  208. while m # nil do
  209. if Imports(root, m, modulename) then
  210. Modules.FreeModule(m.m.name, res, msg);
  211. if res # 0 then
  212. (*context.error.String(msg);*)
  213. else
  214. inc(nbrOfUnloadedModules);
  215. end
  216. end;
  217. m := m.next
  218. end;
  219. return nbrOfUnloadedModules;
  220. end FreeDownTo;
  221. procedure Execute*( const cellNet: array of char; context: Context; diagnostics: Diagnostics.Diagnostics);
  222. type
  223. StartProc = procedure{DELEGATE}();
  224. Starter = object
  225. var
  226. p: StartProc;
  227. c: Cell;
  228. procedure & InitStarter(proc: address; scope: Cell);
  229. var startProcDesc: record proc: address; selfParam: address; end;
  230. begin
  231. startProcDesc.proc := proc;
  232. startProcDesc.selfParam := scope;
  233. system.move(address of startProcDesc, address of p, 2 * size of address);
  234. c := scope;
  235. end InitStarter;
  236. procedure P;
  237. begin
  238. Start(c, p)
  239. end P;
  240. end Starter
  241. var
  242. moduleName, typeName: array 256 of char;
  243. m: Modules.Module;
  244. typeInfo: Modules.TypeDesc;
  245. i, res: longint;
  246. str: array 256 of char;
  247. scope: Cell;
  248. unloaded: longint;
  249. starter: Starter;
  250. launcher: Launcher;
  251. begin
  252. i := Strings.IndexOfByte2(".",cellNet);
  253. if i = -1 then
  254. diagnostics.Error("",Diagnostics.Invalid,Diagnostics.Invalid, "cellNet malformed");
  255. end;
  256. Strings.Copy(cellNet,0,i,moduleName);
  257. Strings.Copy(cellNet,i+1,Strings.Length(cellNet)-Strings.Length(moduleName),typeName);
  258. unloaded := FreeDownTo(moduleName);
  259. if unloaded > 0 then
  260. (*param.ctx.Information("", Diagnostics.Invalid,Diagnostics.Invalid,"unloaded " & unloaded & " modules")*)
  261. end;
  262. m := Modules.ThisModule(moduleName,res,str);
  263. if m = nil then
  264. (*
  265. param.ctx.Error("",Diagnostics.Invalid,HdlBackend.ErrNotFound,'failed to load module "' & moduleName & '"');
  266. *)
  267. end;
  268. typeInfo := Modules.ThisType(m,typeName);
  269. if typeInfo = nil then
  270. (*
  271. param.ctx.Error("",Diagnostics.Invalid,HdlBackend.ErrNotFound,'failed to find cellnet type "' & param.architectureName & '" in module "' & moduleName & '"');
  272. return nil;
  273. *)
  274. end;
  275. assert(len(typeInfo.procedures) = 1);
  276. assert(typeInfo.procedures[0].name^ = "@Body");
  277. (* allocate the top level cellnet *)
  278. AllocateOnContext(context, nil,scope,typeInfo.tag,typeName,true,false);
  279. assert(scope # nil);
  280. assert(scope.c # nil);
  281. new(starter, typeInfo.procedures[0].address, scope);
  282. new(launcher, context);
  283. launcher.Start(starter.P, true);
  284. end Execute;
  285. end ActiveCellsRuntime.