ncgutil.pas 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205
  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,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. procedure firstcomplex(p : tbinarynode);
  33. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  34. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  35. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  36. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  37. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  38. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  39. { Retrieve the location of the data pointed to in location l, when the location is
  40. a register it is expected to contain the address of the data }
  41. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  42. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  43. procedure gen_proc_symbol(list:Taasmoutput);
  44. procedure gen_proc_symbol_end(list:Taasmoutput);
  45. procedure gen_proc_entry_code(list:Taasmoutput);
  46. procedure gen_proc_exit_code(list:Taasmoutput);
  47. procedure gen_stack_check_size_para(list:Taasmoutput);
  48. procedure gen_stack_check_call(list:Taasmoutput);
  49. procedure gen_save_used_regs(list:TAAsmoutput);
  50. procedure gen_restore_used_regs(list:TAAsmoutput);
  51. procedure gen_initialize_code(list:TAAsmoutput);
  52. procedure gen_finalize_code(list:TAAsmoutput);
  53. procedure gen_entry_code(list:TAAsmoutput);
  54. procedure gen_exit_code(list:TAAsmoutput);
  55. procedure gen_load_para_value(list:TAAsmoutput);
  56. procedure gen_load_return_value(list:TAAsmoutput);
  57. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  58. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  59. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  60. {#
  61. Allocate the buffers for exception management and setjmp environment.
  62. Return a pointer to these buffers, send them to the utility routine
  63. so they are registered, and then call setjmp.
  64. Then compare the result of setjmp with 0, and if not equal
  65. to zero, then jump to exceptlabel.
  66. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  67. It is to note that this routine may be called *after* the stackframe of a
  68. routine has been called, therefore on machines where the stack cannot
  69. be modified, all temps should be allocated on the heap instead of the
  70. stack.
  71. }
  72. const
  73. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  74. type
  75. texceptiontemps=record
  76. jmpbuf,
  77. envbuf,
  78. reasonbuf : treference;
  79. end;
  80. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  81. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  82. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  83. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  84. procedure insertbssdata(sym : tglobalvarsym);
  85. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  86. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  87. { rtti and init/final }
  88. procedure generate_rtti(p:Ttypesym);
  89. procedure generate_inittable(p:tsym);
  90. procedure location_free(list: taasmoutput; const location : TLocation);
  91. implementation
  92. uses
  93. version,
  94. cutils,cclasses,
  95. globals,systems,verbose,
  96. ppu,defutil,
  97. procinfo,paramgr,fmodule,
  98. regvars,dwarf,dbgbase,
  99. pass_1,pass_2,
  100. ncon,nld,nutils,
  101. tgobj,cgobj;
  102. {*****************************************************************************
  103. Misc Helpers
  104. *****************************************************************************}
  105. procedure location_free(list: taasmoutput; const location : TLocation);
  106. begin
  107. case location.loc of
  108. LOC_VOID:
  109. ;
  110. LOC_REGISTER,
  111. LOC_CREGISTER:
  112. begin
  113. if getsupreg(location.register)<first_int_imreg then
  114. cg.ungetcpuregister(list,location.register);
  115. end;
  116. LOC_FPUREGISTER,
  117. LOC_CFPUREGISTER:
  118. begin
  119. if getsupreg(location.register)<first_fpu_imreg then
  120. cg.ungetcpuregister(list,location.register);
  121. end;
  122. LOC_MMREGISTER,
  123. LOC_CMMREGISTER :
  124. begin
  125. if getsupreg(location.register)<first_mm_imreg then
  126. cg.ungetcpuregister(list,location.register);
  127. end;
  128. LOC_REFERENCE,
  129. LOC_CREFERENCE :
  130. begin
  131. {$ifdef cputargethasfixedstack}
  132. location_freetemp(list,location);
  133. {$endif cputargethasfixedstack}
  134. end;
  135. else
  136. internalerror(2004110211);
  137. end;
  138. end;
  139. { DO NOT RELY on the fact that the tnode is not yet swaped
  140. because of inlining code PM }
  141. procedure firstcomplex(p : tbinarynode);
  142. var
  143. hp : tnode;
  144. begin
  145. { always calculate boolean AND and OR from left to right }
  146. if (p.nodetype in [orn,andn]) and
  147. is_boolean(p.left.resulttype.def) then
  148. begin
  149. if nf_swaped in p.flags then
  150. internalerror(234234);
  151. end
  152. else
  153. if (
  154. (p.expectloc=LOC_FPUREGISTER) and
  155. (p.right.registersfpu > p.left.registersfpu)
  156. ) or
  157. (
  158. (
  159. (
  160. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  161. (p.expectloc<>LOC_FPUREGISTER)
  162. ) and
  163. (p.left.registersint<p.right.registersint)
  164. )
  165. ) then
  166. begin
  167. hp:=p.left;
  168. p.left:=p.right;
  169. p.right:=hp;
  170. if nf_swaped in p.flags then
  171. exclude(p.flags,nf_swaped)
  172. else
  173. include(p.flags,nf_swaped);
  174. end;
  175. end;
  176. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  177. {
  178. produces jumps to true respectively false labels using boolean expressions
  179. depending on whether the loading of regvars is currently being
  180. synchronized manually (such as in an if-node) or automatically (most of
  181. the other cases where this procedure is called), loadregvars can be
  182. "lr_load_regvars" or "lr_dont_load_regvars"
  183. }
  184. var
  185. opsize : tcgsize;
  186. storepos : tfileposinfo;
  187. begin
  188. if nf_error in p.flags then
  189. exit;
  190. storepos:=aktfilepos;
  191. aktfilepos:=p.fileinfo;
  192. if is_boolean(p.resulttype.def) then
  193. begin
  194. {$ifdef OLDREGVARS}
  195. if loadregvars = lr_load_regvars then
  196. load_all_regvars(list);
  197. {$endif OLDREGVARS}
  198. if is_constboolnode(p) then
  199. begin
  200. if tordconstnode(p).value<>0 then
  201. cg.a_jmp_always(list,truelabel)
  202. else
  203. cg.a_jmp_always(list,falselabel)
  204. end
  205. else
  206. begin
  207. opsize:=def_cgsize(p.resulttype.def);
  208. case p.location.loc of
  209. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  210. begin
  211. {$ifdef OLDREGVARS}
  212. if (p.location.loc = LOC_CREGISTER) then
  213. load_regvar_reg(list,p.location.register);
  214. {$endif OLDREGVARS}
  215. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,truelabel);
  216. cg.a_jmp_always(list,falselabel);
  217. end;
  218. LOC_JUMP:
  219. ;
  220. {$ifdef cpuflags}
  221. LOC_FLAGS :
  222. begin
  223. cg.a_jmp_flags(list,p.location.resflags,truelabel);
  224. cg.a_jmp_always(list,falselabel);
  225. end;
  226. {$endif cpuflags}
  227. else
  228. begin
  229. printnode(output,p);
  230. internalerror(200308241);
  231. end;
  232. end;
  233. end;
  234. end
  235. else
  236. internalerror(200112305);
  237. aktfilepos:=storepos;
  238. end;
  239. (*
  240. This code needs fixing. It is not safe to use rgint; on the m68000 it
  241. would be rgaddr.
  242. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  243. begin
  244. case t.loc of
  245. LOC_REGISTER:
  246. begin
  247. { can't be a regvar, since it would be LOC_CREGISTER then }
  248. exclude(regs,getsupreg(t.register));
  249. if t.register64.reghi<>NR_NO then
  250. exclude(regs,getsupreg(t.register64.reghi));
  251. end;
  252. LOC_CREFERENCE,LOC_REFERENCE:
  253. begin
  254. if not(cs_regvars in aktglobalswitches) or
  255. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  256. exclude(regs,getsupreg(t.reference.base));
  257. if not(cs_regvars in aktglobalswitches) or
  258. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  259. exclude(regs,getsupreg(t.reference.index));
  260. end;
  261. end;
  262. end;
  263. *)
  264. {*****************************************************************************
  265. EXCEPTION MANAGEMENT
  266. *****************************************************************************}
  267. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  268. var
  269. sym : ttypesym;
  270. begin
  271. if jmp_buf_size=-1 then
  272. begin
  273. searchsystype('JMP_BUF',sym);
  274. jmp_buf_size:=sym.restype.def.size;
  275. end;
  276. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  277. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  278. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  279. end;
  280. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  281. begin
  282. tg.Ungettemp(list,t.jmpbuf);
  283. tg.ungettemp(list,t.envbuf);
  284. tg.ungettemp(list,t.reasonbuf);
  285. end;
  286. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  287. var
  288. paraloc1,paraloc2,paraloc3 : tcgpara;
  289. begin
  290. paraloc1.init;
  291. paraloc2.init;
  292. paraloc3.init;
  293. paramanager.getintparaloc(pocall_default,1,paraloc1);
  294. paramanager.getintparaloc(pocall_default,2,paraloc2);
  295. paramanager.getintparaloc(pocall_default,3,paraloc3);
  296. paramanager.allocparaloc(list,paraloc3);
  297. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  298. paramanager.allocparaloc(list,paraloc2);
  299. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  300. { push type of exceptionframe }
  301. paramanager.allocparaloc(list,paraloc1);
  302. cg.a_param_const(list,OS_S32,1,paraloc1);
  303. paramanager.freeparaloc(list,paraloc3);
  304. paramanager.freeparaloc(list,paraloc2);
  305. paramanager.freeparaloc(list,paraloc1);
  306. cg.allocallcpuregisters(list);
  307. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  308. cg.deallocallcpuregisters(list);
  309. paramanager.getintparaloc(pocall_default,1,paraloc1);
  310. paramanager.allocparaloc(list,paraloc1);
  311. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  312. paramanager.freeparaloc(list,paraloc1);
  313. cg.allocallcpuregisters(list);
  314. cg.a_call_name(list,'FPC_SETJMP');
  315. cg.deallocallcpuregisters(list);
  316. cg.g_exception_reason_save(list, t.reasonbuf);
  317. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  318. paraloc1.done;
  319. paraloc2.done;
  320. paraloc3.done;
  321. end;
  322. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  323. begin
  324. cg.allocallcpuregisters(list);
  325. cg.a_call_name(list,'FPC_POPADDRSTACK');
  326. cg.deallocallcpuregisters(list);
  327. if not onlyfree then
  328. begin
  329. cg.g_exception_reason_load(list, t.reasonbuf);
  330. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  331. end;
  332. end;
  333. {*****************************************************************************
  334. TLocation
  335. *****************************************************************************}
  336. {$ifndef cpu64bit}
  337. { 32-bit version }
  338. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  339. var
  340. hregister,
  341. hregisterhi : tregister;
  342. hreg64 : tregister64;
  343. hl : tasmlabel;
  344. oldloc : tlocation;
  345. const_location: boolean;
  346. begin
  347. oldloc:=l;
  348. if dst_size=OS_NO then
  349. internalerror(200309144);
  350. { handle transformations to 64bit separate }
  351. if dst_size in [OS_64,OS_S64] then
  352. begin
  353. if not (l.size in [OS_64,OS_S64]) then
  354. begin
  355. { load a smaller size to OS_64 }
  356. if l.loc=LOC_REGISTER then
  357. begin
  358. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  359. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  360. end
  361. else
  362. hregister:=cg.getintregister(list,OS_INT);
  363. { load value in low register }
  364. case l.loc of
  365. LOC_FLAGS :
  366. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  367. LOC_JUMP :
  368. begin
  369. cg.a_label(list,truelabel);
  370. cg.a_load_const_reg(list,OS_INT,1,hregister);
  371. objectlibrary.getjumplabel(hl);
  372. cg.a_jmp_always(list,hl);
  373. cg.a_label(list,falselabel);
  374. cg.a_load_const_reg(list,OS_INT,0,hregister);
  375. cg.a_label(list,hl);
  376. end;
  377. else
  378. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  379. end;
  380. { reset hi part, take care of the signed bit of the current value }
  381. hregisterhi:=cg.getintregister(list,OS_INT);
  382. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  383. begin
  384. if l.loc=LOC_CONSTANT then
  385. begin
  386. if (longint(l.value)<0) then
  387. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  388. else
  389. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  390. end
  391. else
  392. begin
  393. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  394. hregisterhi);
  395. end;
  396. end
  397. else
  398. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  399. location_reset(l,LOC_REGISTER,dst_size);
  400. l.register64.reglo:=hregister;
  401. l.register64.reghi:=hregisterhi;
  402. end
  403. else
  404. begin
  405. { 64bit to 64bit }
  406. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  407. begin
  408. hregister:=l.register64.reglo;
  409. hregisterhi:=l.register64.reghi;
  410. const_location := true;
  411. end
  412. else
  413. begin
  414. hregister:=cg.getintregister(list,OS_INT);
  415. hregisterhi:=cg.getintregister(list,OS_INT);
  416. const_location := false;
  417. end;
  418. hreg64.reglo:=hregister;
  419. hreg64.reghi:=hregisterhi;
  420. { load value in new register }
  421. cg64.a_load64_loc_reg(list,l,hreg64);
  422. if not const_location then
  423. location_reset(l,LOC_REGISTER,dst_size)
  424. else
  425. location_reset(l,LOC_CREGISTER,dst_size);
  426. l.register64.reglo:=hregister;
  427. l.register64.reghi:=hregisterhi;
  428. end;
  429. end
  430. else
  431. begin
  432. {Do not bother to recycle the existing register. The register
  433. allocator eliminates unnecessary moves, so it's not needed
  434. and trying to recycle registers can cause problems because
  435. the registers changes size and may need aditional constraints.
  436. Not if it's about LOC_CREGISTER's (JM)
  437. }
  438. const_location :=
  439. (maybeconst) and
  440. (l.loc = LOC_CREGISTER) and
  441. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  442. ((l.size = dst_size) or
  443. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  444. if not const_location then
  445. hregister:=cg.getintregister(list,dst_size)
  446. else
  447. hregister := l.register;
  448. { load value in new register }
  449. case l.loc of
  450. LOC_FLAGS :
  451. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  452. LOC_JUMP :
  453. begin
  454. cg.a_label(list,truelabel);
  455. cg.a_load_const_reg(list,dst_size,1,hregister);
  456. objectlibrary.getjumplabel(hl);
  457. cg.a_jmp_always(list,hl);
  458. cg.a_label(list,falselabel);
  459. cg.a_load_const_reg(list,dst_size,0,hregister);
  460. cg.a_label(list,hl);
  461. end;
  462. else
  463. begin
  464. { load_loc_reg can only handle size >= l.size, when the
  465. new size is smaller then we need to adjust the size
  466. of the orignal and maybe recalculate l.register for i386 }
  467. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  468. begin
  469. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  470. l.register:=cg.makeregsize(list,l.register,dst_size);
  471. { for big endian systems, the reference's offset must }
  472. { be increased in this case, since they have the }
  473. { MSB first in memory and e.g. byte(word_var) should }
  474. { return the second byte in this case (JM) }
  475. if (target_info.endian = ENDIAN_BIG) and
  476. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  477. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  478. {$ifdef x86}
  479. l.size:=dst_size;
  480. {$endif x86}
  481. end;
  482. cg.a_load_loc_reg(list,dst_size,l,hregister);
  483. {$ifndef x86}
  484. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  485. l.size:=dst_size;
  486. {$endif not x86}
  487. end;
  488. end;
  489. if not const_location then
  490. location_reset(l,LOC_REGISTER,dst_size)
  491. else
  492. location_reset(l,LOC_CREGISTER,dst_size);
  493. l.register:=hregister;
  494. end;
  495. { Release temp when it was a reference }
  496. if oldloc.loc=LOC_REFERENCE then
  497. location_freetemp(list,oldloc);
  498. end;
  499. {$else cpu64bit}
  500. { 64-bit version }
  501. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  502. var
  503. hregister : tregister;
  504. hl : tasmlabel;
  505. oldloc : tlocation;
  506. begin
  507. oldloc:=l;
  508. hregister:=cg.getintregister(list,dst_size);
  509. { load value in new register }
  510. case l.loc of
  511. LOC_FLAGS :
  512. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  513. LOC_JUMP :
  514. begin
  515. cg.a_label(list,truelabel);
  516. cg.a_load_const_reg(list,dst_size,1,hregister);
  517. objectlibrary.getjumplabel(hl);
  518. cg.a_jmp_always(list,hl);
  519. cg.a_label(list,falselabel);
  520. cg.a_load_const_reg(list,dst_size,0,hregister);
  521. cg.a_label(list,hl);
  522. end;
  523. else
  524. begin
  525. { load_loc_reg can only handle size >= l.size, when the
  526. new size is smaller then we need to adjust the size
  527. of the orignal and maybe recalculate l.register for i386 }
  528. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  529. begin
  530. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  531. l.register:=cg.makeregsize(list,l.register,dst_size);
  532. { for big endian systems, the reference's offset must }
  533. { be increased in this case, since they have the }
  534. { MSB first in memory and e.g. byte(word_var) should }
  535. { return the second byte in this case (JM) }
  536. if (target_info.endian = ENDIAN_BIG) and
  537. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  538. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  539. {$ifdef x86}
  540. l.size:=dst_size;
  541. {$endif x86}
  542. end;
  543. cg.a_load_loc_reg(list,dst_size,l,hregister);
  544. {$ifndef x86}
  545. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  546. l.size:=dst_size;
  547. {$endif not x86}
  548. end;
  549. end;
  550. if (l.loc <> LOC_CREGISTER) or
  551. not maybeconst then
  552. location_reset(l,LOC_REGISTER,dst_size)
  553. else
  554. location_reset(l,LOC_CREGISTER,dst_size);
  555. l.register:=hregister;
  556. { Release temp when it was a reference }
  557. if oldloc.loc=LOC_REFERENCE then
  558. location_freetemp(list,oldloc);
  559. end;
  560. {$endif cpu64bit}
  561. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  562. var
  563. reg : tregister;
  564. href : treference;
  565. begin
  566. if (l.loc<>LOC_FPUREGISTER) and
  567. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  568. begin
  569. { if it's in an mm register, store to memory first }
  570. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  571. begin
  572. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  573. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  574. location_reset(l,LOC_REFERENCE,l.size);
  575. l.reference:=href;
  576. end;
  577. reg:=cg.getfpuregister(list,l.size);
  578. cg.a_loadfpu_loc_reg(list,l,reg);
  579. location_freetemp(list,l);
  580. location_reset(l,LOC_FPUREGISTER,l.size);
  581. l.register:=reg;
  582. end;
  583. end;
  584. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  585. var
  586. reg : tregister;
  587. href : treference;
  588. begin
  589. if (l.loc<>LOC_MMREGISTER) and
  590. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  591. begin
  592. { if it's in an fpu register, store to memory first }
  593. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  594. begin
  595. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  596. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  597. location_reset(l,LOC_REFERENCE,l.size);
  598. l.reference:=href;
  599. end;
  600. reg:=cg.getmmregister(list,l.size);
  601. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  602. location_freetemp(list,l);
  603. location_reset(l,LOC_MMREGISTER,l.size);
  604. l.register:=reg;
  605. end;
  606. end;
  607. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  608. var
  609. r : treference;
  610. begin
  611. case l.loc of
  612. LOC_FPUREGISTER,
  613. LOC_CFPUREGISTER :
  614. begin
  615. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  616. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  617. location_reset(l,LOC_REFERENCE,l.size);
  618. l.reference:=r;
  619. end;
  620. LOC_MMREGISTER,
  621. LOC_CMMREGISTER:
  622. begin
  623. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  624. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  625. location_reset(l,LOC_REFERENCE,l.size);
  626. l.reference:=r;
  627. end;
  628. LOC_CONSTANT,
  629. LOC_REGISTER,
  630. LOC_CREGISTER :
  631. begin
  632. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  633. {$ifndef cpu64bit}
  634. if l.size in [OS_64,OS_S64] then
  635. cg64.a_load64_loc_ref(list,l,r)
  636. else
  637. {$endif cpu64bit}
  638. cg.a_load_loc_ref(list,l.size,l,r);
  639. location_reset(l,LOC_REFERENCE,l.size);
  640. l.reference:=r;
  641. end;
  642. LOC_CREFERENCE,
  643. LOC_REFERENCE : ;
  644. else
  645. internalerror(200203219);
  646. end;
  647. end;
  648. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  649. begin
  650. case l.loc of
  651. LOC_REGISTER,
  652. LOC_CREGISTER :
  653. begin
  654. if not loadref then
  655. internalerror(200410231);
  656. reference_reset_base(ref,l.register,0);
  657. end;
  658. LOC_REFERENCE,
  659. LOC_CREFERENCE :
  660. begin
  661. if loadref then
  662. begin
  663. reference_reset_base(ref,cg.getaddressregister(list),0);
  664. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  665. end
  666. else
  667. ref:=l.reference;
  668. end;
  669. else
  670. internalerror(200309181);
  671. end;
  672. end;
  673. {*****************************************************************************
  674. Maybe_Save
  675. *****************************************************************************}
  676. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  677. begin
  678. {$ifdef i386}
  679. if (needed>=maxfpuregs) and
  680. (l.loc = LOC_FPUREGISTER) then
  681. begin
  682. location_force_mem(list,l);
  683. maybe_pushfpu:=true;
  684. end
  685. else
  686. maybe_pushfpu:=false;
  687. {$else i386}
  688. maybe_pushfpu:=false;
  689. {$endif i386}
  690. end;
  691. {****************************************************************************
  692. Init/Finalize Code
  693. ****************************************************************************}
  694. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  695. var
  696. href : treference;
  697. hreg : tregister;
  698. list : TAAsmoutput;
  699. hsym : tparavarsym;
  700. l : longint;
  701. localcopyloc : tlocation;
  702. begin
  703. list:=taasmoutput(arg);
  704. if (tsym(p).typ=paravarsym) and
  705. (tparavarsym(p).varspez=vs_value) and
  706. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  707. begin
  708. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  709. if is_open_array(tparavarsym(p).vartype.def) or
  710. is_array_of_const(tparavarsym(p).vartype.def) then
  711. begin
  712. { cdecl functions don't have a high pointer so it is not possible to generate
  713. a local copy }
  714. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  715. begin
  716. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  717. if not assigned(hsym) then
  718. internalerror(200306061);
  719. hreg:=cg.getaddressregister(list);
  720. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  721. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  722. end;
  723. end
  724. else
  725. begin
  726. { Allocate space for the local copy }
  727. l:=tparavarsym(p).getsize;
  728. localcopyloc.loc:=LOC_REFERENCE;
  729. localcopyloc.size:=int_cgsize(l);
  730. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  731. { Copy data }
  732. if is_shortstring(tparavarsym(p).vartype.def) then
  733. begin
  734. { this code is only executed before the code for the body and the entry/exit code is generated
  735. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  736. }
  737. include(current_procinfo.flags,pi_do_call);
  738. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  739. end
  740. else
  741. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  742. { update localloc of varsym }
  743. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  744. tparavarsym(p).localloc:=localcopyloc;
  745. end;
  746. end;
  747. end;
  748. { initializes the regvars from staticsymtable with 0 }
  749. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  750. begin
  751. if (tsym(p).typ=globalvarsym) then
  752. begin
  753. case tglobalvarsym(p).localloc.loc of
  754. LOC_CREGISTER :
  755. cg.a_load_const_reg(taasmoutput(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  756. tglobalvarsym(p).localloc.register);
  757. LOC_REFERENCE : ;
  758. LOC_CMMREGISTER :
  759. { clear the whole register }
  760. cg.a_opmm_reg_reg(taasmoutput(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  761. tglobalvarsym(p).localloc.register,
  762. tglobalvarsym(p).localloc.register,
  763. nil);
  764. LOC_CFPUREGISTER :
  765. ;
  766. else
  767. internalerror(200410124);
  768. end;
  769. end;
  770. end;
  771. { generates the code for initialisation of local data }
  772. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  773. var
  774. oldexprasmlist : TAAsmoutput;
  775. hp : tnode;
  776. begin
  777. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  778. (tabstractvarsym(p).refs>0) and
  779. not(is_class(tabstractvarsym(p).vartype.def)) and
  780. tabstractvarsym(p).vartype.def.needs_inittable then
  781. begin
  782. oldexprasmlist:=exprasmlist;
  783. exprasmlist:=taasmoutput(arg);
  784. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  785. firstpass(hp);
  786. secondpass(hp);
  787. hp.free;
  788. exprasmlist:=oldexprasmlist;
  789. end;
  790. end;
  791. procedure finalize_sym(asmlist:taasmoutput;sym:tsym);
  792. var
  793. hp : tnode;
  794. oldexprasmlist : TAAsmoutput;
  795. begin
  796. include(current_procinfo.flags,pi_needs_implicit_finally);
  797. oldexprasmlist:=exprasmlist;
  798. exprasmlist:=asmlist;
  799. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  800. firstpass(hp);
  801. secondpass(hp);
  802. hp.free;
  803. exprasmlist:=oldexprasmlist;
  804. end;
  805. { generates the code for finalisation of local variables }
  806. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  807. begin
  808. if (tsym(p).typ=localvarsym) and
  809. (tlocalvarsym(p).refs>0) and
  810. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  811. not(is_class(tlocalvarsym(p).vartype.def)) and
  812. tlocalvarsym(p).vartype.def.needs_inittable then
  813. finalize_sym(taasmoutput(arg),tsym(p));
  814. end;
  815. { generates the code for finalisation of local typedconsts }
  816. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  817. var
  818. i : longint;
  819. pd : tprocdef;
  820. begin
  821. case tsym(p).typ of
  822. typedconstsym :
  823. begin
  824. if ttypedconstsym(p).is_writable and
  825. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  826. finalize_sym(taasmoutput(arg),tsym(p));
  827. end;
  828. procsym :
  829. begin
  830. for i:=1 to tprocsym(p).procdef_count do
  831. begin
  832. pd:=tprocsym(p).procdef[i];
  833. if assigned(pd.localst) and
  834. (pd.procsym=tprocsym(p)) and
  835. (pd.localst.symtabletype<>staticsymtable) then
  836. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  837. end;
  838. end;
  839. end;
  840. end;
  841. { generates the code for finalization of static symtable and
  842. all local (static) typedconsts }
  843. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  844. var
  845. i : longint;
  846. pd : tprocdef;
  847. begin
  848. case tsym(p).typ of
  849. globalvarsym :
  850. begin
  851. if (tglobalvarsym(p).refs>0) and
  852. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  853. not(is_class(tglobalvarsym(p).vartype.def)) and
  854. tglobalvarsym(p).vartype.def.needs_inittable then
  855. finalize_sym(taasmoutput(arg),tsym(p));
  856. end;
  857. typedconstsym :
  858. begin
  859. if ttypedconstsym(p).is_writable and
  860. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  861. finalize_sym(taasmoutput(arg),tsym(p));
  862. end;
  863. procsym :
  864. begin
  865. for i:=1 to tprocsym(p).procdef_count do
  866. begin
  867. pd:=tprocsym(p).procdef[i];
  868. if assigned(pd.localst) and
  869. (pd.procsym=tprocsym(p)) and
  870. (pd.localst.symtabletype<>staticsymtable) then
  871. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  872. end;
  873. end;
  874. end;
  875. end;
  876. { generates the code for incrementing the reference count of parameters and
  877. initialize out parameters }
  878. procedure init_paras(p : tnamedindexitem;arg:pointer);
  879. var
  880. href : treference;
  881. tmpreg : tregister;
  882. list : TAAsmoutput;
  883. begin
  884. list:=taasmoutput(arg);
  885. if (tsym(p).typ=paravarsym) and
  886. not is_class_or_interface(tparavarsym(p).vartype.def) and
  887. tparavarsym(p).vartype.def.needs_inittable then
  888. begin
  889. case tparavarsym(p).varspez of
  890. vs_value :
  891. begin
  892. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  893. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  894. end;
  895. vs_out :
  896. begin
  897. tmpreg:=cg.getaddressregister(list);
  898. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  899. reference_reset_base(href,tmpreg,0);
  900. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  901. end;
  902. end;
  903. end;
  904. end;
  905. { generates the code for decrementing the reference count of parameters }
  906. procedure final_paras(p : tnamedindexitem;arg:pointer);
  907. var
  908. list : TAAsmoutput;
  909. href : treference;
  910. begin
  911. if not(tsym(p).typ=paravarsym) then
  912. exit;
  913. list:=taasmoutput(arg);
  914. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  915. tparavarsym(p).vartype.def.needs_inittable then
  916. begin
  917. if (tparavarsym(p).varspez=vs_value) then
  918. begin
  919. include(current_procinfo.flags,pi_needs_implicit_finally);
  920. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  921. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  922. end;
  923. end
  924. else
  925. if (tparavarsym(p).varspez=vs_value) and
  926. (is_open_array(tparavarsym(p).vartype.def) or
  927. is_array_of_const(tparavarsym(p).vartype.def)) then
  928. begin
  929. { cdecl functions don't have a high pointer so it is not possible to generate
  930. a local copy }
  931. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  932. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  933. end;
  934. end;
  935. { Initialize temp ansi/widestrings,interfaces }
  936. procedure inittempvariables(list:taasmoutput);
  937. var
  938. hp : ptemprecord;
  939. href : treference;
  940. begin
  941. hp:=tg.templist;
  942. while assigned(hp) do
  943. begin
  944. if assigned(hp^.def) and
  945. hp^.def.needs_inittable then
  946. begin
  947. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  948. cg.g_initialize(list,hp^.def,href);
  949. end;
  950. hp:=hp^.next;
  951. end;
  952. end;
  953. procedure finalizetempvariables(list:taasmoutput);
  954. var
  955. hp : ptemprecord;
  956. href : treference;
  957. begin
  958. hp:=tg.templist;
  959. while assigned(hp) do
  960. begin
  961. if assigned(hp^.def) and
  962. hp^.def.needs_inittable then
  963. begin
  964. include(current_procinfo.flags,pi_needs_implicit_finally);
  965. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  966. cg.g_finalize(list,hp^.def,href);
  967. end;
  968. hp:=hp^.next;
  969. end;
  970. end;
  971. procedure gen_load_return_value(list:TAAsmoutput);
  972. var
  973. {$ifndef cpu64bit}
  974. href : treference;
  975. {$endif cpu64bit}
  976. ressym : tabstractnormalvarsym;
  977. resloc,
  978. restmploc : tlocation;
  979. hreg : tregister;
  980. funcretloc : tlocation;
  981. begin
  982. { Is the loading needed? }
  983. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  984. (
  985. (po_assembler in current_procinfo.procdef.procoptions) and
  986. (not(assigned(current_procinfo.procdef.funcretsym)) or
  987. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  988. ) then
  989. exit;
  990. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  991. { constructors return self }
  992. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  993. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  994. else
  995. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  996. if (ressym.refs>0) then
  997. begin
  998. {$ifdef OLDREGVARS}
  999. case ressym.localloc.loc of
  1000. LOC_CFPUREGISTER,
  1001. LOC_FPUREGISTER:
  1002. begin
  1003. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1004. restmploc.register:=ressym.localloc.register;
  1005. end;
  1006. LOC_CREGISTER,
  1007. LOC_REGISTER:
  1008. begin
  1009. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1010. restmploc.register:=ressym.localloc.register;
  1011. end;
  1012. LOC_MMREGISTER:
  1013. begin
  1014. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1015. restmploc.register:=ressym.localloc.register;
  1016. end;
  1017. LOC_REFERENCE:
  1018. begin
  1019. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1020. restmploc.reference:=ressym.localloc.reference;
  1021. end;
  1022. else
  1023. internalerror(200309184);
  1024. end;
  1025. {$else}
  1026. restmploc:=ressym.localloc;
  1027. {$endif}
  1028. { Here, we return the function result. In most architectures, the value is
  1029. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1030. function returns in a register and the caller receives it in an other one }
  1031. case funcretloc.loc of
  1032. LOC_REGISTER:
  1033. begin
  1034. {$ifndef cpu64bit}
  1035. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1036. begin
  1037. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1038. if resloc.loc<>LOC_REGISTER then
  1039. internalerror(200409141);
  1040. { Load low and high register separate to generate better register
  1041. allocation info }
  1042. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1043. begin
  1044. cg.getcpuregister(list,resloc.register64.reglo);
  1045. cg.ungetcpuregister(list,resloc.register64.reglo);
  1046. { for the optimizer }
  1047. cg.a_reg_alloc(list,resloc.register64.reglo);
  1048. end;
  1049. case restmploc.loc of
  1050. LOC_REFERENCE :
  1051. begin
  1052. href:=restmploc.reference;
  1053. if target_info.endian=ENDIAN_BIG then
  1054. inc(href.offset,4);
  1055. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1056. end;
  1057. LOC_CREGISTER :
  1058. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1059. else
  1060. internalerror(200409203);
  1061. end;
  1062. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1063. begin
  1064. cg.getcpuregister(list,resloc.register64.reghi);
  1065. cg.ungetcpuregister(list,resloc.register64.reghi);
  1066. { for the optimizer }
  1067. cg.a_reg_alloc(list,resloc.register64.reghi);
  1068. end;
  1069. case restmploc.loc of
  1070. LOC_REFERENCE :
  1071. begin
  1072. href:=restmploc.reference;
  1073. if target_info.endian=ENDIAN_LITTLE then
  1074. inc(href.offset,4);
  1075. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1076. end;
  1077. LOC_CREGISTER :
  1078. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1079. else
  1080. internalerror(200409204);
  1081. end;
  1082. end
  1083. else
  1084. {$endif cpu64bit}
  1085. begin
  1086. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1087. if getsupreg(funcretloc.register)<first_int_imreg then
  1088. begin
  1089. cg.getcpuregister(list,funcretloc.register);
  1090. cg.ungetcpuregister(list,hreg);
  1091. { for the optimizer }
  1092. cg.a_reg_alloc(list,funcretloc.register);
  1093. end;
  1094. { it could be that a structure is passed in memory but the function is expected to
  1095. return a pointer to this memory }
  1096. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1097. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1098. else
  1099. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1100. end;
  1101. end;
  1102. LOC_FPUREGISTER:
  1103. begin
  1104. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1105. begin
  1106. cg.getcpuregister(list,funcretloc.register);
  1107. cg.ungetcpuregister(list,funcretloc.register);
  1108. end;
  1109. { we can't do direct moves between fpu and mm registers }
  1110. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1111. location_force_fpureg(list,restmploc,false);
  1112. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1113. end;
  1114. LOC_MMREGISTER:
  1115. begin
  1116. if getsupreg(funcretloc.register)<first_mm_imreg then
  1117. begin
  1118. cg.getcpuregister(list,funcretloc.register);
  1119. cg.ungetcpuregister(list,funcretloc.register);
  1120. end;
  1121. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1122. end;
  1123. LOC_INVALID,
  1124. LOC_REFERENCE:
  1125. ;
  1126. else
  1127. internalerror(200405025);
  1128. end;
  1129. end;
  1130. end;
  1131. procedure gen_load_para_value(list:TAAsmoutput);
  1132. procedure get_para(const paraloc:TCGParaLocation);
  1133. begin
  1134. case paraloc.loc of
  1135. LOC_REGISTER :
  1136. begin
  1137. if getsupreg(paraloc.register)<first_int_imreg then
  1138. cg.getcpuregister(list,paraloc.register);
  1139. end;
  1140. LOC_MMREGISTER :
  1141. begin
  1142. if getsupreg(paraloc.register)<first_mm_imreg then
  1143. cg.getcpuregister(list,paraloc.register);
  1144. end;
  1145. LOC_FPUREGISTER :
  1146. begin
  1147. if getsupreg(paraloc.register)<first_fpu_imreg then
  1148. cg.getcpuregister(list,paraloc.register);
  1149. end;
  1150. end;
  1151. end;
  1152. procedure unget_para(const paraloc:TCGParaLocation);
  1153. begin
  1154. case paraloc.loc of
  1155. LOC_REGISTER :
  1156. begin
  1157. if getsupreg(paraloc.register)<first_int_imreg then
  1158. cg.ungetcpuregister(list,paraloc.register);
  1159. end;
  1160. LOC_MMREGISTER :
  1161. begin
  1162. if getsupreg(paraloc.register)<first_mm_imreg then
  1163. cg.ungetcpuregister(list,paraloc.register);
  1164. end;
  1165. LOC_FPUREGISTER :
  1166. begin
  1167. if getsupreg(paraloc.register)<first_fpu_imreg then
  1168. cg.ungetcpuregister(list,paraloc.register);
  1169. end;
  1170. end;
  1171. end;
  1172. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1173. var
  1174. href : treference;
  1175. begin
  1176. case paraloc.loc of
  1177. LOC_REGISTER :
  1178. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1179. LOC_MMREGISTER :
  1180. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1181. LOC_FPUREGISTER :
  1182. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1183. LOC_REFERENCE :
  1184. begin
  1185. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1186. { use concatcopy, because it can also be a float which fails when
  1187. load_ref_ref is used. Don't copy data when the references are equal }
  1188. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1189. cg.g_concatcopy(list,href,ref,sizeleft);
  1190. end;
  1191. else
  1192. internalerror(2002081302);
  1193. end;
  1194. end;
  1195. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1196. var
  1197. href : treference;
  1198. begin
  1199. case paraloc.loc of
  1200. LOC_REGISTER :
  1201. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1202. LOC_MMREGISTER :
  1203. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1204. LOC_FPUREGISTER :
  1205. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1206. LOC_REFERENCE :
  1207. begin
  1208. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1209. case getregtype(reg) of
  1210. R_INTREGISTER :
  1211. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1212. R_FPUREGISTER :
  1213. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1214. R_MMREGISTER :
  1215. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1216. else
  1217. internalerror(2004101012);
  1218. end;
  1219. end;
  1220. else
  1221. internalerror(2002081302);
  1222. end;
  1223. end;
  1224. var
  1225. i : longint;
  1226. currpara : tparavarsym;
  1227. paraloc : pcgparalocation;
  1228. href : treference;
  1229. sizeleft : aint;
  1230. {$ifdef sparc}
  1231. tempref : treference;
  1232. {$endif sparc}
  1233. begin
  1234. if (po_assembler in current_procinfo.procdef.procoptions) then
  1235. exit;
  1236. { Allocate registers used by parameters }
  1237. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1238. begin
  1239. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1240. paraloc:=currpara.paraloc[calleeside].location;
  1241. while assigned(paraloc) do
  1242. begin
  1243. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1244. get_para(paraloc^);
  1245. paraloc:=paraloc^.next;
  1246. end;
  1247. end;
  1248. { Copy parameters to local references/registers }
  1249. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1250. begin
  1251. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1252. paraloc:=currpara.paraloc[calleeside].location;
  1253. { skip e.g. empty records }
  1254. if (paraloc^.loc = LOC_VOID) then
  1255. continue;
  1256. if not assigned(paraloc) then
  1257. internalerror(200408203);
  1258. case currpara.localloc.loc of
  1259. LOC_REFERENCE :
  1260. begin
  1261. { If the parameter location is reused we don't need to copy
  1262. anything }
  1263. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1264. begin
  1265. href:=currpara.localloc.reference;
  1266. sizeleft:=currpara.paraloc[calleeside].intsize;
  1267. while assigned(paraloc) do
  1268. begin
  1269. unget_para(paraloc^);
  1270. if (paraloc^.size=OS_NO) then
  1271. begin
  1272. { Can only be a reference that contains the rest
  1273. of the parameter }
  1274. if (paraloc^.loc<>LOC_REFERENCE) or
  1275. assigned(paraloc^.next) then
  1276. internalerror(2005013010);
  1277. gen_load_ref(paraloc^,href,sizeleft);
  1278. inc(href.offset,sizeleft);
  1279. sizeleft:=0;
  1280. end
  1281. else
  1282. begin
  1283. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1284. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1285. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1286. end;
  1287. paraloc:=paraloc^.next;
  1288. end;
  1289. end;
  1290. end;
  1291. LOC_CREGISTER :
  1292. begin
  1293. {$ifndef cpu64bit}
  1294. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1295. is_64bit(currpara.vartype.def) then
  1296. begin
  1297. case paraloc^.loc of
  1298. LOC_REGISTER:
  1299. begin
  1300. if not assigned(paraloc^.next) then
  1301. internalerror(200410104);
  1302. if (target_info.endian=ENDIAN_BIG) then
  1303. begin
  1304. { paraloc^ -> high
  1305. paraloc^.next -> low }
  1306. unget_para(paraloc^);
  1307. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1308. unget_para(paraloc^.next^);
  1309. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1310. end
  1311. else
  1312. begin
  1313. { paraloc^ -> low
  1314. paraloc^.next -> high }
  1315. unget_para(paraloc^);
  1316. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1317. unget_para(paraloc^.next^);
  1318. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1319. end;
  1320. end;
  1321. LOC_REFERENCE:
  1322. begin
  1323. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1324. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1325. unget_para(paraloc^);
  1326. end;
  1327. else
  1328. internalerror(2005101501);
  1329. end
  1330. end
  1331. else
  1332. {$endif cpu64bit}
  1333. begin
  1334. if assigned(paraloc^.next) then
  1335. internalerror(200410105);
  1336. unget_para(paraloc^);
  1337. gen_load_reg(paraloc^,currpara.localloc.register);
  1338. end;
  1339. end;
  1340. LOC_CFPUREGISTER :
  1341. begin
  1342. {$ifdef sparc}
  1343. { Sparc passes floats in int registers, when loading to fpu register
  1344. we need a temp }
  1345. sizeleft := TCGSize2Size[currpara.localloc.size];
  1346. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1347. href:=tempref;
  1348. while assigned(paraloc) do
  1349. begin
  1350. unget_para(paraloc^);
  1351. gen_load_ref(paraloc^,href,sizeleft);
  1352. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1353. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1354. paraloc:=paraloc^.next;
  1355. end;
  1356. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1357. tg.UnGetTemp(list,tempref);
  1358. {$else sparc}
  1359. unget_para(paraloc^);
  1360. gen_load_reg(paraloc^,currpara.localloc.register);
  1361. if assigned(paraloc^.next) then
  1362. internalerror(200410109);
  1363. {$endif sparc}
  1364. end;
  1365. LOC_CMMREGISTER :
  1366. begin
  1367. unget_para(paraloc^);
  1368. gen_load_reg(paraloc^,currpara.localloc.register);
  1369. { data could come in two memory locations, for now
  1370. we simply ignore the sanity check (FK)
  1371. if assigned(paraloc^.next) then
  1372. internalerror(200410108);
  1373. }
  1374. end;
  1375. end;
  1376. end;
  1377. { generate copies of call by value parameters, must be done before
  1378. the initialization and body is parsed because the refcounts are
  1379. incremented using the local copies }
  1380. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1381. {$ifdef powerpc}
  1382. { unget the register that contains the stack pointer before the procedure entry, }
  1383. { which is used to access the parameters in their original callee-side location }
  1384. cg.a_reg_dealloc(list,NR_R12);
  1385. {$endif powerpc}
  1386. end;
  1387. procedure gen_initialize_code(list:TAAsmoutput);
  1388. begin
  1389. { initialize local data like ansistrings }
  1390. case current_procinfo.procdef.proctypeoption of
  1391. potype_unitinit:
  1392. begin
  1393. { this is also used for initialization of variables in a
  1394. program which does not have a globalsymtable }
  1395. if assigned(current_module.globalsymtable) then
  1396. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1397. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1398. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1399. end;
  1400. { units have seperate code for initilization and finalization }
  1401. potype_unitfinalize: ;
  1402. { program init/final is generated in separate procedure }
  1403. potype_proginit:
  1404. begin
  1405. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1406. end;
  1407. else
  1408. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1409. end;
  1410. { initialisizes temp. ansi/wide string data }
  1411. inittempvariables(list);
  1412. { initialize ansi/widesstring para's }
  1413. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1414. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1415. {$ifdef OLDREGVARS}
  1416. load_regvars(list,nil);
  1417. {$endif OLDREGVARS}
  1418. end;
  1419. procedure gen_finalize_code(list:TAAsmoutput);
  1420. begin
  1421. {$ifdef OLDREGVARS}
  1422. cleanup_regvars(list);
  1423. {$endif OLDREGVARS}
  1424. { finalize temporary data }
  1425. finalizetempvariables(list);
  1426. { finalize local data like ansistrings}
  1427. case current_procinfo.procdef.proctypeoption of
  1428. potype_unitfinalize:
  1429. begin
  1430. { this is also used for initialization of variables in a
  1431. program which does not have a globalsymtable }
  1432. if assigned(current_module.globalsymtable) then
  1433. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1434. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1435. end;
  1436. { units/progs have separate code for initialization and finalization }
  1437. potype_unitinit: ;
  1438. { program init/final is generated in separate procedure }
  1439. potype_proginit: ;
  1440. else
  1441. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1442. end;
  1443. { finalize paras data }
  1444. if assigned(current_procinfo.procdef.parast) and
  1445. not(po_assembler in current_procinfo.procdef.procoptions) then
  1446. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1447. end;
  1448. procedure gen_entry_code(list:TAAsmoutput);
  1449. var
  1450. paraloc1,
  1451. paraloc2 : tcgpara;
  1452. begin
  1453. paraloc1.init;
  1454. paraloc2.init;
  1455. { the actual profile code can clobber some registers,
  1456. therefore if the context must be saved, do it before
  1457. the actual call to the profile code
  1458. }
  1459. if (cs_profile in aktmoduleswitches) and
  1460. not(po_assembler in current_procinfo.procdef.procoptions) then
  1461. begin
  1462. { non-win32 can call mcout even in main }
  1463. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1464. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1465. begin
  1466. cg.allocallcpuregisters(list);
  1467. cg.g_profilecode(list);
  1468. cg.deallocallcpuregisters(list);
  1469. end;
  1470. end;
  1471. { call startup helpers from main program }
  1472. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1473. begin
  1474. if (target_info.system = system_powerpc_darwin) or
  1475. (target_info.system = system_powerpc_macos) then
  1476. begin
  1477. { the parameters are already in the right registers }
  1478. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1479. end;
  1480. { initialize units }
  1481. cg.allocallcpuregisters(list);
  1482. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1483. cg.deallocallcpuregisters(list);
  1484. end;
  1485. list.concat(Tai_force_line.Create);
  1486. {$ifdef OLDREGVARS}
  1487. load_regvars(list,nil);
  1488. {$endif OLDREGVARS}
  1489. paraloc1.done;
  1490. paraloc2.done;
  1491. end;
  1492. procedure gen_exit_code(list:TAAsmoutput);
  1493. begin
  1494. { call __EXIT for main program }
  1495. if (not DLLsource) and
  1496. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1497. cg.a_call_name(list,'FPC_DO_EXIT');
  1498. end;
  1499. {****************************************************************************
  1500. Entry/Exit
  1501. ****************************************************************************}
  1502. procedure gen_proc_symbol(list:Taasmoutput);
  1503. var
  1504. hs : string;
  1505. begin
  1506. repeat
  1507. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1508. if hs='' then
  1509. break;
  1510. if (cs_profile in aktmoduleswitches) or
  1511. (po_global in current_procinfo.procdef.procoptions) then
  1512. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1513. else
  1514. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1515. if target_info.use_function_relative_addresses then
  1516. list.concat(Tai_function_name.create(hs));
  1517. until false;
  1518. current_procinfo.procdef.procstarttai:=tai(list.last);
  1519. end;
  1520. procedure gen_proc_symbol_end(list:Taasmoutput);
  1521. begin
  1522. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1523. begin
  1524. { Insert Ident of the compiler in the main .text section }
  1525. if (not (cs_create_smart in aktmoduleswitches)) then
  1526. begin
  1527. list.insert(Tai_align.Create(const_align(32)));
  1528. list.insert(Tai_string.Create('FPC '+full_version_string+
  1529. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  1530. end;
  1531. { Reference all DEBUGINFO sections from the main .text section }
  1532. if (cs_debuginfo in aktmoduleswitches) then
  1533. debuginfo.referencesections(list);
  1534. end;
  1535. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1536. current_procinfo.procdef.procendtai:=tai(list.last);
  1537. end;
  1538. procedure gen_proc_entry_code(list:Taasmoutput);
  1539. var
  1540. hitemp,
  1541. lotemp : longint;
  1542. begin
  1543. { generate call frame marker for dwarf call frame info }
  1544. dwarfcfi.start_frame(list);
  1545. { All temps are know, write offsets used for information }
  1546. if (cs_asm_source in aktglobalswitches) then
  1547. begin
  1548. if tg.direction>0 then
  1549. begin
  1550. lotemp:=current_procinfo.tempstart;
  1551. hitemp:=tg.lasttemp;
  1552. end
  1553. else
  1554. begin
  1555. lotemp:=tg.lasttemp;
  1556. hitemp:=current_procinfo.tempstart;
  1557. end;
  1558. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1559. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1560. end;
  1561. { generate target specific proc entry code }
  1562. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1563. end;
  1564. procedure gen_proc_exit_code(list:Taasmoutput);
  1565. var
  1566. parasize : longint;
  1567. begin
  1568. { c style clearstack does not need to remove parameters from the stack, only the
  1569. return value when it was pushed by arguments }
  1570. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1571. begin
  1572. parasize:=0;
  1573. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1574. inc(parasize,sizeof(aint));
  1575. end
  1576. else
  1577. parasize:=current_procinfo.para_stack_size;
  1578. { generate target specific proc exit code }
  1579. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1580. { release return registers, needed for optimizer }
  1581. if not is_void(current_procinfo.procdef.rettype.def) then
  1582. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1583. { end of frame marker for call frame info }
  1584. dwarfcfi.end_frame(list);
  1585. end;
  1586. procedure gen_stack_check_size_para(list:Taasmoutput);
  1587. var
  1588. paraloc1 : tcgpara;
  1589. begin
  1590. paraloc1.init;
  1591. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1592. paramanager.allocparaloc(list,paraloc1);
  1593. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1594. paramanager.freeparaloc(list,paraloc1);
  1595. paraloc1.done;
  1596. end;
  1597. procedure gen_stack_check_call(list:Taasmoutput);
  1598. var
  1599. paraloc1 : tcgpara;
  1600. begin
  1601. paraloc1.init;
  1602. { Also alloc the register needed for the parameter }
  1603. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1604. paramanager.allocparaloc(list,paraloc1);
  1605. paramanager.freeparaloc(list,paraloc1);
  1606. { Call the helper }
  1607. cg.allocallcpuregisters(list);
  1608. cg.a_call_name(list,'FPC_STACKCHECK');
  1609. cg.deallocallcpuregisters(list);
  1610. paraloc1.done;
  1611. end;
  1612. procedure gen_save_used_regs(list:TAAsmoutput);
  1613. begin
  1614. { Pure assembler routines need to save the registers themselves }
  1615. if (po_assembler in current_procinfo.procdef.procoptions) then
  1616. exit;
  1617. { oldfpccall expects all registers to be destroyed }
  1618. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1619. cg.g_save_standard_registers(list);
  1620. end;
  1621. procedure gen_restore_used_regs(list:TAAsmoutput);
  1622. begin
  1623. { Pure assembler routines need to save the registers themselves }
  1624. if (po_assembler in current_procinfo.procdef.procoptions) then
  1625. exit;
  1626. { oldfpccall expects all registers to be destroyed }
  1627. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1628. cg.g_restore_standard_registers(list);
  1629. end;
  1630. {****************************************************************************
  1631. External handling
  1632. ****************************************************************************}
  1633. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1634. begin
  1635. { add the procedure to the al_procedures }
  1636. maybe_new_object_file(list);
  1637. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1638. list.concat(Tai_align.create(aktalignment.procalign));
  1639. if (po_global in pd.procoptions) then
  1640. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1641. else
  1642. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1643. cg.a_jmp_name(list,externalname);
  1644. end;
  1645. {****************************************************************************
  1646. Const Data
  1647. ****************************************************************************}
  1648. procedure insertbssdata(sym : tglobalvarsym);
  1649. var
  1650. l,varalign : longint;
  1651. storefilepos : tfileposinfo;
  1652. list : Taasmoutput;
  1653. sectype : Tasmsectiontype;
  1654. begin
  1655. storefilepos:=aktfilepos;
  1656. aktfilepos:=sym.fileinfo;
  1657. l:=sym.getsize;
  1658. {$ifndef segment_threadvars}
  1659. if (vo_is_thread_var in sym.varoptions) then
  1660. inc(l,sizeof(aint));
  1661. list:=asmlist[al_globals];
  1662. sectype:=sec_bss;
  1663. {$else}
  1664. if (vo_is_thread_var in sym.varoptions) then
  1665. begin
  1666. list:=asmlist[al_threadvars];
  1667. sectype:=sec_threadvar;
  1668. end
  1669. else
  1670. begin
  1671. list:=asmlist[al_globals];
  1672. sectype:=sec_bss;
  1673. end;
  1674. {$endif}
  1675. varalign:=var_align(l);
  1676. maybe_new_object_file(list);
  1677. new_section(list,sectype,lower(sym.mangledname),varalign);
  1678. if (sym.owner.symtabletype=globalsymtable) or
  1679. maybe_smartlink_symbol or
  1680. DLLSource or
  1681. (assigned(current_procinfo) and
  1682. (po_inline in current_procinfo.procdef.procoptions)) or
  1683. (vo_is_exported in sym.varoptions) or
  1684. (vo_is_C_var in sym.varoptions) then
  1685. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1686. else
  1687. list.concat(Tai_datablock.create(sym.mangledname,l));
  1688. aktfilepos:=storefilepos;
  1689. end;
  1690. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1691. var
  1692. sym : tsym;
  1693. isaddr : boolean;
  1694. cgsize : tcgsize;
  1695. begin
  1696. sym:=tsym(st.symindex.first);
  1697. while assigned(sym) do
  1698. begin
  1699. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1700. begin
  1701. with tabstractnormalvarsym(sym) do
  1702. begin
  1703. { Parameters passed to assembler procedures need to be kept
  1704. in the original location }
  1705. if (sym.typ=paravarsym) and
  1706. (po_assembler in current_procinfo.procdef.procoptions) then
  1707. begin
  1708. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1709. end
  1710. else
  1711. begin
  1712. isaddr:=(st.symtabletype=parasymtable) and
  1713. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1714. if isaddr then
  1715. cgsize:=OS_ADDR
  1716. else
  1717. cgsize:=def_cgsize(vartype.def);
  1718. {$ifndef OLDREGVARS}
  1719. { When there is assembler code we can't use regvars }
  1720. if is_regvar then
  1721. begin
  1722. localloc.size:=cgsize;
  1723. case varregable of
  1724. vr_intreg :
  1725. begin
  1726. localloc.loc:=LOC_CREGISTER;
  1727. {$ifndef cpu64bit}
  1728. if cgsize in [OS_64,OS_S64] then
  1729. begin
  1730. localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1731. localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1732. end
  1733. else
  1734. {$endif cpu64bit}
  1735. localloc.register:=cg.getintregister(list,cgsize);
  1736. end;
  1737. vr_fpureg :
  1738. begin
  1739. localloc.loc:=LOC_CFPUREGISTER;
  1740. localloc.register:=cg.getfpuregister(list,cgsize);
  1741. end;
  1742. vr_mmreg :
  1743. begin
  1744. localloc.loc:=LOC_CMMREGISTER;
  1745. localloc.register:=cg.getmmregister(list,cgsize);
  1746. end;
  1747. else
  1748. internalerror(2004101010);
  1749. end;
  1750. { Allocate register already, to prevent first allocation to be
  1751. inside a loop }
  1752. {$ifndef cpu64bit}
  1753. if cgsize in [OS_64,OS_S64] then
  1754. begin
  1755. cg.a_reg_sync(list,localloc.register64.reglo);
  1756. cg.a_reg_sync(list,localloc.register64.reghi);
  1757. end
  1758. else
  1759. {$endif cpu64bit}
  1760. cg.a_reg_sync(list,localloc.register);
  1761. end
  1762. else
  1763. {$endif NOT OLDREGVARS}
  1764. begin
  1765. localloc.loc:=LOC_REFERENCE;
  1766. localloc.size:=cgsize;
  1767. case st.symtabletype of
  1768. parasymtable :
  1769. begin
  1770. { Reuse the parameter location for values to are at a single location on the stack }
  1771. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1772. begin
  1773. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1774. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1775. end
  1776. else
  1777. begin
  1778. if isaddr then
  1779. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1780. else
  1781. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1782. end;
  1783. end;
  1784. localsymtable,
  1785. stt_exceptsymtable :
  1786. begin
  1787. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1788. end;
  1789. staticsymtable :
  1790. begin
  1791. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1792. if not((target_info.system=system_powerpc_darwin) and
  1793. (cs_create_pic in aktmoduleswitches)) and
  1794. not(vo_is_dll_var in varoptions) {$ifndef segment_threadvars} and
  1795. not(vo_is_thread_var in varoptions) {$endif} then
  1796. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1797. end;
  1798. else
  1799. internalerror(200410103);
  1800. end;
  1801. end;
  1802. end;
  1803. if cs_asm_source in aktglobalswitches then
  1804. begin
  1805. case localloc.loc of
  1806. LOC_REGISTER,
  1807. LOC_CREGISTER :
  1808. begin
  1809. if (cs_no_regalloc in aktglobalswitches) then
  1810. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register '+
  1811. std_regname(localloc.register))))
  1812. else
  1813. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register')));
  1814. end;
  1815. LOC_REFERENCE :
  1816. begin
  1817. if not assigned(localloc.reference.symbol) then
  1818. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1819. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1820. end;
  1821. end;
  1822. end;
  1823. end;
  1824. end;
  1825. sym:=tsym(sym.indexnext);
  1826. end;
  1827. end;
  1828. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1829. var
  1830. sym : tsym;
  1831. begin
  1832. sym:=tsym(st.symindex.first);
  1833. while assigned(sym) do
  1834. begin
  1835. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1836. begin
  1837. with tabstractnormalvarsym(sym) do
  1838. begin
  1839. { Note: We need to keep the data available in memory
  1840. for the sub procedures that can access local data
  1841. in the parent procedures }
  1842. case localloc.loc of
  1843. LOC_CREGISTER :
  1844. {$ifndef cpu64bit}
  1845. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1846. begin
  1847. cg.a_reg_sync(list,localloc.register64.reglo);
  1848. cg.a_reg_sync(list,localloc.register64.reghi);
  1849. end
  1850. else
  1851. {$endif cpu64bit}
  1852. cg.a_reg_sync(list,localloc.register);
  1853. LOC_REFERENCE :
  1854. begin
  1855. case st.symtabletype of
  1856. localsymtable,
  1857. parasymtable,
  1858. stt_exceptsymtable :
  1859. tg.Ungetlocal(list,localloc.reference);
  1860. end;
  1861. end;
  1862. end;
  1863. end;
  1864. end;
  1865. sym:=tsym(sym.indexnext);
  1866. end;
  1867. end;
  1868. { persistent rtti generation }
  1869. procedure generate_rtti(p:Ttypesym);
  1870. var
  1871. rsym : trttisym;
  1872. def : tstoreddef;
  1873. begin
  1874. { rtti can only be generated for classes that are always typesyms }
  1875. def:=tstoreddef(ttypesym(p).restype.def);
  1876. { there is an error, skip rtti info }
  1877. if (def.deftype=errordef) or (Errorcount>0) then
  1878. exit;
  1879. { only create rtti once for each definition }
  1880. if not(df_has_rttitable in def.defoptions) then
  1881. begin
  1882. { definition should be in the same symtable as the symbol }
  1883. if p.owner<>def.owner then
  1884. internalerror(200108262);
  1885. { create rttisym }
  1886. rsym:=trttisym.create(p.name,fullrtti);
  1887. p.owner.insert(rsym);
  1888. { register rttisym in definition }
  1889. include(def.defoptions,df_has_rttitable);
  1890. def.rttitablesym:=rsym;
  1891. { write rtti data }
  1892. def.write_child_rtti_data(fullrtti);
  1893. maybe_new_object_file(asmlist[al_rtti]);
  1894. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1895. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1896. def.write_rtti_data(fullrtti);
  1897. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1898. end;
  1899. end;
  1900. { persistent init table generation }
  1901. procedure generate_inittable(p:tsym);
  1902. var
  1903. rsym : trttisym;
  1904. def : tstoreddef;
  1905. begin
  1906. { anonymous types are also allowed for records that can be varsym }
  1907. case p.typ of
  1908. typesym :
  1909. def:=tstoreddef(ttypesym(p).restype.def);
  1910. globalvarsym,
  1911. localvarsym,
  1912. paravarsym :
  1913. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  1914. else
  1915. internalerror(200108263);
  1916. end;
  1917. { only create inittable once for each definition }
  1918. if not(df_has_inittable in def.defoptions) then
  1919. begin
  1920. { definition should be in the same symtable as the symbol }
  1921. if p.owner<>def.owner then
  1922. internalerror(200108264);
  1923. { create rttisym }
  1924. rsym:=trttisym.create(p.name,initrtti);
  1925. p.owner.insert(rsym);
  1926. { register rttisym in definition }
  1927. include(def.defoptions,df_has_inittable);
  1928. def.inittablesym:=rsym;
  1929. { write inittable data }
  1930. def.write_child_rtti_data(initrtti);
  1931. maybe_new_object_file(asmlist[al_rtti]);
  1932. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1933. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1934. def.write_rtti_data(initrtti);
  1935. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1936. end;
  1937. end;
  1938. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  1939. var
  1940. i,j,
  1941. proccount : longint;
  1942. tmps : string;
  1943. begin
  1944. for i:=1 to _class.implementedinterfaces.count do
  1945. begin
  1946. { only if implemented by this class }
  1947. if _class.implementedinterfaces.implindex(i)=i then
  1948. begin
  1949. proccount:=_class.implementedinterfaces.implproccount(i);
  1950. for j:=1 to proccount do
  1951. begin
  1952. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1953. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  1954. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  1955. { create wrapper code }
  1956. new_section(list,sec_code,lower(tmps),0);
  1957. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  1958. end;
  1959. end;
  1960. end;
  1961. end;
  1962. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  1963. var
  1964. def : tstoreddef;
  1965. begin
  1966. def:=tstoreddef(st.defindex.first);
  1967. while assigned(def) do
  1968. begin
  1969. if is_class(def) then
  1970. gen_intf_wrapper(list,tobjectdef(def));
  1971. def:=tstoreddef(def.indexnext);
  1972. end;
  1973. end;
  1974. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1975. var
  1976. href : treference;
  1977. begin
  1978. if is_object(objdef) then
  1979. begin
  1980. case selfloc.loc of
  1981. LOC_CREFERENCE,
  1982. LOC_REFERENCE:
  1983. begin
  1984. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  1985. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1986. end;
  1987. else
  1988. internalerror(200305056);
  1989. end;
  1990. end
  1991. else
  1992. begin
  1993. case selfloc.loc of
  1994. LOC_REGISTER:
  1995. begin
  1996. {$ifdef cpu_uses_separate_address_registers}
  1997. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  1998. begin
  1999. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2000. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2001. end
  2002. else
  2003. {$endif cpu_uses_separate_address_registers}
  2004. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2005. end;
  2006. LOC_CREGISTER,
  2007. LOC_CREFERENCE,
  2008. LOC_REFERENCE:
  2009. begin
  2010. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2011. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2012. end;
  2013. else
  2014. internalerror(200305057);
  2015. end;
  2016. end;
  2017. vmtreg:=cg.getaddressregister(list);
  2018. cg.g_maybe_testself(list,href.base);
  2019. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2020. end;
  2021. end.