ncgutil.pas 103 KB

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