ncgutil.pas 86 KB

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