ncgutil.pas 85 KB

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