ctask.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 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 cancontinue(acandidate,false,m2) then
  158. break;
  159. end;
  160. itm:=itm.Next;
  161. end;
  162. acandidate:=nil;
  163. end;
  164. var
  165. m2 : tmodule;
  166. begin
  167. firstwaiting:=nil;
  168. // We do not need to consider the program as long as there are units that need to be treated.
  169. if (m.is_initial and not m.is_unit) and (list.count>1) then
  170. exit(False);
  171. case m.state of
  172. ms_unknown : cancontinue:=true;
  173. ms_registered : cancontinue:=true;
  174. ms_compile : cancontinue:=true;
  175. ms_compiling_wait : cancontinue:=m.usedunitsloaded(true,firstwaiting);
  176. ms_compiling_waitintf : cancontinue:=m.usedunitsloaded(true,firstwaiting);
  177. ms_compiling_waitimpl : cancontinue:=m.usedunitsloaded(false,firstwaiting);
  178. ms_compiling_waitfinish : cancontinue:=m.nowaitingforunits(firstwaiting);
  179. ms_compiled_waitcrc : cancontinue:=m.usedunitsfinalcrc(firstwaiting);
  180. ms_compiled : cancontinue:=true;
  181. ms_processed : cancontinue:=true;
  182. ms_moduleerror : cancontinue:=true;
  183. else
  184. InternalError(2024011802);
  185. end;
  186. if (not cancontinue) and checksub then
  187. begin
  188. checkused(m2);
  189. if m2<>nil then
  190. firstwaiting:=m2;
  191. end;
  192. {$IFDEF DEBUG_CTASK}
  193. Write(m.ToString,' state: ',m.state,', can continue: ',Result);
  194. if result then
  195. Writeln
  196. else
  197. begin
  198. Write(' (First waiting: ');
  199. If Assigned(FirstWaiting) then
  200. Writeln(FirstWaiting.ToString,' )')
  201. else
  202. Writeln('<none>)');
  203. end;
  204. {$ENDIF}
  205. end;
  206. function ttask_handler.cancontinue(t : ttask_list; out firstwaiting : tmodule): boolean;
  207. begin
  208. Result:=cancontinue(t.module,true,firstwaiting);
  209. end;
  210. function ttask_handler.continue(t : ttask_list) : Boolean;
  211. var
  212. m : tmodule;
  213. orgname : shortstring;
  214. begin
  215. m:=t.module;
  216. orgname:=m.modulename^;
  217. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' Continues. State: ',m.state);{$ENDIF}
  218. if Assigned(t.state) then
  219. t.RestoreState;
  220. case m.state of
  221. ms_registered : parser.compile_module(m);
  222. ms_compile : parser.compile_module(m);
  223. ms_compiled : if (not m.is_initial) or m.is_unit then
  224. (m as tppumodule).post_load_or_compile(m,m.compilecount>1);
  225. ms_compiling_wait : pmodules.proc_program_declarations(m,m.islibrary);
  226. ms_compiling_waitintf : pmodules.parse_unit_interface_declarations(m);
  227. ms_compiling_waitimpl : pmodules.proc_unit_implementation(m);
  228. ms_compiling_waitfinish : pmodules.finish_compile_unit(m);
  229. ms_compiled_waitcrc : pmodules.finish_unit(m);
  230. ms_processed : ;
  231. else
  232. InternalError(2024011801);
  233. end;
  234. if m.state=ms_compiled then
  235. begin
  236. parsing_done(m);
  237. if m.is_initial and not m.is_unit then
  238. m.state:=ms_processed;
  239. end;
  240. Result:=m.state=ms_processed;
  241. {$IFDEF DEBUG_CTASK}
  242. Write(m.ToString,' done: ',Result);
  243. if Result then
  244. Writeln
  245. else
  246. Writeln(', state is now: ',m.state);
  247. {$ENDIF}
  248. if not result then
  249. // Not done, save state
  250. t.SaveState;
  251. {
  252. the name can change as a result of processing, e.g. PROGRAM -> TB0406
  253. Normally only for the initial module, but we'll do a generic check.
  254. }
  255. if m.modulename^<>orgname then
  256. rebuild_hash;
  257. end;
  258. procedure ttask_handler.rebuild_hash;
  259. var
  260. t : ttask_list;
  261. begin
  262. Hash.Clear;
  263. t:=list.firsttask;
  264. While assigned(t) do
  265. begin
  266. Hash.Add(t.module.modulename^,t);
  267. t:=t.nexttask;
  268. end;
  269. end;
  270. procedure ttask_handler.processqueue;
  271. var
  272. t,t2 : ttask_list;
  273. process: boolean;
  274. dummy,firstwaiting : tmodule;
  275. begin
  276. t:=list.firsttask;
  277. process:=true;
  278. While t<>nil do
  279. begin
  280. process:=cancontinue(t,firstwaiting);
  281. {$IFDEF Debug_WaitCRC}
  282. if firstwaiting<>nil then
  283. writeln('ttask_handler.processqueue "',t.module.realmodulename^,'" state=',t.module.state,' waitingfor="',firstwaiting.realmodulename^,'",',firstwaiting.state)
  284. else
  285. writeln('ttask_handler.processqueue "',t.module.realmodulename^,'" state=',t.module.state,' waitingfor=nil');
  286. {$ENDIF}
  287. if process then
  288. begin
  289. if continue(t) then
  290. begin
  291. {$IFDEF DEBUG_CTASK}Writeln(t.module.ToString,' is finished, removing from task list');{$ENDIF}
  292. hash.Remove(t.module);
  293. list.Remove(t);
  294. end;
  295. // first search for any module that is ready to be written as ppu
  296. t2:=list.firsttask;
  297. while (t2<>nil)
  298. and ((t2.module.state<>ms_compiled_waitcrc)
  299. or not t2.module.usedunitsfinalcrc(firstwaiting)) do
  300. t2:=t2.nexttask;
  301. if t2<>nil then
  302. begin
  303. t:=t2;
  304. {$IFDEF Debug_WaitCRC}
  305. writeln('ttask_handler.processqueue FOUND CRC READY ',t.module.realmodulename^,' state=',t.module.state);
  306. {$ENDIF}
  307. end
  308. else
  309. begin
  310. // maybe the strategy can be improved.
  311. t:=list.firsttask;
  312. end;
  313. end
  314. else if assigned(firstwaiting) and cancontinue(firstwaiting,true, dummy) then
  315. begin
  316. t2:=findtask(firstwaiting);
  317. if t2=nil then
  318. t2:=t.nexttask;
  319. t:=t2;
  320. end
  321. else
  322. begin
  323. t:=t.nexttask;
  324. end;
  325. if t=nil then
  326. begin
  327. t:=list.firsttask;
  328. if t<>nil then
  329. // no progress possible
  330. InternalError(2025090301);
  331. end;
  332. end;
  333. end;
  334. procedure ttask_handler.addmodule(m: tmodule);
  335. var
  336. n : TSymStr;
  337. e, t : ttask_list;
  338. begin
  339. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' added to task scheduler. State: ',m.state);{$ENDIF}
  340. n:=m.modulename^;
  341. e:=ttask_list(Hash.Find(n));
  342. if e=nil then
  343. begin
  344. // Clear reset flag.
  345. // This can happen when during load, reset is done and unit is added to task list.
  346. m.is_reset:=false;
  347. t:=ttask_list.create(m);
  348. list.insert(t);
  349. hash.Add(n,t);
  350. if list.count=1 then
  351. main:=m;
  352. end
  353. else
  354. begin
  355. // We have a task, if it was reset, then clear the state and move the task to the start.
  356. if m.is_reset then
  357. begin
  358. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' was reset, resetting flag. State: ',m.state);{$ENDIF}
  359. m.is_reset:=false;
  360. t:=findtask(m);
  361. if assigned(t) then
  362. begin
  363. t.DiscardState;
  364. list.Remove(t);
  365. list.insertbefore(t,list.First);
  366. end;
  367. end;
  368. end;
  369. end;
  370. end.