ncgutil.pas 87 KB

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