ncgutil.pas 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  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 ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bitalu}
  27. ,cg64f32
  28. {$endif not cpu64bitalu}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  50. procedure location_allocate_register(list:TAsmList;out l: tlocation;def: tdef;constant: boolean);
  51. { loads a cgpara into a tlocation; assumes that loc.loc is already
  52. initialised }
  53. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  54. { allocate registers for a tlocation; assumes that loc.loc is already
  55. set to LOC_CREGISTER/LOC_CFPUREGISTER/... }
  56. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  57. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  58. function has_alias_name(pd:tprocdef;const s:string):boolean;
  59. procedure alloc_proc_symbol(pd: tprocdef);
  60. procedure gen_proc_entry_code(list:TAsmList);
  61. procedure gen_proc_exit_code(list:TAsmList);
  62. procedure gen_stack_check_size_para(list:TAsmList);
  63. procedure gen_stack_check_call(list:TAsmList);
  64. procedure gen_save_used_regs(list:TAsmList);
  65. procedure gen_restore_used_regs(list:TAsmList);
  66. procedure gen_load_para_value(list:TAsmList);
  67. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  68. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  69. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  70. { adds the regvars used in n and its children to rv.allregvars,
  71. those which were already in rv.allregvars to rv.commonregvars and
  72. uses rv.myregvars as scratch (so that two uses of the same regvar
  73. in a single tree to make it appear in commonregvars). Useful to
  74. find out which regvars are used in two different node trees
  75. e.g. in the "else" and "then" path, or in various case blocks }
  76. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  77. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  78. { Allocate the buffers for exception management and setjmp environment.
  79. Return a pointer to these buffers, send them to the utility routine
  80. so they are registered, and then call setjmp.
  81. Then compare the result of setjmp with 0, and if not equal
  82. to zero, then jump to exceptlabel.
  83. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  84. It is to note that this routine may be called *after* the stackframe of a
  85. routine has been called, therefore on machines where the stack cannot
  86. be modified, all temps should be allocated on the heap instead of the
  87. stack. }
  88. type
  89. texceptiontemps=record
  90. jmpbuf,
  91. envbuf,
  92. reasonbuf : treference;
  93. end;
  94. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  95. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  96. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  97. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  98. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  99. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  100. procedure location_free(list: TAsmList; const location : TLocation);
  101. function getprocalign : shortint;
  102. procedure gen_fpc_dummy(list : TAsmList);
  103. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  104. implementation
  105. uses
  106. version,
  107. cutils,cclasses,
  108. globals,systems,verbose,export,
  109. ppu,defutil,
  110. procinfo,paramgr,fmodule,
  111. regvars,dbgbase,
  112. pass_1,pass_2,
  113. nbas,ncon,nld,nmem,nutils,ngenutil,
  114. tgobj,cgobj,hlcgobj,hlcgcpu
  115. {$ifdef llvm}
  116. { override create_hlcodegen from hlcgcpu }
  117. , hlcgllvm
  118. {$endif}
  119. {$ifdef powerpc}
  120. , cpupi
  121. {$endif}
  122. {$ifdef powerpc64}
  123. , cpupi
  124. {$endif}
  125. {$ifdef SUPPORT_MMX}
  126. , cgx86
  127. {$endif SUPPORT_MMX}
  128. ;
  129. {*****************************************************************************
  130. Misc Helpers
  131. *****************************************************************************}
  132. {$if first_mm_imreg = 0}
  133. {$WARN 4044 OFF} { Comparison might be always false ... }
  134. {$endif}
  135. procedure location_free(list: TAsmList; const location : TLocation);
  136. begin
  137. case location.loc of
  138. LOC_VOID:
  139. ;
  140. LOC_REGISTER,
  141. LOC_CREGISTER:
  142. begin
  143. {$ifdef cpu64bitalu}
  144. { x86-64 system v abi:
  145. structs with up to 16 bytes are returned in registers }
  146. if location.size in [OS_128,OS_S128] then
  147. begin
  148. if getsupreg(location.register)<first_int_imreg then
  149. cg.ungetcpuregister(list,location.register);
  150. if getsupreg(location.registerhi)<first_int_imreg then
  151. cg.ungetcpuregister(list,location.registerhi);
  152. end
  153. {$else cpu64bitalu}
  154. if location.size in [OS_64,OS_S64] then
  155. begin
  156. if getsupreg(location.register64.reglo)<first_int_imreg then
  157. cg.ungetcpuregister(list,location.register64.reglo);
  158. if getsupreg(location.register64.reghi)<first_int_imreg then
  159. cg.ungetcpuregister(list,location.register64.reghi);
  160. end
  161. {$endif cpu64bitalu}
  162. else
  163. if getsupreg(location.register)<first_int_imreg then
  164. cg.ungetcpuregister(list,location.register);
  165. end;
  166. LOC_FPUREGISTER,
  167. LOC_CFPUREGISTER:
  168. begin
  169. if getsupreg(location.register)<first_fpu_imreg then
  170. cg.ungetcpuregister(list,location.register);
  171. end;
  172. LOC_MMREGISTER,
  173. LOC_CMMREGISTER :
  174. begin
  175. if getsupreg(location.register)<first_mm_imreg then
  176. cg.ungetcpuregister(list,location.register);
  177. end;
  178. LOC_REFERENCE,
  179. LOC_CREFERENCE :
  180. begin
  181. if paramanager.use_fixed_stack then
  182. location_freetemp(list,location);
  183. end;
  184. else
  185. internalerror(2004110211);
  186. end;
  187. end;
  188. procedure firstcomplex(p : tbinarynode);
  189. var
  190. fcl, fcr: longint;
  191. ncl, ncr: longint;
  192. begin
  193. { always calculate boolean AND and OR from left to right }
  194. if (p.nodetype in [orn,andn]) and
  195. is_boolean(p.left.resultdef) then
  196. begin
  197. if nf_swapped in p.flags then
  198. internalerror(200709253);
  199. end
  200. else
  201. begin
  202. fcl:=node_resources_fpu(p.left);
  203. fcr:=node_resources_fpu(p.right);
  204. ncl:=node_complexity(p.left);
  205. ncr:=node_complexity(p.right);
  206. { We swap left and right if
  207. a) right needs more floating point registers than left, and
  208. left needs more than 0 floating point registers (if it
  209. doesn't need any, swapping won't change the floating
  210. point register pressure)
  211. b) both left and right need an equal amount of floating
  212. point registers or right needs no floating point registers,
  213. and in addition right has a higher complexity than left
  214. (+- needs more integer registers, but not necessarily)
  215. }
  216. if ((fcr>fcl) and
  217. (fcl>0)) or
  218. (((fcr=fcl) or
  219. (fcr=0)) and
  220. (ncr>ncl)) then
  221. p.swapleftright
  222. end;
  223. end;
  224. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  225. {
  226. produces jumps to true respectively false labels using boolean expressions
  227. depending on whether the loading of regvars is currently being
  228. synchronized manually (such as in an if-node) or automatically (most of
  229. the other cases where this procedure is called), loadregvars can be
  230. "lr_load_regvars" or "lr_dont_load_regvars"
  231. }
  232. var
  233. opsize : tcgsize;
  234. storepos : tfileposinfo;
  235. tmpreg : tregister;
  236. begin
  237. if nf_error in p.flags then
  238. exit;
  239. storepos:=current_filepos;
  240. current_filepos:=p.fileinfo;
  241. if is_boolean(p.resultdef) then
  242. begin
  243. {$ifdef OLDREGVARS}
  244. if loadregvars = lr_load_regvars then
  245. load_all_regvars(list);
  246. {$endif OLDREGVARS}
  247. if is_constboolnode(p) then
  248. begin
  249. if Tordconstnode(p).value.uvalue<>0 then
  250. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  251. else
  252. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  253. end
  254. else
  255. begin
  256. opsize:=def_cgsize(p.resultdef);
  257. case p.location.loc of
  258. LOC_SUBSETREG,LOC_CSUBSETREG,
  259. LOC_SUBSETREF,LOC_CSUBSETREF:
  260. begin
  261. tmpreg := cg.getintregister(list,OS_INT);
  262. hlcg.a_load_loc_reg(list,p.resultdef,osuinttype,p.location,tmpreg);
  263. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  264. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  265. end;
  266. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  267. begin
  268. {$ifdef cpu64bitalu}
  269. if opsize in [OS_128,OS_S128] then
  270. begin
  271. hlcg.location_force_reg(list,p.location,p.resultdef,cgsize_orddef(opsize),true);
  272. tmpreg:=cg.getintregister(list,OS_64);
  273. cg.a_op_reg_reg_reg(list,OP_OR,OS_64,p.location.register128.reglo,p.location.register128.reghi,tmpreg);
  274. location_reset(p.location,LOC_REGISTER,OS_64);
  275. p.location.register:=tmpreg;
  276. opsize:=OS_64;
  277. end;
  278. {$else cpu64bitalu}
  279. if opsize in [OS_64,OS_S64] then
  280. begin
  281. hlcg.location_force_reg(list,p.location,p.resultdef,cgsize_orddef(opsize),true);
  282. tmpreg:=cg.getintregister(list,OS_32);
  283. cg.a_op_reg_reg_reg(list,OP_OR,OS_32,p.location.register64.reglo,p.location.register64.reghi,tmpreg);
  284. location_reset(p.location,LOC_REGISTER,OS_32);
  285. p.location.register:=tmpreg;
  286. opsize:=OS_32;
  287. end;
  288. {$endif cpu64bitalu}
  289. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  290. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  291. end;
  292. LOC_JUMP:
  293. ;
  294. {$ifdef cpuflags}
  295. LOC_FLAGS :
  296. begin
  297. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  298. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  299. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  300. end;
  301. {$endif cpuflags}
  302. else
  303. begin
  304. printnode(output,p);
  305. internalerror(200308241);
  306. end;
  307. end;
  308. end;
  309. end
  310. else
  311. internalerror(200112305);
  312. current_filepos:=storepos;
  313. end;
  314. (*
  315. This code needs fixing. It is not safe to use rgint; on the m68000 it
  316. would be rgaddr.
  317. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  318. begin
  319. case t.loc of
  320. LOC_REGISTER:
  321. begin
  322. { can't be a regvar, since it would be LOC_CREGISTER then }
  323. exclude(regs,getsupreg(t.register));
  324. if t.register64.reghi<>NR_NO then
  325. exclude(regs,getsupreg(t.register64.reghi));
  326. end;
  327. LOC_CREFERENCE,LOC_REFERENCE:
  328. begin
  329. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  330. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  331. exclude(regs,getsupreg(t.reference.base));
  332. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  333. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  334. exclude(regs,getsupreg(t.reference.index));
  335. end;
  336. end;
  337. end;
  338. *)
  339. {*****************************************************************************
  340. EXCEPTION MANAGEMENT
  341. *****************************************************************************}
  342. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  343. begin
  344. tg.gethltemp(list,rec_exceptaddr,rec_exceptaddr.size,tt_persistent,t.envbuf);
  345. tg.gethltemp(list,rec_jmp_buf,rec_jmp_buf.size,tt_persistent,t.jmpbuf);
  346. tg.gethltemp(list,ossinttype,ossinttype.size,tt_persistent,t.reasonbuf);
  347. end;
  348. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  349. begin
  350. tg.Ungettemp(list,t.jmpbuf);
  351. tg.ungettemp(list,t.envbuf);
  352. tg.ungettemp(list,t.reasonbuf);
  353. end;
  354. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  355. var
  356. paraloc1, paraloc2, paraloc3, pushexceptres, setjmpres: tcgpara;
  357. pd: tprocdef;
  358. tmpresloc: tlocation;
  359. begin
  360. paraloc1.init;
  361. paraloc2.init;
  362. paraloc3.init;
  363. { fpc_pushexceptaddr(exceptionframetype, setjmp_buffer, exception_address_chain_entry) }
  364. pd:=search_system_proc('fpc_pushexceptaddr');
  365. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  366. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,2,paraloc2);
  367. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,3,paraloc3);
  368. if pd.is_pushleftright then
  369. begin
  370. { type of exceptionframe }
  371. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  372. { setjmp buffer }
  373. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  374. { exception address chain entry }
  375. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  376. end
  377. else
  378. begin
  379. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  380. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  381. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  382. end;
  383. paramanager.freecgpara(list,paraloc3);
  384. paramanager.freecgpara(list,paraloc2);
  385. paramanager.freecgpara(list,paraloc1);
  386. { perform the fpc_pushexceptaddr call }
  387. pushexceptres:=hlcg.g_call_system_proc(list,pd,[@paraloc1,@paraloc2,@paraloc3],nil);
  388. { get the result }
  389. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(pushexceptres.def));
  390. tmpresloc.register:=hlcg.getaddressregister(list,pushexceptres.def);
  391. hlcg.gen_load_cgpara_loc(list,pushexceptres.def,pushexceptres,tmpresloc,true);
  392. pushexceptres.resetiftemp;
  393. { fpc_setjmp(result_of_pushexceptaddr_call) }
  394. pd:=search_system_proc('fpc_setjmp');
  395. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  396. hlcg.a_load_reg_cgpara(list,pushexceptres.def,tmpresloc.register,paraloc1);
  397. paramanager.freecgpara(list,paraloc1);
  398. { perform the fpc_setjmp call }
  399. setjmpres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  400. setjmpres.check_simple_location;
  401. if setjmpres.location^.loc<>LOC_REGISTER then
  402. internalerror(2014080701);
  403. hlcg.getcpuregister(list,setjmpres.location^.register);
  404. hlcg.g_exception_reason_save(list,setjmpres.def,ossinttype,setjmpres.location^.register,t.reasonbuf);
  405. { if we get 0 here in the function result register, it means that we
  406. longjmp'd back here }
  407. hlcg.a_cmp_const_reg_label(list,setjmpres.def,OC_NE,0,setjmpres.location^.register,exceptlabel);
  408. hlcg.ungetcpuregister(list,setjmpres.location^.register);
  409. setjmpres.resetiftemp;
  410. paraloc1.done;
  411. paraloc2.done;
  412. paraloc3.done;
  413. end;
  414. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  415. var
  416. reasonreg: tregister;
  417. begin
  418. hlcg.g_call_system_proc(list,'fpc_popaddrstack',[],nil);
  419. if not onlyfree then
  420. begin
  421. reasonreg:=hlcg.getintregister(list,osuinttype);
  422. hlcg.g_exception_reason_load(list,osuinttype,osuinttype,t.reasonbuf,reasonreg);
  423. hlcg.a_cmp_const_reg_label(list,osuinttype,OC_EQ,a,reasonreg,endexceptlabel);
  424. end;
  425. end;
  426. {*****************************************************************************
  427. TLocation
  428. *****************************************************************************}
  429. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  430. var
  431. tmpreg: tregister;
  432. begin
  433. if (setbase<>0) then
  434. begin
  435. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  436. internalerror(2007091502);
  437. { subtract the setbase }
  438. case l.loc of
  439. LOC_CREGISTER:
  440. begin
  441. tmpreg := cg.getintregister(list,l.size);
  442. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  443. l.loc:=LOC_REGISTER;
  444. l.register:=tmpreg;
  445. end;
  446. LOC_REGISTER:
  447. begin
  448. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  449. end;
  450. end;
  451. end;
  452. end;
  453. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  454. var
  455. reg : tregister;
  456. begin
  457. if (l.loc<>LOC_MMREGISTER) and
  458. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  459. begin
  460. reg:=cg.getmmregister(list,OS_VECTOR);
  461. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  462. location_freetemp(list,l);
  463. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  464. l.register:=reg;
  465. end;
  466. end;
  467. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  468. begin
  469. l.size:=def_cgsize(def);
  470. if (def.typ=floatdef) and
  471. not(cs_fp_emulation in current_settings.moduleswitches) then
  472. begin
  473. if use_vectorfpu(def) then
  474. begin
  475. if constant then
  476. location_reset(l,LOC_CMMREGISTER,l.size)
  477. else
  478. location_reset(l,LOC_MMREGISTER,l.size);
  479. l.register:=cg.getmmregister(list,l.size);
  480. end
  481. else
  482. begin
  483. if constant then
  484. location_reset(l,LOC_CFPUREGISTER,l.size)
  485. else
  486. location_reset(l,LOC_FPUREGISTER,l.size);
  487. l.register:=cg.getfpuregister(list,l.size);
  488. end;
  489. end
  490. else
  491. begin
  492. if constant then
  493. location_reset(l,LOC_CREGISTER,l.size)
  494. else
  495. location_reset(l,LOC_REGISTER,l.size);
  496. {$ifdef cpu64bitalu}
  497. if l.size in [OS_128,OS_S128,OS_F128] then
  498. begin
  499. l.register128.reglo:=cg.getintregister(list,OS_64);
  500. l.register128.reghi:=cg.getintregister(list,OS_64);
  501. end
  502. else
  503. {$else cpu64bitalu}
  504. if l.size in [OS_64,OS_S64,OS_F64] then
  505. begin
  506. l.register64.reglo:=cg.getintregister(list,OS_32);
  507. l.register64.reghi:=cg.getintregister(list,OS_32);
  508. end
  509. else
  510. {$endif cpu64bitalu}
  511. { Note: for widths of records (and maybe objects, classes, etc.) an
  512. address register could be set here, but that is later
  513. changed to an intregister neverthless when in the
  514. tcgassignmentnode thlcgobj.maybe_change_load_node_reg is
  515. called for the temporary node; so the workaround for now is
  516. to fix the symptoms... }
  517. l.register:=cg.getintregister(list,l.size);
  518. end;
  519. end;
  520. {****************************************************************************
  521. Init/Finalize Code
  522. ****************************************************************************}
  523. { generates the code for incrementing the reference count of parameters and
  524. initialize out parameters }
  525. procedure init_paras(p:TObject;arg:pointer);
  526. var
  527. href : treference;
  528. hsym : tparavarsym;
  529. eldef : tdef;
  530. list : TAsmList;
  531. needs_inittable : boolean;
  532. begin
  533. list:=TAsmList(arg);
  534. if (tsym(p).typ=paravarsym) then
  535. begin
  536. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  537. if not needs_inittable then
  538. exit;
  539. case tparavarsym(p).varspez of
  540. vs_value :
  541. begin
  542. { variants are already handled by the call to fpc_variant_copy_overwrite if
  543. they are passed by reference }
  544. if not((tparavarsym(p).vardef.typ=variantdef) and
  545. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  546. begin
  547. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,
  548. is_open_array(tparavarsym(p).vardef) or
  549. ((target_info.system in systems_caller_copy_addr_value_para) and
  550. paramanager.push_addr_param(vs_value,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)),
  551. sizeof(pint));
  552. if is_open_array(tparavarsym(p).vardef) then
  553. begin
  554. { open arrays do not contain correct element count in their rtti,
  555. the actual count must be passed separately. }
  556. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  557. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  558. if not assigned(hsym) then
  559. internalerror(201003031);
  560. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  561. end
  562. else
  563. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  564. end;
  565. end;
  566. vs_out :
  567. begin
  568. { we have no idea about the alignment at the callee side,
  569. and the user also cannot specify "unaligned" here, so
  570. assume worst case }
  571. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  572. if is_open_array(tparavarsym(p).vardef) then
  573. begin
  574. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  575. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  576. if not assigned(hsym) then
  577. internalerror(201103033);
  578. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  579. end
  580. else
  581. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  582. end;
  583. end;
  584. end;
  585. end;
  586. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  587. begin
  588. case loc.loc of
  589. LOC_CREGISTER:
  590. begin
  591. {$ifdef cpu64bitalu}
  592. if loc.size in [OS_128,OS_S128] then
  593. begin
  594. loc.register128.reglo:=cg.getintregister(list,OS_64);
  595. loc.register128.reghi:=cg.getintregister(list,OS_64);
  596. end
  597. else
  598. {$else cpu64bitalu}
  599. if loc.size in [OS_64,OS_S64] then
  600. begin
  601. loc.register64.reglo:=cg.getintregister(list,OS_32);
  602. loc.register64.reghi:=cg.getintregister(list,OS_32);
  603. end
  604. else
  605. {$endif cpu64bitalu}
  606. loc.register:=cg.getintregister(list,loc.size);
  607. end;
  608. LOC_CFPUREGISTER:
  609. begin
  610. loc.register:=cg.getfpuregister(list,loc.size);
  611. end;
  612. LOC_CMMREGISTER:
  613. begin
  614. loc.register:=cg.getmmregister(list,loc.size);
  615. end;
  616. end;
  617. end;
  618. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  619. begin
  620. if allocreg then
  621. gen_alloc_regloc(list,sym.initialloc);
  622. if (pi_has_label in current_procinfo.flags) then
  623. begin
  624. { Allocate register already, to prevent first allocation to be
  625. inside a loop }
  626. {$if defined(cpu64bitalu)}
  627. if sym.initialloc.size in [OS_128,OS_S128] then
  628. begin
  629. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  630. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  631. end
  632. else
  633. {$elseif defined(cpu32bitalu)}
  634. if sym.initialloc.size in [OS_64,OS_S64] then
  635. begin
  636. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  637. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  638. end
  639. else
  640. {$elseif defined(cpu16bitalu)}
  641. if sym.initialloc.size in [OS_64,OS_S64] then
  642. begin
  643. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  644. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reglo));
  645. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  646. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reghi));
  647. end
  648. else
  649. if sym.initialloc.size in [OS_32,OS_S32] then
  650. begin
  651. cg.a_reg_sync(list,sym.initialloc.register);
  652. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  653. end
  654. else
  655. {$elseif defined(cpu8bitalu)}
  656. if sym.initialloc.size in [OS_64,OS_S64] then
  657. begin
  658. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  659. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reglo));
  660. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register64.reglo)));
  661. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register64.reglo))));
  662. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  663. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reghi));
  664. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register64.reghi)));
  665. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register64.reghi))));
  666. end
  667. else
  668. if sym.initialloc.size in [OS_32,OS_S32] then
  669. begin
  670. cg.a_reg_sync(list,sym.initialloc.register);
  671. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  672. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register)));
  673. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register))));
  674. end
  675. else
  676. if sym.initialloc.size in [OS_16,OS_S16] then
  677. begin
  678. cg.a_reg_sync(list,sym.initialloc.register);
  679. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  680. end
  681. else
  682. {$endif}
  683. cg.a_reg_sync(list,sym.initialloc.register);
  684. end;
  685. sym.localloc:=sym.initialloc;
  686. end;
  687. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  688. procedure unget_para(const paraloc:TCGParaLocation);
  689. begin
  690. case paraloc.loc of
  691. LOC_REGISTER :
  692. begin
  693. if getsupreg(paraloc.register)<first_int_imreg then
  694. cg.ungetcpuregister(list,paraloc.register);
  695. end;
  696. LOC_MMREGISTER :
  697. begin
  698. if getsupreg(paraloc.register)<first_mm_imreg then
  699. cg.ungetcpuregister(list,paraloc.register);
  700. end;
  701. LOC_FPUREGISTER :
  702. begin
  703. if getsupreg(paraloc.register)<first_fpu_imreg then
  704. cg.ungetcpuregister(list,paraloc.register);
  705. end;
  706. end;
  707. end;
  708. var
  709. paraloc : pcgparalocation;
  710. href : treference;
  711. sizeleft : aint;
  712. alignment : longint;
  713. tempref : treference;
  714. {$ifdef mips}
  715. tmpreg : tregister;
  716. {$endif mips}
  717. {$ifndef cpu64bitalu}
  718. tempreg : tregister;
  719. reg64 : tregister64;
  720. {$endif not cpu64bitalu}
  721. begin
  722. paraloc:=para.location;
  723. if not assigned(paraloc) then
  724. internalerror(200408203);
  725. { skip e.g. empty records }
  726. if (paraloc^.loc = LOC_VOID) then
  727. exit;
  728. case destloc.loc of
  729. LOC_REFERENCE :
  730. begin
  731. { If the parameter location is reused we don't need to copy
  732. anything }
  733. if not reusepara then
  734. begin
  735. href:=destloc.reference;
  736. sizeleft:=para.intsize;
  737. while assigned(paraloc) do
  738. begin
  739. if (paraloc^.size=OS_NO) then
  740. begin
  741. { Can only be a reference that contains the rest
  742. of the parameter }
  743. if (paraloc^.loc<>LOC_REFERENCE) or
  744. assigned(paraloc^.next) then
  745. internalerror(2005013010);
  746. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  747. inc(href.offset,sizeleft);
  748. sizeleft:=0;
  749. end
  750. else
  751. begin
  752. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  753. inc(href.offset,TCGSize2Size[paraloc^.size]);
  754. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  755. end;
  756. unget_para(paraloc^);
  757. paraloc:=paraloc^.next;
  758. end;
  759. end;
  760. end;
  761. LOC_REGISTER,
  762. LOC_CREGISTER :
  763. begin
  764. {$ifdef cpu64bitalu}
  765. if (para.size in [OS_128,OS_S128,OS_F128]) and
  766. ({ in case of fpu emulation, or abi's that pass fpu values
  767. via integer registers }
  768. (vardef.typ=floatdef) or
  769. is_methodpointer(vardef) or
  770. is_record(vardef)) then
  771. begin
  772. case paraloc^.loc of
  773. LOC_REGISTER:
  774. begin
  775. if not assigned(paraloc^.next) then
  776. internalerror(200410104);
  777. if (target_info.endian=ENDIAN_BIG) then
  778. begin
  779. { paraloc^ -> high
  780. paraloc^.next -> low }
  781. unget_para(paraloc^);
  782. gen_alloc_regloc(list,destloc);
  783. { reg->reg, alignment is irrelevant }
  784. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reghi,8);
  785. unget_para(paraloc^.next^);
  786. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reglo,8);
  787. end
  788. else
  789. begin
  790. { paraloc^ -> low
  791. paraloc^.next -> high }
  792. unget_para(paraloc^);
  793. gen_alloc_regloc(list,destloc);
  794. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reglo,8);
  795. unget_para(paraloc^.next^);
  796. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reghi,8);
  797. end;
  798. end;
  799. LOC_REFERENCE:
  800. begin
  801. gen_alloc_regloc(list,destloc);
  802. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  803. cg128.a_load128_ref_reg(list,href,destloc.register128);
  804. unget_para(paraloc^);
  805. end;
  806. else
  807. internalerror(2012090607);
  808. end
  809. end
  810. else
  811. {$else cpu64bitalu}
  812. if (para.size in [OS_64,OS_S64,OS_F64]) and
  813. (is_64bit(vardef) or
  814. { in case of fpu emulation, or abi's that pass fpu values
  815. via integer registers }
  816. (vardef.typ=floatdef) or
  817. is_methodpointer(vardef) or
  818. is_record(vardef)) then
  819. begin
  820. case paraloc^.loc of
  821. LOC_REGISTER:
  822. begin
  823. case para.locations_count of
  824. {$if defined(cpu16bitalu) or defined(cpu8bitalu)}
  825. { 4 paralocs? }
  826. 4:
  827. if (target_info.endian=ENDIAN_BIG) then
  828. begin
  829. { paraloc^ -> high
  830. paraloc^.next^.next -> low }
  831. unget_para(paraloc^);
  832. gen_alloc_regloc(list,destloc);
  833. { reg->reg, alignment is irrelevant }
  834. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^,GetNextReg(destloc.register64.reghi),2);
  835. unget_para(paraloc^.next^);
  836. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^,destloc.register64.reghi,2);
  837. unget_para(paraloc^.next^.next^);
  838. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^,GetNextReg(destloc.register64.reglo),2);
  839. unget_para(paraloc^.next^.next^.next^);
  840. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^.next^,destloc.register64.reglo,2);
  841. end
  842. else
  843. begin
  844. { paraloc^ -> low
  845. paraloc^.next^.next -> high }
  846. unget_para(paraloc^);
  847. gen_alloc_regloc(list,destloc);
  848. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^,destloc.register64.reglo,2);
  849. unget_para(paraloc^.next^);
  850. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^,GetNextReg(destloc.register64.reglo),2);
  851. unget_para(paraloc^.next^.next^);
  852. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^,destloc.register64.reghi,2);
  853. unget_para(paraloc^.next^.next^.next^);
  854. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^.next^,GetNextReg(destloc.register64.reghi),2);
  855. end;
  856. {$endif defined(cpu16bitalu) or defined(cpu8bitalu)}
  857. 2:
  858. if (target_info.endian=ENDIAN_BIG) then
  859. begin
  860. { paraloc^ -> high
  861. paraloc^.next -> low }
  862. unget_para(paraloc^);
  863. gen_alloc_regloc(list,destloc);
  864. { reg->reg, alignment is irrelevant }
  865. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  866. unget_para(paraloc^.next^);
  867. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  868. end
  869. else
  870. begin
  871. { paraloc^ -> low
  872. paraloc^.next -> high }
  873. unget_para(paraloc^);
  874. gen_alloc_regloc(list,destloc);
  875. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  876. unget_para(paraloc^.next^);
  877. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  878. end;
  879. else
  880. { unexpected number of paralocs }
  881. internalerror(200410104);
  882. end;
  883. end;
  884. LOC_REFERENCE:
  885. begin
  886. gen_alloc_regloc(list,destloc);
  887. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  888. cg64.a_load64_ref_reg(list,href,destloc.register64);
  889. unget_para(paraloc^);
  890. end;
  891. else
  892. internalerror(2005101501);
  893. end
  894. end
  895. else
  896. {$endif cpu64bitalu}
  897. begin
  898. if assigned(paraloc^.next) then
  899. begin
  900. if (destloc.size in [OS_PAIR,OS_SPAIR]) and
  901. (para.Size in [OS_PAIR,OS_SPAIR]) then
  902. begin
  903. unget_para(paraloc^);
  904. gen_alloc_regloc(list,destloc);
  905. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^,destloc.register,sizeof(aint));
  906. unget_para(paraloc^.Next^);
  907. {$if defined(cpu16bitalu) or defined(cpu8bitalu)}
  908. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,GetNextReg(destloc.register),sizeof(aint));
  909. {$else}
  910. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,destloc.registerhi,sizeof(aint));
  911. {$endif}
  912. end
  913. {$if defined(cpu8bitalu)}
  914. else if (destloc.size in [OS_32,OS_S32]) and
  915. (para.Size in [OS_32,OS_S32]) then
  916. begin
  917. unget_para(paraloc^);
  918. gen_alloc_regloc(list,destloc);
  919. cg.a_load_cgparaloc_anyreg(list,OS_8,paraloc^,destloc.register,sizeof(aint));
  920. unget_para(paraloc^.Next^);
  921. cg.a_load_cgparaloc_anyreg(list,OS_8,paraloc^.Next^,GetNextReg(destloc.register),sizeof(aint));
  922. unget_para(paraloc^.Next^.Next^);
  923. cg.a_load_cgparaloc_anyreg(list,OS_8,paraloc^.Next^.Next^,GetNextReg(GetNextReg(destloc.register)),sizeof(aint));
  924. unget_para(paraloc^.Next^.Next^.Next^);
  925. cg.a_load_cgparaloc_anyreg(list,OS_8,paraloc^.Next^.Next^.Next^,GetNextReg(GetNextReg(GetNextReg(destloc.register))),sizeof(aint));
  926. end
  927. {$endif defined(cpu8bitalu)}
  928. else
  929. begin
  930. { this can happen if a parameter is spread over
  931. multiple paralocs, e.g. if a record with two single
  932. fields must be passed in two single precision
  933. registers }
  934. { does it fit in the register of destloc? }
  935. sizeleft:=para.intsize;
  936. if sizeleft<>vardef.size then
  937. internalerror(2014122806);
  938. if sizeleft<>tcgsize2size[destloc.size] then
  939. internalerror(200410105);
  940. { store everything first to memory, then load it in
  941. destloc }
  942. tg.gettemp(list,sizeleft,sizeleft,tt_persistent,tempref);
  943. gen_alloc_regloc(list,destloc);
  944. while sizeleft>0 do
  945. begin
  946. if not assigned(paraloc) then
  947. internalerror(2014122807);
  948. unget_para(paraloc^);
  949. cg.a_load_cgparaloc_ref(list,paraloc^,tempref,sizeleft,newalignment(para.alignment,para.intsize-sizeleft));
  950. if (paraloc^.size=OS_NO) and
  951. assigned(paraloc^.next) then
  952. internalerror(2014122805);
  953. inc(tempref.offset,tcgsize2size[paraloc^.size]);
  954. dec(sizeleft,tcgsize2size[paraloc^.size]);
  955. paraloc:=paraloc^.next;
  956. end;
  957. dec(tempref.offset,para.intsize);
  958. cg.a_load_ref_reg(list,para.size,para.size,tempref,destloc.register);
  959. tg.ungettemp(list,tempref);
  960. end;
  961. end
  962. else
  963. begin
  964. unget_para(paraloc^);
  965. gen_alloc_regloc(list,destloc);
  966. { we can't directly move regular registers into fpu
  967. registers }
  968. if getregtype(paraloc^.register)=R_FPUREGISTER then
  969. begin
  970. { store everything first to memory, then load it in
  971. destloc }
  972. tg.gettemp(list,tcgsize2size[paraloc^.size],para.intsize,tt_persistent,tempref);
  973. cg.a_load_cgparaloc_ref(list,paraloc^,tempref,tcgsize2size[paraloc^.size],tempref.alignment);
  974. cg.a_load_ref_reg(list,int_cgsize(tcgsize2size[paraloc^.size]),destloc.size,tempref,destloc.register);
  975. tg.ungettemp(list,tempref);
  976. end
  977. else
  978. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  979. end;
  980. end;
  981. end;
  982. LOC_FPUREGISTER,
  983. LOC_CFPUREGISTER :
  984. begin
  985. {$ifdef mips}
  986. if (destloc.size = paraloc^.Size) and
  987. (paraloc^.Loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  988. begin
  989. unget_para(paraloc^);
  990. gen_alloc_regloc(list,destloc);
  991. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,para.alignment);
  992. end
  993. else if (destloc.size = OS_F32) and
  994. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  995. begin
  996. gen_alloc_regloc(list,destloc);
  997. unget_para(paraloc^);
  998. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,destloc.register));
  999. end
  1000. { TODO: Produces invalid code, needs fixing together with regalloc setup. }
  1001. {
  1002. else if (destloc.size = OS_F64) and
  1003. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) and
  1004. (paraloc^.next^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1005. begin
  1006. gen_alloc_regloc(list,destloc);
  1007. tmpreg:=destloc.register;
  1008. unget_para(paraloc^);
  1009. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,tmpreg));
  1010. setsupreg(tmpreg,getsupreg(tmpreg)+1);
  1011. unget_para(paraloc^.next^);
  1012. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.Next^.register,tmpreg));
  1013. end
  1014. }
  1015. else
  1016. begin
  1017. sizeleft := TCGSize2Size[destloc.size];
  1018. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1019. href:=tempref;
  1020. while assigned(paraloc) do
  1021. begin
  1022. unget_para(paraloc^);
  1023. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1024. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1025. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1026. paraloc:=paraloc^.next;
  1027. end;
  1028. gen_alloc_regloc(list,destloc);
  1029. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1030. tg.UnGetTemp(list,tempref);
  1031. end;
  1032. {$else mips}
  1033. {$if defined(sparc) or defined(arm)}
  1034. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1035. we need a temp }
  1036. sizeleft := TCGSize2Size[destloc.size];
  1037. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1038. href:=tempref;
  1039. while assigned(paraloc) do
  1040. begin
  1041. unget_para(paraloc^);
  1042. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1043. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1044. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1045. paraloc:=paraloc^.next;
  1046. end;
  1047. gen_alloc_regloc(list,destloc);
  1048. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1049. tg.UnGetTemp(list,tempref);
  1050. {$else defined(sparc) or defined(arm)}
  1051. unget_para(paraloc^);
  1052. gen_alloc_regloc(list,destloc);
  1053. { from register to register -> alignment is irrelevant }
  1054. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1055. if assigned(paraloc^.next) then
  1056. internalerror(200410109);
  1057. {$endif defined(sparc) or defined(arm)}
  1058. {$endif mips}
  1059. end;
  1060. LOC_MMREGISTER,
  1061. LOC_CMMREGISTER :
  1062. begin
  1063. {$ifndef cpu64bitalu}
  1064. { ARM vfp floats are passed in integer registers }
  1065. if (para.size=OS_F64) and
  1066. (paraloc^.size in [OS_32,OS_S32]) and
  1067. use_vectorfpu(vardef) then
  1068. begin
  1069. { we need 2x32bit reg }
  1070. if not assigned(paraloc^.next) or
  1071. assigned(paraloc^.next^.next) then
  1072. internalerror(2009112421);
  1073. unget_para(paraloc^.next^);
  1074. case paraloc^.next^.loc of
  1075. LOC_REGISTER:
  1076. tempreg:=paraloc^.next^.register;
  1077. LOC_REFERENCE:
  1078. begin
  1079. tempreg:=cg.getintregister(list,OS_32);
  1080. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1081. end;
  1082. else
  1083. internalerror(2012051301);
  1084. end;
  1085. { don't free before the above, because then the getintregister
  1086. could reallocate this register and overwrite it }
  1087. unget_para(paraloc^);
  1088. gen_alloc_regloc(list,destloc);
  1089. if (target_info.endian=endian_big) then
  1090. { paraloc^ -> high
  1091. paraloc^.next -> low }
  1092. reg64:=joinreg64(tempreg,paraloc^.register)
  1093. else
  1094. reg64:=joinreg64(paraloc^.register,tempreg);
  1095. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1096. end
  1097. else
  1098. {$endif not cpu64bitalu}
  1099. begin
  1100. if not assigned(paraloc^.next) then
  1101. begin
  1102. unget_para(paraloc^);
  1103. gen_alloc_regloc(list,destloc);
  1104. { from register to register -> alignment is irrelevant }
  1105. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1106. end
  1107. else
  1108. begin
  1109. internalerror(200410108);
  1110. end;
  1111. { data could come in two memory locations, for now
  1112. we simply ignore the sanity check (FK)
  1113. if assigned(paraloc^.next) then
  1114. internalerror(200410108);
  1115. }
  1116. end;
  1117. end;
  1118. else
  1119. internalerror(2010052903);
  1120. end;
  1121. end;
  1122. procedure gen_load_para_value(list:TAsmList);
  1123. procedure get_para(const paraloc:TCGParaLocation);
  1124. begin
  1125. case paraloc.loc of
  1126. LOC_REGISTER :
  1127. begin
  1128. if getsupreg(paraloc.register)<first_int_imreg then
  1129. cg.getcpuregister(list,paraloc.register);
  1130. end;
  1131. LOC_MMREGISTER :
  1132. begin
  1133. if getsupreg(paraloc.register)<first_mm_imreg then
  1134. cg.getcpuregister(list,paraloc.register);
  1135. end;
  1136. LOC_FPUREGISTER :
  1137. begin
  1138. if getsupreg(paraloc.register)<first_fpu_imreg then
  1139. cg.getcpuregister(list,paraloc.register);
  1140. end;
  1141. end;
  1142. end;
  1143. var
  1144. i : longint;
  1145. currpara : tparavarsym;
  1146. paraloc : pcgparalocation;
  1147. begin
  1148. if (po_assembler in current_procinfo.procdef.procoptions) or
  1149. { exceptfilters have a single hidden 'parentfp' parameter, which
  1150. is handled by tcg.g_proc_entry. }
  1151. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1152. exit;
  1153. { Allocate registers used by parameters }
  1154. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1155. begin
  1156. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1157. paraloc:=currpara.paraloc[calleeside].location;
  1158. while assigned(paraloc) do
  1159. begin
  1160. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1161. get_para(paraloc^);
  1162. paraloc:=paraloc^.next;
  1163. end;
  1164. end;
  1165. { Copy parameters to local references/registers }
  1166. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1167. begin
  1168. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1169. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1170. { gen_load_cgpara_loc() already allocated the initialloc
  1171. -> don't allocate again }
  1172. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1173. gen_alloc_regvar(list,currpara,false);
  1174. end;
  1175. { generate copies of call by value parameters, must be done before
  1176. the initialization and body is parsed because the refcounts are
  1177. incremented using the local copies }
  1178. current_procinfo.procdef.parast.SymList.ForEachCall(@hlcg.g_copyvalueparas,list);
  1179. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1180. begin
  1181. { initialize refcounted paras, and trash others. Needed here
  1182. instead of in gen_initialize_code, because when a reference is
  1183. intialised or trashed while the pointer to that reference is kept
  1184. in a regvar, we add a register move and that one again has to
  1185. come after the parameter loading code as far as the register
  1186. allocator is concerned }
  1187. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1188. end;
  1189. end;
  1190. {****************************************************************************
  1191. Entry/Exit
  1192. ****************************************************************************}
  1193. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1194. var
  1195. item : TCmdStrListItem;
  1196. begin
  1197. result:=true;
  1198. if pd.mangledname=s then
  1199. exit;
  1200. item := TCmdStrListItem(pd.aliasnames.first);
  1201. while assigned(item) do
  1202. begin
  1203. if item.str=s then
  1204. exit;
  1205. item := TCmdStrListItem(item.next);
  1206. end;
  1207. result:=false;
  1208. end;
  1209. procedure alloc_proc_symbol(pd: tprocdef);
  1210. var
  1211. item : TCmdStrListItem;
  1212. begin
  1213. item := TCmdStrListItem(pd.aliasnames.first);
  1214. while assigned(item) do
  1215. begin
  1216. { The condition to use global or local symbol must match
  1217. the code written in hlcg.gen_proc_symbol to
  1218. avoid change from AB_LOCAL to AB_GLOBAL, which generates
  1219. erroneous code (at least for targets using GOT) }
  1220. if (cs_profile in current_settings.moduleswitches) or
  1221. (po_global in current_procinfo.procdef.procoptions) then
  1222. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION)
  1223. else
  1224. current_asmdata.DefineAsmSymbol(item.str,AB_LOCAL,AT_FUNCTION);
  1225. item := TCmdStrListItem(item.next);
  1226. end;
  1227. end;
  1228. procedure gen_proc_entry_code(list:TAsmList);
  1229. var
  1230. hitemp,
  1231. lotemp, stack_frame_size : longint;
  1232. begin
  1233. { generate call frame marker for dwarf call frame info }
  1234. current_asmdata.asmcfi.start_frame(list);
  1235. { All temps are know, write offsets used for information }
  1236. if (cs_asm_source in current_settings.globalswitches) and
  1237. (current_procinfo.tempstart<>tg.lasttemp) then
  1238. begin
  1239. if tg.direction>0 then
  1240. begin
  1241. lotemp:=current_procinfo.tempstart;
  1242. hitemp:=tg.lasttemp;
  1243. end
  1244. else
  1245. begin
  1246. lotemp:=tg.lasttemp;
  1247. hitemp:=current_procinfo.tempstart;
  1248. end;
  1249. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1250. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1251. end;
  1252. { generate target specific proc entry code }
  1253. stack_frame_size := current_procinfo.calc_stackframe_size;
  1254. if (stack_frame_size <> 0) and
  1255. (po_nostackframe in current_procinfo.procdef.procoptions) then
  1256. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  1257. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1258. end;
  1259. procedure gen_proc_exit_code(list:TAsmList);
  1260. var
  1261. parasize : longint;
  1262. begin
  1263. { c style clearstack does not need to remove parameters from the stack, only the
  1264. return value when it was pushed by arguments }
  1265. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1266. begin
  1267. parasize:=0;
  1268. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1269. inc(parasize,sizeof(pint));
  1270. end
  1271. else
  1272. begin
  1273. parasize:=current_procinfo.para_stack_size;
  1274. { the parent frame pointer para has to be removed by the caller in
  1275. case of Delphi-style parent frame pointer passing }
  1276. if not paramanager.use_fixed_stack and
  1277. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1278. dec(parasize,sizeof(pint));
  1279. end;
  1280. { generate target specific proc exit code }
  1281. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1282. { release return registers, needed for optimizer }
  1283. if not is_void(current_procinfo.procdef.returndef) then
  1284. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1285. { end of frame marker for call frame info }
  1286. current_asmdata.asmcfi.end_frame(list);
  1287. end;
  1288. procedure gen_stack_check_size_para(list:TAsmList);
  1289. var
  1290. paraloc1 : tcgpara;
  1291. pd : tprocdef;
  1292. begin
  1293. pd:=search_system_proc('fpc_stackcheck');
  1294. paraloc1.init;
  1295. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  1296. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1297. paramanager.freecgpara(list,paraloc1);
  1298. paraloc1.done;
  1299. end;
  1300. procedure gen_stack_check_call(list:TAsmList);
  1301. var
  1302. paraloc1 : tcgpara;
  1303. pd : tprocdef;
  1304. begin
  1305. pd:=search_system_proc('fpc_stackcheck');
  1306. paraloc1.init;
  1307. { Also alloc the register needed for the parameter }
  1308. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  1309. paramanager.freecgpara(list,paraloc1);
  1310. { Call the helper }
  1311. cg.allocallcpuregisters(list);
  1312. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1313. cg.deallocallcpuregisters(list);
  1314. paraloc1.done;
  1315. end;
  1316. procedure gen_save_used_regs(list:TAsmList);
  1317. begin
  1318. { Pure assembler routines need to save the registers themselves }
  1319. if (po_assembler in current_procinfo.procdef.procoptions) then
  1320. exit;
  1321. { oldfpccall expects all registers to be destroyed }
  1322. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1323. cg.g_save_registers(list);
  1324. end;
  1325. procedure gen_restore_used_regs(list:TAsmList);
  1326. begin
  1327. { Pure assembler routines need to save the registers themselves }
  1328. if (po_assembler in current_procinfo.procdef.procoptions) then
  1329. exit;
  1330. { oldfpccall expects all registers to be destroyed }
  1331. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1332. cg.g_restore_registers(list);
  1333. end;
  1334. {****************************************************************************
  1335. External handling
  1336. ****************************************************************************}
  1337. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1338. begin
  1339. create_hlcodegen;
  1340. { add the procedure to the al_procedures }
  1341. maybe_new_object_file(list);
  1342. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1343. if (po_global in pd.procoptions) then
  1344. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1345. else
  1346. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1347. hlcg.g_external_wrapper(list,pd,externalname);
  1348. destroy_hlcodegen;
  1349. end;
  1350. {****************************************************************************
  1351. Const Data
  1352. ****************************************************************************}
  1353. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  1354. var
  1355. i : longint;
  1356. highsym,
  1357. sym : tsym;
  1358. vs : tabstractnormalvarsym;
  1359. ptrdef : tdef;
  1360. isaddr : boolean;
  1361. begin
  1362. for i:=0 to st.SymList.Count-1 do
  1363. begin
  1364. sym:=tsym(st.SymList[i]);
  1365. case sym.typ of
  1366. staticvarsym :
  1367. begin
  1368. vs:=tabstractnormalvarsym(sym);
  1369. { The code in loadnode.pass_generatecode will create the
  1370. LOC_REFERENCE instead for all none register variables. This is
  1371. required because we can't store an asmsymbol in the localloc because
  1372. the asmsymbol is invalid after an unit is compiled. This gives
  1373. problems when this procedure is inlined in another unit (PFV) }
  1374. if vs.is_regvar(false) then
  1375. begin
  1376. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1377. vs.initialloc.size:=def_cgsize(vs.vardef);
  1378. gen_alloc_regvar(list,vs,true);
  1379. hlcg.varsym_set_localloc(list,vs);
  1380. end;
  1381. end;
  1382. paravarsym :
  1383. begin
  1384. vs:=tabstractnormalvarsym(sym);
  1385. { Parameters passed to assembler procedures need to be kept
  1386. in the original location }
  1387. if (po_assembler in pd.procoptions) then
  1388. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1389. { exception filters receive their frame pointer as a parameter }
  1390. else if (pd.proctypeoption=potype_exceptfilter) and
  1391. (vo_is_parentfp in vs.varoptions) then
  1392. begin
  1393. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1394. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1395. end
  1396. else
  1397. begin
  1398. { if an open array is used, also its high parameter is used,
  1399. since the hidden high parameters are inserted after the corresponding symbols,
  1400. we can increase the ref. count here }
  1401. if is_open_array(vs.vardef) or is_array_of_const(vs.vardef) then
  1402. begin
  1403. highsym:=get_high_value_sym(tparavarsym(vs));
  1404. if assigned(highsym) then
  1405. inc(highsym.refs);
  1406. end;
  1407. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,pd.proccalloption);
  1408. if isaddr then
  1409. vs.initialloc.size:=def_cgsize(voidpointertype)
  1410. else
  1411. vs.initialloc.size:=def_cgsize(vs.vardef);
  1412. if vs.is_regvar(isaddr) then
  1413. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1414. else
  1415. begin
  1416. vs.initialloc.loc:=LOC_REFERENCE;
  1417. { Reuse the parameter location for values to are at a single location on the stack }
  1418. if paramanager.param_use_paraloc(tparavarsym(vs).paraloc[calleeside]) then
  1419. begin
  1420. hlcg.paravarsym_set_initialloc_to_paraloc(tparavarsym(vs));
  1421. end
  1422. else
  1423. begin
  1424. if isaddr then
  1425. begin
  1426. ptrdef:=getpointerdef(vs.vardef);
  1427. tg.GetLocal(list,ptrdef.size,ptrdef,vs.initialloc.reference)
  1428. end
  1429. else
  1430. tg.GetLocal(list,vs.getsize,tparavarsym(vs).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1431. end;
  1432. end;
  1433. end;
  1434. hlcg.varsym_set_localloc(list,vs);
  1435. end;
  1436. localvarsym :
  1437. begin
  1438. vs:=tabstractnormalvarsym(sym);
  1439. vs.initialloc.size:=def_cgsize(vs.vardef);
  1440. if ([po_assembler,po_nostackframe] * pd.procoptions = [po_assembler,po_nostackframe]) and
  1441. (vo_is_funcret in vs.varoptions) then
  1442. begin
  1443. paramanager.create_funcretloc_info(pd,calleeside);
  1444. if assigned(pd.funcretloc[calleeside].location^.next) then
  1445. begin
  1446. { can't replace references to "result" with a complex
  1447. location expression inside assembler code }
  1448. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  1449. end
  1450. else
  1451. pd.funcretloc[calleeside].get_location(vs.initialloc);
  1452. end
  1453. else if (m_delphi in current_settings.modeswitches) and
  1454. (po_assembler in pd.procoptions) and
  1455. (vo_is_funcret in vs.varoptions) and
  1456. (vs.refs=0) then
  1457. begin
  1458. { not referenced, so don't allocate. Use dummy to }
  1459. { avoid ie's later on because of LOC_INVALID }
  1460. vs.initialloc.loc:=LOC_REGISTER;
  1461. vs.initialloc.size:=OS_INT;
  1462. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1463. end
  1464. else if vs.is_regvar(false) then
  1465. begin
  1466. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1467. gen_alloc_regvar(list,vs,true);
  1468. end
  1469. else
  1470. begin
  1471. vs.initialloc.loc:=LOC_REFERENCE;
  1472. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1473. end;
  1474. hlcg.varsym_set_localloc(list,vs);
  1475. end;
  1476. end;
  1477. end;
  1478. end;
  1479. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1480. begin
  1481. case location.loc of
  1482. LOC_CREGISTER:
  1483. {$if defined(cpu64bitalu)}
  1484. if location.size in [OS_128,OS_S128] then
  1485. begin
  1486. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  1487. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  1488. end
  1489. else
  1490. {$elseif defined(cpu32bitalu)}
  1491. if location.size in [OS_64,OS_S64] then
  1492. begin
  1493. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1494. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1495. end
  1496. else
  1497. {$elseif defined(cpu16bitalu)}
  1498. if location.size in [OS_64,OS_S64] then
  1499. begin
  1500. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1501. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1502. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1503. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1504. end
  1505. else
  1506. if location.size in [OS_32,OS_S32] then
  1507. begin
  1508. rv.intregvars.addnodup(getsupreg(location.register));
  1509. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1510. end
  1511. else
  1512. {$elseif defined(cpu8bitalu)}
  1513. if location.size in [OS_64,OS_S64] then
  1514. begin
  1515. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1516. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1517. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reglo))));
  1518. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reglo)))));
  1519. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1520. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1521. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reghi))));
  1522. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reghi)))));
  1523. end
  1524. else
  1525. if location.size in [OS_32,OS_S32] then
  1526. begin
  1527. rv.intregvars.addnodup(getsupreg(location.register));
  1528. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1529. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register))));
  1530. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register)))));
  1531. end
  1532. else
  1533. if location.size in [OS_16,OS_S16] then
  1534. begin
  1535. rv.intregvars.addnodup(getsupreg(location.register));
  1536. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1537. end
  1538. else
  1539. {$endif}
  1540. rv.intregvars.addnodup(getsupreg(location.register));
  1541. LOC_CFPUREGISTER:
  1542. rv.fpuregvars.addnodup(getsupreg(location.register));
  1543. LOC_CMMREGISTER:
  1544. rv.mmregvars.addnodup(getsupreg(location.register));
  1545. end;
  1546. end;
  1547. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1548. var
  1549. rv: pusedregvars absolute arg;
  1550. begin
  1551. case (n.nodetype) of
  1552. temprefn:
  1553. { We only have to synchronise a tempnode before a loop if it is }
  1554. { not created inside the loop, and only synchronise after the }
  1555. { loop if it's not destroyed inside the loop. If it's created }
  1556. { before the loop and not yet destroyed, then before the loop }
  1557. { is secondpassed tempinfo^.valid will be true, and we get the }
  1558. { correct registers. If it's not destroyed inside the loop, }
  1559. { then after the loop has been secondpassed tempinfo^.valid }
  1560. { be true and we also get the right registers. In other cases, }
  1561. { tempinfo^.valid will be false and so we do not add }
  1562. { unnecessary registers. This way, we don't have to look at }
  1563. { tempcreate and tempdestroy nodes to get this info (JM) }
  1564. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1565. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1566. loadn:
  1567. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1568. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1569. vecn:
  1570. { range checks sometimes need the high parameter }
  1571. if (cs_check_range in current_settings.localswitches) and
  1572. (is_open_array(tvecnode(n).left.resultdef) or
  1573. is_array_of_const(tvecnode(n).left.resultdef)) and
  1574. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1575. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1576. end;
  1577. result := fen_true;
  1578. end;
  1579. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1580. begin
  1581. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1582. end;
  1583. (*
  1584. See comments at declaration of pusedregvarscommon
  1585. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1586. var
  1587. rv: pusedregvarscommon absolute arg;
  1588. begin
  1589. if (n.nodetype = loadn) and
  1590. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1591. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1592. case loc of
  1593. LOC_CREGISTER:
  1594. { if not yet encountered in this node tree }
  1595. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1596. { but nevertheless already encountered somewhere }
  1597. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1598. { then it's a regvar used in two or more node trees }
  1599. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1600. LOC_CFPUREGISTER:
  1601. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1602. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1603. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1604. LOC_CMMREGISTER:
  1605. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1606. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1607. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1608. end;
  1609. result := fen_true;
  1610. end;
  1611. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1612. begin
  1613. rv.myregvars.intregvars.clear;
  1614. rv.myregvars.fpuregvars.clear;
  1615. rv.myregvars.mmregvars.clear;
  1616. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1617. end;
  1618. *)
  1619. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1620. var
  1621. count: longint;
  1622. begin
  1623. for count := 1 to rv.intregvars.length do
  1624. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1625. for count := 1 to rv.fpuregvars.length do
  1626. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1627. for count := 1 to rv.mmregvars.length do
  1628. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1629. end;
  1630. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1631. var
  1632. i : longint;
  1633. sym : tsym;
  1634. begin
  1635. for i:=0 to st.SymList.Count-1 do
  1636. begin
  1637. sym:=tsym(st.SymList[i]);
  1638. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1639. begin
  1640. with tabstractnormalvarsym(sym) do
  1641. begin
  1642. { Note: We need to keep the data available in memory
  1643. for the sub procedures that can access local data
  1644. in the parent procedures }
  1645. case localloc.loc of
  1646. LOC_CREGISTER :
  1647. if (pi_has_label in current_procinfo.flags) then
  1648. {$if defined(cpu64bitalu)}
  1649. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1650. begin
  1651. cg.a_reg_sync(list,localloc.register128.reglo);
  1652. cg.a_reg_sync(list,localloc.register128.reghi);
  1653. end
  1654. else
  1655. {$elseif defined(cpu32bitalu)}
  1656. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1657. begin
  1658. cg.a_reg_sync(list,localloc.register64.reglo);
  1659. cg.a_reg_sync(list,localloc.register64.reghi);
  1660. end
  1661. else
  1662. {$elseif defined(cpu16bitalu)}
  1663. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1664. begin
  1665. cg.a_reg_sync(list,localloc.register64.reglo);
  1666. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1667. cg.a_reg_sync(list,localloc.register64.reghi);
  1668. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1669. end
  1670. else
  1671. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1672. begin
  1673. cg.a_reg_sync(list,localloc.register);
  1674. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1675. end
  1676. else
  1677. {$elseif defined(cpu8bitalu)}
  1678. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1679. begin
  1680. cg.a_reg_sync(list,localloc.register64.reglo);
  1681. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1682. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reglo)));
  1683. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reglo))));
  1684. cg.a_reg_sync(list,localloc.register64.reghi);
  1685. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1686. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reghi)));
  1687. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reghi))));
  1688. end
  1689. else
  1690. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1691. begin
  1692. cg.a_reg_sync(list,localloc.register);
  1693. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1694. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register)));
  1695. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register))));
  1696. end
  1697. else
  1698. if def_cgsize(vardef) in [OS_16,OS_S16] then
  1699. begin
  1700. cg.a_reg_sync(list,localloc.register);
  1701. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1702. end
  1703. else
  1704. {$endif}
  1705. cg.a_reg_sync(list,localloc.register);
  1706. LOC_CFPUREGISTER,
  1707. LOC_CMMREGISTER:
  1708. if (pi_has_label in current_procinfo.flags) then
  1709. cg.a_reg_sync(list,localloc.register);
  1710. LOC_REFERENCE :
  1711. begin
  1712. if typ in [localvarsym,paravarsym] then
  1713. tg.Ungetlocal(list,localloc.reference);
  1714. end;
  1715. end;
  1716. end;
  1717. end;
  1718. end;
  1719. end;
  1720. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1721. var
  1722. href : treference;
  1723. selfdef: tdef;
  1724. begin
  1725. if is_object(objdef) then
  1726. begin
  1727. case selfloc.loc of
  1728. LOC_CREFERENCE,
  1729. LOC_REFERENCE:
  1730. begin
  1731. hlcg.reference_reset_base(href,voidpointertype,hlcg.getaddressregister(list,voidpointertype),objdef.vmt_offset,voidpointertype.size);
  1732. hlcg.a_loadaddr_ref_reg(list,voidpointertype,voidpointertype,selfloc.reference,href.base);
  1733. selfdef:=getpointerdef(objdef);
  1734. end;
  1735. else
  1736. internalerror(200305056);
  1737. end;
  1738. end
  1739. else
  1740. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  1741. and the first "field" of an Objective-C class instance is a pointer
  1742. to its "meta-class". }
  1743. begin
  1744. selfdef:=objdef;
  1745. case selfloc.loc of
  1746. LOC_REGISTER:
  1747. begin
  1748. {$ifdef cpu_uses_separate_address_registers}
  1749. if getregtype(selfloc.register)<>R_ADDRESSREGISTER then
  1750. begin
  1751. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1752. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  1753. end
  1754. else
  1755. {$endif cpu_uses_separate_address_registers}
  1756. hlcg.reference_reset_base(href,voidpointertype,selfloc.register,objdef.vmt_offset,voidpointertype.size);
  1757. end;
  1758. LOC_CONSTANT,
  1759. LOC_CREGISTER,
  1760. LOC_CREFERENCE,
  1761. LOC_REFERENCE,
  1762. LOC_CSUBSETREG,
  1763. LOC_SUBSETREG,
  1764. LOC_CSUBSETREF,
  1765. LOC_SUBSETREF:
  1766. begin
  1767. hlcg.reference_reset_base(href,voidpointertype,hlcg.getaddressregister(list,voidpointertype),objdef.vmt_offset,voidpointertype.size);
  1768. { todo: pass actual vmt pointer type to hlcg }
  1769. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  1770. end;
  1771. else
  1772. internalerror(200305057);
  1773. end;
  1774. end;
  1775. vmtreg:=hlcg.getaddressregister(list,voidpointertype);
  1776. hlcg.g_maybe_testself(list,selfdef,href.base);
  1777. hlcg.a_load_ref_reg(list,voidpointertype,voidpointertype,href,vmtreg);
  1778. { test validity of VMT }
  1779. if not(is_interface(objdef)) and
  1780. not(is_cppclass(objdef)) and
  1781. not(is_objc_class_or_protocol(objdef)) then
  1782. cg.g_maybe_testvmt(list,vmtreg,objdef);
  1783. end;
  1784. function getprocalign : shortint;
  1785. begin
  1786. { gprof uses 16 byte granularity }
  1787. if (cs_profile in current_settings.moduleswitches) then
  1788. result:=16
  1789. else
  1790. result:=current_settings.alignment.procalign;
  1791. end;
  1792. procedure gen_fpc_dummy(list : TAsmList);
  1793. begin
  1794. {$ifdef i386}
  1795. { fix me! }
  1796. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1797. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  1798. {$endif i386}
  1799. end;
  1800. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  1801. var
  1802. para: tparavarsym;
  1803. begin
  1804. para:=tparavarsym(current_procinfo.procdef.paras[0]);
  1805. if not (vo_is_parentfp in para.varoptions) then
  1806. InternalError(201201142);
  1807. if (para.paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  1808. (para.paraloc[calleeside].location^.next<>nil) then
  1809. InternalError(201201143);
  1810. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,para.paraloc[calleeside].location^.register,
  1811. NR_FRAME_POINTER_REG);
  1812. end;
  1813. end.