ncgutil.pas 95 KB

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