ctask.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. ms_moduleerror : cancontinue:=true;
  181. else
  182. InternalError(2024011802);
  183. end;
  184. if (not cancontinue) and checksub then
  185. begin
  186. checkused(m2);
  187. if m2<>nil then
  188. firstwaiting:=m2;
  189. end;
  190. {$IFDEF DEBUG_CTASK}
  191. Write(m.ToString,' state: ',m.state,', can continue: ',Result);
  192. if result then
  193. Writeln
  194. else
  195. begin
  196. Write(' (First waiting: ');
  197. If Assigned(FirstWaiting) then
  198. Writeln(FirstWaiting.ToString,' )')
  199. else
  200. Writeln('<none>)');
  201. end;
  202. {$ENDIF}
  203. end;
  204. function ttask_handler.cancontinue(t : ttask_list; out firstwaiting : tmodule): boolean;
  205. begin
  206. Result:=cancontinue(t.module,true,firstwaiting);
  207. end;
  208. function ttask_handler.continue(t : ttask_list) : Boolean;
  209. var
  210. m : tmodule;
  211. orgname : shortstring;
  212. begin
  213. m:=t.module;
  214. orgname:=m.modulename^;
  215. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' Continues. State: ',m.state);{$ENDIF}
  216. if Assigned(t.state) then
  217. t.RestoreState;
  218. case m.state of
  219. ms_registered : parser.compile_module(m);
  220. ms_compile : parser.compile_module(m);
  221. ms_compiled : if (not m.is_initial) or m.is_unit then
  222. (m as tppumodule).post_load_or_compile(m,m.compilecount>1);
  223. ms_compiling_waitintf : pmodules.parse_unit_interface_declarations(m);
  224. ms_compiling_waitimpl : pmodules.proc_unit_implementation(m);
  225. ms_compiling_waitfinish : pmodules.finish_unit(m);
  226. ms_compiling_wait : pmodules.proc_program_declarations(m,m.islibrary);
  227. ms_processed : ;
  228. else
  229. InternalError(2024011801);
  230. end;
  231. if m.state=ms_compiled then
  232. begin
  233. parsing_done(m);
  234. if m.is_initial and not m.is_unit then
  235. m.state:=ms_processed;
  236. end;
  237. Result:=m.state=ms_processed;
  238. {$IFDEF DEBUG_CTASK}
  239. Write(m.ToString,' done: ',Result);
  240. if Result then
  241. Writeln
  242. else
  243. Writeln(', state is now: ',m.state);
  244. {$ENDIF}
  245. if not result then
  246. // Not done, save state
  247. t.SaveState;
  248. {
  249. the name can change as a result of processing, e.g. PROGRAM -> TB0406
  250. Normally only for the initial module, but we'll do a generic check.
  251. }
  252. if m.modulename^<>orgname then
  253. rebuild_hash;
  254. end;
  255. procedure ttask_handler.rebuild_hash;
  256. var
  257. t : ttask_list;
  258. begin
  259. Hash.Clear;
  260. t:=list.firsttask;
  261. While assigned(t) do
  262. begin
  263. Hash.Add(t.module.modulename^,t);
  264. t:=t.nexttask;
  265. end;
  266. end;
  267. procedure ttask_handler.processqueue;
  268. var
  269. t,t2 : ttask_list;
  270. process : boolean;
  271. dummy,firstwaiting : tmodule;
  272. begin
  273. t:=list.firsttask;
  274. While t<>nil do
  275. begin
  276. process:=cancontinue(t,firstwaiting);
  277. if process then
  278. begin
  279. if continue(t) then
  280. begin
  281. {$IFDEF DEBUG_CTASK}Writeln(t.module.ToString,' is finished, removing from task list');{$ENDIF}
  282. hash.Remove(t.module);
  283. list.Remove(t);
  284. end;
  285. // maybe the strategy can be improved.
  286. t:=list.firsttask;
  287. end
  288. else if assigned(firstwaiting) and cancontinue(firstwaiting,true, dummy) then
  289. begin
  290. t2:=findtask(firstwaiting);
  291. if t2=nil then
  292. t2:=t.nexttask;
  293. t:=t2;
  294. end
  295. else
  296. begin
  297. t:=t.nexttask;
  298. end;
  299. if t=nil then
  300. t:=list.firsttask;
  301. end;
  302. end;
  303. procedure ttask_handler.addmodule(m: tmodule);
  304. var
  305. n : TSymStr;
  306. e, t : ttask_list;
  307. begin
  308. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' added to task scheduler. State: ',m.state);{$ENDIF}
  309. n:=m.modulename^;
  310. e:=ttask_list(Hash.Find(n));
  311. if e=nil then
  312. begin
  313. // Clear reset flag.
  314. // This can happen when during load, reset is done and unit is added to task list.
  315. m.is_reset:=false;
  316. t:=ttask_list.create(m);
  317. list.insert(t);
  318. hash.Add(n,t);
  319. if list.count=1 then
  320. main:=m;
  321. end
  322. else
  323. begin
  324. // We have a task, if it was reset, then clear the state and move the task to the start.
  325. if m.is_reset then
  326. begin
  327. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' was reset, resetting flag. State: ',m.state);{$ENDIF}
  328. m.is_reset:=false;
  329. t:=findtask(m);
  330. if assigned(t) then
  331. begin
  332. t.DiscardState;
  333. list.Remove(t);
  334. list.insertbefore(t,list.First);
  335. end;
  336. end;
  337. end;
  338. end;
  339. end.