ncgbas.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. This unit implements some basic nodes
  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 ncgbas;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,cgutils,
  22. node,nbas;
  23. type
  24. tcgnothingnode = class(tnothingnode)
  25. procedure pass_generate_code;override;
  26. end;
  27. tcgasmnode = class(tasmnode)
  28. procedure pass_generate_code;override;
  29. end;
  30. tcgstatementnode = class(tstatementnode)
  31. procedure pass_generate_code;override;
  32. end;
  33. tcgblocknode = class(tblocknode)
  34. procedure pass_generate_code;override;
  35. end;
  36. tcgtempcreatenode = class(ttempcreatenode)
  37. procedure pass_generate_code;override;
  38. end;
  39. tcgtemprefnode = class(ttemprefnode)
  40. procedure pass_generate_code;override;
  41. { Changes the location of this temp to ref. Useful when assigning }
  42. { another temp to this one. The current location will be freed. }
  43. { Can only be called in pass 2 (since earlier, the temp location }
  44. { isn't known yet) }
  45. procedure changelocation(const ref: treference);
  46. end;
  47. tcgtempdeletenode = class(ttempdeletenode)
  48. procedure pass_generate_code;override;
  49. end;
  50. implementation
  51. uses
  52. globtype,globals,systems,
  53. cutils,verbose,
  54. aasmbase,aasmtai,aasmdata,aasmcpu,
  55. symsym,symconst,symdef,defutil,
  56. nflw,pass_2,
  57. cgbase,cgobj,
  58. procinfo,
  59. tgobj
  60. ;
  61. {*****************************************************************************
  62. TNOTHING
  63. *****************************************************************************}
  64. procedure tcgnothingnode.pass_generate_code;
  65. begin
  66. location_reset(location,LOC_VOID,OS_NO);
  67. { avoid an abstract rte }
  68. end;
  69. {*****************************************************************************
  70. TSTATEMENTNODE
  71. *****************************************************************************}
  72. procedure tcgstatementnode.pass_generate_code;
  73. var
  74. hp : tstatementnode;
  75. begin
  76. location_reset(location,LOC_VOID,OS_NO);
  77. hp:=self;
  78. while assigned(hp) do
  79. begin
  80. if assigned(hp.left) then
  81. begin
  82. secondpass(hp.left);
  83. { Compiler inserted blocks can return values }
  84. location_copy(hp.location,hp.left.location);
  85. end;
  86. hp:=tstatementnode(hp.right);
  87. end;
  88. end;
  89. {*****************************************************************************
  90. TASMNODE
  91. *****************************************************************************}
  92. procedure tcgasmnode.pass_generate_code;
  93. procedure ReLabel(var p:tasmsymbol);
  94. begin
  95. { Only relabel local tasmlabels }
  96. if (p.bind = AB_LOCAL) and
  97. (p is tasmlabel) then
  98. begin
  99. if not assigned(p.altsymbol) then
  100. current_asmdata.GenerateAltSymbol(p);
  101. p:=p.altsymbol;
  102. p.increfs;
  103. end;
  104. end;
  105. procedure ResolveRef(var op:toper);
  106. var
  107. sym : tabstractnormalvarsym;
  108. {$ifdef x86}
  109. scale : byte;
  110. {$endif x86}
  111. forceref,
  112. getoffset : boolean;
  113. indexreg : tregister;
  114. sofs : longint;
  115. begin
  116. if (op.typ=top_local) then
  117. begin
  118. sofs:=op.localoper^.localsymofs;
  119. indexreg:=op.localoper^.localindexreg;
  120. {$ifdef x86}
  121. scale:=op.localoper^.localscale;
  122. {$endif x86}
  123. getoffset:=op.localoper^.localgetoffset;
  124. forceref:=op.localoper^.localforceref;
  125. sym:=tabstractnormalvarsym(pointer(op.localoper^.localsym));
  126. dispose(op.localoper);
  127. case sym.localloc.loc of
  128. LOC_REFERENCE :
  129. begin
  130. if getoffset then
  131. begin
  132. if indexreg=NR_NO then
  133. begin
  134. op.typ:=top_const;
  135. op.val:=sym.localloc.reference.offset+sofs;
  136. end
  137. else
  138. begin
  139. op.typ:=top_ref;
  140. new(op.ref);
  141. reference_reset_base(op.ref^,indexreg,sym.localloc.reference.offset+sofs);
  142. end;
  143. end
  144. else
  145. begin
  146. op.typ:=top_ref;
  147. new(op.ref);
  148. reference_reset_base(op.ref^,sym.localloc.reference.base,sym.localloc.reference.offset+sofs);
  149. op.ref^.index:=indexreg;
  150. {$ifdef x86}
  151. op.ref^.scalefactor:=scale;
  152. {$endif x86}
  153. end;
  154. end;
  155. LOC_REGISTER :
  156. begin
  157. if getoffset then
  158. Message(asmr_e_invalid_reference_syntax);
  159. { Subscribed access }
  160. if forceref or
  161. (sofs<>0) then
  162. begin
  163. op.typ:=top_ref;
  164. new(op.ref);
  165. reference_reset_base(op.ref^,sym.localloc.register,sofs);
  166. op.ref^.index:=indexreg;
  167. {$ifdef x86}
  168. op.ref^.scalefactor:=scale;
  169. {$endif x86}
  170. end
  171. else
  172. begin
  173. op.typ:=top_reg;
  174. op.reg:=sym.localloc.register;
  175. end;
  176. end;
  177. end;
  178. end;
  179. end;
  180. var
  181. hp,hp2 : tai;
  182. i : longint;
  183. skipnode : boolean;
  184. begin
  185. location_reset(location,LOC_VOID,OS_NO);
  186. if (nf_get_asm_position in flags) then
  187. begin
  188. { Add a marker, to be sure the list is not empty }
  189. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_Position));
  190. currenttai:=tai(current_asmdata.CurrAsmList.last);
  191. exit;
  192. end;
  193. { Allocate registers used in the assembler block }
  194. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,used_regs_int);
  195. if (po_inline in current_procinfo.procdef.procoptions) then
  196. begin
  197. hp:=tai(p_asm.first);
  198. while assigned(hp) do
  199. begin
  200. hp2:=tai(hp.getcopy);
  201. skipnode:=false;
  202. case hp2.typ of
  203. ait_label :
  204. ReLabel(tasmsymbol(tai_label(hp2).labsym));
  205. ait_const :
  206. begin
  207. if assigned(tai_const(hp2).sym) then
  208. ReLabel(tai_const(hp2).sym);
  209. if assigned(tai_const(hp2).endsym) then
  210. ReLabel(tai_const(hp2).endsym);
  211. end;
  212. ait_instruction :
  213. begin
  214. { remove cached insentry, because the new code can
  215. require an other less optimized instruction }
  216. {$ifdef i386}
  217. {$ifndef NOAG386BIN}
  218. taicpu(hp2).ResetPass1;
  219. {$endif}
  220. {$endif}
  221. { fixup the references }
  222. for i:=1 to taicpu(hp2).ops do
  223. begin
  224. ResolveRef(taicpu(hp2).oper[i-1]^);
  225. with taicpu(hp2).oper[i-1]^ do
  226. begin
  227. case typ of
  228. top_ref :
  229. begin
  230. if assigned(ref^.symbol) then
  231. ReLabel(ref^.symbol);
  232. if assigned(ref^.relsymbol) then
  233. ReLabel(ref^.relsymbol);
  234. end;
  235. end;
  236. end;
  237. end;
  238. {$ifdef x86}
  239. { can only be checked now that all local operands }
  240. { have been resolved }
  241. taicpu(hp2).CheckIfValid;
  242. {$endif x86}
  243. end;
  244. ait_marker :
  245. begin
  246. { it's not an assembler block anymore }
  247. if (tai_marker(hp2).kind in [mark_AsmBlockStart, mark_AsmBlockEnd]) then
  248. skipnode:=true;
  249. end;
  250. end;
  251. if not skipnode then
  252. current_asmdata.CurrAsmList.concat(hp2)
  253. else
  254. hp2.free;
  255. hp:=tai(hp.next);
  256. end;
  257. { restore used symbols }
  258. current_asmdata.ResetAltSymbols;
  259. end
  260. else
  261. begin
  262. hp:=tai(p_asm.first);
  263. while assigned(hp) do
  264. begin
  265. case hp.typ of
  266. ait_instruction :
  267. begin
  268. { remove cached insentry, because the new code can
  269. require an other less optimized instruction }
  270. {$ifdef i386}
  271. {$ifndef NOAG386BIN}
  272. taicpu(hp).ResetPass1;
  273. {$endif}
  274. {$endif}
  275. { fixup the references }
  276. for i:=1 to taicpu(hp).ops do
  277. ResolveRef(taicpu(hp).oper[i-1]^);
  278. {$ifdef x86}
  279. { can only be checked now that all local operands }
  280. { have been resolved }
  281. taicpu(hp).CheckIfValid;
  282. {$endif x86}
  283. end;
  284. end;
  285. hp:=tai(hp.next);
  286. end;
  287. { insert the list }
  288. current_asmdata.CurrAsmList.concatlist(p_asm);
  289. end;
  290. { Release register used in the assembler block }
  291. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,used_regs_int);
  292. end;
  293. {*****************************************************************************
  294. TBLOCKNODE
  295. *****************************************************************************}
  296. procedure tcgblocknode.pass_generate_code;
  297. var
  298. hp : tstatementnode;
  299. oldexitlabel : tasmlabel;
  300. oldflowcontrol : tflowcontrol;
  301. begin
  302. location_reset(location,LOC_VOID,OS_NO);
  303. { replace exitlabel? }
  304. if nf_block_with_exit in flags then
  305. begin
  306. oldexitlabel:=current_procinfo.CurrExitLabel;
  307. current_asmdata.getjumplabel(current_procinfo.CurrExitLabel);
  308. oldflowcontrol:=flowcontrol;
  309. { the nested block will not span an exit statement of the parent }
  310. exclude(flowcontrol,fc_exit);
  311. end;
  312. { do second pass on left node }
  313. if assigned(left) then
  314. begin
  315. hp:=tstatementnode(left);
  316. while assigned(hp) do
  317. begin
  318. if assigned(hp.left) then
  319. begin
  320. secondpass(hp.left);
  321. location_copy(hp.location,hp.left.location);
  322. end;
  323. location_copy(location,hp.location);
  324. hp:=tstatementnode(hp.right);
  325. end;
  326. end;
  327. { write exitlabel }
  328. if nf_block_with_exit in flags then
  329. begin
  330. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel);
  331. current_procinfo.CurrExitLabel:=oldexitlabel;
  332. { the exit statements inside this block are not exit statements }
  333. { out of the parent }
  334. flowcontrol:=oldflowcontrol+(flowcontrol - [fc_exit]);
  335. end;
  336. end;
  337. {*****************************************************************************
  338. TTEMPCREATENODE
  339. *****************************************************************************}
  340. procedure tcgtempcreatenode.pass_generate_code;
  341. begin
  342. location_reset(location,LOC_VOID,OS_NO);
  343. { if we're secondpassing the same tcgtempcreatenode twice, we have a bug }
  344. if tempinfo^.valid then
  345. internalerror(200108222);
  346. { get a (persistent) temp }
  347. if tempinfo^.typedef.needs_inittable then
  348. begin
  349. location_reset(tempinfo^.location,LOC_REFERENCE,def_cgsize(tempinfo^.typedef));
  350. tg.GetTempTyped(current_asmdata.CurrAsmList,tempinfo^.typedef,tempinfo^.temptype,tempinfo^.location.reference);
  351. { the temp could have been used previously either because the memory location was reused or
  352. because we're in a loop }
  353. cg.g_finalize(current_asmdata.CurrAsmList,tempinfo^.typedef,tempinfo^.location.reference);
  354. end
  355. else if tempinfo^.may_be_in_reg then
  356. begin
  357. if tempinfo^.typedef.typ=floatdef then
  358. begin
  359. {$ifdef x86}
  360. if use_sse(tempinfo^.typedef) then
  361. begin
  362. if (tempinfo^.temptype = tt_persistent) then
  363. location_reset(tempinfo^.location,LOC_CMMREGISTER,def_cgsize(tempinfo^.typedef))
  364. else
  365. location_reset(tempinfo^.location,LOC_MMREGISTER,def_cgsize(tempinfo^.typedef));
  366. tempinfo^.location.register:=cg.getmmregister(current_asmdata.CurrAsmList,tempinfo^.location.size);
  367. end
  368. else
  369. {$endif x86}
  370. begin
  371. if (tempinfo^.temptype = tt_persistent) then
  372. location_reset(tempinfo^.location,LOC_CFPUREGISTER,def_cgsize(tempinfo^.typedef))
  373. else
  374. location_reset(tempinfo^.location,LOC_FPUREGISTER,def_cgsize(tempinfo^.typedef));
  375. tempinfo^.location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,tempinfo^.location.size);
  376. end;
  377. end
  378. else
  379. begin
  380. if (tempinfo^.temptype = tt_persistent) then
  381. location_reset(tempinfo^.location,LOC_CREGISTER,def_cgsize(tempinfo^.typedef))
  382. else
  383. location_reset(tempinfo^.location,LOC_REGISTER,def_cgsize(tempinfo^.typedef));
  384. {$ifndef cpu64bit}
  385. if tempinfo^.location.size in [OS_64,OS_S64] then
  386. begin
  387. tempinfo^.location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  388. tempinfo^.location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  389. end
  390. else
  391. {$endif cpu64bit}
  392. tempinfo^.location.register:=cg.getintregister(current_asmdata.CurrAsmList,tempinfo^.location.size);
  393. end;
  394. end
  395. else
  396. begin
  397. location_reset(tempinfo^.location,LOC_REFERENCE,def_cgsize(tempinfo^.typedef));
  398. tg.GetTemp(current_asmdata.CurrAsmList,size,tempinfo^.temptype,tempinfo^.location.reference);
  399. end;
  400. tempinfo^.valid := true;
  401. end;
  402. {*****************************************************************************
  403. TTEMPREFNODE
  404. *****************************************************************************}
  405. procedure tcgtemprefnode.pass_generate_code;
  406. begin
  407. { check if the temp is valid }
  408. if not tempinfo^.valid then
  409. internalerror(200108231);
  410. location:=tempinfo^.location;
  411. case tempinfo^.location.loc of
  412. LOC_REFERENCE:
  413. begin
  414. inc(location.reference.offset,offset);
  415. { tempinfo^.valid should be set to false it it's a normal temp }
  416. end;
  417. LOC_REGISTER,
  418. LOC_FPUREGISTER,
  419. LOC_MMREGISTER :
  420. tempinfo^.valid := false;
  421. end;
  422. end;
  423. procedure tcgtemprefnode.changelocation(const ref: treference);
  424. begin
  425. { check if the temp is valid }
  426. if not tempinfo^.valid then
  427. internalerror(200306081);
  428. if (tempinfo^.location.loc<>LOC_REFERENCE) then
  429. internalerror(2004020203);
  430. if (tempinfo^.temptype = tt_persistent) then
  431. tg.ChangeTempType(current_asmdata.CurrAsmList,tempinfo^.location.reference,tt_normal);
  432. tg.ungettemp(current_asmdata.CurrAsmList,tempinfo^.location.reference);
  433. tempinfo^.location.reference := ref;
  434. tg.ChangeTempType(current_asmdata.CurrAsmList,tempinfo^.location.reference,tempinfo^.temptype);
  435. { adapt location }
  436. location.reference := ref;
  437. inc(location.reference.offset,offset);
  438. end;
  439. {*****************************************************************************
  440. TTEMPDELETENODE
  441. *****************************************************************************}
  442. procedure tcgtempdeletenode.pass_generate_code;
  443. begin
  444. location_reset(location,LOC_VOID,OS_NO);
  445. case tempinfo^.location.loc of
  446. LOC_REFERENCE:
  447. begin
  448. if release_to_normal then
  449. tg.ChangeTempType(current_asmdata.CurrAsmList,tempinfo^.location.reference,tt_normal)
  450. else
  451. begin
  452. tg.UnGetTemp(current_asmdata.CurrAsmList,tempinfo^.location.reference);
  453. tempinfo^.valid := false;
  454. end;
  455. end;
  456. LOC_CREGISTER,
  457. LOC_REGISTER:
  458. begin
  459. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  460. (pi_has_goto in current_procinfo.flags) then
  461. begin
  462. { make sure the register allocator doesn't reuse the }
  463. { register e.g. in the middle of a loop }
  464. {$ifndef cpu64bit}
  465. if tempinfo^.location.size in [OS_64,OS_S64] then
  466. begin
  467. cg.a_reg_sync(current_asmdata.CurrAsmList,tempinfo^.location.register64.reghi);
  468. cg.a_reg_sync(current_asmdata.CurrAsmList,tempinfo^.location.register64.reglo);
  469. end
  470. else
  471. {$endif cpu64bit}
  472. cg.a_reg_sync(current_asmdata.CurrAsmList,tempinfo^.location.register);
  473. end;
  474. if release_to_normal then
  475. tempinfo^.location.loc := LOC_REGISTER
  476. else
  477. tempinfo^.valid := false;
  478. end;
  479. LOC_CFPUREGISTER,
  480. LOC_FPUREGISTER:
  481. begin
  482. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  483. (pi_has_goto in current_procinfo.flags) then
  484. begin
  485. { make sure the register allocator doesn't reuse the }
  486. { register e.g. in the middle of a loop }
  487. cg.a_reg_sync(current_asmdata.CurrAsmList,tempinfo^.location.register);
  488. end;
  489. if release_to_normal then
  490. tempinfo^.location.loc := LOC_FPUREGISTER
  491. else
  492. tempinfo^.valid := false;
  493. end;
  494. LOC_CMMREGISTER,
  495. LOC_MMREGISTER:
  496. begin
  497. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  498. (pi_has_goto in current_procinfo.flags) then
  499. begin
  500. { make sure the register allocator doesn't reuse the }
  501. { register e.g. in the middle of a loop }
  502. cg.a_reg_sync(current_asmdata.CurrAsmList,tempinfo^.location.register);
  503. end;
  504. if release_to_normal then
  505. tempinfo^.location.loc := LOC_MMREGISTER
  506. else
  507. tempinfo^.valid := false;
  508. end;
  509. else
  510. internalerror(200507161);
  511. end;
  512. end;
  513. begin
  514. cnothingnode:=tcgnothingnode;
  515. casmnode:=tcgasmnode;
  516. cstatementnode:=tcgstatementnode;
  517. cblocknode:=tcgblocknode;
  518. ctempcreatenode:=tcgtempcreatenode;
  519. ctemprefnode:=tcgtemprefnode;
  520. ctempdeletenode:=tcgtempdeletenode;
  521. end.