ncgutil.pas 91 KB

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