2
0

ctask.pas 9.5 KB

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