ncgutil.pas 90 KB

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