ncgutil.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  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. {$ifdef powerpc64}
  1387. { unget the register that contains the stack pointer before the procedure entry, }
  1388. { which is used to access the parameters in their original callee-side location }
  1389. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1390. {$endif powerpc64}
  1391. end;
  1392. procedure gen_initialize_code(list:TAAsmoutput);
  1393. begin
  1394. { initialize local data like ansistrings }
  1395. case current_procinfo.procdef.proctypeoption of
  1396. potype_unitinit:
  1397. begin
  1398. { this is also used for initialization of variables in a
  1399. program which does not have a globalsymtable }
  1400. if assigned(current_module.globalsymtable) then
  1401. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1402. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1403. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1404. end;
  1405. { units have seperate code for initilization and finalization }
  1406. potype_unitfinalize: ;
  1407. { program init/final is generated in separate procedure }
  1408. potype_proginit:
  1409. begin
  1410. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1411. end;
  1412. else
  1413. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1414. end;
  1415. { initialisizes temp. ansi/wide string data }
  1416. inittempvariables(list);
  1417. { initialize ansi/widesstring para's }
  1418. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1419. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1420. {$ifdef OLDREGVARS}
  1421. load_regvars(list,nil);
  1422. {$endif OLDREGVARS}
  1423. end;
  1424. procedure gen_finalize_code(list:TAAsmoutput);
  1425. begin
  1426. {$ifdef OLDREGVARS}
  1427. cleanup_regvars(list);
  1428. {$endif OLDREGVARS}
  1429. { finalize temporary data }
  1430. finalizetempvariables(list);
  1431. { finalize local data like ansistrings}
  1432. case current_procinfo.procdef.proctypeoption of
  1433. potype_unitfinalize:
  1434. begin
  1435. { this is also used for initialization of variables in a
  1436. program which does not have a globalsymtable }
  1437. if assigned(current_module.globalsymtable) then
  1438. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1439. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1440. end;
  1441. { units/progs have separate code for initialization and finalization }
  1442. potype_unitinit: ;
  1443. { program init/final is generated in separate procedure }
  1444. potype_proginit: ;
  1445. else
  1446. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1447. end;
  1448. { finalize paras data }
  1449. if assigned(current_procinfo.procdef.parast) and
  1450. not(po_assembler in current_procinfo.procdef.procoptions) then
  1451. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1452. end;
  1453. procedure gen_entry_code(list:TAAsmoutput);
  1454. var
  1455. paraloc1,
  1456. paraloc2 : tcgpara;
  1457. begin
  1458. paraloc1.init;
  1459. paraloc2.init;
  1460. { the actual profile code can clobber some registers,
  1461. therefore if the context must be saved, do it before
  1462. the actual call to the profile code
  1463. }
  1464. if (cs_profile in aktmoduleswitches) and
  1465. not(po_assembler in current_procinfo.procdef.procoptions) then
  1466. begin
  1467. { non-win32 can call mcout even in main }
  1468. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1469. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1470. begin
  1471. cg.allocallcpuregisters(list);
  1472. cg.g_profilecode(list);
  1473. cg.deallocallcpuregisters(list);
  1474. end;
  1475. end;
  1476. { call startup helpers from main program }
  1477. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1478. begin
  1479. if (target_info.system = system_powerpc_darwin) or
  1480. (target_info.system = system_powerpc_macos) then
  1481. begin
  1482. { the parameters are already in the right registers }
  1483. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1484. end;
  1485. { initialize units }
  1486. cg.allocallcpuregisters(list);
  1487. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1488. cg.deallocallcpuregisters(list);
  1489. end;
  1490. list.concat(Tai_force_line.Create);
  1491. {$ifdef OLDREGVARS}
  1492. load_regvars(list,nil);
  1493. {$endif OLDREGVARS}
  1494. paraloc1.done;
  1495. paraloc2.done;
  1496. end;
  1497. procedure gen_exit_code(list:TAAsmoutput);
  1498. begin
  1499. { call __EXIT for main program }
  1500. if (not DLLsource) and
  1501. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1502. cg.a_call_name(list,'FPC_DO_EXIT');
  1503. end;
  1504. {****************************************************************************
  1505. Entry/Exit
  1506. ****************************************************************************}
  1507. procedure gen_proc_symbol(list:Taasmoutput);
  1508. var
  1509. hs : string;
  1510. begin
  1511. repeat
  1512. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1513. if hs='' then
  1514. break;
  1515. if (cs_profile in aktmoduleswitches) or
  1516. (po_global in current_procinfo.procdef.procoptions) then
  1517. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1518. else
  1519. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1520. if target_info.use_function_relative_addresses then
  1521. list.concat(Tai_function_name.create(hs));
  1522. until false;
  1523. current_procinfo.procdef.procstarttai:=tai(list.last);
  1524. end;
  1525. procedure gen_proc_symbol_end(list:Taasmoutput);
  1526. begin
  1527. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1528. begin
  1529. { Insert Ident of the compiler in the main .text section }
  1530. if (not (cs_create_smart in aktmoduleswitches)) then
  1531. begin
  1532. list.insert(Tai_align.Create(const_align(32)));
  1533. list.insert(Tai_string.Create('FPC '+full_version_string+
  1534. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  1535. end;
  1536. { Reference all DEBUGINFO sections from the main .text section }
  1537. if (cs_debuginfo in aktmoduleswitches) then
  1538. debuginfo.referencesections(list);
  1539. end;
  1540. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1541. current_procinfo.procdef.procendtai:=tai(list.last);
  1542. end;
  1543. procedure gen_proc_entry_code(list:Taasmoutput);
  1544. var
  1545. hitemp,
  1546. lotemp : longint;
  1547. begin
  1548. { generate call frame marker for dwarf call frame info }
  1549. dwarfcfi.start_frame(list);
  1550. { All temps are know, write offsets used for information }
  1551. if (cs_asm_source in aktglobalswitches) then
  1552. begin
  1553. if tg.direction>0 then
  1554. begin
  1555. lotemp:=current_procinfo.tempstart;
  1556. hitemp:=tg.lasttemp;
  1557. end
  1558. else
  1559. begin
  1560. lotemp:=tg.lasttemp;
  1561. hitemp:=current_procinfo.tempstart;
  1562. end;
  1563. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1564. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1565. end;
  1566. { generate target specific proc entry code }
  1567. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1568. end;
  1569. procedure gen_proc_exit_code(list:Taasmoutput);
  1570. var
  1571. parasize : longint;
  1572. begin
  1573. { c style clearstack does not need to remove parameters from the stack, only the
  1574. return value when it was pushed by arguments }
  1575. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1576. begin
  1577. parasize:=0;
  1578. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1579. inc(parasize,sizeof(aint));
  1580. end
  1581. else
  1582. parasize:=current_procinfo.para_stack_size;
  1583. { generate target specific proc exit code }
  1584. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1585. { release return registers, needed for optimizer }
  1586. if not is_void(current_procinfo.procdef.rettype.def) then
  1587. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1588. { end of frame marker for call frame info }
  1589. dwarfcfi.end_frame(list);
  1590. end;
  1591. procedure gen_stack_check_size_para(list:Taasmoutput);
  1592. var
  1593. paraloc1 : tcgpara;
  1594. begin
  1595. paraloc1.init;
  1596. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1597. paramanager.allocparaloc(list,paraloc1);
  1598. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1599. paramanager.freeparaloc(list,paraloc1);
  1600. paraloc1.done;
  1601. end;
  1602. procedure gen_stack_check_call(list:Taasmoutput);
  1603. var
  1604. paraloc1 : tcgpara;
  1605. begin
  1606. paraloc1.init;
  1607. { Also alloc the register needed for the parameter }
  1608. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1609. paramanager.allocparaloc(list,paraloc1);
  1610. paramanager.freeparaloc(list,paraloc1);
  1611. { Call the helper }
  1612. cg.allocallcpuregisters(list);
  1613. cg.a_call_name(list,'FPC_STACKCHECK');
  1614. cg.deallocallcpuregisters(list);
  1615. paraloc1.done;
  1616. end;
  1617. procedure gen_save_used_regs(list:TAAsmoutput);
  1618. begin
  1619. { Pure assembler routines need to save the registers themselves }
  1620. if (po_assembler in current_procinfo.procdef.procoptions) then
  1621. exit;
  1622. { oldfpccall expects all registers to be destroyed }
  1623. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1624. cg.g_save_standard_registers(list);
  1625. end;
  1626. procedure gen_restore_used_regs(list:TAAsmoutput);
  1627. begin
  1628. { Pure assembler routines need to save the registers themselves }
  1629. if (po_assembler in current_procinfo.procdef.procoptions) then
  1630. exit;
  1631. { oldfpccall expects all registers to be destroyed }
  1632. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1633. cg.g_restore_standard_registers(list);
  1634. end;
  1635. {****************************************************************************
  1636. External handling
  1637. ****************************************************************************}
  1638. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1639. begin
  1640. { add the procedure to the al_procedures }
  1641. maybe_new_object_file(list);
  1642. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1643. list.concat(Tai_align.create(aktalignment.procalign));
  1644. if (po_global in pd.procoptions) then
  1645. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1646. else
  1647. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1648. cg.a_jmp_name(list,externalname);
  1649. end;
  1650. {****************************************************************************
  1651. Const Data
  1652. ****************************************************************************}
  1653. procedure insertbssdata(sym : tglobalvarsym);
  1654. var
  1655. l,varalign : longint;
  1656. storefilepos : tfileposinfo;
  1657. list : Taasmoutput;
  1658. sectype : Tasmsectiontype;
  1659. begin
  1660. storefilepos:=aktfilepos;
  1661. aktfilepos:=sym.fileinfo;
  1662. l:=sym.getsize;
  1663. {$ifndef segment_threadvars}
  1664. if (vo_is_thread_var in sym.varoptions) then
  1665. inc(l,sizeof(aint));
  1666. list:=asmlist[al_globals];
  1667. sectype:=sec_bss;
  1668. {$else}
  1669. if (vo_is_thread_var in sym.varoptions) then
  1670. begin
  1671. list:=asmlist[al_threadvars];
  1672. sectype:=sec_threadvar;
  1673. end
  1674. else
  1675. begin
  1676. list:=asmlist[al_globals];
  1677. sectype:=sec_bss;
  1678. end;
  1679. {$endif}
  1680. varalign:=var_align(l);
  1681. maybe_new_object_file(list);
  1682. new_section(list,sectype,lower(sym.mangledname),varalign);
  1683. if (sym.owner.symtabletype=globalsymtable) or
  1684. maybe_smartlink_symbol or
  1685. DLLSource or
  1686. (assigned(current_procinfo) and
  1687. (po_inline in current_procinfo.procdef.procoptions)) or
  1688. (vo_is_exported in sym.varoptions) or
  1689. (vo_is_C_var in sym.varoptions) then
  1690. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1691. else
  1692. list.concat(Tai_datablock.create(sym.mangledname,l));
  1693. aktfilepos:=storefilepos;
  1694. end;
  1695. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1696. var
  1697. sym : tsym;
  1698. isaddr : boolean;
  1699. cgsize : tcgsize;
  1700. begin
  1701. sym:=tsym(st.symindex.first);
  1702. while assigned(sym) do
  1703. begin
  1704. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1705. begin
  1706. with tabstractnormalvarsym(sym) do
  1707. begin
  1708. { Parameters passed to assembler procedures need to be kept
  1709. in the original location }
  1710. if (sym.typ=paravarsym) and
  1711. (po_assembler in current_procinfo.procdef.procoptions) then
  1712. begin
  1713. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1714. end
  1715. else
  1716. begin
  1717. isaddr:=(st.symtabletype=parasymtable) and
  1718. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1719. if isaddr then
  1720. cgsize:=OS_ADDR
  1721. else
  1722. cgsize:=def_cgsize(vartype.def);
  1723. {$ifndef OLDREGVARS}
  1724. { When there is assembler code we can't use regvars }
  1725. if is_regvar then
  1726. begin
  1727. localloc.size:=cgsize;
  1728. case varregable of
  1729. vr_intreg :
  1730. begin
  1731. localloc.loc:=LOC_CREGISTER;
  1732. {$ifndef cpu64bit}
  1733. if cgsize in [OS_64,OS_S64] then
  1734. begin
  1735. localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1736. localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1737. end
  1738. else
  1739. {$endif cpu64bit}
  1740. localloc.register:=cg.getintregister(list,cgsize);
  1741. end;
  1742. vr_fpureg :
  1743. begin
  1744. localloc.loc:=LOC_CFPUREGISTER;
  1745. localloc.register:=cg.getfpuregister(list,cgsize);
  1746. end;
  1747. vr_mmreg :
  1748. begin
  1749. localloc.loc:=LOC_CMMREGISTER;
  1750. localloc.register:=cg.getmmregister(list,cgsize);
  1751. end;
  1752. else
  1753. internalerror(2004101010);
  1754. end;
  1755. { Allocate register already, to prevent first allocation to be
  1756. inside a loop }
  1757. {$ifndef cpu64bit}
  1758. if cgsize in [OS_64,OS_S64] then
  1759. begin
  1760. cg.a_reg_sync(list,localloc.register64.reglo);
  1761. cg.a_reg_sync(list,localloc.register64.reghi);
  1762. end
  1763. else
  1764. {$endif cpu64bit}
  1765. cg.a_reg_sync(list,localloc.register);
  1766. end
  1767. else
  1768. {$endif NOT OLDREGVARS}
  1769. begin
  1770. localloc.loc:=LOC_REFERENCE;
  1771. localloc.size:=cgsize;
  1772. case st.symtabletype of
  1773. parasymtable :
  1774. begin
  1775. { Reuse the parameter location for values to are at a single location on the stack }
  1776. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1777. begin
  1778. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1779. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1780. end
  1781. else
  1782. begin
  1783. if isaddr then
  1784. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1785. else
  1786. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1787. end;
  1788. end;
  1789. localsymtable,
  1790. stt_exceptsymtable :
  1791. begin
  1792. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1793. end;
  1794. staticsymtable :
  1795. begin
  1796. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1797. if not((target_info.system=system_powerpc_darwin) and
  1798. (cs_create_pic in aktmoduleswitches)) and
  1799. not(vo_is_dll_var in varoptions) {$ifndef segment_threadvars} and
  1800. not(vo_is_thread_var in varoptions) {$endif} then
  1801. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1802. end;
  1803. else
  1804. internalerror(200410103);
  1805. end;
  1806. end;
  1807. end;
  1808. if cs_asm_source in aktglobalswitches then
  1809. begin
  1810. case localloc.loc of
  1811. LOC_REGISTER,
  1812. LOC_CREGISTER :
  1813. begin
  1814. if (cs_no_regalloc in aktglobalswitches) then
  1815. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register '+
  1816. std_regname(localloc.register))))
  1817. else
  1818. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register')));
  1819. end;
  1820. LOC_REFERENCE :
  1821. begin
  1822. if not assigned(localloc.reference.symbol) then
  1823. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1824. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1825. end;
  1826. end;
  1827. end;
  1828. end;
  1829. end;
  1830. sym:=tsym(sym.indexnext);
  1831. end;
  1832. end;
  1833. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1834. var
  1835. sym : tsym;
  1836. begin
  1837. sym:=tsym(st.symindex.first);
  1838. while assigned(sym) do
  1839. begin
  1840. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1841. begin
  1842. with tabstractnormalvarsym(sym) do
  1843. begin
  1844. { Note: We need to keep the data available in memory
  1845. for the sub procedures that can access local data
  1846. in the parent procedures }
  1847. case localloc.loc of
  1848. LOC_CREGISTER :
  1849. {$ifndef cpu64bit}
  1850. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1851. begin
  1852. cg.a_reg_sync(list,localloc.register64.reglo);
  1853. cg.a_reg_sync(list,localloc.register64.reghi);
  1854. end
  1855. else
  1856. {$endif cpu64bit}
  1857. cg.a_reg_sync(list,localloc.register);
  1858. LOC_REFERENCE :
  1859. begin
  1860. case st.symtabletype of
  1861. localsymtable,
  1862. parasymtable,
  1863. stt_exceptsymtable :
  1864. tg.Ungetlocal(list,localloc.reference);
  1865. end;
  1866. end;
  1867. end;
  1868. end;
  1869. end;
  1870. sym:=tsym(sym.indexnext);
  1871. end;
  1872. end;
  1873. { persistent rtti generation }
  1874. procedure generate_rtti(p:Ttypesym);
  1875. var
  1876. rsym : trttisym;
  1877. def : tstoreddef;
  1878. begin
  1879. { rtti can only be generated for classes that are always typesyms }
  1880. def:=tstoreddef(ttypesym(p).restype.def);
  1881. { there is an error, skip rtti info }
  1882. if (def.deftype=errordef) or (Errorcount>0) then
  1883. exit;
  1884. { only create rtti once for each definition }
  1885. if not(df_has_rttitable in def.defoptions) then
  1886. begin
  1887. { definition should be in the same symtable as the symbol }
  1888. if p.owner<>def.owner then
  1889. internalerror(200108262);
  1890. { create rttisym }
  1891. rsym:=trttisym.create(p.name,fullrtti);
  1892. p.owner.insert(rsym);
  1893. { register rttisym in definition }
  1894. include(def.defoptions,df_has_rttitable);
  1895. def.rttitablesym:=rsym;
  1896. { write rtti data }
  1897. def.write_child_rtti_data(fullrtti);
  1898. maybe_new_object_file(asmlist[al_rtti]);
  1899. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1900. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1901. def.write_rtti_data(fullrtti);
  1902. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1903. end;
  1904. end;
  1905. { persistent init table generation }
  1906. procedure generate_inittable(p:tsym);
  1907. var
  1908. rsym : trttisym;
  1909. def : tstoreddef;
  1910. begin
  1911. { anonymous types are also allowed for records that can be varsym }
  1912. case p.typ of
  1913. typesym :
  1914. def:=tstoreddef(ttypesym(p).restype.def);
  1915. globalvarsym,
  1916. localvarsym,
  1917. paravarsym :
  1918. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  1919. else
  1920. internalerror(200108263);
  1921. end;
  1922. { only create inittable once for each definition }
  1923. if not(df_has_inittable in def.defoptions) then
  1924. begin
  1925. { definition should be in the same symtable as the symbol }
  1926. if p.owner<>def.owner then
  1927. internalerror(200108264);
  1928. { create rttisym }
  1929. rsym:=trttisym.create(p.name,initrtti);
  1930. p.owner.insert(rsym);
  1931. { register rttisym in definition }
  1932. include(def.defoptions,df_has_inittable);
  1933. def.inittablesym:=rsym;
  1934. { write inittable data }
  1935. def.write_child_rtti_data(initrtti);
  1936. maybe_new_object_file(asmlist[al_rtti]);
  1937. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1938. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1939. def.write_rtti_data(initrtti);
  1940. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1941. end;
  1942. end;
  1943. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  1944. var
  1945. i,j,
  1946. proccount : longint;
  1947. tmps : string;
  1948. begin
  1949. for i:=1 to _class.implementedinterfaces.count do
  1950. begin
  1951. { only if implemented by this class }
  1952. if _class.implementedinterfaces.implindex(i)=i then
  1953. begin
  1954. proccount:=_class.implementedinterfaces.implproccount(i);
  1955. for j:=1 to proccount do
  1956. begin
  1957. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1958. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  1959. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  1960. { create wrapper code }
  1961. new_section(list,sec_code,lower(tmps),0);
  1962. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  1963. end;
  1964. end;
  1965. end;
  1966. end;
  1967. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  1968. var
  1969. def : tstoreddef;
  1970. begin
  1971. def:=tstoreddef(st.defindex.first);
  1972. while assigned(def) do
  1973. begin
  1974. if is_class(def) then
  1975. gen_intf_wrapper(list,tobjectdef(def));
  1976. def:=tstoreddef(def.indexnext);
  1977. end;
  1978. end;
  1979. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1980. var
  1981. href : treference;
  1982. begin
  1983. if is_object(objdef) then
  1984. begin
  1985. case selfloc.loc of
  1986. LOC_CREFERENCE,
  1987. LOC_REFERENCE:
  1988. begin
  1989. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  1990. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1991. end;
  1992. else
  1993. internalerror(200305056);
  1994. end;
  1995. end
  1996. else
  1997. begin
  1998. case selfloc.loc of
  1999. LOC_REGISTER:
  2000. begin
  2001. {$ifdef cpu_uses_separate_address_registers}
  2002. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2003. begin
  2004. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2005. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2006. end
  2007. else
  2008. {$endif cpu_uses_separate_address_registers}
  2009. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2010. end;
  2011. LOC_CREGISTER,
  2012. LOC_CREFERENCE,
  2013. LOC_REFERENCE:
  2014. begin
  2015. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2016. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2017. end;
  2018. else
  2019. internalerror(200305057);
  2020. end;
  2021. end;
  2022. vmtreg:=cg.getaddressregister(list);
  2023. cg.g_maybe_testself(list,href.base);
  2024. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2025. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2026. end;
  2027. end.