ncgutil.pas 101 KB

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