ncgutil.pas 95 KB

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