ncgutil.pas 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Helper routines for all code generators
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,cpuinfo,
  23. globtype,
  24. cpubase,cgbase,
  25. aasmbase,aasmtai,aasmcpu,
  26. symconst,symbase,symdef,symsym,symtype,symtable,
  27. {$ifndef cpu64bit}
  28. cg64f32
  29. {$endif cpu64bit}
  30. ;
  31. type
  32. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  33. procedure firstcomplex(p : tbinarynode);
  34. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  35. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  36. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  37. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  38. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  39. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  40. procedure gen_proc_symbol(list:Taasmoutput);
  41. procedure gen_proc_symbol_end(list:Taasmoutput);
  42. procedure gen_stackalloc_code(list:Taasmoutput);
  43. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  44. procedure gen_save_used_regs(list:TAAsmoutput);
  45. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  46. procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
  47. procedure gen_finalize_code(list:TAAsmoutput;inlined:boolean);
  48. procedure gen_entry_code(list:TAAsmoutput);
  49. procedure gen_exit_code(list:TAAsmoutput);
  50. procedure gen_load_para_value(list:TAAsmoutput);
  51. procedure gen_load_return_value(list:TAAsmoutput; var uses_acc,uses_acchi,uses_fpu : boolean);
  52. (*
  53. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  54. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  55. *)
  56. {#
  57. Allocate the buffers for exception management and setjmp environment.
  58. Return a pointer to these buffers, send them to the utility routine
  59. so they are registered, and then call setjmp.
  60. Then compare the result of setjmp with 0, and if not equal
  61. to zero, then jump to exceptlabel.
  62. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  63. It is to note that this routine may be called *after* the stackframe of a
  64. routine has been called, therefore on machines where the stack cannot
  65. be modified, all temps should be allocated on the heap instead of the
  66. stack.
  67. }
  68. procedure new_exception(list:TAAsmoutput;const jmpbuf,envbuf, href : treference;
  69. a : aword; exceptlabel : tasmlabel);
  70. procedure free_exception(list:TAAsmoutput;const jmpbuf, envbuf, href : treference;
  71. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  72. procedure insertconstdata(sym : ttypedconstsym);
  73. procedure insertbssdata(sym : tvarsym);
  74. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  75. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  76. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  77. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  78. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  79. implementation
  80. uses
  81. {$ifdef Delphi}
  82. Sysutils,
  83. {$else}
  84. strings,
  85. {$endif}
  86. cutils,cclasses,
  87. globals,systems,verbose,
  88. defutil,
  89. procinfo,paramgr,fmodule,
  90. regvars,
  91. {$ifdef GDB}
  92. gdb,
  93. {$endif GDB}
  94. ncon,
  95. tgobj,cgobj;
  96. const
  97. { Please leave this here, this module should NOT use
  98. exprasmlist, the lists are always passed as arguments.
  99. Declaring it as string here results in an error when compiling (PFV) }
  100. exprasmlist = 'error';
  101. {*****************************************************************************
  102. Misc Helpers
  103. *****************************************************************************}
  104. { DO NOT RELY on the fact that the tnode is not yet swaped
  105. because of inlining code PM }
  106. procedure firstcomplex(p : tbinarynode);
  107. var
  108. hp : tnode;
  109. begin
  110. { always calculate boolean AND and OR from left to right }
  111. if (p.nodetype in [orn,andn]) and
  112. is_boolean(p.left.resulttype.def) then
  113. begin
  114. if nf_swaped in p.flags then
  115. internalerror(234234);
  116. end
  117. else
  118. if (
  119. (p.location.loc=LOC_FPUREGISTER) and
  120. (p.right.registersfpu > p.left.registersfpu)
  121. ) or
  122. (
  123. (
  124. (
  125. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  126. (p.location.loc<>LOC_FPUREGISTER)
  127. ) and
  128. (p.left.registers32<p.right.registers32)
  129. )
  130. ) then
  131. begin
  132. hp:=p.left;
  133. p.left:=p.right;
  134. p.right:=hp;
  135. if nf_swaped in p.flags then
  136. exclude(p.flags,nf_swaped)
  137. else
  138. include(p.flags,nf_swaped);
  139. end;
  140. end;
  141. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  142. {
  143. produces jumps to true respectively false labels using boolean expressions
  144. depending on whether the loading of regvars is currently being
  145. synchronized manually (such as in an if-node) or automatically (most of
  146. the other cases where this procedure is called), loadregvars can be
  147. "lr_load_regvars" or "lr_dont_load_regvars"
  148. }
  149. var
  150. opsize : tcgsize;
  151. storepos : tfileposinfo;
  152. begin
  153. if nf_error in p.flags then
  154. exit;
  155. storepos:=aktfilepos;
  156. aktfilepos:=p.fileinfo;
  157. if is_boolean(p.resulttype.def) then
  158. begin
  159. {$ifdef OLDREGVARS}
  160. if loadregvars = lr_load_regvars then
  161. load_all_regvars(list);
  162. {$endif OLDREGVARS}
  163. if is_constboolnode(p) then
  164. begin
  165. if tordconstnode(p).value<>0 then
  166. cg.a_jmp_always(list,truelabel)
  167. else
  168. cg.a_jmp_always(list,falselabel)
  169. end
  170. else
  171. begin
  172. opsize:=def_cgsize(p.resulttype.def);
  173. case p.location.loc of
  174. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  175. begin
  176. {$ifdef OLDREGVARS}
  177. if (p.location.loc = LOC_CREGISTER) then
  178. load_regvar_reg(list,p.location.register);
  179. {$endif OLDREGVARS}
  180. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  181. 0,p.location,truelabel);
  182. { !!! should happen right after cmp (JM) }
  183. location_release(list,p.location);
  184. cg.a_jmp_always(list,falselabel);
  185. end;
  186. LOC_JUMP:
  187. ;
  188. {$ifdef cpuflags}
  189. LOC_FLAGS :
  190. begin
  191. cg.a_jmp_flags(list,p.location.resflags,
  192. truelabel);
  193. cg.a_jmp_always(list,falselabel);
  194. end;
  195. {$endif cpuflags}
  196. else
  197. begin
  198. printnode(output,p);
  199. internalerror(200308241);
  200. end;
  201. end;
  202. end;
  203. end
  204. else
  205. internalerror(200112305);
  206. aktfilepos:=storepos;
  207. end;
  208. (*
  209. This code needs fixing. It is not safe to use rgint; on the m68000 it
  210. would be rgaddr.
  211. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  212. begin
  213. case t.loc of
  214. LOC_REGISTER:
  215. begin
  216. { can't be a regvar, since it would be LOC_CREGISTER then }
  217. exclude(regs,getsupreg(t.register));
  218. if t.registerhigh<>NR_NO then
  219. exclude(regs,getsupreg(t.registerhigh));
  220. end;
  221. LOC_CREFERENCE,LOC_REFERENCE:
  222. begin
  223. if not(cs_regvars in aktglobalswitches) or
  224. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  225. exclude(regs,getsupreg(t.reference.base));
  226. if not(cs_regvars in aktglobalswitches) or
  227. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  228. exclude(regs,getsupreg(t.reference.index));
  229. end;
  230. end;
  231. end;
  232. *)
  233. {*****************************************************************************
  234. EXCEPTION MANAGEMENT
  235. *****************************************************************************}
  236. procedure new_exception(list:TAAsmoutput;const jmpbuf,envbuf, href : treference;
  237. a : aword; exceptlabel : tasmlabel);
  238. var
  239. paraloc1,paraloc2,paraloc3 : tparalocation;
  240. begin
  241. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  242. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  243. paraloc3:=paramanager.getintparaloc(pocall_default,3);
  244. paramanager.allocparaloc(list,paraloc3);
  245. cg.a_paramaddr_ref(list,envbuf,paraloc3);
  246. paramanager.allocparaloc(list,paraloc2);
  247. cg.a_paramaddr_ref(list,jmpbuf,paraloc2);
  248. { push type of exceptionframe }
  249. paramanager.allocparaloc(list,paraloc1);
  250. cg.a_param_const(list,OS_S32,1,paraloc1);
  251. paramanager.freeparaloc(list,paraloc3);
  252. paramanager.freeparaloc(list,paraloc2);
  253. paramanager.freeparaloc(list,paraloc1);
  254. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  255. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  256. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  257. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  258. paramanager.allocparaloc(list,paraloc1);
  259. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  260. paramanager.freeparaloc(list,paraloc1);
  261. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  262. cg.a_call_name(list,'FPC_SETJMP');
  263. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  264. cg.g_exception_reason_save(list, href);
  265. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,NR_FUNCTION_RESULT_REG,exceptlabel);
  266. end;
  267. procedure free_exception(list:TAAsmoutput;const jmpbuf, envbuf, href : treference;
  268. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  269. begin
  270. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  271. cg.a_call_name(list,'FPC_POPADDRSTACK');
  272. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  273. if not onlyfree then
  274. begin
  275. cg.g_exception_reason_load(list, href);
  276. cg.a_cmp_const_reg_label(list,OS_S32,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  277. end;
  278. end;
  279. {*****************************************************************************
  280. TLocation
  281. *****************************************************************************}
  282. {$ifndef cpu64bit}
  283. { 32-bit version }
  284. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  285. var
  286. hregister,
  287. hregisterhi : tregister;
  288. hreg64 : tregister64;
  289. hl : tasmlabel;
  290. oldloc : tlocation;
  291. begin
  292. oldloc:=l;
  293. if dst_size=OS_NO then
  294. internalerror(200309144);
  295. { handle transformations to 64bit separate }
  296. if dst_size in [OS_64,OS_S64] then
  297. begin
  298. if not (l.size in [OS_64,OS_S64]) then
  299. begin
  300. { load a smaller size to OS_64 }
  301. if l.loc=LOC_REGISTER then
  302. begin
  303. hregister:=cg.makeregsize(l.registerlow,OS_32);
  304. cg.a_load_reg_reg(list,l.size,OS_32,l.registerlow,hregister);
  305. end
  306. else
  307. begin
  308. location_release(list,l);
  309. hregister:=cg.getintregister(list,OS_INT);
  310. end;
  311. { load value in low register }
  312. case l.loc of
  313. LOC_FLAGS :
  314. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  315. LOC_JUMP :
  316. begin
  317. cg.a_label(list,truelabel);
  318. cg.a_load_const_reg(list,OS_INT,1,hregister);
  319. objectlibrary.getlabel(hl);
  320. cg.a_jmp_always(list,hl);
  321. cg.a_label(list,falselabel);
  322. cg.a_load_const_reg(list,OS_INT,0,hregister);
  323. cg.a_label(list,hl);
  324. end;
  325. else
  326. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  327. end;
  328. { reset hi part, take care of the signed bit of the current value }
  329. hregisterhi:=cg.getintregister(list,OS_INT);
  330. if (dst_size=OS_S64) and
  331. (l.size in [OS_S8,OS_S16,OS_S32]) then
  332. begin
  333. if l.loc=LOC_CONSTANT then
  334. begin
  335. if (longint(l.value)<0) then
  336. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  337. else
  338. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  339. end
  340. else
  341. begin
  342. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  343. hregisterhi);
  344. end;
  345. end
  346. else
  347. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  348. location_reset(l,LOC_REGISTER,dst_size);
  349. l.registerlow:=hregister;
  350. l.registerhigh:=hregisterhi;
  351. end
  352. else
  353. begin
  354. { 64bit to 64bit }
  355. if (l.loc=LOC_REGISTER) or
  356. ((l.loc=LOC_CREGISTER) and maybeconst) then
  357. begin
  358. hregister:=l.registerlow;
  359. hregisterhi:=l.registerhigh;
  360. end
  361. else
  362. begin
  363. hregister:=cg.getintregister(list,OS_INT);
  364. hregisterhi:=cg.getintregister(list,OS_INT);
  365. location_release(list,l);
  366. end;
  367. hreg64.reglo:=hregister;
  368. hreg64.reghi:=hregisterhi;
  369. { load value in new register }
  370. cg64.a_load64_loc_reg(list,l,hreg64,false);
  371. location_reset(l,LOC_REGISTER,dst_size);
  372. l.registerlow:=hregister;
  373. l.registerhigh:=hregisterhi;
  374. end;
  375. end
  376. else
  377. begin
  378. {Do not bother to recycle the existing register. The register
  379. allocator eliminates unnecessary moves, so it's not needed
  380. and trying to recycle registers can cause problems because
  381. the registers changes size and may need aditional constraints.}
  382. location_release(list,l);
  383. hregister:=cg.getintregister(list,dst_size);
  384. { load value in new register }
  385. case l.loc of
  386. LOC_FLAGS :
  387. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  388. LOC_JUMP :
  389. begin
  390. cg.a_label(list,truelabel);
  391. cg.a_load_const_reg(list,dst_size,1,hregister);
  392. objectlibrary.getlabel(hl);
  393. cg.a_jmp_always(list,hl);
  394. cg.a_label(list,falselabel);
  395. cg.a_load_const_reg(list,dst_size,0,hregister);
  396. cg.a_label(list,hl);
  397. end;
  398. else
  399. begin
  400. { load_loc_reg can only handle size >= l.size, when the
  401. new size is smaller then we need to adjust the size
  402. of the orignal and maybe recalculate l.register for i386 }
  403. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  404. begin
  405. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  406. l.register:=cg.makeregsize(l.register,dst_size);
  407. { for big endian systems, the reference's offset must }
  408. { be increased in this case, since they have the }
  409. { MSB first in memory and e.g. byte(word_var) should }
  410. { return the second byte in this case (JM) }
  411. if (target_info.endian = ENDIAN_BIG) and
  412. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  413. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  414. {$ifdef i386}
  415. l.size:=dst_size;
  416. {$endif i386}
  417. end;
  418. cg.a_load_loc_reg(list,dst_size,l,hregister);
  419. {$ifndef i386}
  420. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  421. l.size:=dst_size;
  422. {$endif not i386}
  423. end;
  424. end;
  425. if (l.loc <> LOC_CREGISTER) or
  426. not maybeconst then
  427. location_reset(l,LOC_REGISTER,dst_size)
  428. else
  429. location_reset(l,LOC_CREGISTER,dst_size);
  430. l.register:=hregister;
  431. end;
  432. { Release temp when it was a reference }
  433. if oldloc.loc=LOC_REFERENCE then
  434. location_freetemp(list,oldloc);
  435. end;
  436. {$else cpu64bit}
  437. { 64-bit version }
  438. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  439. var
  440. hregister : tregister;
  441. hl : tasmlabel;
  442. oldloc : tlocation;
  443. begin
  444. oldloc:=l;
  445. if dst_size=OS_NO then
  446. internalerror(200309144);
  447. { handle transformations to 64bit separate }
  448. if dst_size in [OS_64,OS_S64] then
  449. begin
  450. { load a smaller size to OS_64 }
  451. if l.loc=LOC_REGISTER then
  452. hregister:=cg.makeregsize(l.register,OS_INT)
  453. else
  454. begin
  455. location_release(list,l);
  456. hregister:=cg.getregisterint(list,OS_INT);
  457. end;
  458. { load value in low register }
  459. case l.loc of
  460. {$ifdef cpuflags}
  461. LOC_FLAGS :
  462. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  463. {$endif cpuflags}
  464. LOC_JUMP :
  465. begin
  466. cg.a_label(list,truelabel);
  467. cg.a_load_const_reg(list,OS_INT,1,hregister);
  468. objectlibrary.getlabel(hl);
  469. cg.a_jmp_always(list,hl);
  470. cg.a_label(list,falselabel);
  471. cg.a_load_const_reg(list,OS_INT,0,hregister);
  472. cg.a_label(list,hl);
  473. end;
  474. else
  475. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  476. end;
  477. location_reset(l,LOC_REGISTER,dst_size);
  478. l.register:=hregister;
  479. end
  480. else
  481. begin
  482. { transformations to 32bit or smaller }
  483. if l.loc=LOC_REGISTER then
  484. begin
  485. hregister:=l.register;
  486. end
  487. else
  488. begin
  489. { get new register }
  490. if (l.loc=LOC_CREGISTER) and
  491. maybeconst and
  492. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  493. hregister:=l.register
  494. else
  495. begin
  496. location_release(list,l);
  497. hregister:=cg.getregisterint(list,OS_INT);
  498. end;
  499. end;
  500. hregister:=cg.makeregsize(hregister,dst_size);
  501. { load value in new register }
  502. case l.loc of
  503. {$ifdef cpuflags}
  504. LOC_FLAGS :
  505. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  506. {$endif cpuflags}
  507. LOC_JUMP :
  508. begin
  509. cg.a_label(list,truelabel);
  510. cg.a_load_const_reg(list,dst_size,1,hregister);
  511. objectlibrary.getlabel(hl);
  512. cg.a_jmp_always(list,hl);
  513. cg.a_label(list,falselabel);
  514. cg.a_load_const_reg(list,dst_size,0,hregister);
  515. cg.a_label(list,hl);
  516. end;
  517. else
  518. begin
  519. { load_loc_reg can only handle size >= l.size, when the
  520. new size is smaller then we need to adjust the size
  521. of the orignal and maybe recalculate l.register for i386 }
  522. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  523. begin
  524. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  525. l.register:=cg.makeregsize(l.register,dst_size);
  526. { for big endian systems, the reference's offset must }
  527. { be increased in this case, since they have the }
  528. { MSB first in memory and e.g. byte(word_var) should }
  529. { return the second byte in this case (JM) }
  530. if (target_info.endian = ENDIAN_BIG) and
  531. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  532. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  533. {$ifdef i386}
  534. l.size:=dst_size;
  535. {$endif i386}
  536. end;
  537. cg.a_load_loc_reg(list,dst_size,l,hregister);
  538. {$ifndef i386}
  539. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  540. l.size:=dst_size;
  541. {$endif not i386}
  542. end;
  543. end;
  544. location_reset(l,LOC_REGISTER,dst_size);
  545. l.register:=hregister;
  546. end;
  547. { Release temp when it was a reference }
  548. if oldloc.loc=LOC_REFERENCE then
  549. location_freetemp(list,oldloc);
  550. end;
  551. {$endif cpu64bit}
  552. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  553. var
  554. reg : tregister;
  555. begin
  556. if (l.loc<>LOC_FPUREGISTER) and
  557. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  558. begin
  559. reg:=cg.getfpuregister(list,l.size);
  560. cg.a_loadfpu_loc_reg(list,l,reg);
  561. location_freetemp(list,l);
  562. location_release(list,l);
  563. location_reset(l,LOC_FPUREGISTER,l.size);
  564. l.register:=reg;
  565. end;
  566. end;
  567. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  568. var
  569. r : treference;
  570. begin
  571. case l.loc of
  572. LOC_FPUREGISTER,
  573. LOC_CFPUREGISTER :
  574. begin
  575. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  576. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  577. location_release(list,l);
  578. location_reset(l,LOC_REFERENCE,l.size);
  579. l.reference:=r;
  580. end;
  581. LOC_CONSTANT,
  582. LOC_REGISTER,
  583. LOC_CREGISTER :
  584. begin
  585. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  586. if l.size in [OS_64,OS_S64] then
  587. begin
  588. cg64.a_load64_loc_ref(list,l,r);
  589. location_release(list,l);
  590. end
  591. else
  592. begin
  593. location_release(list,l);
  594. cg.a_load_loc_ref(list,l.size,l,r);
  595. end;
  596. location_reset(l,LOC_REFERENCE,l.size);
  597. l.reference:=r;
  598. end;
  599. LOC_CREFERENCE,
  600. LOC_REFERENCE : ;
  601. else
  602. internalerror(200203219);
  603. end;
  604. end;
  605. {*****************************************************************************
  606. Maybe_Save
  607. *****************************************************************************}
  608. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  609. begin
  610. {$ifdef i386}
  611. if (needed>=maxfpuregs) and
  612. (l.loc = LOC_FPUREGISTER) then
  613. begin
  614. location_force_mem(list,l);
  615. maybe_pushfpu:=true;
  616. end
  617. else
  618. maybe_pushfpu:=false;
  619. {$endif i386}
  620. end;
  621. {****************************************************************************
  622. Init/Finalize Code
  623. ****************************************************************************}
  624. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  625. var
  626. href1,href2 : treference;
  627. list:TAAsmoutput;
  628. hsym : tvarsym;
  629. l : longint;
  630. loadref : boolean;
  631. localcopyloc : tparalocation;
  632. begin
  633. list:=taasmoutput(arg);
  634. if (tsym(p).typ=varsym) and
  635. (tvarsym(p).varspez=vs_value) and
  636. (paramanager.push_addr_param(tvarsym(p).varspez,tvarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  637. begin
  638. loadref:=true;
  639. case tvarsym(p).localloc.loc of
  640. LOC_REGISTER :
  641. begin
  642. reference_reset_base(href1,tvarsym(p).localloc.register,0);
  643. loadref:=false;
  644. end;
  645. LOC_REFERENCE :
  646. reference_reset_base(href1,tvarsym(p).localloc.reference.index,
  647. tvarsym(p).localloc.reference.offset);
  648. else
  649. internalerror(200309181);
  650. end;
  651. if is_open_array(tvarsym(p).vartype.def) or
  652. is_array_of_const(tvarsym(p).vartype.def) then
  653. begin
  654. { cdecl functions don't have a high pointer so it is not possible to generate
  655. a local copy }
  656. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  657. begin
  658. hsym:=tvarsym(tsym(p).owner.search('high'+p.name));
  659. if not assigned(hsym) then
  660. internalerror(200306061);
  661. case hsym.localloc.loc of
  662. LOC_REFERENCE :
  663. begin
  664. { this code is only executed before the code for the body and the entry/exit code is generated
  665. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  666. }
  667. include(current_procinfo.flags,pi_do_call);
  668. reference_reset_base(href2,hsym.localloc.reference.index,hsym.localloc.reference.offset);
  669. cg.g_copyvaluepara_openarray(list,href1,href2,tarraydef(tvarsym(p).vartype.def).elesize)
  670. end
  671. else
  672. internalerror(200309182);
  673. end;
  674. end;
  675. end
  676. else
  677. begin
  678. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  679. internalerror(200309183);
  680. { Allocate space for the local copy }
  681. l:=tvarsym(p).getvaluesize;
  682. localcopyloc.loc:=LOC_REFERENCE;
  683. localcopyloc.size:=int_cgsize(l);
  684. tg.GetLocal(list,l,localcopyloc.reference);
  685. { Copy data }
  686. reference_reset_base(href2,localcopyloc.reference.index,localcopyloc.reference.offset);
  687. if is_shortstring(tvarsym(p).vartype.def) then
  688. begin
  689. { this code is only executed before the code for the body and the entry/exit code is generated
  690. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  691. }
  692. include(current_procinfo.flags,pi_do_call);
  693. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,loadref)
  694. end
  695. else
  696. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,loadref);
  697. { update localloc of varsym }
  698. tg.Ungetlocal(list,tvarsym(p).localloc.reference);
  699. tvarsym(p).localloc:=localcopyloc;
  700. end;
  701. end;
  702. end;
  703. { generates the code for initialisation of local data }
  704. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  705. var
  706. href : treference;
  707. list:TAAsmoutput;
  708. begin
  709. list:=taasmoutput(arg);
  710. if (tsym(p).typ=varsym) and
  711. (tvarsym(p).refs>0) and
  712. assigned(tvarsym(p).vartype.def) and
  713. not(is_class(tvarsym(p).vartype.def)) and
  714. tvarsym(p).vartype.def.needs_inittable then
  715. begin
  716. if (cs_implicit_exceptions in aktmoduleswitches) then
  717. include(current_procinfo.flags,pi_needs_implicit_finally);
  718. if tvarsym(p).owner.symtabletype=localsymtable then
  719. begin
  720. case tvarsym(p).localloc.loc of
  721. LOC_REFERENCE :
  722. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  723. else
  724. internalerror(2003091810);
  725. end;
  726. end
  727. else
  728. reference_reset_symbol(href,objectlibrary.newasmsymboldata(tvarsym(p).mangledname),0);
  729. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  730. end;
  731. end;
  732. { generates the code for finalisation of local data }
  733. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  734. var
  735. href : treference;
  736. list:TAAsmoutput;
  737. begin
  738. list:=taasmoutput(arg);
  739. case tsym(p).typ of
  740. varsym :
  741. begin
  742. if (tvarsym(p).refs>0) and
  743. not(vo_is_funcret in tvarsym(p).varoptions) and
  744. assigned(tvarsym(p).vartype.def) and
  745. not(is_class(tvarsym(p).vartype.def)) and
  746. tvarsym(p).vartype.def.needs_inittable then
  747. begin
  748. if tvarsym(p).owner.symtabletype=localsymtable then
  749. begin
  750. case tvarsym(p).localloc.loc of
  751. LOC_REFERENCE :
  752. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  753. else
  754. internalerror(2003091811);
  755. end;
  756. end
  757. else
  758. reference_reset_symbol(href,objectlibrary.newasmsymboldata(tvarsym(p).mangledname),0);
  759. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  760. end;
  761. end;
  762. typedconstsym :
  763. begin
  764. if ttypedconstsym(p).is_writable and
  765. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  766. begin
  767. reference_reset_symbol(href,objectlibrary.newasmsymboldata(ttypedconstsym(p).mangledname),0);
  768. cg.g_finalize(list,ttypedconstsym(p).typedconsttype.def,href,false);
  769. end;
  770. end;
  771. end;
  772. end;
  773. { generates the code for incrementing the reference count of parameters and
  774. initialize out parameters }
  775. procedure init_paras(p : tnamedindexitem;arg:pointer);
  776. var
  777. href : treference;
  778. tmpreg : tregister;
  779. list:TAAsmoutput;
  780. begin
  781. list:=taasmoutput(arg);
  782. if (tsym(p).typ=varsym) and
  783. not is_class(tvarsym(p).vartype.def) and
  784. tvarsym(p).vartype.def.needs_inittable then
  785. begin
  786. case tvarsym(p).varspez of
  787. vs_value :
  788. begin
  789. if (cs_implicit_exceptions in aktmoduleswitches) then
  790. include(current_procinfo.flags,pi_needs_implicit_finally);
  791. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  792. internalerror(200309187);
  793. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  794. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href,is_open_array(tvarsym(p).vartype.def));
  795. end;
  796. vs_out :
  797. begin
  798. case tvarsym(p).localloc.loc of
  799. LOC_REFERENCE :
  800. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  801. else
  802. internalerror(2003091810);
  803. end;
  804. tmpreg:=cg.getaddressregister(list);
  805. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,tmpreg);
  806. reference_reset_base(href,tmpreg,0);
  807. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  808. cg.ungetregister(list,tmpreg);
  809. end;
  810. end;
  811. end;
  812. end;
  813. { generates the code for decrementing the reference count of parameters }
  814. procedure final_paras(p : tnamedindexitem;arg:pointer);
  815. var
  816. href : treference;
  817. list:TAAsmoutput;
  818. begin
  819. list:=taasmoutput(arg);
  820. if (tsym(p).typ=varsym) and
  821. not is_class(tvarsym(p).vartype.def) and
  822. tvarsym(p).vartype.def.needs_inittable then
  823. begin
  824. if (tvarsym(p).varspez=vs_value) then
  825. begin
  826. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  827. internalerror(200309188);
  828. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  829. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href,is_open_array(tvarsym(p).vartype.def));
  830. end;
  831. end
  832. else if (tsym(p).typ=varsym) and
  833. (tvarsym(p).varspez=vs_value) and
  834. (is_open_array(tvarsym(p).vartype.def) or
  835. is_array_of_const(tvarsym(p).vartype.def)) then
  836. begin
  837. { cdecl functions don't have a high pointer so it is not possible to generate
  838. a local copy }
  839. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  840. begin
  841. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  842. cg.g_releasevaluepara_openarray(list,href);
  843. end;
  844. end;
  845. end;
  846. { Initialize temp ansi/widestrings,interfaces }
  847. procedure inittempvariables(list:taasmoutput);
  848. var
  849. hp : ptemprecord;
  850. href : treference;
  851. begin
  852. hp:=tg.templist;
  853. while assigned(hp) do
  854. begin
  855. if assigned(hp^.def) and
  856. hp^.def.needs_inittable then
  857. begin
  858. if (cs_implicit_exceptions in aktmoduleswitches) then
  859. include(current_procinfo.flags,pi_needs_implicit_finally);
  860. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  861. cg.g_initialize(list,hp^.def,href,false);
  862. end;
  863. hp:=hp^.next;
  864. end;
  865. end;
  866. procedure finalizetempvariables(list:taasmoutput);
  867. var
  868. hp : ptemprecord;
  869. href : treference;
  870. paraloc1 : tparalocation;
  871. begin
  872. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  873. hp:=tg.templist;
  874. while assigned(hp) do
  875. begin
  876. if assigned(hp^.def) and
  877. hp^.def.needs_inittable then
  878. begin
  879. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  880. cg.g_finalize(list,hp^.def,href,false);
  881. end;
  882. hp:=hp^.next;
  883. end;
  884. end;
  885. procedure gen_load_return_value(list:TAAsmoutput; var uses_acc,uses_acchi,uses_fpu : boolean);
  886. var
  887. ressym : tvarsym;
  888. resloc : tlocation;
  889. href : treference;
  890. hreg : tregister;
  891. begin
  892. { Is the loading needed? }
  893. if is_void(current_procinfo.procdef.rettype.def) or
  894. (
  895. (po_assembler in current_procinfo.procdef.procoptions) and
  896. (not(assigned(current_procinfo.procdef.funcretsym)) or
  897. (tvarsym(current_procinfo.procdef.funcretsym).refs=0))
  898. ) then
  899. exit;
  900. { Constructors need to return self }
  901. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  902. begin
  903. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  904. { return the self pointer }
  905. ressym:=tvarsym(current_procinfo.procdef.parast.search('self'));
  906. if not assigned(ressym) then
  907. internalerror(200305058);
  908. case ressym.localloc.loc of
  909. LOC_REFERENCE :
  910. reference_reset_base(href,ressym.localloc.reference.index,ressym.localloc.reference.offset);
  911. else
  912. internalerror(2003091810);
  913. end;
  914. cg.ungetregister(list,NR_FUNCTION_RETURN_REG);
  915. // for the optimizer
  916. cg.a_reg_alloc(list,NR_FUNCTION_RETURN_REG);
  917. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_FUNCTION_RETURN_REG);
  918. uses_acc:=true;
  919. exit;
  920. end;
  921. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  922. if (ressym.refs>0) then
  923. begin
  924. case ressym.localloc.loc of
  925. LOC_FPUREGISTER,
  926. LOC_REGISTER :
  927. begin
  928. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  929. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  930. else
  931. if ressym.vartype.def.deftype = floatdef then
  932. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  933. else
  934. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  935. resloc.register:=ressym.localloc.register;
  936. end;
  937. LOC_REFERENCE :
  938. begin
  939. location_reset(resloc,LOC_REFERENCE,def_cgsize(current_procinfo.procdef.rettype.def));
  940. reference_reset_base(resloc.reference,ressym.localloc.reference.index,ressym.localloc.reference.offset);
  941. end;
  942. else
  943. internalerror(200309184);
  944. end;
  945. { Here, we return the function result. In most architectures, the value is
  946. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  947. function returns in a register and the caller receives it in an other one }
  948. case current_procinfo.procdef.rettype.def.deftype of
  949. orddef,
  950. enumdef :
  951. begin
  952. uses_acc:=true;
  953. {$ifndef cpu64bit}
  954. if resloc.size in [OS_64,OS_S64] then
  955. begin
  956. uses_acchi:=true;
  957. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  958. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  959. cg.ungetregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  960. cg.ungetregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  961. // for the optimizer
  962. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_LOW_REG);
  963. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_HIGH_REG);
  964. cg64.a_load64_loc_reg(list,resloc,joinreg64(NR_FUNCTION_RETURN64_LOW_REG,
  965. NR_FUNCTION_RETURN64_HIGH_REG),false);
  966. end
  967. else
  968. {$endif cpu64bit}
  969. begin
  970. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  971. hreg:=cg.makeregsize(NR_FUNCTION_RETURN_REG,resloc.size);
  972. cg.ungetregister(list,hreg);
  973. // for the optimizer
  974. cg.a_reg_alloc(list,NR_FUNCTION_RETURN_REG);
  975. cg.a_load_loc_reg(list,resloc.size,resloc,hreg);
  976. end;
  977. end;
  978. floatdef :
  979. begin
  980. uses_fpu := true;
  981. {$ifdef cpufpemu}
  982. if cs_fp_emulation in aktmoduleswitches then
  983. cg.a_loadfpu_loc_reg(list,resloc,NR_FUNCTION_RETURN_REG);
  984. else
  985. {$endif cpufpemu}
  986. cg.a_loadfpu_loc_reg(list,resloc,NR_FPU_RESULT_REG);
  987. end;
  988. else
  989. begin
  990. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  991. begin
  992. uses_acc:=true;
  993. {$ifndef cpu64bit}
  994. { Win32 can return records in EAX:EDX }
  995. if resloc.size in [OS_64,OS_S64] then
  996. begin
  997. uses_acchi:=true;
  998. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  999. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  1000. cg.ungetregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  1001. cg.ungetregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  1002. // for the optimizer
  1003. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_LOW_REG);
  1004. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_HIGH_REG);
  1005. cg64.a_load64_loc_reg(list,resloc,joinreg64(NR_FUNCTION_RETURN64_LOW_REG,
  1006. NR_FUNCTION_RETURN64_HIGH_REG),false);
  1007. end
  1008. else
  1009. {$endif cpu64bit}
  1010. begin
  1011. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  1012. hreg:=cg.makeregsize(NR_FUNCTION_RETURN_REG,resloc.size);
  1013. cg.ungetregister(list,hreg);
  1014. // for the optimizer
  1015. cg.a_reg_alloc(list,NR_FUNCTION_RETURN_REG);
  1016. cg.a_load_loc_reg(list,resloc.size,resloc,hreg);
  1017. end;
  1018. end
  1019. end;
  1020. end;
  1021. end;
  1022. end;
  1023. procedure gen_load_para_value(list:TAAsmoutput);
  1024. var
  1025. hp : tparaitem;
  1026. href : treference;
  1027. gotregvarparas : boolean;
  1028. begin
  1029. { Store register parameters in reference or in register variable }
  1030. if assigned(current_procinfo.procdef.parast) and
  1031. not (po_assembler in current_procinfo.procdef.procoptions) then
  1032. begin
  1033. { move register parameters which aren't regable into memory }
  1034. { we do this before init_paras because that one calls routines which may overwrite these }
  1035. { registers and it also expects the values to be in memory }
  1036. hp:=tparaitem(current_procinfo.procdef.para.first);
  1037. gotregvarparas := false;
  1038. while assigned(hp) do
  1039. begin
  1040. case tvarsym(hp.parasym).localloc.loc of
  1041. LOC_REGISTER :
  1042. begin
  1043. gotregvarparas := true;
  1044. { cg.a_load_param_reg will first allocate and then deallocate paraloc }
  1045. { register (if the parameter resides in a register) and then allocate }
  1046. { the regvar (which is currently not allocated) }
  1047. cg.a_loadany_param_reg(list,hp.paraloc[calleeside],tvarsym(hp.parasym).localloc.register,nil);
  1048. end;
  1049. LOC_FPUREGISTER:
  1050. begin
  1051. gotregvarparas := true;
  1052. { cg.a_load_param_reg will first allocate and then deallocate paraloc }
  1053. { register (if the parameter resides in a register) and then allocate }
  1054. { the regvar (which is currently not allocated) }
  1055. cg.a_loadany_param_reg(list,hp.paraloc[calleeside],tvarsym(hp.parasym).localloc.register,nil);
  1056. end;
  1057. LOC_REFERENCE :
  1058. begin
  1059. if hp.paraloc[calleeside].loc<>LOC_REFERENCE then
  1060. begin
  1061. if getsupreg(hp.paraloc[calleeside].register)<first_int_imreg then
  1062. begin
  1063. {$ifndef cpu64bit}
  1064. if (hp.paraloc[calleeside].size in [OS_S64,OS_64]) then
  1065. begin
  1066. cg.getexplicitregister(list,hp.paraloc[calleeside].registerlow);
  1067. cg.getexplicitregister(list,hp.paraloc[calleeside].registerhigh);
  1068. end
  1069. else
  1070. {$endif cpu64bit}
  1071. cg.getexplicitregister(list,hp.paraloc[calleeside].register);
  1072. end;
  1073. { Release parameter register }
  1074. {$ifndef cpu64bit}
  1075. if (hp.paraloc[calleeside].size in [OS_S64,OS_64]) then
  1076. begin
  1077. cg.ungetregister(list,hp.paraloc[calleeside].registerlow);
  1078. cg.ungetregister(list,hp.paraloc[calleeside].registerhigh);
  1079. end
  1080. else
  1081. {$endif cpu64bit}
  1082. cg.ungetregister(list,hp.paraloc[calleeside].register);
  1083. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  1084. cg.a_loadany_param_ref(list,hp.paraloc[calleeside],href,nil);
  1085. end;
  1086. end;
  1087. else
  1088. internalerror(200309185);
  1089. end;
  1090. hp:=tparaitem(hp.next);
  1091. end;
  1092. if gotregvarparas then
  1093. begin
  1094. { deallocate all register variables again }
  1095. hp:=tparaitem(current_procinfo.procdef.para.first);
  1096. while assigned(hp) do
  1097. begin
  1098. if (tvarsym(hp.parasym).localloc.loc=LOC_REGISTER) then
  1099. cg.ungetregister(list,tvarsym(hp.parasym).localloc.register);
  1100. hp:=tparaitem(hp.next);
  1101. end;
  1102. end;
  1103. end;
  1104. { generate copies of call by value parameters, must be done before
  1105. the initialization and body is parsed because the refcounts are
  1106. incremented using the local copies }
  1107. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1108. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1109. end;
  1110. procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
  1111. begin
  1112. { initialize local data like ansistrings }
  1113. case current_procinfo.procdef.proctypeoption of
  1114. potype_unitinit:
  1115. begin
  1116. { this is also used for initialization of variables in a
  1117. program which does not have a globalsymtable }
  1118. if assigned(current_module.globalsymtable) then
  1119. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1120. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1121. end;
  1122. { units have seperate code for initilization and finalization }
  1123. potype_unitfinalize: ;
  1124. { program init/final is generated in separate procedure }
  1125. potype_proginit: ;
  1126. else
  1127. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1128. end;
  1129. { initialisizes temp. ansi/wide string data }
  1130. inittempvariables(list);
  1131. { initialize ansi/widesstring para's }
  1132. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1133. {$ifdef OLDREGVARS}
  1134. load_regvars(list,nil);
  1135. {$endif OLDREGVARS}
  1136. end;
  1137. procedure gen_finalize_code(list:TAAsmoutput;inlined:boolean);
  1138. begin
  1139. {$ifdef OLDREGVARS}
  1140. cleanup_regvars(list);
  1141. {$endif OLDREGVARS}
  1142. { finalize temporary data }
  1143. finalizetempvariables(list);
  1144. { finalize local data like ansistrings}
  1145. case current_procinfo.procdef.proctypeoption of
  1146. potype_unitfinalize:
  1147. begin
  1148. { this is also used for initialization of variables in a
  1149. program which does not have a globalsymtable }
  1150. if assigned(current_module.globalsymtable) then
  1151. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1152. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1153. end;
  1154. { units/progs have separate code for initialization and finalization }
  1155. potype_unitinit: ;
  1156. { program init/final is generated in separate procedure }
  1157. potype_proginit: ;
  1158. else
  1159. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1160. end;
  1161. { finalize paras data }
  1162. if assigned(current_procinfo.procdef.parast) then
  1163. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1164. end;
  1165. procedure gen_entry_code(list:TAAsmoutput);
  1166. var
  1167. href : treference;
  1168. paraloc1,
  1169. paraloc2 : tparalocation;
  1170. begin
  1171. { the actual profile code can clobber some registers,
  1172. therefore if the context must be saved, do it before
  1173. the actual call to the profile code
  1174. }
  1175. if (cs_profile in aktmoduleswitches) and
  1176. not(po_assembler in current_procinfo.procdef.procoptions) then
  1177. begin
  1178. { non-win32 can call mcout even in main }
  1179. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1180. cg.g_profilecode(list)
  1181. else
  1182. { wdosx, and win32 should not call mcount before monstartup has been called }
  1183. if not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1184. cg.g_profilecode(list);
  1185. end;
  1186. { call startup helpers from main program }
  1187. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1188. begin
  1189. { initialize profiling for win32 }
  1190. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1191. (cs_profile in aktmoduleswitches) then
  1192. begin
  1193. reference_reset_symbol(href,objectlibrary.newasmsymboldata('etext'),0);
  1194. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1195. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1196. paramanager.allocparaloc(list,paraloc2);
  1197. cg.a_paramaddr_ref(list,href,paraloc2);
  1198. reference_reset_symbol(href,objectlibrary.newasmsymboldata('__image_base__'),0);
  1199. paramanager.allocparaloc(list,paraloc1);
  1200. cg.a_paramaddr_ref(list,href,paraloc1);
  1201. paramanager.freeparaloc(list,paraloc2);
  1202. paramanager.freeparaloc(list,paraloc1);
  1203. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1204. cg.a_call_name(list,'_monstartup');
  1205. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1206. end;
  1207. { initialize units }
  1208. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1209. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1210. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1211. end;
  1212. {$ifdef GDB}
  1213. if (cs_debuginfo in aktmoduleswitches) then
  1214. list.concat(Tai_force_line.Create);
  1215. {$endif GDB}
  1216. {$ifdef OLDREGVARS}
  1217. load_regvars(list,nil);
  1218. {$endif OLDREGVARS}
  1219. end;
  1220. procedure gen_exit_code(list:TAAsmoutput);
  1221. begin
  1222. { call __EXIT for main program }
  1223. if (not DLLsource) and
  1224. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1225. cg.a_call_name(list,'FPC_DO_EXIT');
  1226. end;
  1227. {****************************************************************************
  1228. Entry/Exit
  1229. ****************************************************************************}
  1230. procedure gen_proc_symbol(list:Taasmoutput);
  1231. var
  1232. hs : string;
  1233. begin
  1234. { add symbol entry point as well as debug information }
  1235. { will be inserted in front of the rest of this list. }
  1236. { Insert alignment and assembler names }
  1237. { Align, gprof uses 16 byte granularity }
  1238. if (cs_profile in aktmoduleswitches) then
  1239. list.concat(Tai_align.create(16))
  1240. else
  1241. list.concat(Tai_align.create(aktalignment.procalign));
  1242. {$ifdef GDB}
  1243. if (cs_debuginfo in aktmoduleswitches) then
  1244. begin
  1245. if (po_public in current_procinfo.procdef.procoptions) then
  1246. Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
  1247. current_procinfo.procdef.concatstabto(list);
  1248. Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
  1249. end;
  1250. {$endif GDB}
  1251. repeat
  1252. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1253. if hs='' then
  1254. break;
  1255. {$ifdef GDB}
  1256. if (cs_debuginfo in aktmoduleswitches) and
  1257. target_info.use_function_relative_addresses then
  1258. list.concat(Tai_stab_function_name.create(strpnew(hs)));
  1259. {$endif GDB}
  1260. if (cs_profile in aktmoduleswitches) or
  1261. (po_public in current_procinfo.procdef.procoptions) then
  1262. list.concat(Tai_symbol.createname_global(hs,0))
  1263. else
  1264. list.concat(Tai_symbol.createname(hs,0));
  1265. until false;
  1266. end;
  1267. procedure gen_proc_symbol_end(list:Taasmoutput);
  1268. {$ifdef GDB}
  1269. var
  1270. stabsendlabel : tasmlabel;
  1271. mangled_length : longint;
  1272. p : pchar;
  1273. {$endif GDB}
  1274. begin
  1275. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1276. {$ifdef GDB}
  1277. if (cs_debuginfo in aktmoduleswitches) then
  1278. begin
  1279. objectlibrary.getlabel(stabsendlabel);
  1280. cg.a_label(list,stabsendlabel);
  1281. { define calling EBP as pseudo local var PM }
  1282. { this enables test if the function is a local one !! }
  1283. {if assigned(current_procinfo.parent) and
  1284. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  1285. list.concat(Tai_stabs.Create(strpnew(
  1286. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1287. tostr(N_LSYM)+',0,0,'+tostr(current_procinfo.parent_framepointer_offset)))); }
  1288. if (not is_void(current_procinfo.procdef.rettype.def)) and
  1289. (tvarsym(current_procinfo.procdef.funcretsym).refs>0) then
  1290. begin
  1291. if tvarsym(current_procinfo.procdef.funcretsym).localloc.loc=LOC_REFERENCE then
  1292. begin
  1293. {$warning Need to add gdb support for ret in param register calling}
  1294. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1295. begin
  1296. list.concat(Tai_stabs.Create(strpnew(
  1297. '"'+current_procinfo.procdef.procsym.name+':X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1298. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1299. if (m_result in aktmodeswitches) then
  1300. list.concat(Tai_stabs.Create(strpnew(
  1301. '"RESULT:X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1302. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))))
  1303. end
  1304. else
  1305. begin
  1306. list.concat(Tai_stabs.Create(strpnew(
  1307. '"'+current_procinfo.procdef.procsym.name+':X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1308. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1309. if (m_result in aktmodeswitches) then
  1310. list.concat(Tai_stabs.Create(strpnew(
  1311. '"RESULT:X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1312. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1313. end;
  1314. end;
  1315. end;
  1316. mangled_length:=length(current_procinfo.procdef.mangledname);
  1317. getmem(p,2*mangled_length+50);
  1318. strpcopy(p,'192,0,0,');
  1319. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1320. if (target_info.use_function_relative_addresses) then
  1321. begin
  1322. strpcopy(strend(p),'-');
  1323. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1324. end;
  1325. list.concat(Tai_stabn.Create(strnew(p)));
  1326. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1327. +current_procinfo.procdef.mangledname))));
  1328. p[0]:='2';p[1]:='2';p[2]:='4';
  1329. strpcopy(strend(p),'_end');}
  1330. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1331. if (target_info.use_function_relative_addresses) then
  1332. begin
  1333. strpcopy(strend(p),'-');
  1334. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1335. end;
  1336. list.concatlist(withdebuglist);
  1337. list.concat(Tai_stabn.Create(strnew(p)));
  1338. { strpnew('224,0,0,'
  1339. +current_procinfo.procdef.mangledname+'_end'))));}
  1340. freemem(p,2*mangled_length+50);
  1341. end;
  1342. {$endif GDB}
  1343. end;
  1344. procedure gen_stackalloc_code(list:Taasmoutput);
  1345. var
  1346. stackframe : longint;
  1347. begin
  1348. { Calculate size of stackframe }
  1349. stackframe:=current_procinfo.calc_stackframe_size;
  1350. {$ifndef powerpc}
  1351. { at least for the ppc this applies always, so this code isn't usable (FK) }
  1352. { omit stack frame ? }
  1353. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1354. begin
  1355. CGmessage(cg_d_stackframe_omited);
  1356. if stackframe<>0 then
  1357. cg.g_stackpointer_alloc(list,stackframe);
  1358. end
  1359. else
  1360. {$endif powerpc}
  1361. begin
  1362. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1363. cg.g_interrupt_stackframe_entry(list);
  1364. cg.g_stackframe_entry(list,stackframe);
  1365. {Never call stack checking before the standard system unit
  1366. has been initialized.}
  1367. if (cs_check_stack in aktlocalswitches) and (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  1368. cg.g_stackcheck(list,stackframe);
  1369. end;
  1370. end;
  1371. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  1372. var
  1373. stacksize,
  1374. retsize : longint;
  1375. begin
  1376. {$ifndef powerpc}
  1377. { remove stackframe }
  1378. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1379. begin
  1380. stacksize:=current_procinfo.calc_stackframe_size;
  1381. if (stacksize<>0) then
  1382. cg.a_op_const_reg(list,OP_ADD,OS_32,stacksize,current_procinfo.framepointer);
  1383. end
  1384. else
  1385. cg.g_restore_frame_pointer(list);
  1386. {$endif}
  1387. { at last, the return is generated }
  1388. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1389. cg.g_interrupt_stackframe_exit(list,usesacc,usesacchi)
  1390. else
  1391. begin
  1392. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1393. begin
  1394. retsize:=0;
  1395. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1396. inc(retsize,POINTER_SIZE);
  1397. end
  1398. else
  1399. retsize:=current_procinfo.para_stack_size;
  1400. cg.g_return_from_proc(list,retsize);
  1401. end;
  1402. end;
  1403. procedure gen_save_used_regs(list:TAAsmoutput);
  1404. begin
  1405. { Pure assembler routines need to save the registers themselves }
  1406. if (po_assembler in current_procinfo.procdef.procoptions) then
  1407. exit;
  1408. { for the save all registers we can simply use a pusha,popa which
  1409. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1410. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1411. cg.g_save_all_registers(list)
  1412. else
  1413. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1414. cg.g_save_standard_registers(list);
  1415. end;
  1416. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  1417. begin
  1418. { Pure assembler routines need to save the registers themselves }
  1419. if (po_assembler in current_procinfo.procdef.procoptions) then
  1420. exit;
  1421. { for the save all registers we can simply use a pusha,popa which
  1422. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1423. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1424. cg.g_restore_all_registers(list,usesacc,usesacchi)
  1425. else
  1426. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1427. cg.g_restore_standard_registers(list);
  1428. end;
  1429. {****************************************************************************
  1430. Inlining
  1431. ****************************************************************************}
  1432. (*
  1433. procedure load_inlined_return_value(list:TAAsmoutput);
  1434. var
  1435. ressym: tvarsym;
  1436. resloc: tlocation;
  1437. r,r2 : tregister;
  1438. begin
  1439. if not is_void(current_procinfo.procdef.rettype.def) then
  1440. begin
  1441. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  1442. if ressym.reg.enum <> R_NO then
  1443. begin
  1444. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1445. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  1446. else
  1447. if ressym.vartype.def.deftype = floatdef then
  1448. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  1449. else
  1450. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1451. resloc.register := ressym.reg;
  1452. end
  1453. else
  1454. begin
  1455. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1456. reference_reset_base(resloc.reference,current_procinfo.framepointer,tvarsym(current_procinfo.procdef.funcretsym).adjusted_address);
  1457. end;
  1458. { Here, we return the function result. In most architectures, the value is
  1459. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1460. function returns in a register and the caller receives it in an other one }
  1461. case current_procinfo.procdef.rettype.def.deftype of
  1462. orddef,
  1463. enumdef :
  1464. begin
  1465. {$ifndef cpu64bit}
  1466. if resloc.size in [OS_64,OS_S64] then
  1467. begin
  1468. r:=cg.getregisterint(list,OS_INT);
  1469. r2:=cg.getregisterint(list,OS_INT);
  1470. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1471. end
  1472. else
  1473. {$endif cpu64bit}
  1474. begin
  1475. r:=cg.getregisterint(list,resloc.size);
  1476. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1477. end;
  1478. end;
  1479. floatdef :
  1480. begin
  1481. {$ifdef cpufpemu}
  1482. if cs_fp_emulation in aktmoduleswitches then
  1483. r.enum := FUNCTION_RETURN_REG
  1484. else
  1485. {$endif cpufpemu}
  1486. r.enum:=FPU_RESULT_REG;
  1487. cg.a_loadfpu_loc_reg(list,resloc,r);
  1488. end;
  1489. else
  1490. begin
  1491. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1492. begin
  1493. {$ifndef cpu64bit}
  1494. { Win32 can return records in EAX:EDX }
  1495. if resloc.size in [OS_64,OS_S64] then
  1496. begin
  1497. r:=cg.getregisterint(list,OS_INT);
  1498. r2:=cg.getregisterint(list,OS_INT);
  1499. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1500. end
  1501. else
  1502. {$endif cpu64bit}
  1503. begin
  1504. r:=cg.getregisterint(list,resloc.size);
  1505. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1506. end;
  1507. end
  1508. end;
  1509. end;
  1510. end;
  1511. end;
  1512. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  1513. begin
  1514. { initialize return value }
  1515. initretvalue(list);
  1516. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1517. { initialisizes temp. ansi/wide string data }
  1518. inittempvariables(list);
  1519. { initialize ansi/widesstring para's }
  1520. if assigned(current_procinfo.procdef.parast) then
  1521. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1522. { generate copies of call by value parameters }
  1523. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1524. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1525. load_regvars(list,nil);
  1526. end;
  1527. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  1528. var
  1529. usesacc,
  1530. usesacchi,
  1531. usesfpu : boolean;
  1532. begin
  1533. if aktexitlabel.is_used then
  1534. cg.a_label(list,aktexitlabel);
  1535. cleanup_regvars(list);
  1536. { finalize temporary data }
  1537. finalizetempvariables(list);
  1538. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1539. { finalize paras data }
  1540. if assigned(current_procinfo.procdef.parast) then
  1541. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1542. { handle return value, this is not done for assembler routines when
  1543. they didn't reference the result variable }
  1544. if not(po_assembler in current_procinfo.procdef.procoptions) or
  1545. (assigned(current_procinfo.procdef.funcretsym) and
  1546. (tvarsym(current_procinfo.procdef.funcretsym).refcount>1)) then
  1547. begin
  1548. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1549. internalerror(200305263);
  1550. // load_inlined_return_value(list);
  1551. load_return_value(list,usesacc,usesacchi,usesfpu)
  1552. end;
  1553. cleanup_regvars(list);
  1554. end;
  1555. *)
  1556. {****************************************************************************
  1557. Const Data
  1558. ****************************************************************************}
  1559. procedure insertconstdata(sym : ttypedconstsym);
  1560. { this does not affect the local stack space, since all
  1561. typed constansts and initialized variables are always
  1562. put in the .data / .rodata section
  1563. }
  1564. var
  1565. storefilepos : tfileposinfo;
  1566. curconstsegment : taasmoutput;
  1567. l : longint;
  1568. begin
  1569. storefilepos:=aktfilepos;
  1570. aktfilepos:=sym.fileinfo;
  1571. if sym.is_writable then
  1572. curconstsegment:=datasegment
  1573. else
  1574. curconstsegment:=consts;
  1575. l:=sym.getsize;
  1576. { insert cut for smartlinking or alignment }
  1577. if (cs_create_smart in aktmoduleswitches) then
  1578. curconstSegment.concat(Tai_cut.Create);
  1579. curconstSegment.concat(Tai_align.create(const_align(l)));
  1580. {$ifdef GDB}
  1581. if cs_debuginfo in aktmoduleswitches then
  1582. sym.concatstabto(curconstsegment);
  1583. {$endif GDB}
  1584. if (sym.owner.symtabletype=globalsymtable) or
  1585. (cs_create_smart in aktmoduleswitches) or
  1586. (assigned(current_procinfo) and
  1587. (current_procinfo.procdef.proccalloption=pocall_inline)) or
  1588. DLLSource then
  1589. curconstSegment.concat(Tai_symbol.Createdataname_global(sym.mangledname,l))
  1590. else
  1591. curconstSegment.concat(Tai_symbol.Createdataname(sym.mangledname,l));
  1592. aktfilepos:=storefilepos;
  1593. end;
  1594. procedure insertbssdata(sym : tvarsym);
  1595. var
  1596. l,varalign : longint;
  1597. storefilepos : tfileposinfo;
  1598. begin
  1599. storefilepos:=aktfilepos;
  1600. aktfilepos:=sym.fileinfo;
  1601. l:=sym.getvaluesize;
  1602. if (vo_is_thread_var in sym.varoptions) then
  1603. inc(l,pointer_size);
  1604. varalign:=var_align(l);
  1605. {
  1606. sym.address:=align(datasize,varalign);
  1607. datasize:=tvarsym(sym).address+l;
  1608. }
  1609. { insert cut for smartlinking or alignment }
  1610. if (cs_create_smart in aktmoduleswitches) then
  1611. bssSegment.concat(Tai_cut.Create);
  1612. bssSegment.concat(Tai_align.create(varalign));
  1613. {$ifdef GDB}
  1614. if cs_debuginfo in aktmoduleswitches then
  1615. sym.concatstabto(bsssegment);
  1616. {$endif GDB}
  1617. if (sym.owner.symtabletype=globalsymtable) or
  1618. (cs_create_smart in aktmoduleswitches) or
  1619. DLLSource or
  1620. (assigned(current_procinfo) and
  1621. (current_procinfo.procdef.proccalloption=pocall_inline)) or
  1622. (vo_is_exported in sym.varoptions) or
  1623. (vo_is_C_var in sym.varoptions) then
  1624. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1625. else
  1626. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1627. aktfilepos:=storefilepos;
  1628. end;
  1629. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  1630. var
  1631. sym : tsym;
  1632. begin
  1633. sym:=tsym(st.symindex.first);
  1634. while assigned(sym) do
  1635. begin
  1636. { Only allocate space for referenced locals }
  1637. if (sym.typ=varsym) and
  1638. (tvarsym(sym).refs>0) then
  1639. begin
  1640. with tvarsym(sym) do
  1641. begin
  1642. {$warning TODO Add support for register variables}
  1643. localloc.loc:=LOC_REFERENCE;
  1644. tg.GetLocal(list,getvaluesize,localloc.reference);
  1645. if cs_asm_source in aktglobalswitches then
  1646. list.concat(Tai_comment.Create(strpnew('Local '+realname+' located at '+
  1647. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1648. end;
  1649. end;
  1650. sym:=tsym(sym.indexnext);
  1651. end;
  1652. end;
  1653. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  1654. var
  1655. sym : tsym;
  1656. begin
  1657. sym:=tsym(st.symindex.first);
  1658. while assigned(sym) do
  1659. begin
  1660. if (sym.typ=varsym) and
  1661. (tvarsym(sym).refs>0) then
  1662. begin
  1663. with tvarsym(sym) do
  1664. begin
  1665. { Note: We need to keep the data available in memory
  1666. for the sub procedures that can access local data
  1667. in the parent procedures }
  1668. case localloc.loc of
  1669. LOC_REFERENCE :
  1670. tg.Ungetlocal(list,localloc.reference);
  1671. LOC_REGISTER :
  1672. begin
  1673. {$ifndef cpu64bit}
  1674. if localloc.size in [OS_64,OS_S64] then
  1675. begin
  1676. cg.ungetregister(list,localloc.registerlow);
  1677. cg.ungetregister(list,localloc.registerhigh);
  1678. end
  1679. else
  1680. {$endif cpu64bit}
  1681. cg.ungetregister(list,localloc.register);
  1682. end;
  1683. end;
  1684. end;
  1685. end;
  1686. sym:=tsym(sym.indexnext);
  1687. end;
  1688. end;
  1689. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  1690. var
  1691. sym : tsym;
  1692. begin
  1693. sym:=tsym(st.symindex.first);
  1694. while assigned(sym) do
  1695. begin
  1696. if sym.typ=varsym then
  1697. begin
  1698. with tvarsym(sym) do
  1699. begin
  1700. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1701. begin
  1702. case paraitem.paraloc[calleeside].loc of
  1703. LOC_FPUREGISTER,
  1704. LOC_REGISTER:
  1705. begin
  1706. (*
  1707. if paraitem.paraloc[calleeside].register=NR_NO then
  1708. begin
  1709. paraitem.paraloc[calleeside].loc:=LOC_REGISTER;
  1710. paraitem.paraloc[calleeside].size:=paraitem.paraloc[calleeside].size;
  1711. {$ifndef cpu64bit}
  1712. if paraitem.paraloc[calleeside].size in [OS_64,OS_S64] then
  1713. begin
  1714. paraitem.paraloc[calleeside].registerlow:=cg.getregisterint(list,OS_32);
  1715. paraitem.paraloc[calleeside].registerhigh:=cg.getregisterint(list,OS_32);
  1716. end
  1717. else
  1718. {$endif cpu64bit}
  1719. paraitem.paraloc[calleeside].register:=cg.getregisterint(list,localloc.size);
  1720. end;
  1721. *)
  1722. (*
  1723. {$warning TODO Allocate register paras}
  1724. localloc.loc:=LOC_REGISTER;
  1725. localloc.size:=paraitem.paraloc[calleeside].size;
  1726. {$ifndef cpu64bit}
  1727. if localloc.size in [OS_64,OS_S64] then
  1728. begin
  1729. localloc.registerlow:=cg.getregisterint(list,OS_32);
  1730. localloc.registerhigh:=cg.getregisterint(list,OS_32);
  1731. end
  1732. else
  1733. {$endif cpu64bit}
  1734. localloc.register:=cg.getregisterint(list,localloc.size);
  1735. *)
  1736. localloc.loc:=LOC_REFERENCE;
  1737. localloc.size:=paraitem.paraloc[calleeside].size;
  1738. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1739. end
  1740. else
  1741. localloc:=paraitem.paraloc[calleeside];
  1742. end;
  1743. end
  1744. else
  1745. localloc:=paraitem.paraloc[calleeside];
  1746. if cs_asm_source in aktglobalswitches then
  1747. begin
  1748. case localloc.loc of
  1749. LOC_REFERENCE :
  1750. list.concat(Tai_comment.Create(strpnew('Para '+realname+' located at '+
  1751. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1752. end;
  1753. end;
  1754. end;
  1755. end;
  1756. sym:=tsym(sym.indexnext);
  1757. end;
  1758. end;
  1759. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  1760. var
  1761. sym : tsym;
  1762. begin
  1763. sym:=tsym(st.symindex.first);
  1764. while assigned(sym) do
  1765. begin
  1766. if sym.typ=varsym then
  1767. begin
  1768. with tvarsym(sym) do
  1769. begin
  1770. {$warning TODO Allocate register paras}
  1771. localloc.loc:=LOC_REFERENCE;
  1772. localloc.size:=int_cgsize(paramanager.push_size(vs_value,vartype.def,pocall_inline));
  1773. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1774. if cs_asm_source in aktglobalswitches then
  1775. begin
  1776. case localloc.loc of
  1777. LOC_REFERENCE :
  1778. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  1779. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1780. end;
  1781. end;
  1782. paraitem.paraloc[callerside]:=localloc;
  1783. paraitem.paraloc[calleeside]:=localloc;
  1784. end;
  1785. end;
  1786. sym:=tsym(sym.indexnext);
  1787. end;
  1788. end;
  1789. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  1790. var
  1791. sym : tsym;
  1792. begin
  1793. sym:=tsym(st.symindex.first);
  1794. while assigned(sym) do
  1795. begin
  1796. if sym.typ=varsym then
  1797. begin
  1798. with tvarsym(sym) do
  1799. begin
  1800. { Note: We need to keep the data available in memory
  1801. for the sub procedures that can access local data
  1802. in the parent procedures }
  1803. case localloc.loc of
  1804. LOC_REFERENCE :
  1805. tg.UngetLocal(list,localloc.reference);
  1806. LOC_REGISTER :
  1807. begin
  1808. if localloc.register<>paraitem.paraloc[calleeside].register then
  1809. begin
  1810. {$ifndef cpu64bit}
  1811. if localloc.size in [OS_64,OS_S64] then
  1812. begin
  1813. cg.ungetregister(list,localloc.registerlow);
  1814. cg.ungetregister(list,localloc.registerhigh);
  1815. end
  1816. else
  1817. {$endif cpu64bit}
  1818. cg.ungetregister(list,localloc.register);
  1819. end;
  1820. end;
  1821. end;
  1822. end;
  1823. end;
  1824. sym:=tsym(sym.indexnext);
  1825. end;
  1826. end;
  1827. end.
  1828. {
  1829. $Log$
  1830. Revision 1.171 2003-11-30 19:35:29 florian
  1831. * fixed several arm related problems
  1832. Revision 1.170 2003/11/29 20:13:25 florian
  1833. * fixed several pi_do_call problems
  1834. Revision 1.169 2003/11/23 17:05:15 peter
  1835. * register calling is left-right
  1836. * parameter ordering
  1837. * left-right calling inserts result parameter last
  1838. Revision 1.168 2003/11/22 00:31:25 jonas
  1839. + extra allocations of function result regs for the optimiser
  1840. Revision 1.167 2003/11/11 21:10:12 peter
  1841. * remove temporary stdcall hack
  1842. Revision 1.166 2003/11/10 22:02:52 peter
  1843. * cross unit inlining fixed
  1844. Revision 1.165 2003/11/07 15:58:32 florian
  1845. * Florian's culmutative nr. 1; contains:
  1846. - invalid calling conventions for a certain cpu are rejected
  1847. - arm softfloat calling conventions
  1848. - -Sp for cpu dependend code generation
  1849. - several arm fixes
  1850. - remaining code for value open array paras on heap
  1851. Revision 1.164 2003/11/04 19:03:50 peter
  1852. * fixes for temp type patch
  1853. Revision 1.163 2003/11/04 15:35:13 peter
  1854. * fix for referencecounted temps
  1855. Revision 1.162 2003/10/25 11:34:02 florian
  1856. * fixed compilation of ppc system unit
  1857. Revision 1.161 2003/10/19 01:34:30 florian
  1858. * some ppc stuff fixed
  1859. * memory leak fixed
  1860. Revision 1.160 2003/10/17 15:08:34 peter
  1861. * commented out more obsolete constants
  1862. Revision 1.159 2003/10/17 14:38:32 peter
  1863. * 64k registers supported
  1864. * fixed some memory leaks
  1865. Revision 1.158 2003/10/10 17:48:13 peter
  1866. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1867. * tregisteralloctor renamed to trgobj
  1868. * removed rgobj from a lot of units
  1869. * moved location_* and reference_* to cgobj
  1870. * first things for mmx register allocation
  1871. Revision 1.157 2003/10/09 21:31:37 daniel
  1872. * Register allocator splitted, ans abstract now
  1873. Revision 1.156 2003/10/07 18:18:16 peter
  1874. * fix register calling for assembler procedures
  1875. * fix result loading for assembler procedures
  1876. Revision 1.155 2003/10/07 15:17:07 peter
  1877. * inline supported again, LOC_REFERENCEs are used to pass the
  1878. parameters
  1879. * inlineparasymtable,inlinelocalsymtable removed
  1880. * exitlabel inserting fixed
  1881. Revision 1.154 2003/10/01 20:34:48 peter
  1882. * procinfo unit contains tprocinfo
  1883. * cginfo renamed to cgbase
  1884. * moved cgmessage to verbose
  1885. * fixed ppc and sparc compiles
  1886. Revision 1.153 2003/09/30 21:02:37 peter
  1887. * updates for inlining
  1888. Revision 1.152 2003/09/29 20:58:56 peter
  1889. * optimized releasing of registers
  1890. Revision 1.151 2003/09/28 21:47:18 peter
  1891. * register paras and local copies updates
  1892. Revision 1.150 2003/09/28 17:55:03 peter
  1893. * parent framepointer changed to hidden parameter
  1894. * tloadparentfpnode added
  1895. Revision 1.149 2003/09/28 13:39:38 peter
  1896. * optimized releasing of registers
  1897. Revision 1.148 2003/09/25 21:28:00 peter
  1898. * parameter fixes
  1899. Revision 1.147 2003/09/23 21:03:59 peter
  1900. * check for refs>0 in init/final local data
  1901. Revision 1.146 2003/09/23 17:56:05 peter
  1902. * locals and paras are allocated in the code generation
  1903. * tvarsym.localloc contains the location of para/local when
  1904. generating code for the current procedure
  1905. Revision 1.145 2003/09/16 16:17:01 peter
  1906. * varspez in calls to push_addr_param
  1907. Revision 1.144 2003/09/14 21:33:37 peter
  1908. * location_force_reg gives IE when size=OS_NO
  1909. Revision 1.143 2003/09/14 19:18:10 peter
  1910. * remove obsolete code already in comments
  1911. Revision 1.142 2003/09/11 11:54:59 florian
  1912. * improved arm code generation
  1913. * move some protected and private field around
  1914. * the temp. register for register parameters/arguments are now released
  1915. before the move to the parameter register is done. This improves
  1916. the code in a lot of cases.
  1917. Revision 1.141 2003/09/10 08:31:47 marco
  1918. * Patch from Peter for paraloc
  1919. Revision 1.140 2003/09/07 22:09:35 peter
  1920. * preparations for different default calling conventions
  1921. * various RA fixes
  1922. Revision 1.139 2003/09/03 15:55:01 peter
  1923. * NEWRA branch merged
  1924. Revision 1.138 2003/09/03 11:18:37 florian
  1925. * fixed arm concatcopy
  1926. + arm support in the common compiler sources added
  1927. * moved some generic cg code around
  1928. + tfputype added
  1929. * ...
  1930. Revision 1.137.2.4 2003/08/31 15:46:26 peter
  1931. * more updates for tregister
  1932. Revision 1.137.2.3 2003/08/29 17:28:59 peter
  1933. * next batch of updates
  1934. Revision 1.137.2.2 2003/08/28 18:35:08 peter
  1935. * tregister changed to cardinal
  1936. Revision 1.137.2.1 2003/08/27 19:55:54 peter
  1937. * first tregister patch
  1938. Revision 1.137 2003/08/20 20:29:06 daniel
  1939. * Some more R_NO changes
  1940. * Preventive code to loadref added
  1941. Revision 1.136 2003/08/20 17:48:49 peter
  1942. * fixed stackalloc to not allocate localst.datasize twice
  1943. * order of stackalloc code fixed for implicit init/final
  1944. Revision 1.135 2003/08/17 16:59:20 jonas
  1945. * fixed regvars so they work with newra (at least for ppc)
  1946. * fixed some volatile register bugs
  1947. + -dnotranslation option for -dnewra, which causes the registers not to
  1948. be translated from virtual to normal registers. Requires support in
  1949. the assembler writer as well, which is only implemented in aggas/
  1950. agppcgas currently
  1951. Revision 1.134 2003/08/11 21:18:20 peter
  1952. * start of sparc support for newra
  1953. Revision 1.133 2003/08/09 18:56:54 daniel
  1954. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1955. allocator
  1956. * Some preventive changes to i386 spillinh code
  1957. Revision 1.132 2003/08/03 14:09:50 daniel
  1958. * Fixed a register allocator bug
  1959. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1960. statements: changes in location_force. These moves are now no longer
  1961. constrained so they are optimized away.
  1962. Revision 1.131 2003/07/23 11:04:15 jonas
  1963. * split en_exit_code into a part that may allocate a register and a part
  1964. that doesn't, so the former can be done before the register colouring
  1965. has been performed
  1966. Revision 1.130 2003/07/06 17:58:22 peter
  1967. * framepointer fixes for sparc
  1968. * parent framepointer code more generic
  1969. Revision 1.129 2003/07/06 15:31:20 daniel
  1970. * Fixed register allocator. *Lots* of fixes.
  1971. Revision 1.128 2003/07/02 22:18:04 peter
  1972. * paraloc splitted in callerparaloc,calleeparaloc
  1973. * sparc calling convention updates
  1974. Revision 1.127 2003/06/17 18:13:51 jonas
  1975. * fixed -dnewra compilation problems
  1976. Revision 1.126 2003/06/17 16:32:44 peter
  1977. * alloc register for function result
  1978. Revision 1.125 2003/06/13 21:19:30 peter
  1979. * current_procdef removed, use current_procinfo.procdef instead
  1980. Revision 1.124 2003/06/09 12:23:30 peter
  1981. * init/final of procedure data splitted from genentrycode
  1982. * use asmnode getposition to insert final at the correct position
  1983. als for the implicit try...finally
  1984. Revision 1.123 2003/06/07 18:57:04 jonas
  1985. + added freeintparaloc
  1986. * ppc get/freeintparaloc now check whether the parameter regs are
  1987. properly allocated/deallocated (and get an extra list para)
  1988. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1989. * fixed lot of missing pi_do_call's
  1990. Revision 1.122 2003/06/06 14:43:02 peter
  1991. * g_copyopenarrayvalue gets length reference
  1992. * don't copy open arrays for cdecl
  1993. Revision 1.121 2003/06/03 21:11:09 peter
  1994. * cg.a_load_* get a from and to size specifier
  1995. * makeregsize only accepts newregister
  1996. * i386 uses generic tcgnotnode,tcgunaryminus
  1997. Revision 1.120 2003/06/03 15:49:49 jonas
  1998. * fixed ref/loc problems
  1999. Revision 1.119 2003/06/03 15:06:37 daniel
  2000. * fixed conflict marks
  2001. Revision 1.118 2003/06/03 13:01:59 daniel
  2002. * Register allocator finished
  2003. Revision 1.117 2003/06/02 21:42:05 jonas
  2004. * function results can now also be regvars
  2005. - removed tprocinfo.return_offset, never use it again since it's invalid
  2006. if the result is a regvar
  2007. Revision 1.116 2003/06/01 21:38:06 peter
  2008. * getregisterfpu size parameter added
  2009. * op_const_reg size parameter added
  2010. * sparc updates
  2011. Revision 1.115 2003/05/31 20:28:17 jonas
  2012. * changed copyvalueparas so it also supports register parameters
  2013. (except for copy_value_openarray, but that one is seriously broken
  2014. anyway, since it expects that the high parameter will always be in
  2015. memory right after the pointer to the array, while it could just as
  2016. will be in a register)
  2017. Revision 1.114 2003/05/31 15:05:28 peter
  2018. * FUNCTION_RESULT64_LOW/HIGH_REG added for int64 results
  2019. Revision 1.113 2003/05/31 00:48:15 jonas
  2020. * fixed my previous commit
  2021. Revision 1.112 2003/05/30 23:57:08 peter
  2022. * more sparc cleanup
  2023. * accumulator removed, splitted in function_return_reg (called) and
  2024. function_result_reg (caller)
  2025. Revision 1.111 2003/05/30 23:49:18 jonas
  2026. * a_load_loc_reg now has an extra size parameter for the destination
  2027. register (properly fixes what I worked around in revision 1.106 of
  2028. ncgutil.pas)
  2029. Revision 1.110 2003/05/30 18:52:10 jonas
  2030. * fixed bug with intregvars
  2031. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2032. rcgppc.a_param_ref, which previously got bogus size values
  2033. Revision 1.109 2003/05/27 21:19:08 jonas
  2034. * fixed ppc cycle
  2035. Revision 1.108 2003/05/27 14:28:14 jonas
  2036. * patch from Peter for nested procedures
  2037. Revision 1.107 2003/05/26 21:17:17 peter
  2038. * procinlinenode removed
  2039. * aktexit2label removed, fast exit removed
  2040. + tcallnode.inlined_pass_2 added
  2041. Revision 1.106 2003/05/24 11:59:42 jonas
  2042. * fixed integer typeconversion problems
  2043. Revision 1.105 2003/05/23 14:27:35 peter
  2044. * remove some unit dependencies
  2045. * current_procinfo changes to store more info
  2046. Revision 1.104 2003/05/15 18:58:53 peter
  2047. * removed selfpointer_offset, vmtpointer_offset
  2048. * tvarsym.adjusted_address
  2049. * address in localsymtable is now in the real direction
  2050. * removed some obsolete globals
  2051. Revision 1.103 2003/05/14 19:37:25 jonas
  2052. * patch from Peter for int64 function results
  2053. Revision 1.102 2003/05/13 19:14:41 peter
  2054. * failn removed
  2055. * inherited result code check moven to pexpr
  2056. Revision 1.101 2003/05/13 15:16:13 peter
  2057. * removed ret_in_acc, it's the reverse of ret_in_param
  2058. * fixed ret_in_param for win32 cdecl array
  2059. Revision 1.100 2003/05/12 08:08:27 jonas
  2060. * fixed several initialization and finalization related bugs (missing
  2061. tg.direction's, wrong paralocation for decreasing refcount of
  2062. everything but ansistrings)
  2063. Revision 1.99 2003/05/11 21:37:03 peter
  2064. * moved implicit exception frame from ncgutil to psub
  2065. * constructor/destructor helpers moved from cobj/ncgutil to psub
  2066. Revision 1.98 2003/05/11 14:45:12 peter
  2067. * tloadnode does not support objectsymtable,withsymtable anymore
  2068. * withnode cleanup
  2069. * direct with rewritten to use temprefnode
  2070. Revision 1.97 2003/05/10 13:20:23 jonas
  2071. * moved storing of register parameters to memory even earlier in the
  2072. entry code to fix problems with constructors
  2073. Revision 1.96 2003/05/09 17:47:02 peter
  2074. * self moved to hidden parameter
  2075. * removed hdisposen,hnewn,selfn
  2076. Revision 1.95 2003/04/29 07:28:52 michael
  2077. + Patch from peter to fix wrong pushing of ansistring function results in open array
  2078. Revision 1.94 2003/04/28 21:17:38 peter
  2079. * do not finalize function results
  2080. Revision 1.93 2003/04/27 16:30:34 jonas
  2081. * store register para's to memory before copying the valuepara's, because
  2082. that one requires them to be there already (and it calls subroutines ->
  2083. could overwrite those registers)
  2084. Revision 1.92 2003/04/27 11:21:33 peter
  2085. * aktprocdef renamed to current_procinfo.procdef
  2086. * procinfo renamed to current_procinfo
  2087. * procinfo will now be stored in current_module so it can be
  2088. cleaned up properly
  2089. * gen_main_procsym changed to create_main_proc and release_main_proc
  2090. to also generate a tprocinfo structure
  2091. * fixed unit implicit initfinal
  2092. Revision 1.91 2003/04/27 07:29:50 peter
  2093. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  2094. a new procdef declaration
  2095. * aktprocsym removed
  2096. * lexlevel removed, use symtable.symtablelevel instead
  2097. * implicit init/final code uses the normal genentry/genexit
  2098. * funcret state checking updated for new funcret handling
  2099. Revision 1.90 2003/04/26 17:21:08 florian
  2100. * fixed passing of fpu values by fpu register
  2101. Revision 1.89 2003/04/25 20:59:33 peter
  2102. * removed funcretn,funcretsym, function result is now in varsym
  2103. and aliases for result and function name are added using absolutesym
  2104. * vs_hidden parameter for funcret passed in parameter
  2105. * vs_hidden fixes
  2106. * writenode changed to printnode and released from extdebug
  2107. * -vp option added to generate a tree.log with the nodetree
  2108. * nicer printnode for statements, callnode
  2109. Revision 1.88 2003/04/23 12:35:34 florian
  2110. * fixed several issues with powerpc
  2111. + applied a patch from Jonas for nested function calls (PowerPC only)
  2112. * ...
  2113. Revision 1.87 2003/04/22 14:33:38 peter
  2114. * removed some notes/hints
  2115. Revision 1.86 2003/04/22 13:47:08 peter
  2116. * fixed C style array of const
  2117. * fixed C array passing
  2118. * fixed left to right with high parameters
  2119. Revision 1.85 2003/04/22 10:09:35 daniel
  2120. + Implemented the actual register allocator
  2121. + Scratch registers unavailable when new register allocator used
  2122. + maybe_save/maybe_restore unavailable when new register allocator used
  2123. Revision 1.84 2003/04/16 09:26:55 jonas
  2124. * assembler procedures now again get a stackframe if they have local
  2125. variables. No space is reserved for a function result however.
  2126. Also, the register parameters aren't automatically saved on the stack
  2127. anymore in assembler procedures.
  2128. Revision 1.83 2003/04/06 21:11:23 olle
  2129. * changed newasmsymbol to newasmsymboldata for data symbols
  2130. Revision 1.82 2003/03/30 20:59:07 peter
  2131. * fix classmethod from classmethod call
  2132. * move BeforeDestruction/AfterConstruction calls to
  2133. genentrycode/genexitcode instead of generating them on the fly
  2134. after a call to a constructor
  2135. Revision 1.81 2003/03/28 19:16:56 peter
  2136. * generic constructor working for i386
  2137. * remove fixed self register
  2138. * esi added as address register for i386
  2139. Revision 1.80 2003/03/17 15:52:20 peter
  2140. * fix range error
  2141. Revision 1.79 2003/03/11 21:46:24 jonas
  2142. * lots of new regallocator fixes, both in generic and ppc-specific code
  2143. (ppc compiler still can't compile the linux system unit though)
  2144. Revision 1.78 2003/02/26 21:15:43 daniel
  2145. * Fixed the optimizer
  2146. Revision 1.77 2003/02/19 22:00:14 daniel
  2147. * Code generator converted to new register notation
  2148. - Horribily outdated todo.txt removed
  2149. Revision 1.76 2003/02/15 22:17:38 carl
  2150. * bugfix of FPU emulation code
  2151. Revision 1.75 2003/01/09 22:00:53 florian
  2152. * fixed some PowerPC issues
  2153. Revision 1.74 2003/01/09 20:41:10 florian
  2154. * fixed broken PowerPC compiler
  2155. Revision 1.73 2003/01/08 18:43:56 daniel
  2156. * Tregister changed into a record
  2157. Revision 1.72 2002/12/29 23:51:43 florian
  2158. * web bug 2214 fixed: ie 10 in const array constructors
  2159. Revision 1.71 2002/12/24 15:56:50 peter
  2160. * stackpointer_alloc added for adjusting ESP. Win32 needs
  2161. this for the pageprotection
  2162. Revision 1.70 2002/12/05 14:39:21 florian
  2163. * added missing then, Carl did you really a make fullcycle :) ?
  2164. Revision 1.69 2002/12/03 22:13:39 carl
  2165. * bugfix of problems with profile code which clobbers some registers
  2166. Revision 1.68 2002/12/01 22:06:59 carl
  2167. * warning of portabilitiy problems with parasize / localsize
  2168. Revision 1.67 2002/11/30 18:44:57 carl
  2169. + profiling support for Win32
  2170. Revision 1.66 2002/11/30 14:39:15 carl
  2171. * try to fix profiling for win32
  2172. Revision 1.65 2002/11/28 23:28:14 florian
  2173. * push_value_para didn't release floatdef locations, fixes tw2045
  2174. Revision 1.64 2002/11/27 02:33:19 peter
  2175. * copy_value_on_stack method added for cdecl record passing
  2176. Revision 1.63 2002/11/25 17:43:18 peter
  2177. * splitted defbase in defutil,symutil,defcmp
  2178. * merged isconvertable and is_equal into compare_defs(_ext)
  2179. * made operator search faster by walking the list only once
  2180. Revision 1.62 2002/11/18 17:31:55 peter
  2181. * pass proccalloption to ret_in_xxx and push_xxx functions
  2182. Revision 1.61 2002/11/17 17:49:08 mazen
  2183. + return_result_reg and FUNCTION_RESULT_REG are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  2184. Revision 1.60 2002/11/17 16:31:56 carl
  2185. * memory optimization (3-4%) : cleanup of tai fields,
  2186. cleanup of tdef and tsym fields.
  2187. * make it work for m68k
  2188. Revision 1.59 2002/11/15 01:58:51 peter
  2189. * merged changes from 1.0.7 up to 04-11
  2190. - -V option for generating bug report tracing
  2191. - more tracing for option parsing
  2192. - errors for cdecl and high()
  2193. - win32 import stabs
  2194. - win32 records<=8 are returned in eax:edx (turned off by default)
  2195. - heaptrc update
  2196. - more info for temp management in .s file with EXTDEBUG
  2197. Revision 1.58 2002/11/10 19:07:45 mazen
  2198. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  2199. Revision 1.57 2002/11/03 20:22:40 mazen
  2200. * parameter handling updated
  2201. Revision 1.56 2002/10/16 19:01:43 peter
  2202. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  2203. implicit exception frames for procedures with initialized variables
  2204. and for constructors. The default is on for compatibility
  2205. Revision 1.55 2002/10/14 19:42:33 peter
  2206. * only use init tables for threadvars
  2207. Revision 1.54 2002/10/06 19:41:30 peter
  2208. * Add finalization of typed consts
  2209. * Finalization of globals in the main program
  2210. Revision 1.53 2002/10/05 15:18:42 carl
  2211. * fix heap leaks
  2212. Revision 1.52 2002/09/30 07:00:46 florian
  2213. * fixes to common code to get the alpha compiler compiled applied
  2214. Revision 1.51 2002/09/22 14:02:35 carl
  2215. * stack checking cannot be called before system unit is initialized
  2216. * MC68020 define
  2217. Revision 1.50 2002/09/17 18:54:03 jonas
  2218. * a_load_reg_reg() now has two size parameters: source and dest. This
  2219. allows some optimizations on architectures that don't encode the
  2220. register size in the register name.
  2221. Revision 1.49 2002/09/10 21:48:30 florian
  2222. * improved handling of procedures with register calling conventions
  2223. Revision 1.48 2002/09/07 15:25:03 peter
  2224. * old logs removed and tabs fixed
  2225. Revision 1.47 2002/09/02 18:44:48 peter
  2226. * fixed (not) pushing of empty parameters
  2227. * fixed implicit initialization/finalization generation
  2228. * fixed/optimized local copy of value arguments init/final
  2229. Revision 1.46 2002/09/01 19:27:34 peter
  2230. * use index register when available for generating a reference with
  2231. only a signle register. Using the base register could possibly
  2232. destroy the framepointer
  2233. Revision 1.45 2002/09/01 18:50:20 peter
  2234. * fixed maybe_save that did not support a reference with only
  2235. a index register. It now also updates the location with the new
  2236. base register only
  2237. Revision 1.44 2002/09/01 14:42:41 peter
  2238. * removevaluepara added to fix the stackpointer so restoring of
  2239. saved registers works
  2240. Revision 1.43 2002/08/25 19:25:18 peter
  2241. * sym.insert_in_data removed
  2242. * symtable.insertvardata/insertconstdata added
  2243. * removed insert_in_data call from symtable.insert, it needs to be
  2244. called separatly. This allows to deref the address calculation
  2245. * procedures now calculate the parast addresses after the procedure
  2246. directives are parsed. This fixes the cdecl parast problem
  2247. * push_addr_param has an extra argument that specifies if cdecl is used
  2248. or not
  2249. Revision 1.42 2002/08/24 18:38:26 peter
  2250. * really use tt_noreuse for exception frame buffers
  2251. Revision 1.41 2002/08/23 16:14:49 peter
  2252. * tempgen cleanup
  2253. * tt_noreuse temp type added that will be used in genentrycode
  2254. Revision 1.40 2002/08/18 10:42:37 florian
  2255. * remaining assembler writer bugs fixed, the errors in the
  2256. system unit are inline assembler problems
  2257. Revision 1.39 2002/08/17 09:23:36 florian
  2258. * first part of procinfo rewrite
  2259. Revision 1.38 2002/08/16 14:24:57 carl
  2260. * issameref() to test if two references are the same (then emit no opcodes)
  2261. + ret_in_reg to replace ret_in_acc
  2262. (fix some register allocation bugs at the same time)
  2263. + save_std_register now has an extra parameter which is the
  2264. usedinproc registers
  2265. Revision 1.37 2002/08/15 15:15:55 carl
  2266. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  2267. * more generic nodes for maths
  2268. * several fixes for better m68k support
  2269. Revision 1.36 2002/08/14 19:25:09 carl
  2270. * fix Florian's last commit for m68k compilation
  2271. Revision 1.35 2002/08/13 21:40:56 florian
  2272. * more fixes for ppc calling conventions
  2273. Revision 1.34 2002/08/12 15:08:39 carl
  2274. + stab register indexes for powerpc (moved from gdb to cpubase)
  2275. + tprocessor enumeration moved to cpuinfo
  2276. + linker in target_info is now a class
  2277. * many many updates for m68k (will soon start to compile)
  2278. - removed some ifdef or correct them for correct cpu
  2279. Revision 1.33 2002/08/11 14:32:27 peter
  2280. * renamed current_library to objectlibrary
  2281. Revision 1.32 2002/08/11 13:24:12 peter
  2282. * saving of asmsymbols in ppu supported
  2283. * asmsymbollist global is removed and moved into a new class
  2284. tasmlibrarydata that will hold the info of a .a file which
  2285. corresponds with a single module. Added librarydata to tmodule
  2286. to keep the library info stored for the module. In the future the
  2287. objectfiles will also be stored to the tasmlibrarydata class
  2288. * all getlabel/newasmsymbol and friends are moved to the new class
  2289. Revision 1.31 2002/08/09 19:16:57 carl
  2290. * stack allocation is now done separately (at the end) of genentrycode
  2291. so temps. can be allocated before.
  2292. * fix generic exception handling
  2293. Revision 1.30 2002/08/06 20:55:21 florian
  2294. * first part of ppc calling conventions fix
  2295. Revision 1.29 2002/08/04 19:09:22 carl
  2296. + added generic exception support (still does not work!)
  2297. + more documentation
  2298. Revision 1.28 2002/07/29 21:23:42 florian
  2299. * more fixes for the ppc
  2300. + wrappers for the tcnvnode.first_* stuff introduced
  2301. }