ncgutil.pas 88 KB

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