ncgutil.pas 85 KB

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