ncgutil.pas 96 KB

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