ctask.pas 11 KB

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