ncgutil.pas 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  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(pd,1,paraloc1);
  366. paramanager.getintparaloc(pd,2,paraloc2);
  367. paramanager.getintparaloc(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(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. procedure copyvalueparas(p:TObject;arg:pointer);
  524. var
  525. href : treference;
  526. hreg : tregister;
  527. list : TAsmList;
  528. hsym : tparavarsym;
  529. l : longint;
  530. localcopyloc : tlocation;
  531. sizedef : tdef;
  532. begin
  533. list:=TAsmList(arg);
  534. if (tsym(p).typ=paravarsym) and
  535. (tparavarsym(p).varspez=vs_value) and
  536. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  537. begin
  538. { we have no idea about the alignment at the caller side }
  539. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  540. if is_open_array(tparavarsym(p).vardef) or
  541. is_array_of_const(tparavarsym(p).vardef) then
  542. begin
  543. { cdecl functions don't have a high pointer so it is not possible to generate
  544. a local copy }
  545. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  546. begin
  547. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  548. if not assigned(hsym) then
  549. internalerror(200306061);
  550. sizedef:=getpointerdef(tparavarsym(p).vardef);
  551. hreg:=hlcg.getaddressregister(list,sizedef);
  552. if not is_packed_array(tparavarsym(p).vardef) then
  553. hlcg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef),hreg)
  554. else
  555. internalerror(2006080401);
  556. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  557. hlcg.a_load_reg_loc(list,sizedef,sizedef,hreg,tparavarsym(p).initialloc);
  558. end;
  559. end
  560. else
  561. begin
  562. { Allocate space for the local copy }
  563. l:=tparavarsym(p).getsize;
  564. localcopyloc.loc:=LOC_REFERENCE;
  565. localcopyloc.size:=int_cgsize(l);
  566. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  567. { Copy data }
  568. if is_shortstring(tparavarsym(p).vardef) then
  569. begin
  570. { this code is only executed before the code for the body and the entry/exit code is generated
  571. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  572. }
  573. include(current_procinfo.flags,pi_do_call);
  574. hlcg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef));
  575. end
  576. else if tparavarsym(p).vardef.typ = variantdef then
  577. begin
  578. { this code is only executed before the code for the body and the entry/exit code is generated
  579. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  580. }
  581. include(current_procinfo.flags,pi_do_call);
  582. hlcg.g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  583. end
  584. else
  585. begin
  586. { pass proper alignment info }
  587. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  588. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  589. end;
  590. { update localloc of varsym }
  591. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  592. tparavarsym(p).localloc:=localcopyloc;
  593. tparavarsym(p).initialloc:=localcopyloc;
  594. end;
  595. end;
  596. end;
  597. { generates the code for incrementing the reference count of parameters and
  598. initialize out parameters }
  599. procedure init_paras(p:TObject;arg:pointer);
  600. var
  601. href : treference;
  602. hsym : tparavarsym;
  603. eldef : tdef;
  604. list : TAsmList;
  605. needs_inittable : boolean;
  606. begin
  607. list:=TAsmList(arg);
  608. if (tsym(p).typ=paravarsym) then
  609. begin
  610. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  611. if not needs_inittable then
  612. exit;
  613. case tparavarsym(p).varspez of
  614. vs_value :
  615. begin
  616. { variants are already handled by the call to fpc_variant_copy_overwrite if
  617. they are passed by reference }
  618. if not((tparavarsym(p).vardef.typ=variantdef) and
  619. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  620. begin
  621. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  622. if is_open_array(tparavarsym(p).vardef) then
  623. begin
  624. { open arrays do not contain correct element count in their rtti,
  625. the actual count must be passed separately. }
  626. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  627. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  628. if not assigned(hsym) then
  629. internalerror(201003031);
  630. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_addref_array');
  631. end
  632. else
  633. hlcg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  634. end;
  635. end;
  636. vs_out :
  637. begin
  638. { we have no idea about the alignment at the callee side,
  639. and the user also cannot specify "unaligned" here, so
  640. assume worst case }
  641. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  642. if is_open_array(tparavarsym(p).vardef) then
  643. begin
  644. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  645. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  646. if not assigned(hsym) then
  647. internalerror(201103033);
  648. hlcg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'fpc_initialize_array');
  649. end
  650. else
  651. hlcg.g_initialize(list,tparavarsym(p).vardef,href);
  652. end;
  653. end;
  654. end;
  655. end;
  656. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  657. begin
  658. case loc.loc of
  659. LOC_CREGISTER:
  660. begin
  661. {$ifdef cpu64bitalu}
  662. if loc.size in [OS_128,OS_S128] then
  663. begin
  664. loc.register128.reglo:=cg.getintregister(list,OS_64);
  665. loc.register128.reghi:=cg.getintregister(list,OS_64);
  666. end
  667. else
  668. {$else cpu64bitalu}
  669. if loc.size in [OS_64,OS_S64] then
  670. begin
  671. loc.register64.reglo:=cg.getintregister(list,OS_32);
  672. loc.register64.reghi:=cg.getintregister(list,OS_32);
  673. end
  674. else
  675. {$endif cpu64bitalu}
  676. loc.register:=cg.getintregister(list,loc.size);
  677. end;
  678. LOC_CFPUREGISTER:
  679. begin
  680. loc.register:=cg.getfpuregister(list,loc.size);
  681. end;
  682. LOC_CMMREGISTER:
  683. begin
  684. loc.register:=cg.getmmregister(list,loc.size);
  685. end;
  686. end;
  687. end;
  688. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  689. begin
  690. if allocreg then
  691. gen_alloc_regloc(list,sym.initialloc);
  692. if (pi_has_label in current_procinfo.flags) then
  693. begin
  694. { Allocate register already, to prevent first allocation to be
  695. inside a loop }
  696. {$if defined(cpu64bitalu)}
  697. if sym.initialloc.size in [OS_128,OS_S128] then
  698. begin
  699. cg.a_reg_sync(list,sym.initialloc.register128.reglo);
  700. cg.a_reg_sync(list,sym.initialloc.register128.reghi);
  701. end
  702. else
  703. {$elseif defined(cpu32bitalu)}
  704. if sym.initialloc.size in [OS_64,OS_S64] then
  705. begin
  706. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  707. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  708. end
  709. else
  710. {$elseif defined(cpu16bitalu)}
  711. if sym.initialloc.size in [OS_64,OS_S64] then
  712. begin
  713. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  714. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reglo));
  715. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  716. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reghi));
  717. end
  718. else
  719. if sym.initialloc.size in [OS_32,OS_S32] then
  720. begin
  721. cg.a_reg_sync(list,sym.initialloc.register);
  722. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  723. end
  724. else
  725. {$elseif defined(cpu8bitalu)}
  726. if sym.initialloc.size in [OS_64,OS_S64] then
  727. begin
  728. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  729. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reglo));
  730. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register64.reglo)));
  731. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register64.reglo))));
  732. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  733. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register64.reghi));
  734. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register64.reghi)));
  735. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register64.reghi))));
  736. end
  737. else
  738. if sym.initialloc.size in [OS_32,OS_S32] then
  739. begin
  740. cg.a_reg_sync(list,sym.initialloc.register);
  741. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  742. cg.a_reg_sync(list,GetNextReg(GetNextReg(sym.initialloc.register)));
  743. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(sym.initialloc.register))));
  744. end
  745. else
  746. if sym.initialloc.size in [OS_16,OS_S16] then
  747. begin
  748. cg.a_reg_sync(list,sym.initialloc.register);
  749. cg.a_reg_sync(list,GetNextReg(sym.initialloc.register));
  750. end
  751. else
  752. {$endif}
  753. cg.a_reg_sync(list,sym.initialloc.register);
  754. end;
  755. sym.localloc:=sym.initialloc;
  756. end;
  757. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  758. procedure unget_para(const paraloc:TCGParaLocation);
  759. begin
  760. case paraloc.loc of
  761. LOC_REGISTER :
  762. begin
  763. if getsupreg(paraloc.register)<first_int_imreg then
  764. cg.ungetcpuregister(list,paraloc.register);
  765. end;
  766. LOC_MMREGISTER :
  767. begin
  768. if getsupreg(paraloc.register)<first_mm_imreg then
  769. cg.ungetcpuregister(list,paraloc.register);
  770. end;
  771. LOC_FPUREGISTER :
  772. begin
  773. if getsupreg(paraloc.register)<first_fpu_imreg then
  774. cg.ungetcpuregister(list,paraloc.register);
  775. end;
  776. end;
  777. end;
  778. var
  779. paraloc : pcgparalocation;
  780. href : treference;
  781. sizeleft : aint;
  782. {$if defined(sparc) or defined(arm) or defined(mips)}
  783. tempref : treference;
  784. {$endif defined(sparc) or defined(arm) or defined(mips)}
  785. {$ifdef mips}
  786. tmpreg : tregister;
  787. {$endif mips}
  788. {$ifndef cpu64bitalu}
  789. tempreg : tregister;
  790. reg64 : tregister64;
  791. {$endif not cpu64bitalu}
  792. begin
  793. paraloc:=para.location;
  794. if not assigned(paraloc) then
  795. internalerror(200408203);
  796. { skip e.g. empty records }
  797. if (paraloc^.loc = LOC_VOID) then
  798. exit;
  799. case destloc.loc of
  800. LOC_REFERENCE :
  801. begin
  802. { If the parameter location is reused we don't need to copy
  803. anything }
  804. if not reusepara then
  805. begin
  806. href:=destloc.reference;
  807. sizeleft:=para.intsize;
  808. while assigned(paraloc) do
  809. begin
  810. if (paraloc^.size=OS_NO) then
  811. begin
  812. { Can only be a reference that contains the rest
  813. of the parameter }
  814. if (paraloc^.loc<>LOC_REFERENCE) or
  815. assigned(paraloc^.next) then
  816. internalerror(2005013010);
  817. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  818. inc(href.offset,sizeleft);
  819. sizeleft:=0;
  820. end
  821. else
  822. begin
  823. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  824. inc(href.offset,TCGSize2Size[paraloc^.size]);
  825. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  826. end;
  827. unget_para(paraloc^);
  828. paraloc:=paraloc^.next;
  829. end;
  830. end;
  831. end;
  832. LOC_REGISTER,
  833. LOC_CREGISTER :
  834. begin
  835. {$ifdef cpu64bitalu}
  836. if (para.size in [OS_128,OS_S128,OS_F128]) and
  837. ({ in case of fpu emulation, or abi's that pass fpu values
  838. via integer registers }
  839. (vardef.typ=floatdef) or
  840. is_methodpointer(vardef) or
  841. is_record(vardef)) then
  842. begin
  843. case paraloc^.loc of
  844. LOC_REGISTER:
  845. begin
  846. if not assigned(paraloc^.next) then
  847. internalerror(200410104);
  848. if (target_info.endian=ENDIAN_BIG) then
  849. begin
  850. { paraloc^ -> high
  851. paraloc^.next -> low }
  852. unget_para(paraloc^);
  853. gen_alloc_regloc(list,destloc);
  854. { reg->reg, alignment is irrelevant }
  855. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reghi,8);
  856. unget_para(paraloc^.next^);
  857. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reglo,8);
  858. end
  859. else
  860. begin
  861. { paraloc^ -> low
  862. paraloc^.next -> high }
  863. unget_para(paraloc^);
  864. gen_alloc_regloc(list,destloc);
  865. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^,destloc.register128.reglo,8);
  866. unget_para(paraloc^.next^);
  867. cg.a_load_cgparaloc_anyreg(list,OS_64,paraloc^.next^,destloc.register128.reghi,8);
  868. end;
  869. end;
  870. LOC_REFERENCE:
  871. begin
  872. gen_alloc_regloc(list,destloc);
  873. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  874. cg128.a_load128_ref_reg(list,href,destloc.register128);
  875. unget_para(paraloc^);
  876. end;
  877. else
  878. internalerror(2012090607);
  879. end
  880. end
  881. else
  882. {$else cpu64bitalu}
  883. if (para.size in [OS_64,OS_S64,OS_F64]) and
  884. (is_64bit(vardef) or
  885. { in case of fpu emulation, or abi's that pass fpu values
  886. via integer registers }
  887. (vardef.typ=floatdef) or
  888. is_methodpointer(vardef) or
  889. is_record(vardef)) then
  890. begin
  891. case paraloc^.loc of
  892. LOC_REGISTER:
  893. begin
  894. case para.locations_count of
  895. {$if defined(cpu16bitalu) or defined(cpu8bitalu)}
  896. { 4 paralocs? }
  897. 4:
  898. if (target_info.endian=ENDIAN_BIG) then
  899. begin
  900. { paraloc^ -> high
  901. paraloc^.next^.next -> low }
  902. unget_para(paraloc^);
  903. gen_alloc_regloc(list,destloc);
  904. { reg->reg, alignment is irrelevant }
  905. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^,GetNextReg(destloc.register64.reghi),2);
  906. unget_para(paraloc^.next^);
  907. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^,destloc.register64.reghi,2);
  908. unget_para(paraloc^.next^.next^);
  909. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^,GetNextReg(destloc.register64.reglo),2);
  910. unget_para(paraloc^.next^.next^.next^);
  911. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^.next^,destloc.register64.reglo,2);
  912. end
  913. else
  914. begin
  915. { paraloc^ -> low
  916. paraloc^.next^.next -> high }
  917. unget_para(paraloc^);
  918. gen_alloc_regloc(list,destloc);
  919. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^,destloc.register64.reglo,2);
  920. unget_para(paraloc^.next^);
  921. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^,GetNextReg(destloc.register64.reglo),2);
  922. unget_para(paraloc^.next^.next^);
  923. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^,destloc.register64.reghi,2);
  924. unget_para(paraloc^.next^.next^.next^);
  925. cg.a_load_cgparaloc_anyreg(list,OS_16,paraloc^.next^.next^.next^,GetNextReg(destloc.register64.reghi),2);
  926. end;
  927. {$endif defined(cpu16bitalu) or defined(cpu8bitalu)}
  928. 2:
  929. if (target_info.endian=ENDIAN_BIG) then
  930. begin
  931. { paraloc^ -> high
  932. paraloc^.next -> low }
  933. unget_para(paraloc^);
  934. gen_alloc_regloc(list,destloc);
  935. { reg->reg, alignment is irrelevant }
  936. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  937. unget_para(paraloc^.next^);
  938. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  939. end
  940. else
  941. begin
  942. { paraloc^ -> low
  943. paraloc^.next -> high }
  944. unget_para(paraloc^);
  945. gen_alloc_regloc(list,destloc);
  946. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  947. unget_para(paraloc^.next^);
  948. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  949. end;
  950. else
  951. { unexpected number of paralocs }
  952. internalerror(200410104);
  953. end;
  954. end;
  955. LOC_REFERENCE:
  956. begin
  957. gen_alloc_regloc(list,destloc);
  958. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  959. cg64.a_load64_ref_reg(list,href,destloc.register64);
  960. unget_para(paraloc^);
  961. end;
  962. else
  963. internalerror(2005101501);
  964. end
  965. end
  966. else
  967. {$endif cpu64bitalu}
  968. begin
  969. if assigned(paraloc^.next) then
  970. begin
  971. if (destloc.size in [OS_PAIR,OS_SPAIR]) and
  972. (para.Size in [OS_PAIR,OS_SPAIR]) then
  973. begin
  974. unget_para(paraloc^);
  975. gen_alloc_regloc(list,destloc);
  976. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^,destloc.register,sizeof(aint));
  977. unget_para(paraloc^.Next^);
  978. {$if defined(cpu16bitalu) or defined(cpu8bitalu)}
  979. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,GetNextReg(destloc.register),sizeof(aint));
  980. {$else}
  981. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,destloc.registerhi,sizeof(aint));
  982. {$endif}
  983. end
  984. {$if defined(cpu8bitalu)}
  985. else if (destloc.size in [OS_32,OS_S32]) and
  986. (para.Size in [OS_32,OS_S32]) then
  987. begin
  988. unget_para(paraloc^);
  989. gen_alloc_regloc(list,destloc);
  990. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^,destloc.register,sizeof(aint));
  991. unget_para(paraloc^.Next^);
  992. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^,GetNextReg(destloc.register),sizeof(aint));
  993. unget_para(paraloc^.Next^.Next^);
  994. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^.Next^,GetNextReg(GetNextReg(destloc.register)),sizeof(aint));
  995. unget_para(paraloc^.Next^.Next^.Next^);
  996. cg.a_load_cgparaloc_anyreg(list,OS_INT,paraloc^.Next^.Next^.Next^,GetNextReg(GetNextReg(GetNextReg(destloc.register))),sizeof(aint));
  997. end
  998. {$endif defined(cpu8bitalu)}
  999. else
  1000. internalerror(200410105);
  1001. end
  1002. else
  1003. begin
  1004. unget_para(paraloc^);
  1005. gen_alloc_regloc(list,destloc);
  1006. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  1007. end;
  1008. end;
  1009. end;
  1010. LOC_FPUREGISTER,
  1011. LOC_CFPUREGISTER :
  1012. begin
  1013. {$ifdef mips}
  1014. if (destloc.size = paraloc^.Size) and
  1015. (paraloc^.Loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  1016. begin
  1017. unget_para(paraloc^);
  1018. gen_alloc_regloc(list,destloc);
  1019. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,para.alignment);
  1020. end
  1021. else if (destloc.size = OS_F32) and
  1022. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1023. begin
  1024. gen_alloc_regloc(list,destloc);
  1025. unget_para(paraloc^);
  1026. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,destloc.register));
  1027. end
  1028. { TODO: Produces invalid code, needs fixing together with regalloc setup. }
  1029. {
  1030. else if (destloc.size = OS_F64) and
  1031. (paraloc^.Loc in [LOC_REGISTER,LOC_CREGISTER]) and
  1032. (paraloc^.next^.Loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1033. begin
  1034. gen_alloc_regloc(list,destloc);
  1035. tmpreg:=destloc.register;
  1036. unget_para(paraloc^);
  1037. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.register,tmpreg));
  1038. setsupreg(tmpreg,getsupreg(tmpreg)+1);
  1039. unget_para(paraloc^.next^);
  1040. list.Concat(taicpu.op_reg_reg(A_MTC1,paraloc^.Next^.register,tmpreg));
  1041. end
  1042. }
  1043. else
  1044. begin
  1045. sizeleft := TCGSize2Size[destloc.size];
  1046. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1047. href:=tempref;
  1048. while assigned(paraloc) do
  1049. begin
  1050. unget_para(paraloc^);
  1051. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1052. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1053. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1054. paraloc:=paraloc^.next;
  1055. end;
  1056. gen_alloc_regloc(list,destloc);
  1057. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1058. tg.UnGetTemp(list,tempref);
  1059. end;
  1060. {$else mips}
  1061. {$if defined(sparc) or defined(arm)}
  1062. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1063. we need a temp }
  1064. sizeleft := TCGSize2Size[destloc.size];
  1065. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1066. href:=tempref;
  1067. while assigned(paraloc) do
  1068. begin
  1069. unget_para(paraloc^);
  1070. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1071. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1072. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1073. paraloc:=paraloc^.next;
  1074. end;
  1075. gen_alloc_regloc(list,destloc);
  1076. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1077. tg.UnGetTemp(list,tempref);
  1078. {$else defined(sparc) or defined(arm)}
  1079. unget_para(paraloc^);
  1080. gen_alloc_regloc(list,destloc);
  1081. { from register to register -> alignment is irrelevant }
  1082. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1083. if assigned(paraloc^.next) then
  1084. internalerror(200410109);
  1085. {$endif defined(sparc) or defined(arm)}
  1086. {$endif mips}
  1087. end;
  1088. LOC_MMREGISTER,
  1089. LOC_CMMREGISTER :
  1090. begin
  1091. {$ifndef cpu64bitalu}
  1092. { ARM vfp floats are passed in integer registers }
  1093. if (para.size=OS_F64) and
  1094. (paraloc^.size in [OS_32,OS_S32]) and
  1095. use_vectorfpu(vardef) then
  1096. begin
  1097. { we need 2x32bit reg }
  1098. if not assigned(paraloc^.next) or
  1099. assigned(paraloc^.next^.next) then
  1100. internalerror(2009112421);
  1101. unget_para(paraloc^.next^);
  1102. case paraloc^.next^.loc of
  1103. LOC_REGISTER:
  1104. tempreg:=paraloc^.next^.register;
  1105. LOC_REFERENCE:
  1106. begin
  1107. tempreg:=cg.getintregister(list,OS_32);
  1108. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,tempreg,4);
  1109. end;
  1110. else
  1111. internalerror(2012051301);
  1112. end;
  1113. { don't free before the above, because then the getintregister
  1114. could reallocate this register and overwrite it }
  1115. unget_para(paraloc^);
  1116. gen_alloc_regloc(list,destloc);
  1117. if (target_info.endian=endian_big) then
  1118. { paraloc^ -> high
  1119. paraloc^.next -> low }
  1120. reg64:=joinreg64(tempreg,paraloc^.register)
  1121. else
  1122. reg64:=joinreg64(paraloc^.register,tempreg);
  1123. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1124. end
  1125. else
  1126. {$endif not cpu64bitalu}
  1127. begin
  1128. unget_para(paraloc^);
  1129. gen_alloc_regloc(list,destloc);
  1130. { from register to register -> alignment is irrelevant }
  1131. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1132. { data could come in two memory locations, for now
  1133. we simply ignore the sanity check (FK)
  1134. if assigned(paraloc^.next) then
  1135. internalerror(200410108);
  1136. }
  1137. end;
  1138. end;
  1139. else
  1140. internalerror(2010052903);
  1141. end;
  1142. end;
  1143. procedure gen_load_para_value(list:TAsmList);
  1144. procedure get_para(const paraloc:TCGParaLocation);
  1145. begin
  1146. case paraloc.loc of
  1147. LOC_REGISTER :
  1148. begin
  1149. if getsupreg(paraloc.register)<first_int_imreg then
  1150. cg.getcpuregister(list,paraloc.register);
  1151. end;
  1152. LOC_MMREGISTER :
  1153. begin
  1154. if getsupreg(paraloc.register)<first_mm_imreg then
  1155. cg.getcpuregister(list,paraloc.register);
  1156. end;
  1157. LOC_FPUREGISTER :
  1158. begin
  1159. if getsupreg(paraloc.register)<first_fpu_imreg then
  1160. cg.getcpuregister(list,paraloc.register);
  1161. end;
  1162. end;
  1163. end;
  1164. var
  1165. i : longint;
  1166. currpara : tparavarsym;
  1167. paraloc : pcgparalocation;
  1168. begin
  1169. if (po_assembler in current_procinfo.procdef.procoptions) or
  1170. { exceptfilters have a single hidden 'parentfp' parameter, which
  1171. is handled by tcg.g_proc_entry. }
  1172. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  1173. exit;
  1174. { Allocate registers used by parameters }
  1175. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1176. begin
  1177. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1178. paraloc:=currpara.paraloc[calleeside].location;
  1179. while assigned(paraloc) do
  1180. begin
  1181. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1182. get_para(paraloc^);
  1183. paraloc:=paraloc^.next;
  1184. end;
  1185. end;
  1186. { Copy parameters to local references/registers }
  1187. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1188. begin
  1189. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1190. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1191. { gen_load_cgpara_loc() already allocated the initialloc
  1192. -> don't allocate again }
  1193. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1194. gen_alloc_regvar(list,currpara,false);
  1195. end;
  1196. { generate copies of call by value parameters, must be done before
  1197. the initialization and body is parsed because the refcounts are
  1198. incremented using the local copies }
  1199. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1200. {$ifdef powerpc}
  1201. { unget the register that contains the stack pointer before the procedure entry, }
  1202. { which is used to access the parameters in their original callee-side location }
  1203. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1204. cg.a_reg_dealloc(list,NR_R12);
  1205. {$endif powerpc}
  1206. {$ifdef powerpc64}
  1207. { unget the register that contains the stack pointer before the procedure entry, }
  1208. { which is used to access the parameters in their original callee-side location }
  1209. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1210. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1211. {$endif powerpc64}
  1212. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1213. begin
  1214. { initialize refcounted paras, and trash others. Needed here
  1215. instead of in gen_initialize_code, because when a reference is
  1216. intialised or trashed while the pointer to that reference is kept
  1217. in a regvar, we add a register move and that one again has to
  1218. come after the parameter loading code as far as the register
  1219. allocator is concerned }
  1220. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1221. end;
  1222. end;
  1223. {****************************************************************************
  1224. Entry/Exit
  1225. ****************************************************************************}
  1226. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1227. var
  1228. item : TCmdStrListItem;
  1229. begin
  1230. result:=true;
  1231. if pd.mangledname=s then
  1232. exit;
  1233. item := TCmdStrListItem(pd.aliasnames.first);
  1234. while assigned(item) do
  1235. begin
  1236. if item.str=s then
  1237. exit;
  1238. item := TCmdStrListItem(item.next);
  1239. end;
  1240. result:=false;
  1241. end;
  1242. procedure alloc_proc_symbol(pd: tprocdef);
  1243. var
  1244. item : TCmdStrListItem;
  1245. begin
  1246. item := TCmdStrListItem(pd.aliasnames.first);
  1247. while assigned(item) do
  1248. begin
  1249. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1250. item := TCmdStrListItem(item.next);
  1251. end;
  1252. end;
  1253. procedure gen_proc_entry_code(list:TAsmList);
  1254. var
  1255. hitemp,
  1256. lotemp, stack_frame_size : longint;
  1257. begin
  1258. { generate call frame marker for dwarf call frame info }
  1259. current_asmdata.asmcfi.start_frame(list);
  1260. { All temps are know, write offsets used for information }
  1261. if (cs_asm_source in current_settings.globalswitches) and
  1262. (current_procinfo.tempstart<>tg.lasttemp) then
  1263. begin
  1264. if tg.direction>0 then
  1265. begin
  1266. lotemp:=current_procinfo.tempstart;
  1267. hitemp:=tg.lasttemp;
  1268. end
  1269. else
  1270. begin
  1271. lotemp:=tg.lasttemp;
  1272. hitemp:=current_procinfo.tempstart;
  1273. end;
  1274. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1275. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1276. end;
  1277. { generate target specific proc entry code }
  1278. stack_frame_size := current_procinfo.calc_stackframe_size;
  1279. if (stack_frame_size <> 0) and
  1280. (po_nostackframe in current_procinfo.procdef.procoptions) then
  1281. message1(parser_e_nostackframe_with_locals,tostr(stack_frame_size));
  1282. hlcg.g_proc_entry(list,stack_frame_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1283. end;
  1284. procedure gen_proc_exit_code(list:TAsmList);
  1285. var
  1286. parasize : longint;
  1287. begin
  1288. { c style clearstack does not need to remove parameters from the stack, only the
  1289. return value when it was pushed by arguments }
  1290. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1291. begin
  1292. parasize:=0;
  1293. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1294. inc(parasize,sizeof(pint));
  1295. end
  1296. else
  1297. begin
  1298. parasize:=current_procinfo.para_stack_size;
  1299. { the parent frame pointer para has to be removed by the caller in
  1300. case of Delphi-style parent frame pointer passing }
  1301. if not paramanager.use_fixed_stack and
  1302. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1303. dec(parasize,sizeof(pint));
  1304. end;
  1305. { generate target specific proc exit code }
  1306. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1307. { release return registers, needed for optimizer }
  1308. if not is_void(current_procinfo.procdef.returndef) then
  1309. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1310. { end of frame marker for call frame info }
  1311. current_asmdata.asmcfi.end_frame(list);
  1312. end;
  1313. procedure gen_stack_check_size_para(list:TAsmList);
  1314. var
  1315. paraloc1 : tcgpara;
  1316. pd : tprocdef;
  1317. begin
  1318. pd:=search_system_proc('fpc_stackcheck');
  1319. paraloc1.init;
  1320. paramanager.getintparaloc(pd,1,paraloc1);
  1321. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1322. paramanager.freecgpara(list,paraloc1);
  1323. paraloc1.done;
  1324. end;
  1325. procedure gen_stack_check_call(list:TAsmList);
  1326. var
  1327. paraloc1 : tcgpara;
  1328. pd : tprocdef;
  1329. begin
  1330. pd:=search_system_proc('fpc_stackcheck');
  1331. paraloc1.init;
  1332. { Also alloc the register needed for the parameter }
  1333. paramanager.getintparaloc(pd,1,paraloc1);
  1334. paramanager.freecgpara(list,paraloc1);
  1335. { Call the helper }
  1336. cg.allocallcpuregisters(list);
  1337. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1338. cg.deallocallcpuregisters(list);
  1339. paraloc1.done;
  1340. end;
  1341. procedure gen_save_used_regs(list:TAsmList);
  1342. begin
  1343. { Pure assembler routines need to save the registers themselves }
  1344. if (po_assembler in current_procinfo.procdef.procoptions) then
  1345. exit;
  1346. { oldfpccall expects all registers to be destroyed }
  1347. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1348. cg.g_save_registers(list);
  1349. end;
  1350. procedure gen_restore_used_regs(list:TAsmList);
  1351. begin
  1352. { Pure assembler routines need to save the registers themselves }
  1353. if (po_assembler in current_procinfo.procdef.procoptions) then
  1354. exit;
  1355. { oldfpccall expects all registers to be destroyed }
  1356. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1357. cg.g_restore_registers(list);
  1358. end;
  1359. {****************************************************************************
  1360. External handling
  1361. ****************************************************************************}
  1362. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1363. begin
  1364. create_hlcodegen;
  1365. { add the procedure to the al_procedures }
  1366. maybe_new_object_file(list);
  1367. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1368. if (po_global in pd.procoptions) then
  1369. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1370. else
  1371. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1372. cg.g_external_wrapper(list,pd,externalname);
  1373. destroy_hlcodegen;
  1374. end;
  1375. {****************************************************************************
  1376. Const Data
  1377. ****************************************************************************}
  1378. procedure gen_alloc_symtable(list:TAsmList;pd:tprocdef;st:TSymtable);
  1379. var
  1380. i : longint;
  1381. highsym,
  1382. sym : tsym;
  1383. vs : tabstractnormalvarsym;
  1384. ptrdef : tdef;
  1385. isaddr : boolean;
  1386. begin
  1387. for i:=0 to st.SymList.Count-1 do
  1388. begin
  1389. sym:=tsym(st.SymList[i]);
  1390. case sym.typ of
  1391. staticvarsym :
  1392. begin
  1393. vs:=tabstractnormalvarsym(sym);
  1394. { The code in loadnode.pass_generatecode will create the
  1395. LOC_REFERENCE instead for all none register variables. This is
  1396. required because we can't store an asmsymbol in the localloc because
  1397. the asmsymbol is invalid after an unit is compiled. This gives
  1398. problems when this procedure is inlined in another unit (PFV) }
  1399. if vs.is_regvar(false) then
  1400. begin
  1401. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1402. vs.initialloc.size:=def_cgsize(vs.vardef);
  1403. gen_alloc_regvar(list,vs,true);
  1404. hlcg.varsym_set_localloc(list,vs);
  1405. end;
  1406. end;
  1407. paravarsym :
  1408. begin
  1409. vs:=tabstractnormalvarsym(sym);
  1410. { Parameters passed to assembler procedures need to be kept
  1411. in the original location }
  1412. if (po_assembler in pd.procoptions) then
  1413. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  1414. { exception filters receive their frame pointer as a parameter }
  1415. else if (pd.proctypeoption=potype_exceptfilter) and
  1416. (vo_is_parentfp in vs.varoptions) then
  1417. begin
  1418. location_reset(vs.initialloc,LOC_REGISTER,OS_ADDR);
  1419. vs.initialloc.register:=NR_FRAME_POINTER_REG;
  1420. end
  1421. else
  1422. begin
  1423. { if an open array is used, also its high parameter is used,
  1424. since the hidden high parameters are inserted after the corresponding symbols,
  1425. we can increase the ref. count here }
  1426. if is_open_array(vs.vardef) or is_array_of_const(vs.vardef) then
  1427. begin
  1428. highsym:=get_high_value_sym(tparavarsym(vs));
  1429. if assigned(highsym) then
  1430. inc(highsym.refs);
  1431. end;
  1432. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,pd.proccalloption);
  1433. if isaddr then
  1434. vs.initialloc.size:=def_cgsize(voidpointertype)
  1435. else
  1436. vs.initialloc.size:=def_cgsize(vs.vardef);
  1437. if vs.is_regvar(isaddr) then
  1438. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  1439. else
  1440. begin
  1441. vs.initialloc.loc:=LOC_REFERENCE;
  1442. { Reuse the parameter location for values to are at a single location on the stack }
  1443. if paramanager.param_use_paraloc(tparavarsym(vs).paraloc[calleeside]) then
  1444. begin
  1445. hlcg.paravarsym_set_initialloc_to_paraloc(tparavarsym(vs));
  1446. end
  1447. else
  1448. begin
  1449. if isaddr then
  1450. begin
  1451. ptrdef:=getpointerdef(vs.vardef);
  1452. tg.GetLocal(list,ptrdef.size,ptrdef,vs.initialloc.reference)
  1453. end
  1454. else
  1455. tg.GetLocal(list,vs.getsize,tparavarsym(vs).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  1456. end;
  1457. end;
  1458. end;
  1459. hlcg.varsym_set_localloc(list,vs);
  1460. end;
  1461. localvarsym :
  1462. begin
  1463. vs:=tabstractnormalvarsym(sym);
  1464. vs.initialloc.size:=def_cgsize(vs.vardef);
  1465. if ([po_assembler,po_nostackframe] * pd.procoptions = [po_assembler,po_nostackframe]) and
  1466. (vo_is_funcret in vs.varoptions) then
  1467. begin
  1468. paramanager.create_funcretloc_info(pd,calleeside);
  1469. if assigned(pd.funcretloc[calleeside].location^.next) then
  1470. begin
  1471. { can't replace references to "result" with a complex
  1472. location expression inside assembler code }
  1473. location_reset(vs.initialloc,LOC_INVALID,OS_NO);
  1474. end
  1475. else
  1476. pd.funcretloc[calleeside].get_location(vs.initialloc);
  1477. end
  1478. else if (m_delphi in current_settings.modeswitches) and
  1479. (po_assembler in pd.procoptions) and
  1480. (vo_is_funcret in vs.varoptions) and
  1481. (vs.refs=0) then
  1482. begin
  1483. { not referenced, so don't allocate. Use dummy to }
  1484. { avoid ie's later on because of LOC_INVALID }
  1485. vs.initialloc.loc:=LOC_REGISTER;
  1486. vs.initialloc.size:=OS_INT;
  1487. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  1488. end
  1489. else if vs.is_regvar(false) then
  1490. begin
  1491. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  1492. gen_alloc_regvar(list,vs,true);
  1493. end
  1494. else
  1495. begin
  1496. vs.initialloc.loc:=LOC_REFERENCE;
  1497. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  1498. end;
  1499. hlcg.varsym_set_localloc(list,vs);
  1500. end;
  1501. end;
  1502. end;
  1503. end;
  1504. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1505. begin
  1506. case location.loc of
  1507. LOC_CREGISTER:
  1508. {$if defined(cpu64bitalu)}
  1509. if location.size in [OS_128,OS_S128] then
  1510. begin
  1511. rv.intregvars.addnodup(getsupreg(location.register128.reglo));
  1512. rv.intregvars.addnodup(getsupreg(location.register128.reghi));
  1513. end
  1514. else
  1515. {$elseif defined(cpu32bitalu)}
  1516. if location.size in [OS_64,OS_S64] then
  1517. begin
  1518. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1519. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1520. end
  1521. else
  1522. {$elseif defined(cpu16bitalu)}
  1523. if location.size in [OS_64,OS_S64] then
  1524. begin
  1525. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1526. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1527. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1528. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1529. end
  1530. else
  1531. if location.size in [OS_32,OS_S32] then
  1532. begin
  1533. rv.intregvars.addnodup(getsupreg(location.register));
  1534. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1535. end
  1536. else
  1537. {$elseif defined(cpu8bitalu)}
  1538. if location.size in [OS_64,OS_S64] then
  1539. begin
  1540. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1541. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reglo)));
  1542. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reglo))));
  1543. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reglo)))));
  1544. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1545. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register64.reghi)));
  1546. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register64.reghi))));
  1547. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register64.reghi)))));
  1548. end
  1549. else
  1550. if location.size in [OS_32,OS_S32] then
  1551. begin
  1552. rv.intregvars.addnodup(getsupreg(location.register));
  1553. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1554. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(location.register))));
  1555. rv.intregvars.addnodup(getsupreg(GetNextReg(GetNextReg(GetNextReg(location.register)))));
  1556. end
  1557. else
  1558. if location.size in [OS_16,OS_S16] then
  1559. begin
  1560. rv.intregvars.addnodup(getsupreg(location.register));
  1561. rv.intregvars.addnodup(getsupreg(GetNextReg(location.register)));
  1562. end
  1563. else
  1564. {$endif}
  1565. rv.intregvars.addnodup(getsupreg(location.register));
  1566. LOC_CFPUREGISTER:
  1567. rv.fpuregvars.addnodup(getsupreg(location.register));
  1568. LOC_CMMREGISTER:
  1569. rv.mmregvars.addnodup(getsupreg(location.register));
  1570. end;
  1571. end;
  1572. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1573. var
  1574. rv: pusedregvars absolute arg;
  1575. begin
  1576. case (n.nodetype) of
  1577. temprefn:
  1578. { We only have to synchronise a tempnode before a loop if it is }
  1579. { not created inside the loop, and only synchronise after the }
  1580. { loop if it's not destroyed inside the loop. If it's created }
  1581. { before the loop and not yet destroyed, then before the loop }
  1582. { is secondpassed tempinfo^.valid will be true, and we get the }
  1583. { correct registers. If it's not destroyed inside the loop, }
  1584. { then after the loop has been secondpassed tempinfo^.valid }
  1585. { be true and we also get the right registers. In other cases, }
  1586. { tempinfo^.valid will be false and so we do not add }
  1587. { unnecessary registers. This way, we don't have to look at }
  1588. { tempcreate and tempdestroy nodes to get this info (JM) }
  1589. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  1590. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1591. loadn:
  1592. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1593. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1594. vecn:
  1595. { range checks sometimes need the high parameter }
  1596. if (cs_check_range in current_settings.localswitches) and
  1597. (is_open_array(tvecnode(n).left.resultdef) or
  1598. is_array_of_const(tvecnode(n).left.resultdef)) and
  1599. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1600. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  1601. end;
  1602. result := fen_true;
  1603. end;
  1604. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1605. begin
  1606. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1607. end;
  1608. (*
  1609. See comments at declaration of pusedregvarscommon
  1610. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1611. var
  1612. rv: pusedregvarscommon absolute arg;
  1613. begin
  1614. if (n.nodetype = loadn) and
  1615. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  1616. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1617. case loc of
  1618. LOC_CREGISTER:
  1619. { if not yet encountered in this node tree }
  1620. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1621. { but nevertheless already encountered somewhere }
  1622. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1623. { then it's a regvar used in two or more node trees }
  1624. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1625. LOC_CFPUREGISTER:
  1626. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1627. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1628. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1629. LOC_CMMREGISTER:
  1630. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1631. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1632. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1633. end;
  1634. result := fen_true;
  1635. end;
  1636. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  1637. begin
  1638. rv.myregvars.intregvars.clear;
  1639. rv.myregvars.fpuregvars.clear;
  1640. rv.myregvars.mmregvars.clear;
  1641. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  1642. end;
  1643. *)
  1644. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  1645. var
  1646. count: longint;
  1647. begin
  1648. for count := 1 to rv.intregvars.length do
  1649. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  1650. for count := 1 to rv.fpuregvars.length do
  1651. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  1652. for count := 1 to rv.mmregvars.length do
  1653. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  1654. end;
  1655. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  1656. var
  1657. i : longint;
  1658. sym : tsym;
  1659. begin
  1660. for i:=0 to st.SymList.Count-1 do
  1661. begin
  1662. sym:=tsym(st.SymList[i]);
  1663. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  1664. begin
  1665. with tabstractnormalvarsym(sym) do
  1666. begin
  1667. { Note: We need to keep the data available in memory
  1668. for the sub procedures that can access local data
  1669. in the parent procedures }
  1670. case localloc.loc of
  1671. LOC_CREGISTER :
  1672. if (pi_has_label in current_procinfo.flags) then
  1673. {$if defined(cpu64bitalu)}
  1674. if def_cgsize(vardef) in [OS_128,OS_S128] then
  1675. begin
  1676. cg.a_reg_sync(list,localloc.register128.reglo);
  1677. cg.a_reg_sync(list,localloc.register128.reghi);
  1678. end
  1679. else
  1680. {$elseif defined(cpu32bitalu)}
  1681. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1682. begin
  1683. cg.a_reg_sync(list,localloc.register64.reglo);
  1684. cg.a_reg_sync(list,localloc.register64.reghi);
  1685. end
  1686. else
  1687. {$elseif defined(cpu16bitalu)}
  1688. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1689. begin
  1690. cg.a_reg_sync(list,localloc.register64.reglo);
  1691. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1692. cg.a_reg_sync(list,localloc.register64.reghi);
  1693. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1694. end
  1695. else
  1696. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1697. begin
  1698. cg.a_reg_sync(list,localloc.register);
  1699. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1700. end
  1701. else
  1702. {$elseif defined(cpu8bitalu)}
  1703. if def_cgsize(vardef) in [OS_64,OS_S64] then
  1704. begin
  1705. cg.a_reg_sync(list,localloc.register64.reglo);
  1706. cg.a_reg_sync(list,GetNextReg(localloc.register64.reglo));
  1707. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reglo)));
  1708. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reglo))));
  1709. cg.a_reg_sync(list,localloc.register64.reghi);
  1710. cg.a_reg_sync(list,GetNextReg(localloc.register64.reghi));
  1711. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register64.reghi)));
  1712. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register64.reghi))));
  1713. end
  1714. else
  1715. if def_cgsize(vardef) in [OS_32,OS_S32] then
  1716. begin
  1717. cg.a_reg_sync(list,localloc.register);
  1718. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1719. cg.a_reg_sync(list,GetNextReg(GetNextReg(localloc.register)));
  1720. cg.a_reg_sync(list,GetNextReg(GetNextReg(GetNextReg(localloc.register))));
  1721. end
  1722. else
  1723. if def_cgsize(vardef) in [OS_16,OS_S16] then
  1724. begin
  1725. cg.a_reg_sync(list,localloc.register);
  1726. cg.a_reg_sync(list,GetNextReg(localloc.register));
  1727. end
  1728. else
  1729. {$endif}
  1730. cg.a_reg_sync(list,localloc.register);
  1731. LOC_CFPUREGISTER,
  1732. LOC_CMMREGISTER:
  1733. if (pi_has_label in current_procinfo.flags) then
  1734. cg.a_reg_sync(list,localloc.register);
  1735. LOC_REFERENCE :
  1736. begin
  1737. if typ in [localvarsym,paravarsym] then
  1738. tg.Ungetlocal(list,localloc.reference);
  1739. end;
  1740. end;
  1741. end;
  1742. end;
  1743. end;
  1744. end;
  1745. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1746. var
  1747. href : treference;
  1748. selfdef: tdef;
  1749. begin
  1750. if is_object(objdef) then
  1751. begin
  1752. case selfloc.loc of
  1753. LOC_CREFERENCE,
  1754. LOC_REFERENCE:
  1755. begin
  1756. hlcg.reference_reset_base(href,voidpointertype,hlcg.getaddressregister(list,voidpointertype),objdef.vmt_offset,voidpointertype.size);
  1757. hlcg.a_loadaddr_ref_reg(list,voidpointertype,voidpointertype,selfloc.reference,href.base);
  1758. selfdef:=getpointerdef(objdef);
  1759. end;
  1760. else
  1761. internalerror(200305056);
  1762. end;
  1763. end
  1764. else
  1765. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  1766. and the first "field" of an Objective-C class instance is a pointer
  1767. to its "meta-class". }
  1768. begin
  1769. selfdef:=objdef;
  1770. case selfloc.loc of
  1771. LOC_REGISTER:
  1772. begin
  1773. {$ifdef cpu_uses_separate_address_registers}
  1774. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  1775. begin
  1776. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  1777. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  1778. end
  1779. else
  1780. {$endif cpu_uses_separate_address_registers}
  1781. hlcg.reference_reset_base(href,voidpointertype,selfloc.register,objdef.vmt_offset,voidpointertype.size);
  1782. end;
  1783. LOC_CONSTANT,
  1784. LOC_CREGISTER,
  1785. LOC_CREFERENCE,
  1786. LOC_REFERENCE,
  1787. LOC_CSUBSETREG,
  1788. LOC_SUBSETREG,
  1789. LOC_CSUBSETREF,
  1790. LOC_SUBSETREF:
  1791. begin
  1792. hlcg.reference_reset_base(href,voidpointertype,hlcg.getaddressregister(list,voidpointertype),objdef.vmt_offset,voidpointertype.size);
  1793. { todo: pass actual vmt pointer type to hlcg }
  1794. hlcg.a_load_loc_reg(list,voidpointertype,voidpointertype,selfloc,href.base);
  1795. end;
  1796. else
  1797. internalerror(200305057);
  1798. end;
  1799. end;
  1800. vmtreg:=hlcg.getaddressregister(list,voidpointertype);
  1801. hlcg.g_maybe_testself(list,selfdef,href.base);
  1802. hlcg.a_load_ref_reg(list,voidpointertype,voidpointertype,href,vmtreg);
  1803. { test validity of VMT }
  1804. if not(is_interface(objdef)) and
  1805. not(is_cppclass(objdef)) and
  1806. not(is_objc_class_or_protocol(objdef)) then
  1807. cg.g_maybe_testvmt(list,vmtreg,objdef);
  1808. end;
  1809. function getprocalign : shortint;
  1810. begin
  1811. { gprof uses 16 byte granularity }
  1812. if (cs_profile in current_settings.moduleswitches) then
  1813. result:=16
  1814. else
  1815. result:=current_settings.alignment.procalign;
  1816. end;
  1817. procedure gen_fpc_dummy(list : TAsmList);
  1818. begin
  1819. {$ifdef i386}
  1820. { fix me! }
  1821. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1822. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  1823. {$endif i386}
  1824. end;
  1825. procedure gen_load_frame_for_exceptfilter(list : TAsmList);
  1826. var
  1827. para: tparavarsym;
  1828. begin
  1829. para:=tparavarsym(current_procinfo.procdef.paras[0]);
  1830. if not (vo_is_parentfp in para.varoptions) then
  1831. InternalError(201201142);
  1832. if (para.paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  1833. (para.paraloc[calleeside].location^.next<>nil) then
  1834. InternalError(201201143);
  1835. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,para.paraloc[calleeside].location^.register,
  1836. NR_FRAME_POINTER_REG);
  1837. end;
  1838. end.