ncgutil.pas 82 KB

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