ncgutil.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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. begin
  1179. {$IFDEF CPUPOWERPC64}
  1180. if (paraloc.shiftval <> 0) then
  1181. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1182. {$ENDIF CPUPOWERPC64}
  1183. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1184. end;
  1185. LOC_MMREGISTER :
  1186. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1187. LOC_FPUREGISTER :
  1188. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1189. LOC_REFERENCE :
  1190. begin
  1191. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1192. { use concatcopy, because it can also be a float which fails when
  1193. load_ref_ref is used. Don't copy data when the references are equal }
  1194. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1195. cg.g_concatcopy(list,href,ref,sizeleft);
  1196. end;
  1197. else
  1198. internalerror(2002081302);
  1199. end;
  1200. end;
  1201. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1202. var
  1203. href : treference;
  1204. begin
  1205. case paraloc.loc of
  1206. LOC_REGISTER :
  1207. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1208. LOC_MMREGISTER :
  1209. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1210. LOC_FPUREGISTER :
  1211. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1212. LOC_REFERENCE :
  1213. begin
  1214. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1215. case getregtype(reg) of
  1216. R_INTREGISTER :
  1217. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1218. R_FPUREGISTER :
  1219. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1220. R_MMREGISTER :
  1221. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1222. else
  1223. internalerror(2004101012);
  1224. end;
  1225. end;
  1226. else
  1227. internalerror(2002081302);
  1228. end;
  1229. end;
  1230. var
  1231. i : longint;
  1232. currpara : tparavarsym;
  1233. paraloc : pcgparalocation;
  1234. href : treference;
  1235. sizeleft : aint;
  1236. {$ifdef sparc}
  1237. tempref : treference;
  1238. {$endif sparc}
  1239. begin
  1240. if (po_assembler in current_procinfo.procdef.procoptions) then
  1241. exit;
  1242. { Allocate registers used by parameters }
  1243. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1244. begin
  1245. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1246. paraloc:=currpara.paraloc[calleeside].location;
  1247. while assigned(paraloc) do
  1248. begin
  1249. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1250. get_para(paraloc^);
  1251. paraloc:=paraloc^.next;
  1252. end;
  1253. end;
  1254. { Copy parameters to local references/registers }
  1255. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1256. begin
  1257. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1258. paraloc:=currpara.paraloc[calleeside].location;
  1259. { skip e.g. empty records }
  1260. if (paraloc^.loc = LOC_VOID) then
  1261. continue;
  1262. if not assigned(paraloc) then
  1263. internalerror(200408203);
  1264. case currpara.localloc.loc of
  1265. LOC_REFERENCE :
  1266. begin
  1267. { If the parameter location is reused we don't need to copy
  1268. anything }
  1269. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1270. begin
  1271. href:=currpara.localloc.reference;
  1272. sizeleft:=currpara.paraloc[calleeside].intsize;
  1273. while assigned(paraloc) do
  1274. begin
  1275. unget_para(paraloc^);
  1276. if (paraloc^.size=OS_NO) then
  1277. begin
  1278. { Can only be a reference that contains the rest
  1279. of the parameter }
  1280. if (paraloc^.loc<>LOC_REFERENCE) or
  1281. assigned(paraloc^.next) then
  1282. internalerror(2005013010);
  1283. gen_load_ref(paraloc^,href,sizeleft);
  1284. inc(href.offset,sizeleft);
  1285. sizeleft:=0;
  1286. end
  1287. else
  1288. begin
  1289. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1290. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1291. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1292. end;
  1293. paraloc:=paraloc^.next;
  1294. end;
  1295. end;
  1296. end;
  1297. LOC_CREGISTER :
  1298. begin
  1299. {$ifndef cpu64bit}
  1300. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1301. is_64bit(currpara.vartype.def) then
  1302. begin
  1303. case paraloc^.loc of
  1304. LOC_REGISTER:
  1305. begin
  1306. if not assigned(paraloc^.next) then
  1307. internalerror(200410104);
  1308. if (target_info.endian=ENDIAN_BIG) then
  1309. begin
  1310. { paraloc^ -> high
  1311. paraloc^.next -> low }
  1312. unget_para(paraloc^);
  1313. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1314. unget_para(paraloc^.next^);
  1315. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1316. end
  1317. else
  1318. begin
  1319. { paraloc^ -> low
  1320. paraloc^.next -> high }
  1321. unget_para(paraloc^);
  1322. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1323. unget_para(paraloc^.next^);
  1324. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1325. end;
  1326. end;
  1327. LOC_REFERENCE:
  1328. begin
  1329. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1330. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1331. unget_para(paraloc^);
  1332. end;
  1333. else
  1334. internalerror(2005101501);
  1335. end
  1336. end
  1337. else
  1338. {$endif cpu64bit}
  1339. begin
  1340. if assigned(paraloc^.next) then
  1341. internalerror(200410105);
  1342. unget_para(paraloc^);
  1343. gen_load_reg(paraloc^,currpara.localloc.register);
  1344. end;
  1345. end;
  1346. LOC_CFPUREGISTER :
  1347. begin
  1348. {$ifdef sparc}
  1349. { Sparc passes floats in int registers, when loading to fpu register
  1350. we need a temp }
  1351. sizeleft := TCGSize2Size[currpara.localloc.size];
  1352. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1353. href:=tempref;
  1354. while assigned(paraloc) do
  1355. begin
  1356. unget_para(paraloc^);
  1357. gen_load_ref(paraloc^,href,sizeleft);
  1358. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1359. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1360. paraloc:=paraloc^.next;
  1361. end;
  1362. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1363. tg.UnGetTemp(list,tempref);
  1364. {$else sparc}
  1365. unget_para(paraloc^);
  1366. gen_load_reg(paraloc^,currpara.localloc.register);
  1367. if assigned(paraloc^.next) then
  1368. internalerror(200410109);
  1369. {$endif sparc}
  1370. end;
  1371. LOC_CMMREGISTER :
  1372. begin
  1373. unget_para(paraloc^);
  1374. gen_load_reg(paraloc^,currpara.localloc.register);
  1375. { data could come in two memory locations, for now
  1376. we simply ignore the sanity check (FK)
  1377. if assigned(paraloc^.next) then
  1378. internalerror(200410108);
  1379. }
  1380. end;
  1381. end;
  1382. end;
  1383. { generate copies of call by value parameters, must be done before
  1384. the initialization and body is parsed because the refcounts are
  1385. incremented using the local copies }
  1386. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1387. {$ifdef powerpc}
  1388. { unget the register that contains the stack pointer before the procedure entry, }
  1389. { which is used to access the parameters in their original callee-side location }
  1390. cg.a_reg_dealloc(list,NR_R12);
  1391. {$endif powerpc}
  1392. {$ifdef powerpc64}
  1393. { unget the register that contains the stack pointer before the procedure entry, }
  1394. { which is used to access the parameters in their original callee-side location }
  1395. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1396. {$endif powerpc64}
  1397. end;
  1398. procedure gen_initialize_code(list:TAAsmoutput);
  1399. begin
  1400. { initialize local data like ansistrings }
  1401. case current_procinfo.procdef.proctypeoption of
  1402. potype_unitinit:
  1403. begin
  1404. { this is also used for initialization of variables in a
  1405. program which does not have a globalsymtable }
  1406. if assigned(current_module.globalsymtable) then
  1407. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1408. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1409. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1410. end;
  1411. { units have seperate code for initilization and finalization }
  1412. potype_unitfinalize: ;
  1413. { program init/final is generated in separate procedure }
  1414. potype_proginit:
  1415. begin
  1416. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1417. end;
  1418. else
  1419. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1420. end;
  1421. { initialisizes temp. ansi/wide string data }
  1422. inittempvariables(list);
  1423. { initialize ansi/widesstring para's }
  1424. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1425. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1426. {$ifdef OLDREGVARS}
  1427. load_regvars(list,nil);
  1428. {$endif OLDREGVARS}
  1429. end;
  1430. procedure gen_finalize_code(list:TAAsmoutput);
  1431. begin
  1432. {$ifdef OLDREGVARS}
  1433. cleanup_regvars(list);
  1434. {$endif OLDREGVARS}
  1435. { finalize temporary data }
  1436. finalizetempvariables(list);
  1437. { finalize local data like ansistrings}
  1438. case current_procinfo.procdef.proctypeoption of
  1439. potype_unitfinalize:
  1440. begin
  1441. { this is also used for initialization of variables in a
  1442. program which does not have a globalsymtable }
  1443. if assigned(current_module.globalsymtable) then
  1444. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1445. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1446. end;
  1447. { units/progs have separate code for initialization and finalization }
  1448. potype_unitinit: ;
  1449. { program init/final is generated in separate procedure }
  1450. potype_proginit: ;
  1451. else
  1452. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1453. end;
  1454. { finalize paras data }
  1455. if assigned(current_procinfo.procdef.parast) and
  1456. not(po_assembler in current_procinfo.procdef.procoptions) then
  1457. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1458. end;
  1459. procedure gen_entry_code(list:TAAsmoutput);
  1460. var
  1461. paraloc1,
  1462. paraloc2 : tcgpara;
  1463. begin
  1464. paraloc1.init;
  1465. paraloc2.init;
  1466. { the actual profile code can clobber some registers,
  1467. therefore if the context must be saved, do it before
  1468. the actual call to the profile code
  1469. }
  1470. if (cs_profile in aktmoduleswitches) and
  1471. not(po_assembler in current_procinfo.procdef.procoptions) then
  1472. begin
  1473. { non-win32 can call mcout even in main }
  1474. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1475. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1476. begin
  1477. cg.allocallcpuregisters(list);
  1478. cg.g_profilecode(list);
  1479. cg.deallocallcpuregisters(list);
  1480. end;
  1481. end;
  1482. { call startup helpers from main program }
  1483. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1484. begin
  1485. if (target_info.system = system_powerpc_darwin) or
  1486. (target_info.system = system_powerpc_macos) then
  1487. begin
  1488. { the parameters are already in the right registers }
  1489. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1490. end;
  1491. { initialize units }
  1492. cg.allocallcpuregisters(list);
  1493. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1494. cg.deallocallcpuregisters(list);
  1495. end;
  1496. list.concat(Tai_force_line.Create);
  1497. {$ifdef OLDREGVARS}
  1498. load_regvars(list,nil);
  1499. {$endif OLDREGVARS}
  1500. paraloc1.done;
  1501. paraloc2.done;
  1502. end;
  1503. procedure gen_exit_code(list:TAAsmoutput);
  1504. begin
  1505. { call __EXIT for main program }
  1506. if (not DLLsource) and
  1507. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1508. cg.a_call_name(list,'FPC_DO_EXIT');
  1509. end;
  1510. {****************************************************************************
  1511. Entry/Exit
  1512. ****************************************************************************}
  1513. procedure gen_proc_symbol(list:Taasmoutput);
  1514. var
  1515. hs : string;
  1516. begin
  1517. repeat
  1518. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1519. if hs='' then
  1520. break;
  1521. if (cs_profile in aktmoduleswitches) or
  1522. (po_global in current_procinfo.procdef.procoptions) then
  1523. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1524. else
  1525. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1526. if target_info.use_function_relative_addresses then
  1527. list.concat(Tai_function_name.create(hs));
  1528. until false;
  1529. current_procinfo.procdef.procstarttai:=tai(list.last);
  1530. end;
  1531. procedure gen_proc_symbol_end(list:Taasmoutput);
  1532. begin
  1533. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1534. begin
  1535. { Insert Ident of the compiler in the main .text section }
  1536. if (not (cs_create_smart in aktmoduleswitches)) then
  1537. begin
  1538. list.insert(Tai_align.Create(const_align(32)));
  1539. list.insert(Tai_string.Create('FPC '+full_version_string+
  1540. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  1541. end;
  1542. { Reference all DEBUGINFO sections from the main .text section }
  1543. if (cs_debuginfo in aktmoduleswitches) then
  1544. debuginfo.referencesections(list);
  1545. end;
  1546. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1547. current_procinfo.procdef.procendtai:=tai(list.last);
  1548. end;
  1549. procedure gen_proc_entry_code(list:Taasmoutput);
  1550. var
  1551. hitemp,
  1552. lotemp : longint;
  1553. begin
  1554. { generate call frame marker for dwarf call frame info }
  1555. dwarfcfi.start_frame(list);
  1556. { All temps are know, write offsets used for information }
  1557. if (cs_asm_source in aktglobalswitches) then
  1558. begin
  1559. if tg.direction>0 then
  1560. begin
  1561. lotemp:=current_procinfo.tempstart;
  1562. hitemp:=tg.lasttemp;
  1563. end
  1564. else
  1565. begin
  1566. lotemp:=tg.lasttemp;
  1567. hitemp:=current_procinfo.tempstart;
  1568. end;
  1569. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1570. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1571. end;
  1572. { generate target specific proc entry code }
  1573. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1574. end;
  1575. procedure gen_proc_exit_code(list:Taasmoutput);
  1576. var
  1577. parasize : longint;
  1578. begin
  1579. { c style clearstack does not need to remove parameters from the stack, only the
  1580. return value when it was pushed by arguments }
  1581. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1582. begin
  1583. parasize:=0;
  1584. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1585. inc(parasize,sizeof(aint));
  1586. end
  1587. else
  1588. parasize:=current_procinfo.para_stack_size;
  1589. { generate target specific proc exit code }
  1590. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1591. { release return registers, needed for optimizer }
  1592. if not is_void(current_procinfo.procdef.rettype.def) then
  1593. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1594. { end of frame marker for call frame info }
  1595. dwarfcfi.end_frame(list);
  1596. end;
  1597. procedure gen_stack_check_size_para(list:Taasmoutput);
  1598. var
  1599. paraloc1 : tcgpara;
  1600. begin
  1601. paraloc1.init;
  1602. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1603. paramanager.allocparaloc(list,paraloc1);
  1604. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1605. paramanager.freeparaloc(list,paraloc1);
  1606. paraloc1.done;
  1607. end;
  1608. procedure gen_stack_check_call(list:Taasmoutput);
  1609. var
  1610. paraloc1 : tcgpara;
  1611. begin
  1612. paraloc1.init;
  1613. { Also alloc the register needed for the parameter }
  1614. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1615. paramanager.allocparaloc(list,paraloc1);
  1616. paramanager.freeparaloc(list,paraloc1);
  1617. { Call the helper }
  1618. cg.allocallcpuregisters(list);
  1619. cg.a_call_name(list,'FPC_STACKCHECK');
  1620. cg.deallocallcpuregisters(list);
  1621. paraloc1.done;
  1622. end;
  1623. procedure gen_save_used_regs(list:TAAsmoutput);
  1624. begin
  1625. { Pure assembler routines need to save the registers themselves }
  1626. if (po_assembler in current_procinfo.procdef.procoptions) then
  1627. exit;
  1628. { oldfpccall expects all registers to be destroyed }
  1629. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1630. cg.g_save_standard_registers(list);
  1631. end;
  1632. procedure gen_restore_used_regs(list:TAAsmoutput);
  1633. begin
  1634. { Pure assembler routines need to save the registers themselves }
  1635. if (po_assembler in current_procinfo.procdef.procoptions) then
  1636. exit;
  1637. { oldfpccall expects all registers to be destroyed }
  1638. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1639. cg.g_restore_standard_registers(list);
  1640. end;
  1641. {****************************************************************************
  1642. External handling
  1643. ****************************************************************************}
  1644. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1645. begin
  1646. { add the procedure to the al_procedures }
  1647. maybe_new_object_file(list);
  1648. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1649. list.concat(Tai_align.create(aktalignment.procalign));
  1650. if (po_global in pd.procoptions) then
  1651. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1652. else
  1653. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1654. cg.a_jmp_name(list,externalname);
  1655. end;
  1656. {****************************************************************************
  1657. Const Data
  1658. ****************************************************************************}
  1659. procedure insertbssdata(sym : tglobalvarsym);
  1660. var
  1661. l,varalign : longint;
  1662. storefilepos : tfileposinfo;
  1663. list : Taasmoutput;
  1664. sectype : Tasmsectiontype;
  1665. begin
  1666. storefilepos:=aktfilepos;
  1667. aktfilepos:=sym.fileinfo;
  1668. l:=sym.getsize;
  1669. {$ifndef segment_threadvars}
  1670. if (vo_is_thread_var in sym.varoptions) then
  1671. inc(l,sizeof(aint));
  1672. list:=asmlist[al_globals];
  1673. sectype:=sec_bss;
  1674. {$else}
  1675. if (vo_is_thread_var in sym.varoptions) then
  1676. begin
  1677. list:=asmlist[al_threadvars];
  1678. sectype:=sec_threadvar;
  1679. end
  1680. else
  1681. begin
  1682. list:=asmlist[al_globals];
  1683. sectype:=sec_bss;
  1684. end;
  1685. {$endif}
  1686. varalign:=var_align(l);
  1687. maybe_new_object_file(list);
  1688. new_section(list,sectype,lower(sym.mangledname),varalign);
  1689. if (sym.owner.symtabletype=globalsymtable) or
  1690. maybe_smartlink_symbol or
  1691. DLLSource or
  1692. (assigned(current_procinfo) and
  1693. (po_inline in current_procinfo.procdef.procoptions)) or
  1694. (vo_is_exported in sym.varoptions) or
  1695. (vo_is_C_var in sym.varoptions) then
  1696. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1697. else
  1698. list.concat(Tai_datablock.create(sym.mangledname,l));
  1699. aktfilepos:=storefilepos;
  1700. end;
  1701. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1702. var
  1703. sym : tsym;
  1704. isaddr : boolean;
  1705. cgsize : tcgsize;
  1706. begin
  1707. sym:=tsym(st.symindex.first);
  1708. while assigned(sym) do
  1709. begin
  1710. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1711. begin
  1712. with tabstractnormalvarsym(sym) do
  1713. begin
  1714. { Parameters passed to assembler procedures need to be kept
  1715. in the original location }
  1716. if (sym.typ=paravarsym) and
  1717. (po_assembler in current_procinfo.procdef.procoptions) then
  1718. begin
  1719. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1720. end
  1721. else
  1722. begin
  1723. isaddr:=(st.symtabletype=parasymtable) and
  1724. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1725. if isaddr then
  1726. cgsize:=OS_ADDR
  1727. else
  1728. cgsize:=def_cgsize(vartype.def);
  1729. {$ifndef OLDREGVARS}
  1730. { When there is assembler code we can't use regvars }
  1731. if is_regvar then
  1732. begin
  1733. localloc.size:=cgsize;
  1734. case varregable of
  1735. vr_intreg :
  1736. begin
  1737. localloc.loc:=LOC_CREGISTER;
  1738. {$ifndef cpu64bit}
  1739. if cgsize in [OS_64,OS_S64] then
  1740. begin
  1741. localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1742. localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1743. end
  1744. else
  1745. {$endif cpu64bit}
  1746. localloc.register:=cg.getintregister(list,cgsize);
  1747. end;
  1748. vr_fpureg :
  1749. begin
  1750. localloc.loc:=LOC_CFPUREGISTER;
  1751. localloc.register:=cg.getfpuregister(list,cgsize);
  1752. end;
  1753. vr_mmreg :
  1754. begin
  1755. localloc.loc:=LOC_CMMREGISTER;
  1756. localloc.register:=cg.getmmregister(list,cgsize);
  1757. end;
  1758. else
  1759. internalerror(2004101010);
  1760. end;
  1761. { Allocate register already, to prevent first allocation to be
  1762. inside a loop }
  1763. {$ifndef cpu64bit}
  1764. if cgsize in [OS_64,OS_S64] then
  1765. begin
  1766. cg.a_reg_sync(list,localloc.register64.reglo);
  1767. cg.a_reg_sync(list,localloc.register64.reghi);
  1768. end
  1769. else
  1770. {$endif cpu64bit}
  1771. cg.a_reg_sync(list,localloc.register);
  1772. end
  1773. else
  1774. {$endif NOT OLDREGVARS}
  1775. begin
  1776. localloc.loc:=LOC_REFERENCE;
  1777. localloc.size:=cgsize;
  1778. case st.symtabletype of
  1779. parasymtable :
  1780. begin
  1781. { Reuse the parameter location for values to are at a single location on the stack }
  1782. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1783. begin
  1784. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1785. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1786. end
  1787. else
  1788. begin
  1789. if isaddr then
  1790. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1791. else
  1792. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1793. end;
  1794. end;
  1795. localsymtable,
  1796. stt_exceptsymtable :
  1797. begin
  1798. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1799. end;
  1800. staticsymtable :
  1801. begin
  1802. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1803. if not((target_info.system=system_powerpc_darwin) and
  1804. (cs_create_pic in aktmoduleswitches)) and
  1805. not(vo_is_dll_var in varoptions) {$ifndef segment_threadvars} and
  1806. not(vo_is_thread_var in varoptions) {$endif} then
  1807. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1808. end;
  1809. else
  1810. internalerror(200410103);
  1811. end;
  1812. end;
  1813. end;
  1814. if cs_asm_source in aktglobalswitches then
  1815. begin
  1816. case localloc.loc of
  1817. LOC_REGISTER,
  1818. LOC_CREGISTER :
  1819. begin
  1820. if (cs_no_regalloc in aktglobalswitches) then
  1821. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register '+
  1822. std_regname(localloc.register))))
  1823. else
  1824. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register')));
  1825. end;
  1826. LOC_REFERENCE :
  1827. begin
  1828. if not assigned(localloc.reference.symbol) then
  1829. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1830. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1831. end;
  1832. end;
  1833. end;
  1834. end;
  1835. end;
  1836. sym:=tsym(sym.indexnext);
  1837. end;
  1838. end;
  1839. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1840. var
  1841. sym : tsym;
  1842. begin
  1843. sym:=tsym(st.symindex.first);
  1844. while assigned(sym) do
  1845. begin
  1846. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1847. begin
  1848. with tabstractnormalvarsym(sym) do
  1849. begin
  1850. { Note: We need to keep the data available in memory
  1851. for the sub procedures that can access local data
  1852. in the parent procedures }
  1853. case localloc.loc of
  1854. LOC_CREGISTER :
  1855. {$ifndef cpu64bit}
  1856. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1857. begin
  1858. cg.a_reg_sync(list,localloc.register64.reglo);
  1859. cg.a_reg_sync(list,localloc.register64.reghi);
  1860. end
  1861. else
  1862. {$endif cpu64bit}
  1863. cg.a_reg_sync(list,localloc.register);
  1864. LOC_REFERENCE :
  1865. begin
  1866. case st.symtabletype of
  1867. localsymtable,
  1868. parasymtable,
  1869. stt_exceptsymtable :
  1870. tg.Ungetlocal(list,localloc.reference);
  1871. end;
  1872. end;
  1873. end;
  1874. end;
  1875. end;
  1876. sym:=tsym(sym.indexnext);
  1877. end;
  1878. end;
  1879. { persistent rtti generation }
  1880. procedure generate_rtti(p:Ttypesym);
  1881. var
  1882. rsym : trttisym;
  1883. def : tstoreddef;
  1884. begin
  1885. { rtti can only be generated for classes that are always typesyms }
  1886. def:=tstoreddef(ttypesym(p).restype.def);
  1887. { there is an error, skip rtti info }
  1888. if (def.deftype=errordef) or (Errorcount>0) then
  1889. exit;
  1890. { only create rtti once for each definition }
  1891. if not(df_has_rttitable in def.defoptions) then
  1892. begin
  1893. { definition should be in the same symtable as the symbol }
  1894. if p.owner<>def.owner then
  1895. internalerror(200108262);
  1896. { create rttisym }
  1897. rsym:=trttisym.create(p.name,fullrtti);
  1898. p.owner.insert(rsym);
  1899. { register rttisym in definition }
  1900. include(def.defoptions,df_has_rttitable);
  1901. def.rttitablesym:=rsym;
  1902. { write rtti data }
  1903. def.write_child_rtti_data(fullrtti);
  1904. maybe_new_object_file(asmlist[al_rtti]);
  1905. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1906. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1907. def.write_rtti_data(fullrtti);
  1908. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1909. end;
  1910. end;
  1911. { persistent init table generation }
  1912. procedure generate_inittable(p:tsym);
  1913. var
  1914. rsym : trttisym;
  1915. def : tstoreddef;
  1916. begin
  1917. { anonymous types are also allowed for records that can be varsym }
  1918. case p.typ of
  1919. typesym :
  1920. def:=tstoreddef(ttypesym(p).restype.def);
  1921. globalvarsym,
  1922. localvarsym,
  1923. paravarsym :
  1924. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  1925. else
  1926. internalerror(200108263);
  1927. end;
  1928. { only create inittable once for each definition }
  1929. if not(df_has_inittable in def.defoptions) then
  1930. begin
  1931. { definition should be in the same symtable as the symbol }
  1932. if p.owner<>def.owner then
  1933. internalerror(200108264);
  1934. { create rttisym }
  1935. rsym:=trttisym.create(p.name,initrtti);
  1936. p.owner.insert(rsym);
  1937. { register rttisym in definition }
  1938. include(def.defoptions,df_has_inittable);
  1939. def.inittablesym:=rsym;
  1940. { write inittable data }
  1941. def.write_child_rtti_data(initrtti);
  1942. maybe_new_object_file(asmlist[al_rtti]);
  1943. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1944. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1945. def.write_rtti_data(initrtti);
  1946. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1947. end;
  1948. end;
  1949. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  1950. var
  1951. i,j,
  1952. proccount : longint;
  1953. tmps : string;
  1954. begin
  1955. for i:=1 to _class.implementedinterfaces.count do
  1956. begin
  1957. { only if implemented by this class }
  1958. if _class.implementedinterfaces.implindex(i)=i then
  1959. begin
  1960. proccount:=_class.implementedinterfaces.implproccount(i);
  1961. for j:=1 to proccount do
  1962. begin
  1963. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  1964. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  1965. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  1966. { create wrapper code }
  1967. new_section(list,sec_code,lower(tmps),0);
  1968. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  1969. end;
  1970. end;
  1971. end;
  1972. end;
  1973. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  1974. var
  1975. def : tstoreddef;
  1976. begin
  1977. def:=tstoreddef(st.defindex.first);
  1978. while assigned(def) do
  1979. begin
  1980. if is_class(def) then
  1981. gen_intf_wrapper(list,tobjectdef(def));
  1982. def:=tstoreddef(def.indexnext);
  1983. end;
  1984. end;
  1985. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  1986. var
  1987. href : treference;
  1988. begin
  1989. if is_object(objdef) then
  1990. begin
  1991. case selfloc.loc of
  1992. LOC_CREFERENCE,
  1993. LOC_REFERENCE:
  1994. begin
  1995. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  1996. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  1997. end;
  1998. else
  1999. internalerror(200305056);
  2000. end;
  2001. end
  2002. else
  2003. begin
  2004. case selfloc.loc of
  2005. LOC_REGISTER:
  2006. begin
  2007. {$ifdef cpu_uses_separate_address_registers}
  2008. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2009. begin
  2010. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2011. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2012. end
  2013. else
  2014. {$endif cpu_uses_separate_address_registers}
  2015. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2016. end;
  2017. LOC_CREGISTER,
  2018. LOC_CREFERENCE,
  2019. LOC_REFERENCE:
  2020. begin
  2021. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2022. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2023. end;
  2024. else
  2025. internalerror(200305057);
  2026. end;
  2027. end;
  2028. vmtreg:=cg.getaddressregister(list);
  2029. cg.g_maybe_testself(list,href.base);
  2030. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2031. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2032. end;
  2033. end.