ncgutil.pas 90 KB

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