ncgutil.pas 87 KB

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