ncgutil.pas 94 KB

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