ncgutil.pas 99 KB

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