ncgutil.pas 83 KB

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