ncgutil.pas 93 KB

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