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