ctask.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. // write current queue and what is waiting for what
  61. procedure write_queue;
  62. end;
  63. var
  64. task_handler : TTask_handler;
  65. procedure InitTaskHandler;
  66. procedure DoneTaskHandler;
  67. implementation
  68. uses verbose, fppu, finput, globtype, sysutils, scanner, parser, pmodules;
  69. procedure InitTaskHandler;
  70. begin
  71. task_handler:=ttask_handler.create;
  72. end;
  73. procedure DoneTaskHandler;
  74. begin
  75. freeandnil(task_handler);
  76. end;
  77. { ttasklinkedlist }
  78. function ttasklinkedlist.firsttask: ttask_list;
  79. begin
  80. Result:=ttask_list(first);
  81. end;
  82. { ttask_list }
  83. constructor ttask_list.create(_m: tmodule);
  84. begin
  85. inherited create;
  86. module:=_m;
  87. state:=nil;
  88. end;
  89. destructor ttask_list.destroy;
  90. begin
  91. DiscardState;
  92. Inherited;
  93. end;
  94. procedure ttask_list.DiscardState;
  95. begin
  96. FreeAndNil(state);
  97. end;
  98. function ttask_list.nexttask: ttask_list;
  99. begin
  100. Result:=ttask_list(next);
  101. end;
  102. procedure ttask_list.SaveState;
  103. begin
  104. if State=Nil then
  105. State:=tglobalstate.Create(true)
  106. else
  107. State.save(true);
  108. end;
  109. procedure ttask_list.RestoreState;
  110. begin
  111. if not module.is_reset then
  112. state.restore(true);
  113. if assigned(current_scanner) and assigned(current_scanner.inputfile) then
  114. if current_scanner.inputfile.closed then
  115. begin
  116. current_scanner.tempopeninputfile;
  117. current_scanner.gettokenpos;
  118. end;
  119. end;
  120. { ttask_handler }
  121. constructor ttask_handler.create;
  122. begin
  123. list:=ttasklinkedlist.Create;
  124. hash:=TFPHashList.Create;
  125. end;
  126. destructor ttask_handler.destroy;
  127. begin
  128. hash.free;
  129. List.Clear;
  130. FreeAndNil(list);
  131. inherited destroy;
  132. end;
  133. function ttask_handler.findtask(m: tmodule): ttask_list;
  134. begin
  135. result:=list.FirstTask;
  136. while result<>nil do
  137. begin
  138. if result.module=m then
  139. exit;
  140. result:=result.nexttask;
  141. end;
  142. {$IFDEF DEBUG_CTASK}Writeln('No task found for '+m.ToString);{$ENDIF}
  143. end;
  144. function ttask_handler.cancontinue(m: tmodule; checksub : boolean; out firstwaiting: tmodule): boolean;
  145. procedure CheckUsed(out acandidate : tmodule);
  146. var
  147. itm : TLinkedListItem;
  148. iscandidate : boolean;
  149. m2 : tmodule;
  150. begin
  151. acandidate:=nil;
  152. itm:=m.used_units.First;
  153. while assigned(itm) do
  154. begin
  155. iscandidate:=Not (tused_unit(itm).u.state in [ms_processed,ms_compiled]);
  156. if iscandidate then
  157. begin
  158. acandidate:=tused_unit(itm).u;
  159. if cancontinue(acandidate,false,m2) then
  160. break;
  161. end;
  162. itm:=itm.Next;
  163. end;
  164. acandidate:=nil;
  165. end;
  166. var
  167. m2 : tmodule;
  168. begin
  169. firstwaiting:=nil;
  170. // We do not need to consider the program as long as there are units that need to be treated.
  171. if (m.is_initial and not m.is_unit) and (list.count>1) then
  172. exit(False);
  173. case m.state of
  174. ms_unknown : cancontinue:=true;
  175. ms_registered : cancontinue:=true;
  176. ms_compile : cancontinue:=true;
  177. ms_compiling_wait : cancontinue:=m.usedunitsloaded(true,firstwaiting);
  178. ms_compiling_waitintf : cancontinue:=m.usedunitsloaded(true,firstwaiting);
  179. ms_compiling_waitimpl : cancontinue:=m.usedunitsloaded(false,firstwaiting);
  180. ms_compiling_waitfinish : cancontinue:=m.nowaitingforunits(firstwaiting);
  181. ms_compiled_waitcrc : cancontinue:=m.usedunitsfinalcrc(firstwaiting);
  182. ms_compiled : cancontinue:=true;
  183. ms_processed : cancontinue:=true;
  184. ms_moduleerror : cancontinue:=true;
  185. else
  186. InternalError(2024011802);
  187. end;
  188. if (not cancontinue) and checksub then
  189. begin
  190. checkused(m2);
  191. if m2<>nil then
  192. firstwaiting:=m2;
  193. end;
  194. {$IFDEF DEBUG_CTASK}
  195. Write(m.ToString,' state: ',m.state,', can continue: ',Result);
  196. if result then
  197. Writeln
  198. else
  199. begin
  200. Write(' (First waiting: ');
  201. If Assigned(FirstWaiting) then
  202. Writeln(FirstWaiting.ToString,' )')
  203. else
  204. Writeln('<none>)');
  205. end;
  206. {$ENDIF}
  207. end;
  208. function ttask_handler.cancontinue(t : ttask_list; out firstwaiting : tmodule): boolean;
  209. begin
  210. Result:=cancontinue(t.module,true,firstwaiting);
  211. end;
  212. function ttask_handler.continue(t : ttask_list) : Boolean;
  213. var
  214. m : tmodule;
  215. orgname : shortstring;
  216. begin
  217. m:=t.module;
  218. orgname:=m.modulename^;
  219. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' Continues. State: ',m.state);{$ENDIF}
  220. if Assigned(t.state) then
  221. t.RestoreState;
  222. case m.state of
  223. ms_registered : parser.compile_module(m);
  224. ms_compile : parser.compile_module(m);
  225. ms_compiled : if (not m.is_initial) or m.is_unit then
  226. (m as tppumodule).post_load_or_compile(m,m.compilecount>1);
  227. ms_compiling_wait : pmodules.proc_program_declarations(m,m.islibrary);
  228. ms_compiling_waitintf : pmodules.parse_unit_interface_declarations(m);
  229. ms_compiling_waitimpl : pmodules.proc_unit_implementation(m);
  230. ms_compiling_waitfinish : pmodules.finish_compile_unit(m);
  231. ms_compiled_waitcrc : pmodules.finish_unit(m);
  232. ms_processed : ;
  233. else
  234. InternalError(2024011801);
  235. end;
  236. if m.state=ms_compiled then
  237. begin
  238. parsing_done(m);
  239. if m.is_initial and not m.is_unit then
  240. m.state:=ms_processed;
  241. end;
  242. Result:=m.state=ms_processed;
  243. {$IFDEF DEBUG_CTASK}
  244. Write(m.ToString,' done: ',Result);
  245. if Result then
  246. Writeln
  247. else
  248. Writeln(', state is now: ',m.state);
  249. {$ENDIF}
  250. if not result then
  251. // Not done, save state
  252. t.SaveState;
  253. {
  254. the name can change as a result of processing, e.g. PROGRAM -> TB0406
  255. Normally only for the initial module, but we'll do a generic check.
  256. }
  257. if m.modulename^<>orgname then
  258. rebuild_hash;
  259. end;
  260. procedure ttask_handler.rebuild_hash;
  261. var
  262. t : ttask_list;
  263. begin
  264. Hash.Clear;
  265. t:=list.firsttask;
  266. While assigned(t) do
  267. begin
  268. Hash.Add(t.module.modulename^,t);
  269. t:=t.nexttask;
  270. end;
  271. end;
  272. procedure ttask_handler.processqueue;
  273. var
  274. t,t2 : ttask_list;
  275. process: boolean;
  276. dummy,firstwaiting : tmodule;
  277. begin
  278. t:=list.firsttask;
  279. process:=true;
  280. While t<>nil do
  281. begin
  282. process:=cancontinue(t,firstwaiting);
  283. {$IFDEF Debug_WaitCRC}
  284. if firstwaiting<>nil then
  285. writeln('ttask_handler.processqueue "',t.module.realmodulename^,'" state=',t.module.state,' waitingfor="',firstwaiting.realmodulename^,'",',firstwaiting.state)
  286. else
  287. writeln('ttask_handler.processqueue "',t.module.realmodulename^,'" state=',t.module.state,' waitingfor=nil');
  288. {$ENDIF}
  289. if process then
  290. begin
  291. if continue(t) then
  292. begin
  293. {$IFDEF DEBUG_CTASK}Writeln(t.module.ToString,' is finished, removing from task list');{$ENDIF}
  294. hash.Remove(t.module);
  295. list.Remove(t);
  296. end;
  297. // first search for any module that is ready to be written as ppu
  298. t2:=list.firsttask;
  299. while (t2<>nil)
  300. and ((t2.module.state<>ms_compiled_waitcrc)
  301. or not t2.module.usedunitsfinalcrc(firstwaiting)) do
  302. t2:=t2.nexttask;
  303. if t2<>nil then
  304. begin
  305. t:=t2;
  306. {$IFDEF Debug_WaitCRC}
  307. writeln('ttask_handler.processqueue FOUND CRC READY ',t.module.realmodulename^,' state=',t.module.state);
  308. {$ENDIF}
  309. end
  310. else
  311. begin
  312. // maybe the strategy can be improved.
  313. t:=list.firsttask;
  314. end;
  315. end
  316. else if assigned(firstwaiting) and cancontinue(firstwaiting,true, dummy) then
  317. begin
  318. t2:=findtask(firstwaiting);
  319. if t2=nil then
  320. t2:=t.nexttask;
  321. t:=t2;
  322. end
  323. else
  324. begin
  325. t:=t.nexttask;
  326. end;
  327. if t=nil then
  328. begin
  329. t:=list.firsttask;
  330. if t<>nil then
  331. begin
  332. // no progress possible
  333. write_queue;
  334. InternalError(2025090301);
  335. end;
  336. end;
  337. end;
  338. end;
  339. procedure ttask_handler.addmodule(m: tmodule);
  340. var
  341. n : TSymStr;
  342. e, t : ttask_list;
  343. begin
  344. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' added to task scheduler. State: ',m.state);{$ENDIF}
  345. n:=m.modulename^;
  346. e:=ttask_list(Hash.Find(n));
  347. if e=nil then
  348. begin
  349. // Clear reset flag.
  350. // This can happen when during load, reset is done and unit is added to task list.
  351. m.is_reset:=false;
  352. t:=ttask_list.create(m);
  353. list.insert(t);
  354. hash.Add(n,t);
  355. if list.count=1 then
  356. main:=m;
  357. end
  358. else
  359. begin
  360. // We have a task, if it was reset, then clear the state and move the task to the start.
  361. if m.is_reset then
  362. begin
  363. {$IFDEF DEBUG_CTASK}Writeln(m.ToString,' was reset, resetting flag. State: ',m.state);{$ENDIF}
  364. m.is_reset:=false;
  365. t:=findtask(m);
  366. if assigned(t) then
  367. begin
  368. t.DiscardState;
  369. list.Remove(t);
  370. list.insertbefore(t,list.First);
  371. end;
  372. end;
  373. end;
  374. end;
  375. procedure ttask_handler.write_queue;
  376. var
  377. t: ttask_list;
  378. firstwaiting: tmodule;
  379. cc: Boolean;
  380. begin
  381. writeln('ttask_handler.write_queue:');
  382. t:=list.firsttask;
  383. while t<>nil do
  384. begin
  385. cc:=cancontinue(t,firstwaiting);
  386. if firstwaiting<>nil then
  387. writeln('queue: ',t.module.realmodulename^,' ',t.module.state,' cancontinue=',cc,' firstwaiting=',firstwaiting.realmodulename^,' ',firstwaiting.state)
  388. else
  389. writeln('queue: ',t.module.realmodulename^,' ',t.module.state,' cancontinue=',cc,' firstwaiting=nil');
  390. t:=t.nexttask;
  391. end;
  392. end;
  393. end.