ncgutil.pas 85 KB

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