2
0

ctask.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. {
  2. Copyright (c) 2024- by Michael Van Canneyt
  3. This unit handles the compiler tasks.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ctask;
  18. {$mode ObjFPC}
  19. { $DEFINE DEBUG_CTASK}
  20. interface
  21. uses
  22. fmodule, cclasses, globstat;
  23. type
  24. { ttask_list }
  25. ttask_list = class(tlinkedlistitem)
  26. module : tmodule;
  27. state : tglobalstate;
  28. constructor create(_m : tmodule);
  29. destructor destroy; override;
  30. procedure SaveState;
  31. Procedure RestoreState;
  32. procedure DiscardState;
  33. function nexttask : ttask_list; inline;
  34. end;
  35. ttasklinkedlist = class(tlinkedlist)
  36. function firsttask : ttask_list; inline;
  37. end;
  38. { ttask_handler }
  39. ttask_handler = class
  40. private
  41. list : ttasklinkedlist;
  42. hash : TFPHashList;
  43. main : tmodule;
  44. procedure rebuild_hash;
  45. public
  46. constructor create;
  47. destructor destroy; override;
  48. // Find the task for module m
  49. function findtask(m : tmodule) : ttask_list;
  50. // Can we continue processing this module ? If not, firstwaiting contains first module that m is waiting for.
  51. function cancontinue(m : tmodule; checksub : boolean; out firstwaiting: tmodule): boolean;
  52. // Overload of cancontinue, based on task.
  53. function cancontinue(t: ttask_list; out firstwaiting: tmodule): boolean; inline;
  54. // Continue processing this module. Return true if the module is done and can be removed.
  55. function continue(t : ttask_list): Boolean;
  56. // process the queue. Note that while processing the queue, elements will be added.
  57. procedure processqueue;
  58. // add a module to the queue. If a module is already in the queue, we do not add it again.
  59. procedure addmodule(m : tmodule);
  60. end;
  61. var
  62. task_handler : TTask_handler;
  63. procedure InitTaskHandler;
  64. procedure DoneTaskHandler;
  65. implementation
  66. uses verbose, fppu, finput, globtype, sysutils, scanner, parser, pmodules;
  67. procedure InitTaskHandler;
  68. begin
  69. task_handler:=ttask_handler.create;
  70. end;
  71. procedure DoneTaskHandler;
  72. begin
  73. freeandnil(task_handler);
  74. end;
  75. { ttasklinkedlist }
  76. function ttasklinkedlist.firsttask: ttask_list;
  77. begin
  78. Result:=ttask_list(first);
  79. end;
  80. { ttask_list }
  81. constructor ttask_list.create(_m: tmodule);
  82. begin
  83. inherited create;
  84. module:=_m;
  85. state:=nil;
  86. end;
  87. destructor ttask_list.destroy;
  88. begin
  89. DiscardState;
  90. Inherited;
  91. end;
  92. procedure ttask_list.DiscardState;
  93. begin
  94. FreeAndNil(state);
  95. end;
  96. function ttask_list.nexttask: ttask_list;
  97. begin
  98. Result:=ttask_list(next);
  99. end;
  100. procedure ttask_list.SaveState;
  101. begin
  102. if State=Nil then
  103. State:=tglobalstate.Create(true)
  104. else
  105. State.save(true);
  106. end;
  107. procedure ttask_list.RestoreState;
  108. begin
  109. if not module.is_reset then
  110. state.restore(true);
  111. if assigned(current_scanner) and assigned(current_scanner.inputfile) then
  112. if current_scanner.inputfile.closed then
  113. begin
  114. current_scanner.tempopeninputfile;
  115. current_scanner.gettokenpos;
  116. end;
  117. end;
  118. { ttask_handler }
  119. constructor ttask_handler.create;
  120. begin
  121. list:=ttasklinkedlist.Create;
  122. hash:=TFPHashList.Create;
  123. end;
  124. destructor ttask_handler.destroy;
  125. begin
  126. hash.free;
  127. List.Clear;
  128. FreeAndNil(list);
  129. inherited destroy;
  130. end;
  131. function ttask_handler.findtask(m: tmodule): ttask_list;
  132. begin
  133. result:=list.FirstTask;
  134. while result<>nil do
  135. begin
  136. if result.module=m then
  137. exit;
  138. result:=result.nexttask;
  139. end;
  140. {$IFDEF DEBUG_CTASK}Writeln('No task found for '+m.ToString);{$ENDIF}
  141. end;
  142. function ttask_handler.cancontinue(m: tmodule; checksub : boolean; out firstwaiting: tmodule): boolean;
  143. procedure CheckUsed(out acandidate : tmodule);
  144. var
  145. itm : TLinkedListItem;
  146. iscandidate : boolean;
  147. m2 : tmodule;
  148. begin
  149. acandidate:=nil;
  150. itm:=m.used_units.First;
  151. while (acandidate=Nil) and assigned(itm) do
  152. begin
  153. iscandidate:=Not (tused_unit(itm).u.state in [ms_processed,ms_compiled]);
  154. if iscandidate then
  155. begin
  156. acandidate:=tused_unit(itm).u;
  157. if not cancontinue(acandidate,false,m2) then
  158. acandidate:=nil;
  159. end;
  160. itm:=itm.Next;
  161. end;
  162. end;
  163. var
  164. m2 : tmodule;
  165. begin
  166. firstwaiting:=nil;
  167. // We do not need to consider the program as long as there are units that need to be treated.
  168. if (m.is_initial and not m.is_unit) and (list.count>1) then
  169. exit(False);
  170. case m.state of
  171. ms_unknown : cancontinue:=true;
  172. ms_registered : cancontinue:=true;
  173. ms_compile : cancontinue:=true;
  174. ms_compiling_waitimpl : cancontinue:=m.usedunitsloaded(false,firstwaiting);
  175. ms_compiling_waitfinish : cancontinue:=m.nowaitingforunits(firstwaiting);
  176. ms_compiling_waitintf : cancontinue:=m.usedunitsloaded(true,firstwaiting);
  177. ms_compiling_wait : cancontinue:=m.usedunitsloaded(true,firstwaiting);
  178. ms_compiled : cancontinue:=true;
  179. ms_processed : cancontinue:=true;
  180. else
  181. InternalError(2024011802);
  182. end;
  183. if (not cancontinue) and checksub then
  184. begin
  185. checkused(m2);
  186. if m2<>nil then
  187. firstwaiting:=m2;
  188. end;
  189. {$IFDEF DEBUG_CTASK}
  190. Write(m.ToString,' state: ',m.state,', can continue: ',Result);
  191. if result then
  192. Writeln
  193. else
  194. begin
  195. Write(' (First waiting: ');
  196. If Assigned(FirstWaiting) then
  197. Writeln(FirstWaiting.ToString,' )')
  198. else
  199. Writeln('<none>)');
  200. end;
  201. {$ENDIF}
  202. end;
  203. function ttask_handler.cancontinue(t : ttask_list; out firstwaiting : tmodule): boolean;
  204. begin
  205. Result:=cancontinue(t.module,true,firstwaiting);
  206. end;
  207. function ttask_handler.continue(t : ttask_list) : Boolean;
  208. var
  209. m : tmodule;
  210. orgname : shortstring;
  211. begin
  212. m:=t.module;
  213. orgname:=m.modulename^;
  214. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' Continues. State: ',m.state);{$ENDIF}
  215. if Assigned(t.state) then
  216. t.RestoreState;
  217. case m.state of
  218. ms_registered : parser.compile_module(m);
  219. ms_compile : parser.compile_module(m);
  220. ms_compiled : if (not m.is_initial) or m.is_unit then
  221. (m as tppumodule).post_load_or_compile(m,m.compilecount>1);
  222. ms_compiling_waitintf : pmodules.parse_unit_interface_declarations(m);
  223. ms_compiling_waitimpl : pmodules.proc_unit_implementation(m);
  224. ms_compiling_waitfinish : pmodules.finish_unit(m);
  225. ms_compiling_wait : pmodules.proc_program_declarations(m,m.islibrary);
  226. ms_processed : ;
  227. else
  228. InternalError(2024011801);
  229. end;
  230. if m.state=ms_compiled then
  231. begin
  232. parsing_done(m);
  233. if m.is_initial and not m.is_unit then
  234. m.state:=ms_processed;
  235. end;
  236. Result:=m.state=ms_processed;
  237. {$IFDEF DEBUG_CTASK}
  238. Write(m.ToString,' done: ',Result);
  239. if Result then
  240. Writeln
  241. else
  242. Writeln(', state is now: ',m.state);
  243. {$ENDIF}
  244. if not result then
  245. // Not done, save state
  246. t.SaveState;
  247. {
  248. the name can change as a result of processing, e.g. PROGRAM -> TB0406
  249. Normally only for the initial module, but we'll do a generic check.
  250. }
  251. if m.modulename^<>orgname then
  252. rebuild_hash;
  253. end;
  254. procedure ttask_handler.rebuild_hash;
  255. var
  256. t : ttask_list;
  257. begin
  258. Hash.Clear;
  259. t:=list.firsttask;
  260. While assigned(t) do
  261. begin
  262. Hash.Add(t.module.modulename^,t);
  263. t:=t.nexttask;
  264. end;
  265. end;
  266. procedure ttask_handler.processqueue;
  267. var
  268. t,t2 : ttask_list;
  269. process : boolean;
  270. dummy,firstwaiting : tmodule;
  271. begin
  272. t:=list.firsttask;
  273. While t<>nil do
  274. begin
  275. process:=cancontinue(t,firstwaiting);
  276. if process then
  277. begin
  278. if continue(t) then
  279. begin
  280. {$IFDEF DEBUG_CTASK}Writeln(t.module.ToString,' is finished, removing from task list');{$ENDIF}
  281. hash.Remove(t.module);
  282. list.Remove(t);
  283. end;
  284. // maybe the strategy can be improved.
  285. t:=list.firsttask;
  286. end
  287. else if assigned(firstwaiting) and cancontinue(firstwaiting,true, dummy) then
  288. begin
  289. t2:=findtask(firstwaiting);
  290. if t2=nil then
  291. t2:=t.nexttask;
  292. t:=t2;
  293. end
  294. else
  295. begin
  296. t:=t.nexttask;
  297. end;
  298. if t=nil then
  299. t:=list.firsttask;
  300. end;
  301. end;
  302. procedure ttask_handler.addmodule(m: tmodule);
  303. var
  304. n : TSymStr;
  305. e, t : ttask_list;
  306. begin
  307. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' added to task scheduler. State: ',m.state);{$ENDIF}
  308. n:=m.modulename^;
  309. e:=ttask_list(Hash.Find(n));
  310. if e=nil then
  311. begin
  312. // Clear reset flag.
  313. // This can happen when during load, reset is done and unit is added to task list.
  314. m.is_reset:=false;
  315. t:=ttask_list.create(m);
  316. list.insert(t);
  317. hash.Add(n,t);
  318. if list.count=1 then
  319. main:=m;
  320. end
  321. else
  322. begin
  323. // We have a task, if it was reset, then clear the state and move the task to the start.
  324. if m.is_reset then
  325. begin
  326. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' was reset, resetting flag. State: ',m.state);{$ENDIF}
  327. m.is_reset:=false;
  328. t:=findtask(m);
  329. if assigned(t) then
  330. begin
  331. t.DiscardState;
  332. list.Remove(t);
  333. list.insertbefore(t,list.First);
  334. end;
  335. end;
  336. end;
  337. end;
  338. end.