ncgutil.pas 87 KB

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