ncgutil.pas 95 KB

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