ncgutil.pas 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  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. hitemp,
  1347. lotemp,
  1348. stackframe : longint;
  1349. begin
  1350. { Calculate size of stackframe }
  1351. stackframe:=current_procinfo.calc_stackframe_size;
  1352. { All temps are know, write offsets used for information }
  1353. if (cs_asm_source in aktglobalswitches) then
  1354. begin
  1355. if tg.direction>0 then
  1356. begin
  1357. lotemp:=current_procinfo.tempstart;
  1358. hitemp:=tg.lasttemp;
  1359. end
  1360. else
  1361. begin
  1362. lotemp:=tg.lasttemp;
  1363. hitemp:=current_procinfo.tempstart;
  1364. end;
  1365. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1366. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1367. end;
  1368. {$ifndef powerpc}
  1369. { at least for the ppc this applies always, so this code isn't usable (FK) }
  1370. { omit stack frame ? }
  1371. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1372. begin
  1373. CGmessage(cg_d_stackframe_omited);
  1374. if stackframe<>0 then
  1375. cg.g_stackpointer_alloc(list,stackframe);
  1376. end
  1377. else
  1378. {$endif powerpc}
  1379. begin
  1380. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1381. cg.g_interrupt_stackframe_entry(list);
  1382. cg.g_stackframe_entry(list,stackframe);
  1383. {Never call stack checking before the standard system unit
  1384. has been initialized.}
  1385. if (cs_check_stack in aktlocalswitches) and (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  1386. cg.g_stackcheck(list,stackframe);
  1387. end;
  1388. end;
  1389. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  1390. var
  1391. stacksize,
  1392. retsize : longint;
  1393. begin
  1394. {$ifndef powerpc}
  1395. { remove stackframe }
  1396. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1397. begin
  1398. stacksize:=current_procinfo.calc_stackframe_size;
  1399. if (stacksize<>0) then
  1400. cg.a_op_const_reg(list,OP_ADD,OS_32,stacksize,current_procinfo.framepointer);
  1401. end
  1402. else
  1403. cg.g_restore_frame_pointer(list);
  1404. {$endif}
  1405. { at last, the return is generated }
  1406. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1407. cg.g_interrupt_stackframe_exit(list,usesacc,usesacchi)
  1408. else
  1409. begin
  1410. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1411. begin
  1412. retsize:=0;
  1413. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1414. inc(retsize,POINTER_SIZE);
  1415. end
  1416. else
  1417. retsize:=current_procinfo.para_stack_size;
  1418. cg.g_return_from_proc(list,retsize);
  1419. end;
  1420. end;
  1421. procedure gen_save_used_regs(list:TAAsmoutput);
  1422. begin
  1423. { Pure assembler routines need to save the registers themselves }
  1424. if (po_assembler in current_procinfo.procdef.procoptions) then
  1425. exit;
  1426. { for the save all registers we can simply use a pusha,popa which
  1427. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1428. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1429. cg.g_save_all_registers(list)
  1430. else
  1431. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1432. cg.g_save_standard_registers(list);
  1433. end;
  1434. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  1435. begin
  1436. { Pure assembler routines need to save the registers themselves }
  1437. if (po_assembler in current_procinfo.procdef.procoptions) then
  1438. exit;
  1439. { for the save all registers we can simply use a pusha,popa which
  1440. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1441. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1442. cg.g_restore_all_registers(list,usesacc,usesacchi)
  1443. else
  1444. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1445. cg.g_restore_standard_registers(list);
  1446. end;
  1447. {****************************************************************************
  1448. Inlining
  1449. ****************************************************************************}
  1450. (*
  1451. procedure load_inlined_return_value(list:TAAsmoutput);
  1452. var
  1453. ressym: tvarsym;
  1454. resloc: tlocation;
  1455. r,r2 : tregister;
  1456. begin
  1457. if not is_void(current_procinfo.procdef.rettype.def) then
  1458. begin
  1459. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  1460. if ressym.reg.enum <> R_NO then
  1461. begin
  1462. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1463. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  1464. else
  1465. if ressym.vartype.def.deftype = floatdef then
  1466. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  1467. else
  1468. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1469. resloc.register := ressym.reg;
  1470. end
  1471. else
  1472. begin
  1473. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1474. reference_reset_base(resloc.reference,current_procinfo.framepointer,tvarsym(current_procinfo.procdef.funcretsym).adjusted_address);
  1475. end;
  1476. { Here, we return the function result. In most architectures, the value is
  1477. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1478. function returns in a register and the caller receives it in an other one }
  1479. case current_procinfo.procdef.rettype.def.deftype of
  1480. orddef,
  1481. enumdef :
  1482. begin
  1483. {$ifndef cpu64bit}
  1484. if resloc.size in [OS_64,OS_S64] then
  1485. begin
  1486. r:=cg.getregisterint(list,OS_INT);
  1487. r2:=cg.getregisterint(list,OS_INT);
  1488. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1489. end
  1490. else
  1491. {$endif cpu64bit}
  1492. begin
  1493. r:=cg.getregisterint(list,resloc.size);
  1494. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1495. end;
  1496. end;
  1497. floatdef :
  1498. begin
  1499. {$ifdef cpufpemu}
  1500. if cs_fp_emulation in aktmoduleswitches then
  1501. r.enum := FUNCTION_RETURN_REG
  1502. else
  1503. {$endif cpufpemu}
  1504. r.enum:=FPU_RESULT_REG;
  1505. cg.a_loadfpu_loc_reg(list,resloc,r);
  1506. end;
  1507. else
  1508. begin
  1509. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1510. begin
  1511. {$ifndef cpu64bit}
  1512. { Win32 can return records in EAX:EDX }
  1513. if resloc.size in [OS_64,OS_S64] then
  1514. begin
  1515. r:=cg.getregisterint(list,OS_INT);
  1516. r2:=cg.getregisterint(list,OS_INT);
  1517. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1518. end
  1519. else
  1520. {$endif cpu64bit}
  1521. begin
  1522. r:=cg.getregisterint(list,resloc.size);
  1523. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1524. end;
  1525. end
  1526. end;
  1527. end;
  1528. end;
  1529. end;
  1530. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  1531. begin
  1532. { initialize return value }
  1533. initretvalue(list);
  1534. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1535. { initialisizes temp. ansi/wide string data }
  1536. inittempvariables(list);
  1537. { initialize ansi/widesstring para's }
  1538. if assigned(current_procinfo.procdef.parast) then
  1539. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1540. { generate copies of call by value parameters }
  1541. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1542. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1543. load_regvars(list,nil);
  1544. end;
  1545. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  1546. var
  1547. usesacc,
  1548. usesacchi,
  1549. usesfpu : boolean;
  1550. begin
  1551. if aktexitlabel.is_used then
  1552. cg.a_label(list,aktexitlabel);
  1553. cleanup_regvars(list);
  1554. { finalize temporary data }
  1555. finalizetempvariables(list);
  1556. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1557. { finalize paras data }
  1558. if assigned(current_procinfo.procdef.parast) then
  1559. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1560. { handle return value, this is not done for assembler routines when
  1561. they didn't reference the result variable }
  1562. if not(po_assembler in current_procinfo.procdef.procoptions) or
  1563. (assigned(current_procinfo.procdef.funcretsym) and
  1564. (tvarsym(current_procinfo.procdef.funcretsym).refcount>1)) then
  1565. begin
  1566. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1567. internalerror(200305263);
  1568. // load_inlined_return_value(list);
  1569. load_return_value(list,usesacc,usesacchi,usesfpu)
  1570. end;
  1571. cleanup_regvars(list);
  1572. end;
  1573. *)
  1574. {****************************************************************************
  1575. Const Data
  1576. ****************************************************************************}
  1577. procedure insertconstdata(sym : ttypedconstsym);
  1578. { this does not affect the local stack space, since all
  1579. typed constansts and initialized variables are always
  1580. put in the .data / .rodata section
  1581. }
  1582. var
  1583. storefilepos : tfileposinfo;
  1584. curconstsegment : taasmoutput;
  1585. l : longint;
  1586. begin
  1587. storefilepos:=aktfilepos;
  1588. aktfilepos:=sym.fileinfo;
  1589. if sym.is_writable then
  1590. curconstsegment:=datasegment
  1591. else
  1592. curconstsegment:=consts;
  1593. l:=sym.getsize;
  1594. { insert cut for smartlinking or alignment }
  1595. if (cs_create_smart in aktmoduleswitches) then
  1596. curconstSegment.concat(Tai_cut.Create);
  1597. curconstSegment.concat(Tai_align.create(const_align(l)));
  1598. {$ifdef GDB}
  1599. if cs_debuginfo in aktmoduleswitches then
  1600. sym.concatstabto(curconstsegment);
  1601. {$endif GDB}
  1602. if (sym.owner.symtabletype=globalsymtable) or
  1603. (cs_create_smart in aktmoduleswitches) or
  1604. (assigned(current_procinfo) and
  1605. (current_procinfo.procdef.proccalloption=pocall_inline)) or
  1606. DLLSource then
  1607. curconstSegment.concat(Tai_symbol.Createdataname_global(sym.mangledname,l))
  1608. else
  1609. curconstSegment.concat(Tai_symbol.Createdataname(sym.mangledname,l));
  1610. aktfilepos:=storefilepos;
  1611. end;
  1612. procedure insertbssdata(sym : tvarsym);
  1613. var
  1614. l,varalign : longint;
  1615. storefilepos : tfileposinfo;
  1616. begin
  1617. storefilepos:=aktfilepos;
  1618. aktfilepos:=sym.fileinfo;
  1619. l:=sym.getvaluesize;
  1620. if (vo_is_thread_var in sym.varoptions) then
  1621. inc(l,pointer_size);
  1622. varalign:=var_align(l);
  1623. {
  1624. sym.address:=align(datasize,varalign);
  1625. datasize:=tvarsym(sym).address+l;
  1626. }
  1627. { insert cut for smartlinking or alignment }
  1628. if (cs_create_smart in aktmoduleswitches) then
  1629. bssSegment.concat(Tai_cut.Create);
  1630. bssSegment.concat(Tai_align.create(varalign));
  1631. {$ifdef GDB}
  1632. if cs_debuginfo in aktmoduleswitches then
  1633. sym.concatstabto(bsssegment);
  1634. {$endif GDB}
  1635. if (sym.owner.symtabletype=globalsymtable) or
  1636. (cs_create_smart in aktmoduleswitches) or
  1637. DLLSource or
  1638. (assigned(current_procinfo) and
  1639. (current_procinfo.procdef.proccalloption=pocall_inline)) or
  1640. (vo_is_exported in sym.varoptions) or
  1641. (vo_is_C_var in sym.varoptions) then
  1642. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1643. else
  1644. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1645. aktfilepos:=storefilepos;
  1646. end;
  1647. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  1648. var
  1649. sym : tsym;
  1650. begin
  1651. sym:=tsym(st.symindex.first);
  1652. while assigned(sym) do
  1653. begin
  1654. { Only allocate space for referenced locals }
  1655. if (sym.typ=varsym) and
  1656. (tvarsym(sym).refs>0) then
  1657. begin
  1658. with tvarsym(sym) do
  1659. begin
  1660. {$warning TODO Add support for register variables}
  1661. localloc.loc:=LOC_REFERENCE;
  1662. tg.GetLocal(list,getvaluesize,localloc.reference);
  1663. if cs_asm_source in aktglobalswitches then
  1664. list.concat(Tai_comment.Create(strpnew('Local '+realname+' located at '+
  1665. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1666. end;
  1667. end;
  1668. sym:=tsym(sym.indexnext);
  1669. end;
  1670. end;
  1671. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  1672. var
  1673. sym : tsym;
  1674. begin
  1675. sym:=tsym(st.symindex.first);
  1676. while assigned(sym) do
  1677. begin
  1678. if (sym.typ=varsym) and
  1679. (tvarsym(sym).refs>0) then
  1680. begin
  1681. with tvarsym(sym) do
  1682. begin
  1683. { Note: We need to keep the data available in memory
  1684. for the sub procedures that can access local data
  1685. in the parent procedures }
  1686. case localloc.loc of
  1687. LOC_REFERENCE :
  1688. tg.Ungetlocal(list,localloc.reference);
  1689. LOC_REGISTER :
  1690. begin
  1691. {$ifndef cpu64bit}
  1692. if localloc.size in [OS_64,OS_S64] then
  1693. begin
  1694. cg.ungetregister(list,localloc.registerlow);
  1695. cg.ungetregister(list,localloc.registerhigh);
  1696. end
  1697. else
  1698. {$endif cpu64bit}
  1699. cg.ungetregister(list,localloc.register);
  1700. end;
  1701. end;
  1702. end;
  1703. end;
  1704. sym:=tsym(sym.indexnext);
  1705. end;
  1706. end;
  1707. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  1708. var
  1709. sym : tsym;
  1710. begin
  1711. sym:=tsym(st.symindex.first);
  1712. while assigned(sym) do
  1713. begin
  1714. if sym.typ=varsym then
  1715. begin
  1716. with tvarsym(sym) do
  1717. begin
  1718. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1719. begin
  1720. case paraitem.paraloc[calleeside].loc of
  1721. LOC_FPUREGISTER,
  1722. LOC_REGISTER:
  1723. begin
  1724. (*
  1725. if paraitem.paraloc[calleeside].register=NR_NO then
  1726. begin
  1727. paraitem.paraloc[calleeside].loc:=LOC_REGISTER;
  1728. paraitem.paraloc[calleeside].size:=paraitem.paraloc[calleeside].size;
  1729. {$ifndef cpu64bit}
  1730. if paraitem.paraloc[calleeside].size in [OS_64,OS_S64] then
  1731. begin
  1732. paraitem.paraloc[calleeside].registerlow:=cg.getregisterint(list,OS_32);
  1733. paraitem.paraloc[calleeside].registerhigh:=cg.getregisterint(list,OS_32);
  1734. end
  1735. else
  1736. {$endif cpu64bit}
  1737. paraitem.paraloc[calleeside].register:=cg.getregisterint(list,localloc.size);
  1738. end;
  1739. *)
  1740. (*
  1741. {$warning TODO Allocate register paras}
  1742. localloc.loc:=LOC_REGISTER;
  1743. localloc.size:=paraitem.paraloc[calleeside].size;
  1744. {$ifndef cpu64bit}
  1745. if localloc.size in [OS_64,OS_S64] then
  1746. begin
  1747. localloc.registerlow:=cg.getregisterint(list,OS_32);
  1748. localloc.registerhigh:=cg.getregisterint(list,OS_32);
  1749. end
  1750. else
  1751. {$endif cpu64bit}
  1752. localloc.register:=cg.getregisterint(list,localloc.size);
  1753. *)
  1754. localloc.loc:=LOC_REFERENCE;
  1755. localloc.size:=paraitem.paraloc[calleeside].size;
  1756. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1757. end
  1758. else
  1759. localloc:=paraitem.paraloc[calleeside];
  1760. end;
  1761. end
  1762. else
  1763. localloc:=paraitem.paraloc[calleeside];
  1764. if cs_asm_source in aktglobalswitches then
  1765. begin
  1766. case localloc.loc of
  1767. LOC_REFERENCE :
  1768. list.concat(Tai_comment.Create(strpnew('Para '+realname+' located at '+
  1769. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1770. end;
  1771. end;
  1772. end;
  1773. end;
  1774. sym:=tsym(sym.indexnext);
  1775. end;
  1776. end;
  1777. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  1778. var
  1779. sym : tsym;
  1780. begin
  1781. sym:=tsym(st.symindex.first);
  1782. while assigned(sym) do
  1783. begin
  1784. if sym.typ=varsym then
  1785. begin
  1786. with tvarsym(sym) do
  1787. begin
  1788. {$warning TODO Allocate register paras}
  1789. localloc.loc:=LOC_REFERENCE;
  1790. localloc.size:=int_cgsize(paramanager.push_size(vs_value,vartype.def,pocall_inline));
  1791. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1792. if cs_asm_source in aktglobalswitches then
  1793. begin
  1794. case localloc.loc of
  1795. LOC_REFERENCE :
  1796. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  1797. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1798. end;
  1799. end;
  1800. paraitem.paraloc[callerside]:=localloc;
  1801. paraitem.paraloc[calleeside]:=localloc;
  1802. end;
  1803. end;
  1804. sym:=tsym(sym.indexnext);
  1805. end;
  1806. end;
  1807. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  1808. var
  1809. sym : tsym;
  1810. begin
  1811. sym:=tsym(st.symindex.first);
  1812. while assigned(sym) do
  1813. begin
  1814. if sym.typ=varsym then
  1815. begin
  1816. with tvarsym(sym) do
  1817. begin
  1818. { Note: We need to keep the data available in memory
  1819. for the sub procedures that can access local data
  1820. in the parent procedures }
  1821. case localloc.loc of
  1822. LOC_REFERENCE :
  1823. tg.UngetLocal(list,localloc.reference);
  1824. LOC_REGISTER :
  1825. begin
  1826. if localloc.register<>paraitem.paraloc[calleeside].register then
  1827. begin
  1828. {$ifndef cpu64bit}
  1829. if localloc.size in [OS_64,OS_S64] then
  1830. begin
  1831. cg.ungetregister(list,localloc.registerlow);
  1832. cg.ungetregister(list,localloc.registerhigh);
  1833. end
  1834. else
  1835. {$endif cpu64bit}
  1836. cg.ungetregister(list,localloc.register);
  1837. end;
  1838. end;
  1839. end;
  1840. end;
  1841. end;
  1842. sym:=tsym(sym.indexnext);
  1843. end;
  1844. end;
  1845. end.
  1846. {
  1847. $Log$
  1848. Revision 1.173 2003-12-06 01:15:22 florian
  1849. * reverted Peter's alloctemp patch; hopefully properly
  1850. Revision 1.172 2003/12/03 23:13:20 peter
  1851. * delayed paraloc allocation, a_param_*() gets extra parameter
  1852. if it needs to allocate temp or real paralocation
  1853. * optimized/simplified int-real loading
  1854. Revision 1.171 2003/11/30 19:35:29 florian
  1855. * fixed several arm related problems
  1856. Revision 1.170 2003/11/29 20:13:25 florian
  1857. * fixed several pi_do_call problems
  1858. Revision 1.169 2003/11/23 17:05:15 peter
  1859. * register calling is left-right
  1860. * parameter ordering
  1861. * left-right calling inserts result parameter last
  1862. Revision 1.168 2003/11/22 00:31:25 jonas
  1863. + extra allocations of function result regs for the optimiser
  1864. Revision 1.167 2003/11/11 21:10:12 peter
  1865. * remove temporary stdcall hack
  1866. Revision 1.166 2003/11/10 22:02:52 peter
  1867. * cross unit inlining fixed
  1868. Revision 1.165 2003/11/07 15:58:32 florian
  1869. * Florian's culmutative nr. 1; contains:
  1870. - invalid calling conventions for a certain cpu are rejected
  1871. - arm softfloat calling conventions
  1872. - -Sp for cpu dependend code generation
  1873. - several arm fixes
  1874. - remaining code for value open array paras on heap
  1875. Revision 1.164 2003/11/04 19:03:50 peter
  1876. * fixes for temp type patch
  1877. Revision 1.163 2003/11/04 15:35:13 peter
  1878. * fix for referencecounted temps
  1879. Revision 1.162 2003/10/25 11:34:02 florian
  1880. * fixed compilation of ppc system unit
  1881. Revision 1.161 2003/10/19 01:34:30 florian
  1882. * some ppc stuff fixed
  1883. * memory leak fixed
  1884. Revision 1.160 2003/10/17 15:08:34 peter
  1885. * commented out more obsolete constants
  1886. Revision 1.159 2003/10/17 14:38:32 peter
  1887. * 64k registers supported
  1888. * fixed some memory leaks
  1889. Revision 1.158 2003/10/10 17:48:13 peter
  1890. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1891. * tregisteralloctor renamed to trgobj
  1892. * removed rgobj from a lot of units
  1893. * moved location_* and reference_* to cgobj
  1894. * first things for mmx register allocation
  1895. Revision 1.157 2003/10/09 21:31:37 daniel
  1896. * Register allocator splitted, ans abstract now
  1897. Revision 1.156 2003/10/07 18:18:16 peter
  1898. * fix register calling for assembler procedures
  1899. * fix result loading for assembler procedures
  1900. Revision 1.155 2003/10/07 15:17:07 peter
  1901. * inline supported again, LOC_REFERENCEs are used to pass the
  1902. parameters
  1903. * inlineparasymtable,inlinelocalsymtable removed
  1904. * exitlabel inserting fixed
  1905. Revision 1.154 2003/10/01 20:34:48 peter
  1906. * procinfo unit contains tprocinfo
  1907. * cginfo renamed to cgbase
  1908. * moved cgmessage to verbose
  1909. * fixed ppc and sparc compiles
  1910. Revision 1.153 2003/09/30 21:02:37 peter
  1911. * updates for inlining
  1912. Revision 1.152 2003/09/29 20:58:56 peter
  1913. * optimized releasing of registers
  1914. Revision 1.151 2003/09/28 21:47:18 peter
  1915. * register paras and local copies updates
  1916. Revision 1.150 2003/09/28 17:55:03 peter
  1917. * parent framepointer changed to hidden parameter
  1918. * tloadparentfpnode added
  1919. Revision 1.149 2003/09/28 13:39:38 peter
  1920. * optimized releasing of registers
  1921. Revision 1.148 2003/09/25 21:28:00 peter
  1922. * parameter fixes
  1923. Revision 1.147 2003/09/23 21:03:59 peter
  1924. * check for refs>0 in init/final local data
  1925. Revision 1.146 2003/09/23 17:56:05 peter
  1926. * locals and paras are allocated in the code generation
  1927. * tvarsym.localloc contains the location of para/local when
  1928. generating code for the current procedure
  1929. Revision 1.145 2003/09/16 16:17:01 peter
  1930. * varspez in calls to push_addr_param
  1931. Revision 1.144 2003/09/14 21:33:37 peter
  1932. * location_force_reg gives IE when size=OS_NO
  1933. Revision 1.143 2003/09/14 19:18:10 peter
  1934. * remove obsolete code already in comments
  1935. Revision 1.142 2003/09/11 11:54:59 florian
  1936. * improved arm code generation
  1937. * move some protected and private field around
  1938. * the temp. register for register parameters/arguments are now released
  1939. before the move to the parameter register is done. This improves
  1940. the code in a lot of cases.
  1941. Revision 1.141 2003/09/10 08:31:47 marco
  1942. * Patch from Peter for paraloc
  1943. Revision 1.140 2003/09/07 22:09:35 peter
  1944. * preparations for different default calling conventions
  1945. * various RA fixes
  1946. Revision 1.139 2003/09/03 15:55:01 peter
  1947. * NEWRA branch merged
  1948. Revision 1.138 2003/09/03 11:18:37 florian
  1949. * fixed arm concatcopy
  1950. + arm support in the common compiler sources added
  1951. * moved some generic cg code around
  1952. + tfputype added
  1953. * ...
  1954. Revision 1.137.2.4 2003/08/31 15:46:26 peter
  1955. * more updates for tregister
  1956. Revision 1.137.2.3 2003/08/29 17:28:59 peter
  1957. * next batch of updates
  1958. Revision 1.137.2.2 2003/08/28 18:35:08 peter
  1959. * tregister changed to cardinal
  1960. Revision 1.137.2.1 2003/08/27 19:55:54 peter
  1961. * first tregister patch
  1962. Revision 1.137 2003/08/20 20:29:06 daniel
  1963. * Some more R_NO changes
  1964. * Preventive code to loadref added
  1965. Revision 1.136 2003/08/20 17:48:49 peter
  1966. * fixed stackalloc to not allocate localst.datasize twice
  1967. * order of stackalloc code fixed for implicit init/final
  1968. Revision 1.135 2003/08/17 16:59:20 jonas
  1969. * fixed regvars so they work with newra (at least for ppc)
  1970. * fixed some volatile register bugs
  1971. + -dnotranslation option for -dnewra, which causes the registers not to
  1972. be translated from virtual to normal registers. Requires support in
  1973. the assembler writer as well, which is only implemented in aggas/
  1974. agppcgas currently
  1975. Revision 1.134 2003/08/11 21:18:20 peter
  1976. * start of sparc support for newra
  1977. Revision 1.133 2003/08/09 18:56:54 daniel
  1978. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1979. allocator
  1980. * Some preventive changes to i386 spillinh code
  1981. Revision 1.132 2003/08/03 14:09:50 daniel
  1982. * Fixed a register allocator bug
  1983. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1984. statements: changes in location_force. These moves are now no longer
  1985. constrained so they are optimized away.
  1986. Revision 1.131 2003/07/23 11:04:15 jonas
  1987. * split en_exit_code into a part that may allocate a register and a part
  1988. that doesn't, so the former can be done before the register colouring
  1989. has been performed
  1990. Revision 1.130 2003/07/06 17:58:22 peter
  1991. * framepointer fixes for sparc
  1992. * parent framepointer code more generic
  1993. Revision 1.129 2003/07/06 15:31:20 daniel
  1994. * Fixed register allocator. *Lots* of fixes.
  1995. Revision 1.128 2003/07/02 22:18:04 peter
  1996. * paraloc splitted in callerparaloc,calleeparaloc
  1997. * sparc calling convention updates
  1998. Revision 1.127 2003/06/17 18:13:51 jonas
  1999. * fixed -dnewra compilation problems
  2000. Revision 1.126 2003/06/17 16:32:44 peter
  2001. * alloc register for function result
  2002. Revision 1.125 2003/06/13 21:19:30 peter
  2003. * current_procdef removed, use current_procinfo.procdef instead
  2004. Revision 1.124 2003/06/09 12:23:30 peter
  2005. * init/final of procedure data splitted from genentrycode
  2006. * use asmnode getposition to insert final at the correct position
  2007. als for the implicit try...finally
  2008. Revision 1.123 2003/06/07 18:57:04 jonas
  2009. + added freeintparaloc
  2010. * ppc get/freeintparaloc now check whether the parameter regs are
  2011. properly allocated/deallocated (and get an extra list para)
  2012. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  2013. * fixed lot of missing pi_do_call's
  2014. Revision 1.122 2003/06/06 14:43:02 peter
  2015. * g_copyopenarrayvalue gets length reference
  2016. * don't copy open arrays for cdecl
  2017. Revision 1.121 2003/06/03 21:11:09 peter
  2018. * cg.a_load_* get a from and to size specifier
  2019. * makeregsize only accepts newregister
  2020. * i386 uses generic tcgnotnode,tcgunaryminus
  2021. Revision 1.120 2003/06/03 15:49:49 jonas
  2022. * fixed ref/loc problems
  2023. Revision 1.119 2003/06/03 15:06:37 daniel
  2024. * fixed conflict marks
  2025. Revision 1.118 2003/06/03 13:01:59 daniel
  2026. * Register allocator finished
  2027. Revision 1.117 2003/06/02 21:42:05 jonas
  2028. * function results can now also be regvars
  2029. - removed tprocinfo.return_offset, never use it again since it's invalid
  2030. if the result is a regvar
  2031. Revision 1.116 2003/06/01 21:38:06 peter
  2032. * getregisterfpu size parameter added
  2033. * op_const_reg size parameter added
  2034. * sparc updates
  2035. Revision 1.115 2003/05/31 20:28:17 jonas
  2036. * changed copyvalueparas so it also supports register parameters
  2037. (except for copy_value_openarray, but that one is seriously broken
  2038. anyway, since it expects that the high parameter will always be in
  2039. memory right after the pointer to the array, while it could just as
  2040. will be in a register)
  2041. Revision 1.114 2003/05/31 15:05:28 peter
  2042. * FUNCTION_RESULT64_LOW/HIGH_REG added for int64 results
  2043. Revision 1.113 2003/05/31 00:48:15 jonas
  2044. * fixed my previous commit
  2045. Revision 1.112 2003/05/30 23:57:08 peter
  2046. * more sparc cleanup
  2047. * accumulator removed, splitted in function_return_reg (called) and
  2048. function_result_reg (caller)
  2049. Revision 1.111 2003/05/30 23:49:18 jonas
  2050. * a_load_loc_reg now has an extra size parameter for the destination
  2051. register (properly fixes what I worked around in revision 1.106 of
  2052. ncgutil.pas)
  2053. Revision 1.110 2003/05/30 18:52:10 jonas
  2054. * fixed bug with intregvars
  2055. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2056. rcgppc.a_param_ref, which previously got bogus size values
  2057. Revision 1.109 2003/05/27 21:19:08 jonas
  2058. * fixed ppc cycle
  2059. Revision 1.108 2003/05/27 14:28:14 jonas
  2060. * patch from Peter for nested procedures
  2061. Revision 1.107 2003/05/26 21:17:17 peter
  2062. * procinlinenode removed
  2063. * aktexit2label removed, fast exit removed
  2064. + tcallnode.inlined_pass_2 added
  2065. Revision 1.106 2003/05/24 11:59:42 jonas
  2066. * fixed integer typeconversion problems
  2067. Revision 1.105 2003/05/23 14:27:35 peter
  2068. * remove some unit dependencies
  2069. * current_procinfo changes to store more info
  2070. Revision 1.104 2003/05/15 18:58:53 peter
  2071. * removed selfpointer_offset, vmtpointer_offset
  2072. * tvarsym.adjusted_address
  2073. * address in localsymtable is now in the real direction
  2074. * removed some obsolete globals
  2075. Revision 1.103 2003/05/14 19:37:25 jonas
  2076. * patch from Peter for int64 function results
  2077. Revision 1.102 2003/05/13 19:14:41 peter
  2078. * failn removed
  2079. * inherited result code check moven to pexpr
  2080. Revision 1.101 2003/05/13 15:16:13 peter
  2081. * removed ret_in_acc, it's the reverse of ret_in_param
  2082. * fixed ret_in_param for win32 cdecl array
  2083. Revision 1.100 2003/05/12 08:08:27 jonas
  2084. * fixed several initialization and finalization related bugs (missing
  2085. tg.direction's, wrong paralocation for decreasing refcount of
  2086. everything but ansistrings)
  2087. Revision 1.99 2003/05/11 21:37:03 peter
  2088. * moved implicit exception frame from ncgutil to psub
  2089. * constructor/destructor helpers moved from cobj/ncgutil to psub
  2090. Revision 1.98 2003/05/11 14:45:12 peter
  2091. * tloadnode does not support objectsymtable,withsymtable anymore
  2092. * withnode cleanup
  2093. * direct with rewritten to use temprefnode
  2094. Revision 1.97 2003/05/10 13:20:23 jonas
  2095. * moved storing of register parameters to memory even earlier in the
  2096. entry code to fix problems with constructors
  2097. Revision 1.96 2003/05/09 17:47:02 peter
  2098. * self moved to hidden parameter
  2099. * removed hdisposen,hnewn,selfn
  2100. Revision 1.95 2003/04/29 07:28:52 michael
  2101. + Patch from peter to fix wrong pushing of ansistring function results in open array
  2102. Revision 1.94 2003/04/28 21:17:38 peter
  2103. * do not finalize function results
  2104. Revision 1.93 2003/04/27 16:30:34 jonas
  2105. * store register para's to memory before copying the valuepara's, because
  2106. that one requires them to be there already (and it calls subroutines ->
  2107. could overwrite those registers)
  2108. Revision 1.92 2003/04/27 11:21:33 peter
  2109. * aktprocdef renamed to current_procinfo.procdef
  2110. * procinfo renamed to current_procinfo
  2111. * procinfo will now be stored in current_module so it can be
  2112. cleaned up properly
  2113. * gen_main_procsym changed to create_main_proc and release_main_proc
  2114. to also generate a tprocinfo structure
  2115. * fixed unit implicit initfinal
  2116. Revision 1.91 2003/04/27 07:29:50 peter
  2117. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  2118. a new procdef declaration
  2119. * aktprocsym removed
  2120. * lexlevel removed, use symtable.symtablelevel instead
  2121. * implicit init/final code uses the normal genentry/genexit
  2122. * funcret state checking updated for new funcret handling
  2123. Revision 1.90 2003/04/26 17:21:08 florian
  2124. * fixed passing of fpu values by fpu register
  2125. Revision 1.89 2003/04/25 20:59:33 peter
  2126. * removed funcretn,funcretsym, function result is now in varsym
  2127. and aliases for result and function name are added using absolutesym
  2128. * vs_hidden parameter for funcret passed in parameter
  2129. * vs_hidden fixes
  2130. * writenode changed to printnode and released from extdebug
  2131. * -vp option added to generate a tree.log with the nodetree
  2132. * nicer printnode for statements, callnode
  2133. Revision 1.88 2003/04/23 12:35:34 florian
  2134. * fixed several issues with powerpc
  2135. + applied a patch from Jonas for nested function calls (PowerPC only)
  2136. * ...
  2137. Revision 1.87 2003/04/22 14:33:38 peter
  2138. * removed some notes/hints
  2139. Revision 1.86 2003/04/22 13:47:08 peter
  2140. * fixed C style array of const
  2141. * fixed C array passing
  2142. * fixed left to right with high parameters
  2143. Revision 1.85 2003/04/22 10:09:35 daniel
  2144. + Implemented the actual register allocator
  2145. + Scratch registers unavailable when new register allocator used
  2146. + maybe_save/maybe_restore unavailable when new register allocator used
  2147. Revision 1.84 2003/04/16 09:26:55 jonas
  2148. * assembler procedures now again get a stackframe if they have local
  2149. variables. No space is reserved for a function result however.
  2150. Also, the register parameters aren't automatically saved on the stack
  2151. anymore in assembler procedures.
  2152. Revision 1.83 2003/04/06 21:11:23 olle
  2153. * changed newasmsymbol to newasmsymboldata for data symbols
  2154. Revision 1.82 2003/03/30 20:59:07 peter
  2155. * fix classmethod from classmethod call
  2156. * move BeforeDestruction/AfterConstruction calls to
  2157. genentrycode/genexitcode instead of generating them on the fly
  2158. after a call to a constructor
  2159. Revision 1.81 2003/03/28 19:16:56 peter
  2160. * generic constructor working for i386
  2161. * remove fixed self register
  2162. * esi added as address register for i386
  2163. Revision 1.80 2003/03/17 15:52:20 peter
  2164. * fix range error
  2165. Revision 1.79 2003/03/11 21:46:24 jonas
  2166. * lots of new regallocator fixes, both in generic and ppc-specific code
  2167. (ppc compiler still can't compile the linux system unit though)
  2168. Revision 1.78 2003/02/26 21:15:43 daniel
  2169. * Fixed the optimizer
  2170. Revision 1.77 2003/02/19 22:00:14 daniel
  2171. * Code generator converted to new register notation
  2172. - Horribily outdated todo.txt removed
  2173. Revision 1.76 2003/02/15 22:17:38 carl
  2174. * bugfix of FPU emulation code
  2175. Revision 1.75 2003/01/09 22:00:53 florian
  2176. * fixed some PowerPC issues
  2177. Revision 1.74 2003/01/09 20:41:10 florian
  2178. * fixed broken PowerPC compiler
  2179. Revision 1.73 2003/01/08 18:43:56 daniel
  2180. * Tregister changed into a record
  2181. Revision 1.72 2002/12/29 23:51:43 florian
  2182. * web bug 2214 fixed: ie 10 in const array constructors
  2183. Revision 1.71 2002/12/24 15:56:50 peter
  2184. * stackpointer_alloc added for adjusting ESP. Win32 needs
  2185. this for the pageprotection
  2186. Revision 1.70 2002/12/05 14:39:21 florian
  2187. * added missing then, Carl did you really a make fullcycle :) ?
  2188. Revision 1.69 2002/12/03 22:13:39 carl
  2189. * bugfix of problems with profile code which clobbers some registers
  2190. Revision 1.68 2002/12/01 22:06:59 carl
  2191. * warning of portabilitiy problems with parasize / localsize
  2192. Revision 1.67 2002/11/30 18:44:57 carl
  2193. + profiling support for Win32
  2194. Revision 1.66 2002/11/30 14:39:15 carl
  2195. * try to fix profiling for win32
  2196. Revision 1.65 2002/11/28 23:28:14 florian
  2197. * push_value_para didn't release floatdef locations, fixes tw2045
  2198. Revision 1.64 2002/11/27 02:33:19 peter
  2199. * copy_value_on_stack method added for cdecl record passing
  2200. Revision 1.63 2002/11/25 17:43:18 peter
  2201. * splitted defbase in defutil,symutil,defcmp
  2202. * merged isconvertable and is_equal into compare_defs(_ext)
  2203. * made operator search faster by walking the list only once
  2204. Revision 1.62 2002/11/18 17:31:55 peter
  2205. * pass proccalloption to ret_in_xxx and push_xxx functions
  2206. Revision 1.61 2002/11/17 17:49:08 mazen
  2207. + 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
  2208. Revision 1.60 2002/11/17 16:31:56 carl
  2209. * memory optimization (3-4%) : cleanup of tai fields,
  2210. cleanup of tdef and tsym fields.
  2211. * make it work for m68k
  2212. Revision 1.59 2002/11/15 01:58:51 peter
  2213. * merged changes from 1.0.7 up to 04-11
  2214. - -V option for generating bug report tracing
  2215. - more tracing for option parsing
  2216. - errors for cdecl and high()
  2217. - win32 import stabs
  2218. - win32 records<=8 are returned in eax:edx (turned off by default)
  2219. - heaptrc update
  2220. - more info for temp management in .s file with EXTDEBUG
  2221. Revision 1.58 2002/11/10 19:07:45 mazen
  2222. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  2223. Revision 1.57 2002/11/03 20:22:40 mazen
  2224. * parameter handling updated
  2225. Revision 1.56 2002/10/16 19:01:43 peter
  2226. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  2227. implicit exception frames for procedures with initialized variables
  2228. and for constructors. The default is on for compatibility
  2229. Revision 1.55 2002/10/14 19:42:33 peter
  2230. * only use init tables for threadvars
  2231. Revision 1.54 2002/10/06 19:41:30 peter
  2232. * Add finalization of typed consts
  2233. * Finalization of globals in the main program
  2234. Revision 1.53 2002/10/05 15:18:42 carl
  2235. * fix heap leaks
  2236. Revision 1.52 2002/09/30 07:00:46 florian
  2237. * fixes to common code to get the alpha compiler compiled applied
  2238. Revision 1.51 2002/09/22 14:02:35 carl
  2239. * stack checking cannot be called before system unit is initialized
  2240. * MC68020 define
  2241. Revision 1.50 2002/09/17 18:54:03 jonas
  2242. * a_load_reg_reg() now has two size parameters: source and dest. This
  2243. allows some optimizations on architectures that don't encode the
  2244. register size in the register name.
  2245. Revision 1.49 2002/09/10 21:48:30 florian
  2246. * improved handling of procedures with register calling conventions
  2247. Revision 1.48 2002/09/07 15:25:03 peter
  2248. * old logs removed and tabs fixed
  2249. Revision 1.47 2002/09/02 18:44:48 peter
  2250. * fixed (not) pushing of empty parameters
  2251. * fixed implicit initialization/finalization generation
  2252. * fixed/optimized local copy of value arguments init/final
  2253. Revision 1.46 2002/09/01 19:27:34 peter
  2254. * use index register when available for generating a reference with
  2255. only a signle register. Using the base register could possibly
  2256. destroy the framepointer
  2257. Revision 1.45 2002/09/01 18:50:20 peter
  2258. * fixed maybe_save that did not support a reference with only
  2259. a index register. It now also updates the location with the new
  2260. base register only
  2261. Revision 1.44 2002/09/01 14:42:41 peter
  2262. * removevaluepara added to fix the stackpointer so restoring of
  2263. saved registers works
  2264. Revision 1.43 2002/08/25 19:25:18 peter
  2265. * sym.insert_in_data removed
  2266. * symtable.insertvardata/insertconstdata added
  2267. * removed insert_in_data call from symtable.insert, it needs to be
  2268. called separatly. This allows to deref the address calculation
  2269. * procedures now calculate the parast addresses after the procedure
  2270. directives are parsed. This fixes the cdecl parast problem
  2271. * push_addr_param has an extra argument that specifies if cdecl is used
  2272. or not
  2273. Revision 1.42 2002/08/24 18:38:26 peter
  2274. * really use tt_noreuse for exception frame buffers
  2275. Revision 1.41 2002/08/23 16:14:49 peter
  2276. * tempgen cleanup
  2277. * tt_noreuse temp type added that will be used in genentrycode
  2278. Revision 1.40 2002/08/18 10:42:37 florian
  2279. * remaining assembler writer bugs fixed, the errors in the
  2280. system unit are inline assembler problems
  2281. Revision 1.39 2002/08/17 09:23:36 florian
  2282. * first part of procinfo rewrite
  2283. Revision 1.38 2002/08/16 14:24:57 carl
  2284. * issameref() to test if two references are the same (then emit no opcodes)
  2285. + ret_in_reg to replace ret_in_acc
  2286. (fix some register allocation bugs at the same time)
  2287. + save_std_register now has an extra parameter which is the
  2288. usedinproc registers
  2289. Revision 1.37 2002/08/15 15:15:55 carl
  2290. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  2291. * more generic nodes for maths
  2292. * several fixes for better m68k support
  2293. Revision 1.36 2002/08/14 19:25:09 carl
  2294. * fix Florian's last commit for m68k compilation
  2295. Revision 1.35 2002/08/13 21:40:56 florian
  2296. * more fixes for ppc calling conventions
  2297. Revision 1.34 2002/08/12 15:08:39 carl
  2298. + stab register indexes for powerpc (moved from gdb to cpubase)
  2299. + tprocessor enumeration moved to cpuinfo
  2300. + linker in target_info is now a class
  2301. * many many updates for m68k (will soon start to compile)
  2302. - removed some ifdef or correct them for correct cpu
  2303. Revision 1.33 2002/08/11 14:32:27 peter
  2304. * renamed current_library to objectlibrary
  2305. Revision 1.32 2002/08/11 13:24:12 peter
  2306. * saving of asmsymbols in ppu supported
  2307. * asmsymbollist global is removed and moved into a new class
  2308. tasmlibrarydata that will hold the info of a .a file which
  2309. corresponds with a single module. Added librarydata to tmodule
  2310. to keep the library info stored for the module. In the future the
  2311. objectfiles will also be stored to the tasmlibrarydata class
  2312. * all getlabel/newasmsymbol and friends are moved to the new class
  2313. Revision 1.31 2002/08/09 19:16:57 carl
  2314. * stack allocation is now done separately (at the end) of genentrycode
  2315. so temps. can be allocated before.
  2316. * fix generic exception handling
  2317. Revision 1.30 2002/08/06 20:55:21 florian
  2318. * first part of ppc calling conventions fix
  2319. Revision 1.29 2002/08/04 19:09:22 carl
  2320. + added generic exception support (still does not work!)
  2321. + more documentation
  2322. Revision 1.28 2002/07/29 21:23:42 florian
  2323. * more fixes for the ppc
  2324. + wrappers for the tcnvnode.first_* stuff introduced
  2325. }