ncgutil.pas 84 KB

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