cga.pas 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  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 cga;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cpubase,cpuasm,
  23. symconst,symtype,symdef,aasm;
  24. {$define TESTGETTEMP to store const that
  25. are written into temps for later release PM }
  26. function def_opsize(p1:tdef):topsize;
  27. function def2def_opsize(p1,p2:tdef):topsize;
  28. function def_getreg(p1:tdef):tregister;
  29. function makereg8(r:tregister):tregister;
  30. function makereg16(r:tregister):tregister;
  31. function makereg32(r:tregister):tregister;
  32. procedure locflags2reg(var l:tlocation;opsize:topsize);
  33. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  34. procedure emitlab(var l : tasmlabel);
  35. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  36. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  37. procedure emit_none(i : tasmop;s : topsize);
  38. procedure emit_const(i : tasmop;s : topsize;c : longint);
  39. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  40. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  41. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  42. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  43. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  44. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  45. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  46. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  47. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  48. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  49. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  50. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  51. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  52. procedure emitcall(const routine:string);
  53. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  54. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  55. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  56. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  57. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  58. procedure emit_push_loc(const t:tlocation);
  59. procedure emit_push_mem_size(const t: treference; size: longint);
  60. { pushes qword location to the stack }
  61. procedure emit_pushq_loc(const t : tlocation);
  62. procedure release_qword_loc(const t : tlocation);
  63. { remove non regvar registers in loc from regs (in the format }
  64. { pushusedregisters uses) }
  65. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  66. { releases the registers of a location }
  67. procedure release_loc(const t : tlocation);
  68. procedure emit_pushw_loc(const t:tlocation);
  69. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  70. procedure emit_to_mem(var t:tlocation;def:tdef);
  71. procedure emit_to_reg16(var hr:tregister);
  72. procedure emit_to_reg32(var hr:tregister);
  73. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  74. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  75. procedure copyshortstring(const dref,sref : treference;len : byte;
  76. loadref, del_sref: boolean);
  77. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  78. procedure incrstringref(t : tdef;const ref : treference);
  79. procedure decrstringref(t : tdef;const ref : treference);
  80. procedure push_int(l : longint);
  81. procedure emit_push_mem(const ref : treference);
  82. procedure emitpushreferenceaddr(const ref : treference);
  83. procedure incrcomintfref(t: tdef; const ref: treference);
  84. procedure decrcomintfref(t: tdef; const ref: treference);
  85. procedure floatload(t : tfloattype;const ref : treference);
  86. procedure floatstore(t : tfloattype;const ref : treference);
  87. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  88. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  89. procedure maybe_loadself;
  90. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;destreg:Tregister;delloc:boolean);
  91. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  92. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  93. stackframe:longint;
  94. var parasize:longint;var nostackframe:boolean;
  95. inlined : boolean);
  96. procedure genexitcode(alist : TAAsmoutput;parasize:longint;
  97. nostackframe,inlined:boolean);
  98. { if a unit doesn't have a explicit init/final code, }
  99. { we've to generate one, if the units has ansistrings }
  100. { in the interface or implementation }
  101. procedure genimplicitunitfinal(alist : TAAsmoutput);
  102. procedure genimplicitunitinit(alist : TAAsmoutput);
  103. {$ifdef test_dest_loc}
  104. const
  105. { used to avoid temporary assignments }
  106. dest_loc_known : boolean = false;
  107. in_dest_loc : boolean = false;
  108. dest_loc_tree : ptree = nil;
  109. var
  110. dest_loc : tlocation;
  111. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  112. {$endif test_dest_loc}
  113. implementation
  114. uses
  115. {$ifdef delphi}
  116. sysutils,
  117. {$else}
  118. strings,
  119. {$endif}
  120. cutils,cclasses,
  121. globtype,systems,globals,verbose,
  122. fmodule,
  123. symbase,symsym,symtable,types,
  124. tgcpu,temp_gen,cgbase,regvars
  125. {$ifdef GDB}
  126. ,gdb
  127. {$endif}
  128. ;
  129. {$ifndef NOTARGETWIN32}
  130. const
  131. winstackpagesize = 4096;
  132. {$endif}
  133. {*****************************************************************************
  134. Helpers
  135. *****************************************************************************}
  136. function def_opsize(p1:tdef):topsize;
  137. begin
  138. case p1.size of
  139. 1 : def_opsize:=S_B;
  140. 2 : def_opsize:=S_W;
  141. 4 : def_opsize:=S_L;
  142. { I don't know if we need it (FK) }
  143. 8 : def_opsize:=S_L;
  144. else
  145. internalerror(130820001);
  146. end;
  147. end;
  148. function def2def_opsize(p1,p2:tdef):topsize;
  149. var
  150. o1 : topsize;
  151. begin
  152. case p1.size of
  153. 1 : o1:=S_B;
  154. 2 : o1:=S_W;
  155. 4 : o1:=S_L;
  156. { I don't know if we need it (FK) }
  157. 8 : o1:=S_L;
  158. else
  159. internalerror(130820002);
  160. end;
  161. if assigned(p2) then
  162. begin
  163. case p2.size of
  164. 1 : o1:=S_B;
  165. 2 : begin
  166. if o1=S_B then
  167. o1:=S_BW
  168. else
  169. o1:=S_W;
  170. end;
  171. 4,8:
  172. begin
  173. case o1 of
  174. S_B : o1:=S_BL;
  175. S_W : o1:=S_WL;
  176. end;
  177. end;
  178. end;
  179. end;
  180. def2def_opsize:=o1;
  181. end;
  182. function def_getreg(p1:tdef):tregister;
  183. begin
  184. case p1.size of
  185. 1 : def_getreg:=reg32toreg8(getregister32);
  186. 2 : def_getreg:=reg32toreg16(getregister32);
  187. 4 : def_getreg:=getregister32;
  188. else
  189. internalerror(130820003);
  190. end;
  191. end;
  192. function makereg8(r:tregister):tregister;
  193. begin
  194. case r of
  195. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  196. makereg8:=reg32toreg8(r);
  197. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  198. makereg8:=reg16toreg8(r);
  199. R_AL,R_BL,R_CL,R_DL :
  200. makereg8:=r;
  201. end;
  202. end;
  203. function makereg16(r:tregister):tregister;
  204. begin
  205. case r of
  206. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  207. makereg16:=reg32toreg16(r);
  208. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  209. makereg16:=r;
  210. R_AL,R_BL,R_CL,R_DL :
  211. makereg16:=reg8toreg16(r);
  212. end;
  213. end;
  214. function makereg32(r:tregister):tregister;
  215. begin
  216. case r of
  217. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  218. makereg32:=r;
  219. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  220. makereg32:=reg16toreg32(r);
  221. R_AL,R_BL,R_CL,R_DL :
  222. makereg32:=reg8toreg32(r);
  223. end;
  224. end;
  225. procedure locflags2reg(var l:tlocation;opsize:topsize);
  226. var
  227. hregister : tregister;
  228. begin
  229. if (l.loc=LOC_FLAGS) then
  230. begin
  231. hregister:=getregister32;
  232. case opsize of
  233. S_W : hregister:=reg32toreg16(hregister);
  234. S_B : hregister:=reg32toreg8(hregister);
  235. end;
  236. emit_flag2reg(l.resflags,hregister);
  237. l.loc:=LOC_REGISTER;
  238. l.register:=hregister;
  239. end
  240. else internalerror(270720001);
  241. end;
  242. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  243. var
  244. hregister : tregister;
  245. hl : tasmlabel;
  246. begin
  247. if l.loc = LOC_JUMP then
  248. begin
  249. hregister:=getregister32;
  250. case opsize of
  251. S_W : hregister:=reg32toreg16(hregister);
  252. S_B : hregister:=reg32toreg8(hregister);
  253. end;
  254. l.loc:=LOC_REGISTER;
  255. l.register:=hregister;
  256. emitlab(truelabel);
  257. truelabel:=otl;
  258. emit_const_reg(A_MOV,opsize,1,hregister);
  259. getlabel(hl);
  260. emitjmp(C_None,hl);
  261. emitlab(falselabel);
  262. falselabel:=ofl;
  263. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  264. makereg32(hregister));
  265. emitlab(hl);
  266. end
  267. else internalerror(270720002);
  268. end;
  269. {*****************************************************************************
  270. Emit Assembler
  271. *****************************************************************************}
  272. procedure emitlab(var l : tasmlabel);
  273. begin
  274. if not l.is_set then
  275. exprasmList.concat(Tai_label.Create(l))
  276. else
  277. internalerror(7453984);
  278. end;
  279. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  280. var
  281. ai : taicpu;
  282. begin
  283. if c=C_None then
  284. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  285. else
  286. begin
  287. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  288. ai.SetCondition(c);
  289. end;
  290. ai.is_jmp:=true;
  291. exprasmList.concat(ai);
  292. end;
  293. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  294. var
  295. ai : taicpu;
  296. hreg : tregister;
  297. begin
  298. hreg:=makereg8(hregister);
  299. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  300. ai.SetCondition(flag_2_cond[flag]);
  301. exprasmList.concat(ai);
  302. if hreg<>hregister then
  303. begin
  304. if hregister in regset16bit then
  305. emit_to_reg16(hreg)
  306. else
  307. emit_to_reg32(hreg);
  308. end;
  309. end;
  310. procedure emit_none(i : tasmop;s : topsize);
  311. begin
  312. exprasmList.concat(Taicpu.Op_none(i,s));
  313. end;
  314. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  315. begin
  316. exprasmList.concat(Taicpu.Op_reg(i,s,reg));
  317. end;
  318. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  319. begin
  320. exprasmList.concat(Taicpu.Op_ref(i,s,ref));
  321. end;
  322. procedure emit_const(i : tasmop;s : topsize;c : longint);
  323. begin
  324. exprasmList.concat(Taicpu.Op_const(i,s,c));
  325. end;
  326. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  327. begin
  328. exprasmList.concat(Taicpu.Op_const_reg(i,s,c,reg));
  329. end;
  330. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  331. begin
  332. exprasmList.concat(Taicpu.Op_const_ref(i,s,c,ref));
  333. end;
  334. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  335. begin
  336. exprasmList.concat(Taicpu.Op_ref_reg(i,s,ref,reg));
  337. end;
  338. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  339. begin
  340. exprasmList.concat(Taicpu.Op_reg_ref(i,s,reg,ref));
  341. end;
  342. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  343. begin
  344. if (reg1<>reg2) or (i<>A_MOV) then
  345. exprasmList.concat(Taicpu.Op_reg_reg(i,s,reg1,reg2));
  346. end;
  347. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  348. begin
  349. exprasmList.concat(Taicpu.Op_const_reg_reg(i,s,c,reg1,reg2));
  350. end;
  351. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  352. begin
  353. exprasmList.concat(Taicpu.Op_reg_reg_reg(i,s,reg1,reg2,reg3));
  354. end;
  355. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  356. begin
  357. exprasmList.concat(Taicpu.Op_sym(i,s,op));
  358. end;
  359. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  360. begin
  361. exprasmList.concat(Taicpu.Op_sym_ofs(i,s,op,ofs));
  362. end;
  363. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  364. begin
  365. exprasmList.concat(Taicpu.Op_sym_ofs_reg(i,s,op,ofs,reg));
  366. end;
  367. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  368. begin
  369. exprasmList.concat(Taicpu.Op_sym_ofs_ref(i,s,op,ofs,ref));
  370. end;
  371. procedure emitcall(const routine:string);
  372. begin
  373. exprasmList.concat(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  374. end;
  375. { only usefull in startup code }
  376. procedure emitinsertcall(const routine:string);
  377. begin
  378. exprasmList.insert(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  379. end;
  380. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  381. var
  382. hreg : tregister;
  383. pushedeax : boolean;
  384. begin
  385. pushedeax:=false;
  386. case t.loc of
  387. LOC_REGISTER,
  388. LOC_CREGISTER : begin
  389. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  390. t.register,newreference(ref)));
  391. ungetregister32(t.register); { the register is not needed anymore }
  392. end;
  393. LOC_MEM,
  394. LOC_REFERENCE : begin
  395. if t.reference.is_immediate then
  396. emit_const_ref(A_MOV,siz,
  397. t.reference.offset,newreference(ref))
  398. else
  399. begin
  400. case siz of
  401. S_B : begin
  402. { we can't do a getregister in the code generator }
  403. { without problems!!! }
  404. if usablereg32>0 then
  405. hreg:=reg32toreg8(getregister32)
  406. else
  407. begin
  408. emit_reg(A_PUSH,S_L,R_EAX);
  409. pushedeax:=true;
  410. hreg:=R_AL;
  411. end;
  412. end;
  413. S_W : hreg:=R_DI;
  414. S_L : hreg:=R_EDI;
  415. end;
  416. if hreg in [R_DI,R_EDI] then
  417. getexplicitregister32(R_EDI);
  418. emit_ref_reg(A_MOV,siz,
  419. newreference(t.reference),hreg);
  420. del_reference(t.reference);
  421. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  422. hreg,newreference(ref)));
  423. if siz=S_B then
  424. begin
  425. if pushedeax then
  426. emit_reg(A_POP,S_L,R_EAX)
  427. else
  428. ungetregister(hreg);
  429. end;
  430. if hreg in [R_DI,R_EDI] then
  431. ungetregister32(R_EDI);
  432. { we can release the registers }
  433. { but only AFTER the MOV! Important for the optimizer!
  434. (JM)}
  435. del_reference(ref);
  436. end;
  437. if freetemp then
  438. ungetiftemp(t.reference);
  439. end;
  440. else
  441. internalerror(330);
  442. end;
  443. end;
  444. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  445. begin
  446. case t.loc of
  447. LOC_REGISTER,
  448. LOC_CREGISTER : begin
  449. emit_reg_reg(A_MOV,S_L,t.register,reg);
  450. ungetregister32(t.register); { the register is not needed anymore }
  451. end;
  452. LOC_MEM,
  453. LOC_REFERENCE : begin
  454. if t.reference.is_immediate then
  455. emit_const_reg(A_MOV,S_L,
  456. t.reference.offset,reg)
  457. else
  458. begin
  459. emit_ref_reg(A_MOV,S_L,
  460. newreference(t.reference),reg);
  461. end;
  462. end;
  463. else
  464. internalerror(330);
  465. end;
  466. end;
  467. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  468. begin
  469. case t.loc of
  470. LOC_REGISTER,
  471. LOC_CREGISTER : begin
  472. emit_reg_reg(A_MOV,RegSize(Reg),
  473. reg,t.register);
  474. end;
  475. LOC_MEM,
  476. LOC_REFERENCE : begin
  477. if t.reference.is_immediate then
  478. internalerror(334)
  479. else
  480. begin
  481. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,RegSize(Reg),
  482. Reg,newreference(t.reference)));
  483. end;
  484. end;
  485. else
  486. internalerror(330);
  487. end;
  488. end;
  489. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  490. begin
  491. case t.loc of
  492. LOC_MEM,
  493. LOC_REFERENCE : begin
  494. if t.reference.is_immediate then
  495. internalerror(331)
  496. else
  497. begin
  498. emit_ref_reg(A_LEA,S_L,
  499. newreference(t.reference),reg);
  500. end;
  501. if freetemp then
  502. ungetiftemp(t.reference);
  503. end;
  504. else
  505. internalerror(332);
  506. end;
  507. end;
  508. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  509. begin
  510. case t.loc of
  511. LOC_REGISTER,
  512. LOC_CREGISTER : begin
  513. emit_reg_reg(A_MOV,S_L,
  514. reglow,t.registerlow);
  515. emit_reg_reg(A_MOV,S_L,
  516. reghigh,t.registerhigh);
  517. end;
  518. LOC_MEM,
  519. LOC_REFERENCE : begin
  520. if t.reference.is_immediate then
  521. internalerror(334)
  522. else
  523. begin
  524. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  525. Reglow,newreference(t.reference)));
  526. inc(t.reference.offset,4);
  527. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  528. Reghigh,newreference(t.reference)));
  529. end;
  530. end;
  531. else
  532. internalerror(330);
  533. end;
  534. end;
  535. procedure emit_pushq_loc(const t : tlocation);
  536. var
  537. hr : preference;
  538. begin
  539. case t.loc of
  540. LOC_REGISTER,
  541. LOC_CREGISTER:
  542. begin
  543. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  544. t.registerhigh));
  545. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  546. t.registerlow));
  547. end;
  548. LOC_MEM,
  549. LOC_REFERENCE:
  550. begin
  551. hr:=newreference(t.reference);
  552. inc(hr^.offset,4);
  553. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  554. hr));
  555. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  556. newreference(t.reference)));
  557. ungetiftemp(t.reference);
  558. end;
  559. else internalerror(331);
  560. end;
  561. end;
  562. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  563. begin
  564. case t.loc of
  565. LOC_REGISTER:
  566. begin
  567. { can't be a regvar, since it would be LOC_CREGISTER then }
  568. regs := regs and not($80 shr byte(t.register));
  569. if t.registerhigh <> R_NO then
  570. regs := regs and not($80 shr byte(t.registerhigh));
  571. end;
  572. LOC_MEM,LOC_REFERENCE:
  573. begin
  574. if not(cs_regalloc in aktglobalswitches) or
  575. (t.reference.base in usableregs) then
  576. regs := regs and
  577. not($80 shr byte(t.reference.base));
  578. if not(cs_regalloc in aktglobalswitches) or
  579. (t.reference.index in usableregs) then
  580. regs := regs and
  581. not($80 shr byte(t.reference.index));
  582. end;
  583. end;
  584. end;
  585. procedure release_loc(const t : tlocation);
  586. begin
  587. case t.loc of
  588. LOC_REGISTER,
  589. LOC_CREGISTER:
  590. begin
  591. ungetregister32(t.register);
  592. end;
  593. LOC_MEM,
  594. LOC_REFERENCE:
  595. del_reference(t.reference);
  596. else internalerror(332);
  597. end;
  598. end;
  599. procedure release_qword_loc(const t : tlocation);
  600. begin
  601. case t.loc of
  602. LOC_REGISTER,
  603. LOC_CREGISTER:
  604. begin
  605. ungetregister32(t.registerhigh);
  606. ungetregister32(t.registerlow);
  607. end;
  608. LOC_MEM,
  609. LOC_REFERENCE:
  610. del_reference(t.reference);
  611. else internalerror(331);
  612. end;
  613. end;
  614. procedure emit_push_loc(const t:tlocation);
  615. begin
  616. case t.loc of
  617. LOC_REGISTER,
  618. LOC_CREGISTER : begin
  619. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)));
  620. ungetregister(t.register); { the register is not needed anymore }
  621. end;
  622. LOC_MEM,
  623. LOC_REFERENCE : begin
  624. if t.reference.is_immediate then
  625. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.reference.offset))
  626. else
  627. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(t.reference)));
  628. del_reference(t.reference);
  629. ungetiftemp(t.reference);
  630. end;
  631. else
  632. internalerror(330);
  633. end;
  634. end;
  635. procedure emit_pushw_loc(const t:tlocation);
  636. var
  637. opsize : topsize;
  638. begin
  639. case t.loc of
  640. LOC_REGISTER,
  641. LOC_CREGISTER : begin
  642. if aktalignment.paraalign=4 then
  643. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)))
  644. else
  645. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,makereg16(t.register)));
  646. ungetregister(t.register); { the register is not needed anymore }
  647. end;
  648. LOC_MEM,
  649. LOC_REFERENCE : begin
  650. if aktalignment.paraalign=4 then
  651. opsize:=S_L
  652. else
  653. opsize:=S_W;
  654. if t.reference.is_immediate then
  655. exprasmList.concat(Taicpu.Op_const(A_PUSH,opsize,t.reference.offset))
  656. else
  657. exprasmList.concat(Taicpu.Op_ref(A_PUSH,opsize,newreference(t.reference)));
  658. del_reference(t.reference);
  659. ungetiftemp(t.reference);
  660. end;
  661. else
  662. internalerror(330);
  663. end;
  664. end;
  665. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  666. begin
  667. case t.loc of
  668. LOC_MEM,
  669. LOC_REFERENCE : begin
  670. if t.reference.is_immediate then
  671. internalerror(331)
  672. else
  673. begin
  674. getexplicitregister32(R_EDI);
  675. emit_ref_reg(A_LEA,S_L,
  676. newreference(t.reference),R_EDI);
  677. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  678. R_EDI,newreference(ref)));
  679. ungetregister32(R_EDI);
  680. end;
  681. { release the registers }
  682. del_reference(t.reference);
  683. if freetemp then
  684. ungetiftemp(t.reference);
  685. end;
  686. else
  687. internalerror(332);
  688. end;
  689. end;
  690. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  691. begin
  692. case t.loc of
  693. LOC_MEM,
  694. LOC_REFERENCE : begin
  695. if t.reference.is_immediate then
  696. internalerror(331)
  697. else
  698. begin
  699. getexplicitregister32(R_EDI);
  700. emit_ref_reg(A_LEA,S_L,
  701. newreference(t.reference),R_EDI);
  702. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  703. ungetregister32(R_EDI);
  704. end;
  705. if freetemp then
  706. ungetiftemp(t.reference);
  707. end;
  708. else
  709. internalerror(332);
  710. end;
  711. end;
  712. procedure emit_push_mem_size(const t: treference; size: longint);
  713. var
  714. s: topsize;
  715. begin
  716. if t.is_immediate then
  717. begin
  718. if (size=4) or
  719. (aktalignment.paraalign=4) then
  720. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.offset))
  721. else
  722. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_W,t.offset));
  723. end
  724. else
  725. if size < 4 then
  726. begin
  727. getexplicitregister32(R_EDI);
  728. case size of
  729. 1: s := S_BL;
  730. 2: s := S_WL;
  731. else internalerror(200008071);
  732. end;
  733. exprasmList.concat(Taicpu.Op_ref_reg(A_MOVZX,s,
  734. newreference(t),R_EDI));
  735. if aktalignment.paraalign=4 then
  736. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI))
  737. else
  738. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,R_DI));
  739. ungetregister32(R_EDI);
  740. end
  741. else
  742. if size = 4 then
  743. emit_push_mem(t)
  744. else
  745. internalerror(200008072);
  746. end;
  747. procedure emit_to_mem(var t:tlocation;def:tdef);
  748. var
  749. r : treference;
  750. begin
  751. case t.loc of
  752. LOC_FPU : begin
  753. reset_reference(t.reference);
  754. gettempofsizereference(10,t.reference);
  755. floatstore(tfloatdef(def).typ,t.reference);
  756. end;
  757. LOC_REGISTER:
  758. begin
  759. if is_64bitint(def) then
  760. begin
  761. gettempofsizereference(8,r);
  762. emit_reg_ref(A_MOV,S_L,t.registerlow,newreference(r));
  763. inc(r.offset,4);
  764. emit_reg_ref(A_MOV,S_L,t.registerhigh,newreference(r));
  765. dec(r.offset,4);
  766. t.reference:=r;
  767. end
  768. else
  769. internalerror(1405001);
  770. end;
  771. LOC_MEM,
  772. LOC_REFERENCE : ;
  773. LOC_CFPUREGISTER : begin
  774. emit_reg(A_FLD,S_NO,correct_fpuregister(t.register,fpuvaroffset));
  775. inc(fpuvaroffset);
  776. reset_reference(t.reference);
  777. gettempofsizereference(10,t.reference);
  778. floatstore(tfloatdef(def).typ,t.reference);
  779. end;
  780. else
  781. internalerror(333);
  782. end;
  783. t.loc:=LOC_MEM;
  784. end;
  785. procedure emit_to_reg16(var hr:tregister);
  786. begin
  787. { ranges are a little bit bug sensitive ! }
  788. case hr of
  789. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  790. begin
  791. hr:=reg32toreg16(hr);
  792. end;
  793. R_AL,R_BL,R_CL,R_DL:
  794. begin
  795. hr:=reg8toreg16(hr);
  796. emit_const_reg(A_AND,S_W,$ff,hr);
  797. end;
  798. R_AH,R_BH,R_CH,R_DH:
  799. begin
  800. hr:=reg8toreg16(hr);
  801. emit_const_reg(A_AND,S_W,$ff00,hr);
  802. end;
  803. end;
  804. end;
  805. procedure emit_to_reg32(var hr:tregister);
  806. begin
  807. { ranges are a little bit bug sensitive ! }
  808. case hr of
  809. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  810. begin
  811. hr:=reg16toreg32(hr);
  812. emit_const_reg(A_AND,S_L,$ffff,hr);
  813. end;
  814. R_AL,R_BL,R_CL,R_DL:
  815. begin
  816. hr:=reg8toreg32(hr);
  817. emit_const_reg(A_AND,S_L,$ff,hr);
  818. end;
  819. R_AH,R_BH,R_CH,R_DH:
  820. begin
  821. hr:=reg8toreg32(hr);
  822. emit_const_reg(A_AND,S_L,$ff00,hr);
  823. end;
  824. end;
  825. end;
  826. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  827. var
  828. hr : preference;
  829. begin
  830. { if we load a 64 bit reference, we must be careful because }
  831. { we could overwrite the registers of the reference by }
  832. { accident }
  833. getexplicitregister32(R_EDI);
  834. if r.base=rl then
  835. begin
  836. emit_reg_reg(A_MOV,S_L,r.base,
  837. R_EDI);
  838. r.base:=R_EDI;
  839. end
  840. else if r.index=rl then
  841. begin
  842. emit_reg_reg(A_MOV,S_L,r.index,
  843. R_EDI);
  844. r.index:=R_EDI;
  845. end;
  846. emit_ref_reg(A_MOV,S_L,
  847. newreference(r),rl);
  848. hr:=newreference(r);
  849. inc(hr^.offset,4);
  850. emit_ref_reg(A_MOV,S_L,
  851. hr,rh);
  852. ungetregister32(R_EDI);
  853. end;
  854. {*****************************************************************************
  855. Emit String Functions
  856. *****************************************************************************}
  857. procedure incrcomintfref(t: tdef; const ref: treference);
  858. var
  859. pushedregs : tpushed;
  860. begin
  861. pushusedregisters(pushedregs,$ff);
  862. emit_ref(A_PUSH,S_L,newreference(ref));
  863. saveregvars($ff);
  864. if is_interfacecom(t) then
  865. emitcall('FPC_INTF_INCR_REF')
  866. else
  867. internalerror(1859);
  868. popusedregisters(pushedregs);
  869. end;
  870. procedure decrcomintfref(t: tdef; const ref: treference);
  871. var
  872. pushedregs : tpushed;
  873. begin
  874. pushusedregisters(pushedregs,$ff);
  875. emitpushreferenceaddr(ref);
  876. saveregvars($ff);
  877. if is_interfacecom(t) then
  878. begin
  879. emitcall('FPC_INTF_DECR_REF');
  880. end
  881. else internalerror(1859);
  882. popusedregisters(pushedregs);
  883. end;
  884. procedure copyshortstring(const dref,sref : treference;len : byte;
  885. loadref, del_sref: boolean);
  886. begin
  887. emitpushreferenceaddr(dref);
  888. { if it's deleted right before it's used, the optimizer can move }
  889. { the reg deallocations to the right places (JM) }
  890. if del_sref then
  891. del_reference(sref);
  892. if loadref then
  893. emit_push_mem(sref)
  894. else
  895. emitpushreferenceaddr(sref);
  896. push_int(len);
  897. emitcall('FPC_SHORTSTR_COPY');
  898. maybe_loadself;
  899. end;
  900. {$ifdef unused}
  901. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  902. begin
  903. emitpushreferenceaddr(dref);
  904. if loadref then
  905. emit_push_mem(sref)
  906. else
  907. emitpushreferenceaddr(sref);
  908. push_int(len);
  909. saveregvars($ff);
  910. emitcall('FPC_LONGSTR_COPY');
  911. maybe_loadself;
  912. end;
  913. {$endif unused}
  914. procedure incrstringref(t : tdef;const ref : treference);
  915. var
  916. pushedregs : tpushed;
  917. begin
  918. pushusedregisters(pushedregs,$ff);
  919. emitpushreferenceaddr(ref);
  920. saveregvars($ff);
  921. if is_ansistring(t) then
  922. begin
  923. emitcall('FPC_ANSISTR_INCR_REF');
  924. end
  925. else if is_widestring(t) then
  926. begin
  927. emitcall('FPC_WIDESTR_INCR_REF');
  928. end
  929. else internalerror(1859);
  930. popusedregisters(pushedregs);
  931. end;
  932. procedure decrstringref(t : tdef;const ref : treference);
  933. var
  934. pushedregs : tpushed;
  935. begin
  936. pushusedregisters(pushedregs,$ff);
  937. emitpushreferenceaddr(ref);
  938. saveregvars($ff);
  939. if is_ansistring(t) then
  940. begin
  941. emitcall('FPC_ANSISTR_DECR_REF');
  942. end
  943. else if is_widestring(t) then
  944. begin
  945. emitcall('FPC_WIDESTR_DECR_REF');
  946. end
  947. else internalerror(1859);
  948. popusedregisters(pushedregs);
  949. end;
  950. {*****************************************************************************
  951. Emit Push Functions
  952. *****************************************************************************}
  953. procedure push_int(l : longint);
  954. begin
  955. if (l = 0) and
  956. not(aktoptprocessor in [Class386, ClassP6]) and
  957. not(cs_littlesize in aktglobalswitches)
  958. Then
  959. begin
  960. getexplicitregister32(R_EDI);
  961. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  962. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  963. ungetregister32(R_EDI);
  964. end
  965. else
  966. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,l));
  967. end;
  968. procedure emit_push_mem(const ref : treference);
  969. begin
  970. if ref.is_immediate then
  971. push_int(ref.offset)
  972. else
  973. begin
  974. if not(aktoptprocessor in [Class386, ClassP6]) and
  975. not(cs_littlesize in aktglobalswitches)
  976. then
  977. begin
  978. getexplicitregister32(R_EDI);
  979. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  980. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  981. ungetregister32(R_EDI);
  982. end
  983. else exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)));
  984. end;
  985. end;
  986. procedure emitpushreferenceaddr(const ref : treference);
  987. var
  988. href : treference;
  989. begin
  990. { this will fail for references to other segments !!! }
  991. if ref.is_immediate then
  992. { is this right ? }
  993. begin
  994. { push_int(ref.offset)}
  995. gettempofsizereference(4,href);
  996. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  997. emitpushreferenceaddr(href);
  998. del_reference(href);
  999. end
  1000. else
  1001. begin
  1002. if ref.segment<>R_NO then
  1003. CGMessage(cg_e_cant_use_far_pointer_there);
  1004. if (ref.base=R_NO) and (ref.index=R_NO) then
  1005. exprasmList.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset))
  1006. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  1007. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  1008. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.index))
  1009. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  1010. (ref.offset=0) and (ref.symbol=nil) then
  1011. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.base))
  1012. else
  1013. begin
  1014. getexplicitregister32(R_EDI);
  1015. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1016. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1017. ungetregister32(R_EDI);
  1018. end;
  1019. end;
  1020. end;
  1021. {*****************************************************************************
  1022. Emit Float Functions
  1023. *****************************************************************************}
  1024. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1025. begin
  1026. case t of
  1027. s32real : begin
  1028. op:=A_FLD;
  1029. s:=S_FS;
  1030. end;
  1031. s64real : begin
  1032. op:=A_FLD;
  1033. { ???? }
  1034. s:=S_FL;
  1035. end;
  1036. s80real : begin
  1037. op:=A_FLD;
  1038. s:=S_FX;
  1039. end;
  1040. s64comp : begin
  1041. op:=A_FILD;
  1042. s:=S_IQ;
  1043. end;
  1044. else internalerror(17);
  1045. end;
  1046. end;
  1047. procedure floatload(t : tfloattype;const ref : treference);
  1048. var
  1049. op : tasmop;
  1050. s : topsize;
  1051. begin
  1052. floatloadops(t,op,s);
  1053. exprasmList.concat(Taicpu.Op_ref(op,s,
  1054. newreference(ref)));
  1055. inc(fpuvaroffset);
  1056. end;
  1057. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1058. begin
  1059. case t of
  1060. s32real : begin
  1061. op:=A_FSTP;
  1062. s:=S_FS;
  1063. end;
  1064. s64real : begin
  1065. op:=A_FSTP;
  1066. s:=S_FL;
  1067. end;
  1068. s80real : begin
  1069. op:=A_FSTP;
  1070. s:=S_FX;
  1071. end;
  1072. s64comp : begin
  1073. op:=A_FISTP;
  1074. s:=S_IQ;
  1075. end;
  1076. else
  1077. internalerror(17);
  1078. end;
  1079. end;
  1080. procedure floatstore(t : tfloattype;const ref : treference);
  1081. var
  1082. op : tasmop;
  1083. s : topsize;
  1084. begin
  1085. floatstoreops(t,op,s);
  1086. exprasmList.concat(Taicpu.Op_ref(op,s,
  1087. newreference(ref)));
  1088. dec(fpuvaroffset);
  1089. end;
  1090. {*****************************************************************************
  1091. Emit Functions
  1092. *****************************************************************************}
  1093. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1094. const
  1095. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1096. ishr : array[0..3] of byte=(2,0,1,0);
  1097. var
  1098. ecxpushed : boolean;
  1099. oldsourceoffset,
  1100. helpsize : longint;
  1101. i : byte;
  1102. reg8,reg32 : tregister;
  1103. swap : boolean;
  1104. procedure maybepushecx;
  1105. begin
  1106. if not(R_ECX in unused) then
  1107. begin
  1108. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1109. ecxpushed:=true;
  1110. end
  1111. else getexplicitregister32(R_ECX);
  1112. end;
  1113. begin
  1114. oldsourceoffset:=source.offset;
  1115. if (not loadref) and
  1116. ((size<=8) or
  1117. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1118. begin
  1119. helpsize:=size shr 2;
  1120. getexplicitregister32(R_EDI);
  1121. for i:=1 to helpsize do
  1122. begin
  1123. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1124. If (size = 4) and delsource then
  1125. del_reference(source);
  1126. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest)));
  1127. inc(source.offset,4);
  1128. inc(dest.offset,4);
  1129. dec(size,4);
  1130. end;
  1131. if size>1 then
  1132. begin
  1133. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1134. If (size = 2) and delsource then
  1135. del_reference(source);
  1136. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_W,R_DI,newreference(dest)));
  1137. inc(source.offset,2);
  1138. inc(dest.offset,2);
  1139. dec(size,2);
  1140. end;
  1141. ungetregister32(R_EDI);
  1142. if size>0 then
  1143. begin
  1144. { and now look for an 8 bit register }
  1145. swap:=false;
  1146. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1147. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1148. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1149. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1150. else
  1151. begin
  1152. swap:=true;
  1153. { we need only to check 3 registers, because }
  1154. { one is always not index or base }
  1155. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1156. begin
  1157. reg8:=R_AL;
  1158. reg32:=R_EAX;
  1159. end
  1160. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1161. begin
  1162. reg8:=R_BL;
  1163. reg32:=R_EBX;
  1164. end
  1165. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1166. begin
  1167. reg8:=R_CL;
  1168. reg32:=R_ECX;
  1169. end;
  1170. end;
  1171. if swap then
  1172. { was earlier XCHG, of course nonsense }
  1173. begin
  1174. getexplicitregister32(R_EDI);
  1175. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1176. end;
  1177. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1178. If delsource then
  1179. del_reference(source);
  1180. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_B,reg8,newreference(dest)));
  1181. if swap then
  1182. begin
  1183. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1184. ungetregister32(R_EDI);
  1185. end
  1186. else
  1187. ungetregister(reg8);
  1188. end;
  1189. end
  1190. else
  1191. begin
  1192. getexplicitregister32(R_EDI);
  1193. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1194. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1195. if loadref then
  1196. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1197. else
  1198. begin
  1199. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1200. if delsource then
  1201. del_reference(source);
  1202. end;
  1203. exprasmList.concat(Taicpu.Op_none(A_CLD,S_NO));
  1204. ecxpushed:=false;
  1205. if cs_littlesize in aktglobalswitches then
  1206. begin
  1207. maybepushecx;
  1208. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1209. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1210. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1211. end
  1212. else
  1213. begin
  1214. helpsize:=size shr 2;
  1215. size:=size and 3;
  1216. if helpsize>1 then
  1217. begin
  1218. maybepushecx;
  1219. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1220. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1221. end;
  1222. if helpsize>0 then
  1223. exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1224. if size>1 then
  1225. begin
  1226. dec(size,2);
  1227. exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1228. end;
  1229. if size=1 then
  1230. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1231. end;
  1232. ungetregister32(R_EDI);
  1233. exprasmList.concat(Tairegalloc.DeAlloc(R_ESI));
  1234. if ecxpushed then
  1235. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1236. else
  1237. ungetregister32(R_ECX);
  1238. { loading SELF-reference again }
  1239. maybe_loadself;
  1240. end;
  1241. if delsource then
  1242. begin
  1243. source.offset:=oldsourceoffset;
  1244. ungetiftemp(source);
  1245. end;
  1246. end;
  1247. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;
  1248. destreg:Tregister;delloc:boolean);
  1249. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1250. var tai:Taicpu;
  1251. r:Preference;
  1252. begin
  1253. tai := nil;
  1254. case location.loc of
  1255. LOC_REGISTER,LOC_CREGISTER:
  1256. begin
  1257. case orddef.typ of
  1258. u8bit,uchar,bool8bit:
  1259. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_BL,location.register,destreg);
  1260. s8bit:
  1261. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_BL,location.register,destreg);
  1262. u16bit,uwidechar,bool16bit:
  1263. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_WL,location.register,destreg);
  1264. s16bit:
  1265. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_WL,location.register,destreg);
  1266. u32bit,bool32bit,s32bit:
  1267. if location.register <> destreg then
  1268. tai:=Taicpu.Op_reg_reg(A_MOV,S_L,location.register,destreg);
  1269. else
  1270. internalerror(330);
  1271. end;
  1272. if delloc then
  1273. ungetregister(location.register);
  1274. end;
  1275. LOC_MEM,
  1276. LOC_REFERENCE:
  1277. begin
  1278. if location.reference.is_immediate then
  1279. tai:=Taicpu.Op_const_reg(A_MOV,S_L,location.reference.offset,destreg)
  1280. else
  1281. begin
  1282. r:=newreference(location.reference);
  1283. case orddef.typ of
  1284. u8bit,uchar,bool8bit:
  1285. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_BL,r,destreg);
  1286. s8bit:
  1287. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_BL,r,destreg);
  1288. u16bit,uwidechar,bool16bit:
  1289. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_WL,r,destreg);
  1290. s16bit:
  1291. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_WL,r,destreg);
  1292. u32bit,bool32bit:
  1293. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1294. s32bit:
  1295. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1296. else
  1297. internalerror(330);
  1298. end;
  1299. end;
  1300. if delloc then
  1301. del_reference(location.reference);
  1302. end
  1303. else
  1304. internalerror(6);
  1305. end;
  1306. if assigned(tai) then
  1307. exprasmList.concat(tai);
  1308. end;
  1309. { if necessary ESI is reloaded after a call}
  1310. procedure maybe_loadself;
  1311. var
  1312. hp : preference;
  1313. p : pprocinfo;
  1314. i : longint;
  1315. begin
  1316. if assigned(procinfo^._class) then
  1317. begin
  1318. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1319. if lexlevel>normal_function_level then
  1320. begin
  1321. new(hp);
  1322. reset_reference(hp^);
  1323. hp^.offset:=procinfo^.framepointer_offset;
  1324. hp^.base:=procinfo^.framepointer;
  1325. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1326. p:=procinfo^.parent;
  1327. for i:=3 to lexlevel-1 do
  1328. begin
  1329. new(hp);
  1330. reset_reference(hp^);
  1331. hp^.offset:=p^.framepointer_offset;
  1332. hp^.base:=R_ESI;
  1333. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1334. p:=p^.parent;
  1335. end;
  1336. new(hp);
  1337. reset_reference(hp^);
  1338. hp^.offset:=p^.selfpointer_offset;
  1339. hp^.base:=R_ESI;
  1340. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1341. end
  1342. else
  1343. begin
  1344. new(hp);
  1345. reset_reference(hp^);
  1346. hp^.offset:=procinfo^.selfpointer_offset;
  1347. hp^.base:=procinfo^.framepointer;
  1348. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1349. end;
  1350. end;
  1351. end;
  1352. {*****************************************************************************
  1353. Entry/Exit Code Functions
  1354. *****************************************************************************}
  1355. procedure genprofilecode;
  1356. var
  1357. pl : tasmlabel;
  1358. begin
  1359. if (po_assembler in aktprocdef.procoptions) then
  1360. exit;
  1361. case target_info.target of
  1362. target_i386_win32,
  1363. target_i386_freebsd,
  1364. target_i386_linux:
  1365. begin
  1366. getaddrlabel(pl);
  1367. emitinsertcall(target_info.Cprefix+'mcount');
  1368. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1369. exprasmList.insert(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1370. exprasmList.insert(Tai_section.Create(sec_code));
  1371. exprasmList.insert(Tai_const.Create_32bit(0));
  1372. exprasmList.insert(Tai_label.Create(pl));
  1373. exprasmList.insert(Tai_align.Create(4));
  1374. exprasmList.insert(Tai_section.Create(sec_data));
  1375. end;
  1376. target_i386_go32v2:
  1377. begin
  1378. emitinsertcall('MCOUNT');
  1379. end;
  1380. end;
  1381. end;
  1382. procedure generate_interrupt_stackframe_entry;
  1383. begin
  1384. { save the registers of an interrupt procedure }
  1385. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1386. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1387. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1388. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1389. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1390. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1391. { .... also the segment registers }
  1392. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1393. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1394. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1395. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1396. end;
  1397. procedure generate_interrupt_stackframe_exit;
  1398. begin
  1399. { restore the registers of an interrupt procedure }
  1400. { this was all with entrycode instead of exitcode !!}
  1401. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1402. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1403. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1404. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1405. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1406. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1407. { .... also the segment registers }
  1408. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1409. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1410. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1411. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1412. { this restores the flags }
  1413. procinfo^.aktexitcode.concat(Taicpu.Op_none(A_IRET,S_NO));
  1414. end;
  1415. { generates the code for threadvar initialisation }
  1416. procedure initialize_threadvar(p : tnamedindexitem);
  1417. var
  1418. hr : treference;
  1419. begin
  1420. if (tsym(p).typ=varsym) and
  1421. (vo_is_thread_var in tvarsym(p).varoptions) then
  1422. begin
  1423. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,tvarsym(p).getsize));
  1424. reset_reference(hr);
  1425. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1426. emitpushreferenceaddr(hr);
  1427. saveregvars($ff);
  1428. emitcall('FPC_INIT_THREADVAR');
  1429. end;
  1430. end;
  1431. { initilizes data of type t }
  1432. { if is_already_ref is true then the routines assumes }
  1433. { that r points to the data to initialize }
  1434. procedure initialize(t : tdef;const ref : treference;is_already_ref : boolean);
  1435. var
  1436. hr : treference;
  1437. begin
  1438. if is_ansistring(t) or
  1439. is_widestring(t) or
  1440. is_interfacecom(t) then
  1441. begin
  1442. emit_const_ref(A_MOV,S_L,0,
  1443. newreference(ref));
  1444. end
  1445. else
  1446. begin
  1447. reset_reference(hr);
  1448. hr.symbol:=tstoreddef(t).get_rtti_label(initrtti);
  1449. emitpushreferenceaddr(hr);
  1450. if is_already_ref then
  1451. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)))
  1452. else
  1453. emitpushreferenceaddr(ref);
  1454. emitcall('FPC_INITIALIZE');
  1455. end;
  1456. end;
  1457. { finalizes data of type t }
  1458. { if is_already_ref is true then the routines assumes }
  1459. { that r points to the data to finalizes }
  1460. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  1461. var
  1462. r : treference;
  1463. begin
  1464. if is_ansistring(t) or
  1465. is_widestring(t) then
  1466. begin
  1467. decrstringref(t,ref);
  1468. end
  1469. else if is_interfacecom(t) then
  1470. begin
  1471. decrcomintfref(t,ref);
  1472. end
  1473. else
  1474. begin
  1475. reset_reference(r);
  1476. r.symbol:=tstoreddef(t).get_rtti_label(initrtti);
  1477. emitpushreferenceaddr(r);
  1478. if is_already_ref then
  1479. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1480. newreference(ref)))
  1481. else
  1482. emitpushreferenceaddr(ref);
  1483. emitcall('FPC_FINALIZE');
  1484. end;
  1485. end;
  1486. { generates the code for initialisation of local data }
  1487. procedure initialize_data(p : tnamedindexitem);
  1488. var
  1489. hr : treference;
  1490. begin
  1491. if (tsym(p).typ=varsym) and
  1492. assigned(tvarsym(p).vartype.def) and
  1493. not(is_class(tvarsym(p).vartype.def)) and
  1494. tvarsym(p).vartype.def.needs_inittable then
  1495. begin
  1496. if assigned(procinfo) then
  1497. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1498. reset_reference(hr);
  1499. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  1500. begin
  1501. hr.base:=procinfo^.framepointer;
  1502. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1503. end
  1504. else
  1505. begin
  1506. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1507. end;
  1508. initialize(tvarsym(p).vartype.def,hr,false);
  1509. end;
  1510. end;
  1511. { generates the code for incrementing the reference count of parameters and
  1512. initialize out parameters }
  1513. procedure init_paras(p : tnamedindexitem);
  1514. var
  1515. hrv : treference;
  1516. hr: treference;
  1517. begin
  1518. if (tsym(p).typ=varsym) and
  1519. not is_class(tvarsym(p).vartype.def) and
  1520. tvarsym(p).vartype.def.needs_inittable then
  1521. begin
  1522. if (tvarsym(p).varspez=vs_value) then
  1523. begin
  1524. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1525. reset_reference(hrv);
  1526. hrv.base:=procinfo^.framepointer;
  1527. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1528. if is_ansistring(tvarsym(p).vartype.def) or
  1529. is_widestring(tvarsym(p).vartype.def) then
  1530. begin
  1531. incrstringref(tvarsym(p).vartype.def,hrv)
  1532. end
  1533. else if is_interfacecom(tvarsym(p).vartype.def) then
  1534. begin
  1535. incrcomintfref(tvarsym(p).vartype.def,hrv)
  1536. end
  1537. else
  1538. begin
  1539. reset_reference(hr);
  1540. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_rtti_label(initrtti);
  1541. emitpushreferenceaddr(hr);
  1542. emitpushreferenceaddr(hrv);
  1543. emitcall('FPC_ADDREF');
  1544. end;
  1545. end
  1546. else if (tvarsym(p).varspez=vs_out) then
  1547. begin
  1548. reset_reference(hrv);
  1549. hrv.base:=procinfo^.framepointer;
  1550. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1551. getexplicitregister32(R_EDI);
  1552. exprasmList.concat(Taicpu.Op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI));
  1553. reset_reference(hr);
  1554. hr.base:=R_EDI;
  1555. initialize(tvarsym(p).vartype.def,hr,false);
  1556. end;
  1557. end;
  1558. end;
  1559. { generates the code for decrementing the reference count of parameters }
  1560. procedure final_paras(p : tnamedindexitem);
  1561. var
  1562. hrv : treference;
  1563. hr: treference;
  1564. begin
  1565. if (tsym(p).typ=varsym) and
  1566. not is_class(tvarsym(p).vartype.def) and
  1567. tvarsym(p).vartype.def.needs_inittable then
  1568. begin
  1569. if (tvarsym(p).varspez=vs_value) then
  1570. begin
  1571. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1572. reset_reference(hrv);
  1573. hrv.base:=procinfo^.framepointer;
  1574. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1575. if is_ansistring(tvarsym(p).vartype.def) or
  1576. is_widestring(tvarsym(p).vartype.def) then
  1577. begin
  1578. decrstringref(tvarsym(p).vartype.def,hrv)
  1579. end
  1580. else if is_interfacecom(tvarsym(p).vartype.def) then
  1581. begin
  1582. decrcomintfref(tvarsym(p).vartype.def,hrv)
  1583. end
  1584. else
  1585. begin
  1586. reset_reference(hr);
  1587. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_rtti_label(initrtti);
  1588. emitpushreferenceaddr(hr);
  1589. emitpushreferenceaddr(hrv);
  1590. emitcall('FPC_DECREF');
  1591. end;
  1592. end;
  1593. end;
  1594. end;
  1595. { generates the code for finalisation of local data }
  1596. procedure finalize_data(p : tnamedindexitem);
  1597. var
  1598. hr : treference;
  1599. begin
  1600. if (tsym(p).typ=varsym) and
  1601. assigned(tvarsym(p).vartype.def) and
  1602. not(is_class(tvarsym(p).vartype.def)) and
  1603. tvarsym(p).vartype.def.needs_inittable then
  1604. begin
  1605. if assigned(procinfo) then
  1606. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1607. reset_reference(hr);
  1608. case tsym(p).owner.symtabletype of
  1609. localsymtable,inlinelocalsymtable:
  1610. begin
  1611. hr.base:=procinfo^.framepointer;
  1612. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1613. end;
  1614. else
  1615. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1616. end;
  1617. finalize(tvarsym(p).vartype.def,hr,false);
  1618. end;
  1619. end;
  1620. { generates the code to make local copies of the value parameters }
  1621. procedure copyvalueparas(p : tnamedindexitem);
  1622. var
  1623. href1,href2 : treference;
  1624. r : preference;
  1625. power,len : longint;
  1626. opsize : topsize;
  1627. {$ifndef NOTARGETWIN32}
  1628. again,ok : tasmlabel;
  1629. {$endif}
  1630. begin
  1631. if (tsym(p).typ=varsym) and
  1632. (tvarsym(p).varspez=vs_value) and
  1633. (push_addr_param(tvarsym(p).vartype.def)) then
  1634. begin
  1635. if is_open_array(tvarsym(p).vartype.def) or
  1636. is_array_of_const(tvarsym(p).vartype.def) then
  1637. begin
  1638. { get stack space }
  1639. new(r);
  1640. reset_reference(r^);
  1641. r^.base:=procinfo^.framepointer;
  1642. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1643. getexplicitregister32(R_EDI);
  1644. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1645. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1646. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1647. begin
  1648. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1649. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1650. else
  1651. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1652. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1653. end;
  1654. {$ifndef NOTARGETWIN32}
  1655. { windows guards only a few pages for stack growing, }
  1656. { so we have to access every page first }
  1657. if target_info.target=target_i386_win32 then
  1658. begin
  1659. getlabel(again);
  1660. getlabel(ok);
  1661. emitlab(again);
  1662. exprasmList.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1663. emitjmp(C_C,ok);
  1664. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1665. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1666. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1667. emitjmp(C_None,again);
  1668. emitlab(ok);
  1669. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1670. ungetregister32(R_EDI);
  1671. { now reload EDI }
  1672. new(r);
  1673. reset_reference(r^);
  1674. r^.base:=procinfo^.framepointer;
  1675. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1676. getexplicitregister32(R_EDI);
  1677. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1678. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1679. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1680. begin
  1681. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1682. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1683. else
  1684. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1685. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1686. end;
  1687. end
  1688. else
  1689. {$endif NOTARGETWIN32}
  1690. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1691. { load destination }
  1692. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1693. { don't destroy the registers! }
  1694. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1695. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1696. { load count }
  1697. new(r);
  1698. reset_reference(r^);
  1699. r^.base:=procinfo^.framepointer;
  1700. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1701. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ECX));
  1702. { load source }
  1703. new(r);
  1704. reset_reference(r^);
  1705. r^.base:=procinfo^.framepointer;
  1706. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1707. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ESI));
  1708. { scheduled .... }
  1709. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1710. { calculate size }
  1711. len:=tarraydef(tvarsym(p).vartype.def).elesize;
  1712. opsize:=S_B;
  1713. if (len and 3)=0 then
  1714. begin
  1715. opsize:=S_L;
  1716. len:=len shr 2;
  1717. end
  1718. else
  1719. if (len and 1)=0 then
  1720. begin
  1721. opsize:=S_W;
  1722. len:=len shr 1;
  1723. end;
  1724. if ispowerof2(len, power) then
  1725. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1726. else
  1727. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1728. exprasmList.concat(Taicpu.op_none(A_REP,S_NO));
  1729. case opsize of
  1730. S_B : exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1731. S_W : exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1732. S_L : exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1733. end;
  1734. ungetregister32(R_EDI);
  1735. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1736. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1737. { patch the new address }
  1738. new(r);
  1739. reset_reference(r^);
  1740. r^.base:=procinfo^.framepointer;
  1741. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1742. exprasmList.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,r));
  1743. end
  1744. else
  1745. if is_shortstring(tvarsym(p).vartype.def) then
  1746. begin
  1747. reset_reference(href1);
  1748. href1.base:=procinfo^.framepointer;
  1749. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1750. reset_reference(href2);
  1751. href2.base:=procinfo^.framepointer;
  1752. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1753. copyshortstring(href2,href1,tstringdef(tvarsym(p).vartype.def).len,true,false);
  1754. end
  1755. else
  1756. begin
  1757. reset_reference(href1);
  1758. href1.base:=procinfo^.framepointer;
  1759. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1760. reset_reference(href2);
  1761. href2.base:=procinfo^.framepointer;
  1762. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1763. concatcopy(href1,href2,tvarsym(p).vartype.def.size,true,true);
  1764. end;
  1765. end;
  1766. end;
  1767. procedure inittempvariables;
  1768. var
  1769. hp : ptemprecord;
  1770. r : preference;
  1771. begin
  1772. hp:=templist;
  1773. while assigned(hp) do
  1774. begin
  1775. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  1776. tt_widestring,tt_freewidestring,
  1777. tt_interfacecom] then
  1778. begin
  1779. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1780. new(r);
  1781. reset_reference(r^);
  1782. r^.base:=procinfo^.framepointer;
  1783. r^.offset:=hp^.pos;
  1784. emit_const_ref(A_MOV,S_L,0,r);
  1785. end;
  1786. hp:=hp^.next;
  1787. end;
  1788. end;
  1789. procedure finalizetempvariables;
  1790. var
  1791. hp : ptemprecord;
  1792. hr : treference;
  1793. begin
  1794. hp:=templist;
  1795. while assigned(hp) do
  1796. begin
  1797. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1798. begin
  1799. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1800. reset_reference(hr);
  1801. hr.base:=procinfo^.framepointer;
  1802. hr.offset:=hp^.pos;
  1803. emitpushreferenceaddr(hr);
  1804. emitcall('FPC_ANSISTR_DECR_REF');
  1805. end
  1806. else if hp^.temptype in [tt_widestring,tt_freewidestring] then
  1807. begin
  1808. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1809. reset_reference(hr);
  1810. hr.base:=procinfo^.framepointer;
  1811. hr.offset:=hp^.pos;
  1812. emitpushreferenceaddr(hr);
  1813. emitcall('FPC_WIDESTR_DECR_REF');
  1814. end
  1815. else if hp^.temptype=tt_interfacecom then
  1816. begin
  1817. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1818. reset_reference(hr);
  1819. hr.base:=procinfo^.framepointer;
  1820. hr.offset:=hp^.pos;
  1821. emitpushreferenceaddr(hr);
  1822. emitcall('FPC_INTF_DECR_REF');
  1823. end;
  1824. hp:=hp^.next;
  1825. end;
  1826. end;
  1827. {$ifdef dummy}
  1828. var
  1829. ls : longint;
  1830. procedure largest_size(p : tnamedindexitem);
  1831. begin
  1832. if (tsym(p).typ=varsym) and
  1833. (tvarsym(p).getvaluesize>ls) then
  1834. ls:=tvarsym(p).getvaluesize;
  1835. end;
  1836. {$endif dummy}
  1837. procedure alignstack(alist : TAAsmoutput);
  1838. begin
  1839. {$ifdef dummy}
  1840. if (cs_optimize in aktglobalswitches) and
  1841. (aktoptprocessor in [classp5,classp6]) then
  1842. begin
  1843. ls:=0;
  1844. aktprocdef.localst.foreach({$ifndef TP}@{$endif}largest_size);
  1845. if ls>=8 then
  1846. aList.insert(Taicpu.Op_const_reg(A_AND,S_L,-8,R_ESP));
  1847. end;
  1848. {$endif dummy}
  1849. end;
  1850. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  1851. stackframe:longint;
  1852. var parasize:longint;var nostackframe:boolean;
  1853. inlined : boolean);
  1854. {
  1855. Generates the entry code for a procedure
  1856. }
  1857. var
  1858. hs : string;
  1859. {$ifdef GDB}
  1860. stab_function_name : tai_stab_function_name;
  1861. {$endif GDB}
  1862. hr : preference;
  1863. p : tsymtable;
  1864. r : treference;
  1865. oldlist,
  1866. oldexprasmlist : TAAsmoutput;
  1867. again : tasmlabel;
  1868. i : longint;
  1869. tempbuf,tempaddr : treference;
  1870. begin
  1871. oldexprasmlist:=exprasmlist;
  1872. exprasmlist:=alist;
  1873. if (not inlined) and (aktprocdef.proctypeoption=potype_proginit) then
  1874. begin
  1875. emitinsertcall('FPC_INITIALIZEUNITS');
  1876. { initialize profiling for win32 }
  1877. if (target_info.target=target_I386_WIN32) and
  1878. (cs_profile in aktmoduleswitches) then
  1879. emitinsertcall('__monstartup');
  1880. { add threadvars }
  1881. oldlist:=exprasmlist;
  1882. exprasmlist:=TAAsmoutput.Create;
  1883. p:=symtablestack;
  1884. while assigned(p) do
  1885. begin
  1886. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar);
  1887. p:=p.next;
  1888. end;
  1889. oldList.insertlist(exprasmlist);
  1890. exprasmlist.free;
  1891. exprasmlist:=oldlist;
  1892. end;
  1893. {$ifdef GDB}
  1894. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1895. exprasmList.insert(Tai_force_line.Create);
  1896. {$endif GDB}
  1897. { a constructor needs a help procedure }
  1898. if (aktprocdef.proctypeoption=potype_constructor) then
  1899. begin
  1900. if is_class(procinfo^._class) then
  1901. begin
  1902. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1903. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1904. emitinsertcall('FPC_NEW_CLASS');
  1905. end
  1906. else if is_object(procinfo^._class) then
  1907. begin
  1908. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1909. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1910. getexplicitregister32(R_EDI);
  1911. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  1912. end
  1913. else
  1914. Internalerror(200006161);
  1915. end;
  1916. { don't load ESI, does the caller }
  1917. { we must do it for local function }
  1918. { that can be called from a foreach_static }
  1919. { of another object than self !! PM }
  1920. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1921. (lexlevel>normal_function_level) then
  1922. maybe_loadself;
  1923. { When message method contains self as a parameter,
  1924. we must load it into ESI }
  1925. If (po_containsself in aktprocdef.procoptions) then
  1926. begin
  1927. new(hr);
  1928. reset_reference(hr^);
  1929. hr^.offset:=procinfo^.selfpointer_offset;
  1930. hr^.base:=procinfo^.framepointer;
  1931. exprasmList.insert(Taicpu.Op_ref_reg(A_MOV,S_L,hr,R_ESI));
  1932. exprasmList.insert(Tairegalloc.Alloc(R_ESI));
  1933. end;
  1934. { should we save edi,esi,ebx like C ? }
  1935. if (po_savestdregs in aktprocdef.procoptions) then
  1936. begin
  1937. if (aktprocdef.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1938. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1939. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1940. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1941. end;
  1942. { for the save all registers we can simply use a pusha,popa which
  1943. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1944. if (po_saveregisters in aktprocdef.procoptions) then
  1945. begin
  1946. exprasmList.insert(Taicpu.Op_none(A_PUSHA,S_L));
  1947. end;
  1948. { omit stack frame ? }
  1949. if (not inlined) then
  1950. if (procinfo^.framepointer=stack_pointer) then
  1951. begin
  1952. CGMessage(cg_d_stackframe_omited);
  1953. nostackframe:=true;
  1954. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1955. parasize:=0
  1956. else
  1957. parasize:=aktprocdef.parast.datasize+procinfo^.para_offset-4;
  1958. if stackframe<>0 then
  1959. exprasmList.insert(Taicpu.op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  1960. end
  1961. else
  1962. begin
  1963. alignstack(alist);
  1964. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1965. parasize:=0
  1966. else
  1967. parasize:=aktprocdef.parast.datasize+procinfo^.para_offset-8;
  1968. nostackframe:=false;
  1969. if stackframe<>0 then
  1970. begin
  1971. {$ifndef NOTARGETWIN32}
  1972. { windows guards only a few pages for stack growing, }
  1973. { so we have to access every page first }
  1974. if (target_info.target=target_i386_win32) and
  1975. (stackframe>=winstackpagesize) then
  1976. begin
  1977. if stackframe div winstackpagesize<=5 then
  1978. begin
  1979. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe-4,R_ESP));
  1980. for i:=1 to stackframe div winstackpagesize do
  1981. begin
  1982. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  1983. exprasmList.concat(Taicpu.op_const_ref(A_MOV,S_L,0,hr));
  1984. end;
  1985. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1986. end
  1987. else
  1988. begin
  1989. getlabel(again);
  1990. getexplicitregister32(R_EDI);
  1991. exprasmList.concat(Taicpu.op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI));
  1992. emitlab(again);
  1993. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1994. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1995. exprasmList.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  1996. emitjmp(C_NZ,again);
  1997. ungetregister32(R_EDI);
  1998. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP));
  1999. end
  2000. end
  2001. else
  2002. {$endif NOTARGETWIN32}
  2003. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  2004. if (cs_check_stack in aktlocalswitches) and
  2005. not(target_info.target in [target_i386_freebsd,target_i386_netbsd,
  2006. target_i386_linux,target_i386_win32]) then
  2007. begin
  2008. emitinsertcall('FPC_STACKCHECK');
  2009. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  2010. end;
  2011. if cs_profile in aktmoduleswitches then
  2012. genprofilecode;
  2013. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2014. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2015. end { endif stackframe <> 0 }
  2016. else
  2017. begin
  2018. if cs_profile in aktmoduleswitches then
  2019. genprofilecode;
  2020. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2021. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2022. end;
  2023. end;
  2024. if (po_interrupt in aktprocdef.procoptions) then
  2025. generate_interrupt_stackframe_entry;
  2026. { initialize return value }
  2027. if (not is_void(aktprocdef.rettype.def)) and
  2028. (aktprocdef.rettype.def.needs_inittable) then
  2029. begin
  2030. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2031. reset_reference(r);
  2032. r.offset:=procinfo^.return_offset;
  2033. r.base:=procinfo^.framepointer;
  2034. initialize(aktprocdef.rettype.def,r,ret_in_param(aktprocdef.rettype.def));
  2035. end;
  2036. { initialisize local data like ansistrings }
  2037. case aktprocdef.proctypeoption of
  2038. potype_unitinit:
  2039. begin
  2040. { using current_module.globalsymtable is hopefully }
  2041. { more robust than symtablestack and symtablestack.next }
  2042. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2043. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2044. end;
  2045. { units have seperate code for initilization and finalization }
  2046. potype_unitfinalize: ;
  2047. else
  2048. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data);
  2049. end;
  2050. { initialisizes temp. ansi/wide string data }
  2051. inittempvariables;
  2052. { generate copies of call by value parameters }
  2053. if not(po_assembler in aktprocdef.procoptions) and
  2054. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_palmossyscall,pocall_system]) then
  2055. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas);
  2056. if assigned( aktprocdef.parast) then
  2057. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras);
  2058. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2059. if not inlined and
  2060. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2061. { but it's useless in init/final code of units }
  2062. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2063. begin
  2064. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2065. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,36,R_ESP));
  2066. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  2067. reset_reference(tempaddr);
  2068. tempaddr.base:=R_EDI;
  2069. emitpushreferenceaddr(tempaddr);
  2070. reset_reference(tempbuf);
  2071. tempbuf.base:=R_EDI;
  2072. tempbuf.offset:=12;
  2073. emitpushreferenceaddr(tempbuf);
  2074. { Type of stack-frame must be pushed}
  2075. exprasmList.concat(Taicpu.op_const(A_PUSH,S_L,1));
  2076. emitcall('FPC_PUSHEXCEPTADDR');
  2077. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2078. emitcall('FPC_SETJMP');
  2079. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2080. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2081. emitjmp(C_NE,aktexitlabel);
  2082. { probably we've to reload self here }
  2083. maybe_loadself;
  2084. end;
  2085. if not inlined then
  2086. begin
  2087. if (cs_profile in aktmoduleswitches) or
  2088. (aktprocdef.owner.symtabletype=globalsymtable) or
  2089. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  2090. make_global:=true;
  2091. hs:=aktprocdef.aliasnames.getfirst;
  2092. {$ifdef GDB}
  2093. if (cs_debuginfo in aktmoduleswitches) and target_info.use_function_relative_addresses then
  2094. stab_function_name := Tai_stab_function_name.Create(strpnew(hs));
  2095. {$EndIf GDB}
  2096. while hs<>'' do
  2097. begin
  2098. if make_global then
  2099. exprasmList.insert(Tai_symbol.Createname_global(hs,0))
  2100. else
  2101. exprasmList.insert(Tai_symbol.Createname(hs,0));
  2102. {$ifdef GDB}
  2103. if (cs_debuginfo in aktmoduleswitches) and
  2104. target_info.use_function_relative_addresses then
  2105. exprasmList.insert(Tai_stab_function_name.Create(strpnew(hs)));
  2106. {$endif GDB}
  2107. hs:=aktprocdef.aliasnames.getfirst;
  2108. end;
  2109. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2110. aktprocsym.is_global := True;
  2111. {$ifdef GDB}
  2112. if (cs_debuginfo in aktmoduleswitches) then
  2113. begin
  2114. if target_info.use_function_relative_addresses then
  2115. exprasmList.insert(stab_function_name);
  2116. exprasmList.insert(Tai_stabs.Create(aktprocdef.stabstring));
  2117. aktprocsym.isstabwritten:=true;
  2118. end;
  2119. {$endif GDB}
  2120. { Align, gprof uses 16 byte granularity }
  2121. if (cs_profile in aktmoduleswitches) then
  2122. exprasmList.insert(Tai_align.Create_op(16,$90))
  2123. else
  2124. exprasmList.insert(Tai_align.Create(aktalignment.procalign));
  2125. end;
  2126. if inlined then
  2127. load_regvars(exprasmlist,nil);
  2128. exprasmlist:=oldexprasmlist;
  2129. end;
  2130. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2131. var
  2132. hr : preference;
  2133. op : Tasmop;
  2134. s : Topsize;
  2135. begin
  2136. if not is_void(aktprocdef.rettype.def) then
  2137. begin
  2138. {if ((procinfo^.flags and pi_operator)<>0) and
  2139. assigned(otsym) then
  2140. procinfo^.funcret_is_valid:=
  2141. procinfo^.funcret_is_valid or (otsym.refs>0);}
  2142. if (tfuncretsym(aktprocdef.funcretsym).funcretstate<>vs_assigned) and not inlined { and
  2143. ((procinfo^.flags and pi_uses_asm)=0)} then
  2144. CGMessage(sym_w_function_result_not_set);
  2145. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2146. if (aktprocdef.rettype.def.deftype in [orddef,enumdef]) then
  2147. begin
  2148. uses_eax:=true;
  2149. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2150. case aktprocdef.rettype.def.size of
  2151. 8:
  2152. begin
  2153. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2154. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2155. exprasmList.concat(Tairegalloc.Alloc(R_EDX));
  2156. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2157. uses_edx:=true;
  2158. end;
  2159. 4:
  2160. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2161. 2:
  2162. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2163. 1:
  2164. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2165. end;
  2166. end
  2167. else
  2168. if ret_in_acc(aktprocdef.rettype.def) then
  2169. begin
  2170. uses_eax:=true;
  2171. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2172. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2173. end
  2174. else
  2175. if (aktprocdef.rettype.def.deftype=floatdef) then
  2176. begin
  2177. floatloadops(tfloatdef(aktprocdef.rettype.def).typ,op,s);
  2178. exprasmList.concat(Taicpu.Op_ref(op,s,hr));
  2179. end
  2180. else
  2181. dispose(hr);
  2182. end
  2183. end;
  2184. procedure handle_fast_exit_return_value;
  2185. var
  2186. hr : preference;
  2187. op : Tasmop;
  2188. s : Topsize;
  2189. begin
  2190. if not is_void(aktprocdef.rettype.def) then
  2191. begin
  2192. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2193. if (aktprocdef.rettype.def.deftype in [orddef,enumdef]) then
  2194. begin
  2195. case aktprocdef.rettype.def.size of
  2196. 8:
  2197. begin
  2198. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2199. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2200. emit_reg_ref(A_MOV,S_L,R_EDX,hr);
  2201. end;
  2202. 4:
  2203. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2204. 2:
  2205. emit_reg_ref(A_MOV,S_W,R_AX,hr);
  2206. 1:
  2207. emit_reg_ref(A_MOV,S_B,R_AL,hr);
  2208. end;
  2209. end
  2210. else
  2211. if ret_in_acc(aktprocdef.rettype.def) then
  2212. begin
  2213. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2214. end
  2215. else
  2216. if (aktprocdef.rettype.def.deftype=floatdef) then
  2217. begin
  2218. floatstoreops(tfloatdef(aktprocdef.rettype.def).typ,op,s);
  2219. exprasmlist.concat(taicpu.op_ref(op,s,hr));
  2220. end
  2221. else
  2222. dispose(hr);
  2223. end
  2224. end;
  2225. procedure genexitcode(alist : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  2226. var
  2227. {$ifdef GDB}
  2228. mangled_length : longint;
  2229. p : pchar;
  2230. st : string[2];
  2231. {$endif GDB}
  2232. stabsendlabel,nofinal,okexitlabel,
  2233. noreraiselabel,nodestroycall : tasmlabel;
  2234. hr : treference;
  2235. uses_eax,uses_edx,uses_esi : boolean;
  2236. oldexprasmlist : TAAsmoutput;
  2237. ai : taicpu;
  2238. pd : tprocdef;
  2239. begin
  2240. oldexprasmlist:=exprasmlist;
  2241. exprasmlist:=alist;
  2242. if aktexit2label.is_used and
  2243. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  2244. begin
  2245. exprasmlist.concat(taicpu.op_sym(A_JMP,S_NO,aktexitlabel));
  2246. exprasmlist.concat(tai_label.create(aktexit2label));
  2247. handle_fast_exit_return_value;
  2248. end;
  2249. if aktexitlabel.is_used then
  2250. exprasmList.concat(Tai_label.Create(aktexitlabel));
  2251. cleanup_regvars(alist);
  2252. { call the destructor help procedure }
  2253. if (aktprocdef.proctypeoption=potype_destructor) and
  2254. assigned(procinfo^._class) then
  2255. begin
  2256. if is_class(procinfo^._class) then
  2257. begin
  2258. emitinsertcall('FPC_DISPOSE_CLASS');
  2259. end
  2260. else if is_object(procinfo^._class) then
  2261. begin
  2262. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2263. getexplicitregister32(R_EDI);
  2264. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  2265. { must the object be finalized ? }
  2266. if procinfo^._class.needs_inittable then
  2267. begin
  2268. getlabel(nofinal);
  2269. exprasmList.insert(Tai_label.Create(nofinal));
  2270. emitinsertcall('FPC_FINALIZE');
  2271. ungetregister32(R_EDI);
  2272. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  2273. exprasmList.insert(Taicpu.Op_sym(A_PUSH,S_L,procinfo^._class.get_rtti_label(initrtti)));
  2274. ai:=Taicpu.Op_sym(A_Jcc,S_NO,nofinal);
  2275. ai.SetCondition(C_Z);
  2276. exprasmList.insert(ai);
  2277. reset_reference(hr);
  2278. hr.base:=R_EBP;
  2279. hr.offset:=8;
  2280. exprasmList.insert(Taicpu.Op_const_ref(A_CMP,S_L,0,newreference(hr)));
  2281. end;
  2282. end
  2283. else
  2284. begin
  2285. Internalerror(200006161);
  2286. end;
  2287. end;
  2288. { finalize temporary data }
  2289. finalizetempvariables;
  2290. { finalize local data like ansistrings}
  2291. case aktprocdef.proctypeoption of
  2292. potype_unitfinalize:
  2293. begin
  2294. { using current_module.globalsymtable is hopefully }
  2295. { more robust than symtablestack and symtablestack.next }
  2296. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2297. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2298. end;
  2299. { units have seperate code for initialization and finalization }
  2300. potype_unitinit: ;
  2301. else
  2302. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data);
  2303. end;
  2304. { finalize paras data }
  2305. if assigned(aktprocdef.parast) then
  2306. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras);
  2307. { do we need to handle exceptions because of ansi/widestrings ? }
  2308. if not inlined and
  2309. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2310. { but it's useless in init/final code of units }
  2311. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2312. begin
  2313. { the exception helper routines modify all registers }
  2314. aktprocdef.usedregisters:=$ff;
  2315. getlabel(noreraiselabel);
  2316. emitcall('FPC_POPADDRSTACK');
  2317. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2318. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  2319. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2320. ungetregister32(R_EAX);
  2321. emitjmp(C_E,noreraiselabel);
  2322. if (aktprocdef.proctypeoption=potype_constructor) then
  2323. begin
  2324. if assigned(procinfo^._class) then
  2325. begin
  2326. pd:=procinfo^._class.searchdestructor;
  2327. if assigned(pd) then
  2328. begin
  2329. getlabel(nodestroycall);
  2330. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2331. procinfo^.selfpointer_offset));
  2332. emitjmp(C_E,nodestroycall);
  2333. if is_class(procinfo^._class) then
  2334. begin
  2335. emit_const(A_PUSH,S_L,1);
  2336. emit_reg(A_PUSH,S_L,R_ESI);
  2337. end
  2338. else if is_object(procinfo^._class) then
  2339. begin
  2340. emit_reg(A_PUSH,S_L,R_ESI);
  2341. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class.vmt_mangledname));
  2342. end
  2343. else
  2344. begin
  2345. Internalerror(200006161);
  2346. end;
  2347. if (po_virtualmethod in pd.procoptions) then
  2348. begin
  2349. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2350. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class.vmtmethodoffset(pd.extnumber)));
  2351. end
  2352. else
  2353. emitcall(pd.mangledname);
  2354. { not necessary because the result is never assigned in the
  2355. case of an exception (FK)
  2356. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2357. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2358. }
  2359. emitlab(nodestroycall);
  2360. end;
  2361. end
  2362. end
  2363. else
  2364. { must be the return value finalized before reraising the exception? }
  2365. if (not is_void(aktprocdef.rettype.def)) and
  2366. (aktprocdef.rettype.def.needs_inittable) and
  2367. ((aktprocdef.rettype.def.deftype<>objectdef) or
  2368. not is_class(aktprocdef.rettype.def)) then
  2369. begin
  2370. reset_reference(hr);
  2371. hr.offset:=procinfo^.return_offset;
  2372. hr.base:=procinfo^.framepointer;
  2373. finalize(aktprocdef.rettype.def,hr,ret_in_param(aktprocdef.rettype.def));
  2374. end;
  2375. emitcall('FPC_RERAISE');
  2376. emitlab(noreraiselabel);
  2377. end;
  2378. { call __EXIT for main program }
  2379. if (not DLLsource) and (not inlined) and (aktprocdef.proctypeoption=potype_proginit) then
  2380. begin
  2381. emitcall('FPC_DO_EXIT');
  2382. end;
  2383. { handle return value }
  2384. uses_eax:=false;
  2385. uses_edx:=false;
  2386. uses_esi:=false;
  2387. if not(po_assembler in aktprocdef.procoptions) then
  2388. if (aktprocdef.proctypeoption<>potype_constructor) then
  2389. handle_return_value(inlined,uses_eax,uses_edx)
  2390. else
  2391. begin
  2392. { successful constructor deletes the zero flag }
  2393. { and returns self in eax }
  2394. { eax must be set to zero if the allocation failed !!! }
  2395. getlabel(okexitlabel);
  2396. emitjmp(C_NONE,okexitlabel);
  2397. emitlab(faillabel);
  2398. if is_class(procinfo^._class) then
  2399. begin
  2400. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2401. emitcall('FPC_HELP_FAIL_CLASS');
  2402. end
  2403. else if is_object(procinfo^._class) then
  2404. begin
  2405. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2406. getexplicitregister32(R_EDI);
  2407. emit_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI);
  2408. emitcall('FPC_HELP_FAIL');
  2409. ungetregister32(R_EDI);
  2410. end
  2411. else
  2412. Internalerror(200006161);
  2413. emitlab(okexitlabel);
  2414. { for classes this is done after the call to }
  2415. { AfterConstruction }
  2416. if is_object(procinfo^._class) then
  2417. begin
  2418. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2419. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2420. uses_eax:=true;
  2421. end;
  2422. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2423. uses_esi:=true;
  2424. end;
  2425. if aktexit2label.is_used and not aktexit2label.is_set then
  2426. emitlab(aktexit2label);
  2427. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2428. begin
  2429. getlabel(stabsendlabel);
  2430. emitlab(stabsendlabel);
  2431. end;
  2432. { gives problems for long mangled names }
  2433. {List.concat(Tai_symbol.Create(aktprocdef.mangledname+'_end'));}
  2434. { should we restore edi ? }
  2435. { for all i386 gcc implementations }
  2436. if (po_savestdregs in aktprocdef.procoptions) then
  2437. begin
  2438. if (aktprocdef.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2439. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  2440. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  2441. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  2442. { here we could reset R_EBX
  2443. but that is risky because it only works
  2444. if genexitcode is called after genentrycode
  2445. so lets skip this for the moment PM
  2446. aktprocdef.usedregisters:=
  2447. aktprocdef.usedregisters or not ($80 shr byte(R_EBX));
  2448. }
  2449. end;
  2450. { for the save all registers we can simply use a pusha,popa which
  2451. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2452. if (po_saveregisters in aktprocdef.procoptions) then
  2453. begin
  2454. if uses_esi then
  2455. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4)));
  2456. if uses_edx then
  2457. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20)));
  2458. if uses_eax then
  2459. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28)));
  2460. exprasmList.concat(Taicpu.Op_none(A_POPA,S_L));
  2461. { We add a NOP because of the 386DX CPU bugs with POPAD }
  2462. exprasmlist.concat(taicpu.op_none(A_NOP,S_L));
  2463. end;
  2464. if not(nostackframe) then
  2465. begin
  2466. if not inlined then
  2467. exprasmList.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  2468. end
  2469. else
  2470. begin
  2471. if (gettempsize<>0) and not inlined then
  2472. exprasmList.insert(Taicpu.op_const_reg(A_ADD,S_L,gettempsize,R_ESP));
  2473. end;
  2474. { parameters are limited to 65535 bytes because }
  2475. { ret allows only imm16 }
  2476. if (parasize>65535) and not(po_clearstack in aktprocdef.procoptions) then
  2477. CGMessage(cg_e_parasize_too_big);
  2478. { at last, the return is generated }
  2479. if not inlined then
  2480. if (po_interrupt in aktprocdef.procoptions) then
  2481. begin
  2482. if uses_esi then
  2483. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16)));
  2484. if uses_edx then
  2485. begin
  2486. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2487. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12)));
  2488. end;
  2489. if uses_eax then
  2490. begin
  2491. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2492. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0)));
  2493. end;
  2494. generate_interrupt_stackframe_exit;
  2495. end
  2496. else
  2497. begin
  2498. {Routines with the poclearstack flag set use only a ret.}
  2499. { also routines with parasize=0 }
  2500. if (po_clearstack in aktprocdef.procoptions) then
  2501. begin
  2502. {$ifndef OLD_C_STACK}
  2503. { complex return values are removed from stack in C code PM }
  2504. if ret_in_param(aktprocdef.rettype.def) then
  2505. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,4))
  2506. else
  2507. {$endif not OLD_C_STACK}
  2508. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2509. end
  2510. else if (parasize=0) then
  2511. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO))
  2512. else
  2513. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  2514. end;
  2515. if not inlined then
  2516. exprasmList.concat(Tai_symbol_end.Createname(aktprocdef.mangledname));
  2517. {$ifdef GDB}
  2518. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2519. begin
  2520. aktprocdef.concatstabto(exprasmlist);
  2521. if assigned(procinfo^._class) then
  2522. if (not assigned(procinfo^.parent) or
  2523. not assigned(procinfo^.parent^._class)) then
  2524. begin
  2525. if (po_classmethod in aktprocdef.procoptions) or
  2526. ((po_virtualmethod in aktprocdef.procoptions) and
  2527. (potype_constructor=aktprocdef.proctypeoption)) or
  2528. (po_staticmethod in aktprocdef.procoptions) then
  2529. begin
  2530. exprasmList.concat(Tai_stabs.Create(strpnew(
  2531. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  2532. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2533. end
  2534. else
  2535. begin
  2536. if not(is_class(procinfo^._class)) then
  2537. st:='v'
  2538. else
  2539. st:='p';
  2540. exprasmList.concat(Tai_stabs.Create(strpnew(
  2541. '"$t:'+st+procinfo^._class.numberstring+'",'+
  2542. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2543. end;
  2544. end
  2545. else
  2546. begin
  2547. if not is_class(procinfo^._class) then
  2548. st:='*'
  2549. else
  2550. st:='';
  2551. exprasmList.concat(Tai_stabs.Create(strpnew(
  2552. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  2553. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI]))));
  2554. end;
  2555. { define calling EBP as pseudo local var PM }
  2556. { this enables test if the function is a local one !! }
  2557. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2558. exprasmList.concat(Tai_stabs.Create(strpnew(
  2559. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  2560. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  2561. if (not is_void(aktprocdef.rettype.def)) then
  2562. begin
  2563. if ret_in_param(aktprocdef.rettype.def) then
  2564. exprasmList.concat(Tai_stabs.Create(strpnew(
  2565. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  2566. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2567. else
  2568. exprasmList.concat(Tai_stabs.Create(strpnew(
  2569. '"'+aktprocsym.name+':X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  2570. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2571. if (m_result in aktmodeswitches) then
  2572. if ret_in_param(aktprocdef.rettype.def) then
  2573. exprasmList.concat(Tai_stabs.Create(strpnew(
  2574. '"RESULT:X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  2575. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2576. else
  2577. exprasmList.concat(Tai_stabs.Create(strpnew(
  2578. '"RESULT:X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  2579. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2580. end;
  2581. mangled_length:=length(aktprocdef.mangledname);
  2582. getmem(p,2*mangled_length+50);
  2583. strpcopy(p,'192,0,0,');
  2584. strpcopy(strend(p),aktprocdef.mangledname);
  2585. if (target_info.use_function_relative_addresses) then
  2586. begin
  2587. strpcopy(strend(p),'-');
  2588. strpcopy(strend(p),aktprocdef.mangledname);
  2589. end;
  2590. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2591. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  2592. +aktprocdef.mangledname))));
  2593. p[0]:='2';p[1]:='2';p[2]:='4';
  2594. strpcopy(strend(p),'_end');}
  2595. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  2596. if (target_info.use_function_relative_addresses) then
  2597. begin
  2598. strpcopy(strend(p),'-');
  2599. strpcopy(strend(p),aktprocdef.mangledname);
  2600. end;
  2601. exprasmList.concatlist(withdebuglist);
  2602. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2603. { strpnew('224,0,0,'
  2604. +aktprocdef.mangledname+'_end'))));}
  2605. freemem(p,2*mangled_length+50);
  2606. end;
  2607. {$endif GDB}
  2608. if inlined then
  2609. cleanup_regvars(exprasmlist);
  2610. exprasmlist:=oldexprasmlist;
  2611. end;
  2612. procedure genimplicitunitfinal(alist : TAAsmoutput);
  2613. begin
  2614. { using current_module.globalsymtable is hopefully }
  2615. { more robust than symtablestack and symtablestack.next }
  2616. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2617. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2618. exprasmList.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  2619. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  2620. {$ifdef GDB}
  2621. if (cs_debuginfo in aktmoduleswitches) and
  2622. target_info.use_function_relative_addresses then
  2623. exprasmList.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  2624. {$endif GDB}
  2625. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2626. aList.concatlist(exprasmlist);
  2627. end;
  2628. procedure genimplicitunitinit(alist : TAAsmoutput);
  2629. begin
  2630. { using current_module.globalsymtable is hopefully }
  2631. { more robust than symtablestack and symtablestack.next }
  2632. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2633. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2634. exprasmList.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  2635. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  2636. {$ifdef GDB}
  2637. if (cs_debuginfo in aktmoduleswitches) and
  2638. target_info.use_function_relative_addresses then
  2639. exprasmList.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  2640. {$endif GDB}
  2641. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2642. aList.concatlist(exprasmlist);
  2643. end;
  2644. {$ifdef test_dest_loc}
  2645. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2646. begin
  2647. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2648. begin
  2649. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2650. set_location(p^.location,dest_loc);
  2651. in_dest_loc:=true;
  2652. end
  2653. else
  2654. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2655. begin
  2656. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference)));
  2657. set_location(p^.location,dest_loc);
  2658. in_dest_loc:=true;
  2659. end
  2660. else
  2661. internalerror(20080);
  2662. end;
  2663. {$endif test_dest_loc}
  2664. end.
  2665. {
  2666. $Log$
  2667. Revision 1.11 2001-11-18 18:59:59 peter
  2668. * changed aktprocsym to aktprocdef for stabs generation
  2669. Revision 1.10 2001/11/06 16:39:02 jonas
  2670. * moved call to "cleanup_regvars" to cga.pas for i386 because it has
  2671. to insert "fstp %st0" instructions after the exit label
  2672. Revision 1.9 2001/11/02 22:58:09 peter
  2673. * procsym definition rewrite
  2674. Revision 1.8 2001/10/25 21:22:41 peter
  2675. * calling convention rewrite
  2676. Revision 1.7 2001/10/20 17:22:57 peter
  2677. * concatcopy could release a wrong reference because the offset was
  2678. increased without restoring the original before the release of
  2679. a temp
  2680. Revision 1.6 2001/10/14 11:49:51 jonas
  2681. * finetuned register allocation info for assignments
  2682. Revision 1.5 2001/09/30 21:28:34 peter
  2683. * int64->boolean fixed
  2684. Revision 1.4 2001/08/30 20:13:57 peter
  2685. * rtti/init table updates
  2686. * rttisym for reusable global rtti/init info
  2687. * support published for interfaces
  2688. Revision 1.3 2001/08/29 12:01:47 jonas
  2689. + support for int64 LOC_REGISTERS in remove_non_regvars_from_loc
  2690. Revision 1.2 2001/08/26 13:36:52 florian
  2691. * some cg reorganisation
  2692. * some PPC updates
  2693. Revision 1.29 2001/08/12 20:23:02 peter
  2694. * netbsd doesn't use stackchecking
  2695. Revision 1.28 2001/08/07 18:47:13 peter
  2696. * merged netbsd start
  2697. * profile for win32
  2698. Revision 1.27 2001/08/06 21:40:49 peter
  2699. * funcret moved from tprocinfo to tprocdef
  2700. Revision 1.26 2001/07/30 20:59:28 peter
  2701. * m68k updates from v10 merged
  2702. Revision 1.25 2001/07/01 20:16:18 peter
  2703. * alignmentinfo record added
  2704. * -Oa argument supports more alignment settings that can be specified
  2705. per type: PROC,LOOP,VARMIN,VARMAX,CONSTMIN,CONSTMAX,RECORDMIN
  2706. RECORDMAX,LOCALMIN,LOCALMAX. It is possible to set the mimimum
  2707. required alignment and the maximum usefull alignment. The final
  2708. alignment will be choosen per variable size dependent on these
  2709. settings
  2710. Revision 1.24 2001/05/27 14:30:55 florian
  2711. + some widestring stuff added
  2712. Revision 1.23 2001/04/21 13:33:16 peter
  2713. * move winstackpagesize const to cgai386 to remove uses t_win32
  2714. Revision 1.22 2001/04/21 12:05:32 peter
  2715. * add nop after popa (merged)
  2716. Revision 1.21 2001/04/18 22:02:00 peter
  2717. * registration of targets and assemblers
  2718. Revision 1.20 2001/04/13 01:22:17 peter
  2719. * symtable change to classes
  2720. * range check generation and errors fixed, make cycle DEBUG=1 works
  2721. * memory leaks fixed
  2722. Revision 1.19 2001/04/05 21:33:07 peter
  2723. * fast exit fix merged
  2724. Revision 1.18 2001/04/02 21:20:35 peter
  2725. * resulttype rewrite
  2726. Revision 1.17 2001/01/05 17:36:58 florian
  2727. * the info about exception frames is stored now on the stack
  2728. instead on the heap
  2729. Revision 1.16 2000/12/25 00:07:31 peter
  2730. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2731. tlinkedlist objects)
  2732. Revision 1.15 2000/12/05 11:44:32 jonas
  2733. + new integer regvar handling, should be much more efficient
  2734. Revision 1.14 2000/11/29 00:30:43 florian
  2735. * unused units removed from uses clause
  2736. * some changes for widestrings
  2737. Revision 1.13 2000/11/28 00:28:07 pierre
  2738. * stabs fixing
  2739. Revision 1.12 2000/11/22 15:12:06 jonas
  2740. * fixed inline-related problems (partially "merges")
  2741. Revision 1.11 2000/11/17 10:30:24 florian
  2742. * passing interfaces as parameters fixed
  2743. Revision 1.10 2000/11/07 23:40:48 florian
  2744. + AfterConstruction and BeforeDestruction impemented
  2745. Revision 1.9 2000/11/06 23:49:20 florian
  2746. * fixed init_paras call
  2747. Revision 1.8 2000/11/06 23:15:01 peter
  2748. * added copyvaluepara call again
  2749. Revision 1.7 2000/11/04 14:25:23 florian
  2750. + merged Attila's changes for interfaces, not tested yet
  2751. Revision 1.6 2000/10/31 22:02:55 peter
  2752. * symtable splitted, no real code changes
  2753. Revision 1.5 2000/10/24 22:23:04 peter
  2754. * emitcall -> emitinsertcall for profiling (merged)
  2755. Revision 1.4 2000/10/24 12:47:45 jonas
  2756. * allocate registers which hold function result
  2757. Revision 1.3 2000/10/24 08:54:25 michael
  2758. + Extra patch from peter
  2759. Revision 1.2 2000/10/24 07:20:03 pierre
  2760. * fix for bug 1193 (merged)
  2761. Revision 1.1 2000/10/15 09:47:42 peter
  2762. * moved to i386/
  2763. Revision 1.19 2000/10/14 10:14:46 peter
  2764. * moehrendorf oct 2000 rewrite
  2765. Revision 1.18 2000/10/10 14:55:28 jonas
  2766. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2767. Revision 1.17 2000/10/01 19:48:23 peter
  2768. * lot of compile updates for cg11
  2769. Revision 1.16 2000/09/30 16:08:45 peter
  2770. * more cg11 updates
  2771. Revision 1.15 2000/09/24 15:06:12 peter
  2772. * use defines.inc
  2773. Revision 1.14 2000/09/16 12:22:52 peter
  2774. * freebsd support merged
  2775. Revision 1.13 2000/08/27 16:11:49 peter
  2776. * moved some util functions from globals,cobjects to cutils
  2777. * splitted files into finput,fmodule
  2778. Revision 1.12 2000/08/24 19:07:54 peter
  2779. * don't initialize if localvarsym is set because that varsym will
  2780. already be initialized
  2781. * first initialize local data before copy of value para's (merged)
  2782. Revision 1.11 2000/08/19 20:09:33 peter
  2783. * check size after checking openarray in push_value_para (merged)
  2784. Revision 1.10 2000/08/16 13:06:06 florian
  2785. + support of 64 bit integer constants
  2786. Revision 1.9 2000/08/10 18:42:03 peter
  2787. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2788. Revision 1.8 2000/08/07 11:29:40 jonas
  2789. + emit_push_mem_size() which pushes a value in memory of a certain size
  2790. * pushsetelement() and pushvaluepara() use this new procedure, because
  2791. otherwise they could sometimes try to push data past the end of the
  2792. heap, causing a crash
  2793. (merged from fixes branch)
  2794. Revision 1.7 2000/08/03 13:17:25 jonas
  2795. + allow regvars to be used inside inlined procs, which required the
  2796. following changes:
  2797. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2798. * moved all regvar related code to new regvars unit
  2799. + added pregvarinfo type to hcodegen
  2800. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2801. * deallocate the regvars of the caller in secondprocinline before
  2802. inlining the called procedure and reallocate them afterwards
  2803. Revision 1.6 2000/08/02 08:05:04 jonas
  2804. * fixed web bug1087
  2805. * allocate R_ECX explicitely if it's used
  2806. (merged from fixes branch)
  2807. Revision 1.5 2000/07/27 09:25:05 jonas
  2808. * moved locflags2reg() procedure from cg386add to cgai386
  2809. + added locjump2reg() procedure to cgai386
  2810. * fixed internalerror(2002) when the result of a case expression has
  2811. LOC_JUMP
  2812. (all merged from fixes branch)
  2813. Revision 1.4 2000/07/21 15:14:02 jonas
  2814. + added is_addr field for labels, if they are only used for getting the address
  2815. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2816. Revision 1.3 2000/07/13 12:08:25 michael
  2817. + patched to 1.1.0 with former 1.09patch from peter
  2818. Revision 1.2 2000/07/13 11:32:37 michael
  2819. + removed logs
  2820. }