cga.pas 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177
  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. helpsize : longint;
  1100. i : byte;
  1101. reg8,reg32 : tregister;
  1102. swap : boolean;
  1103. procedure maybepushecx;
  1104. begin
  1105. if not(R_ECX in unused) then
  1106. begin
  1107. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1108. ecxpushed:=true;
  1109. end
  1110. else getexplicitregister32(R_ECX);
  1111. end;
  1112. begin
  1113. {$IfNDef regallocfix}
  1114. If delsource then
  1115. del_reference(source);
  1116. {$EndIf regallocfix}
  1117. if (not loadref) and
  1118. ((size<=8) or
  1119. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1120. begin
  1121. helpsize:=size shr 2;
  1122. getexplicitregister32(R_EDI);
  1123. for i:=1 to helpsize do
  1124. begin
  1125. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1126. {$ifdef regallocfix}
  1127. If (size = 4) and delsource then
  1128. del_reference(source);
  1129. {$endif regallocfix}
  1130. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest)));
  1131. inc(source.offset,4);
  1132. inc(dest.offset,4);
  1133. dec(size,4);
  1134. end;
  1135. if size>1 then
  1136. begin
  1137. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1138. {$ifdef regallocfix}
  1139. If (size = 2) and delsource then
  1140. del_reference(source);
  1141. {$endif regallocfix}
  1142. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_W,R_DI,newreference(dest)));
  1143. inc(source.offset,2);
  1144. inc(dest.offset,2);
  1145. dec(size,2);
  1146. end;
  1147. ungetregister32(R_EDI);
  1148. if size>0 then
  1149. begin
  1150. { and now look for an 8 bit register }
  1151. swap:=false;
  1152. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1153. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1154. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1155. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1156. else
  1157. begin
  1158. swap:=true;
  1159. { we need only to check 3 registers, because }
  1160. { one is always not index or base }
  1161. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1162. begin
  1163. reg8:=R_AL;
  1164. reg32:=R_EAX;
  1165. end
  1166. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1167. begin
  1168. reg8:=R_BL;
  1169. reg32:=R_EBX;
  1170. end
  1171. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1172. begin
  1173. reg8:=R_CL;
  1174. reg32:=R_ECX;
  1175. end;
  1176. end;
  1177. if swap then
  1178. { was earlier XCHG, of course nonsense }
  1179. begin
  1180. getexplicitregister32(R_EDI);
  1181. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1182. end;
  1183. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1184. {$ifdef regallocfix}
  1185. If delsource then
  1186. del_reference(source);
  1187. {$endif regallocfix}
  1188. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_B,reg8,newreference(dest)));
  1189. if swap then
  1190. begin
  1191. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1192. ungetregister32(R_EDI);
  1193. end
  1194. else
  1195. ungetregister(reg8);
  1196. end;
  1197. end
  1198. else
  1199. begin
  1200. getexplicitregister32(R_EDI);
  1201. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1202. {$ifdef regallocfix}
  1203. {is this ok?? (JM)}
  1204. del_reference(dest);
  1205. {$endif regallocfix}
  1206. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1207. if loadref then
  1208. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1209. else
  1210. begin
  1211. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1212. {$ifdef regallocfix}
  1213. if delsource then
  1214. del_reference(source);
  1215. {$endif regallocfix}
  1216. end;
  1217. exprasmList.concat(Taicpu.Op_none(A_CLD,S_NO));
  1218. ecxpushed:=false;
  1219. if cs_littlesize in aktglobalswitches then
  1220. begin
  1221. maybepushecx;
  1222. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1223. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1224. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1225. end
  1226. else
  1227. begin
  1228. helpsize:=size shr 2;
  1229. size:=size and 3;
  1230. if helpsize>1 then
  1231. begin
  1232. maybepushecx;
  1233. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1234. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1235. end;
  1236. if helpsize>0 then
  1237. exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1238. if size>1 then
  1239. begin
  1240. dec(size,2);
  1241. exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1242. end;
  1243. if size=1 then
  1244. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1245. end;
  1246. ungetregister32(R_EDI);
  1247. exprasmList.concat(Tairegalloc.DeAlloc(R_ESI));
  1248. if ecxpushed then
  1249. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1250. else
  1251. ungetregister32(R_ECX);
  1252. { loading SELF-reference again }
  1253. maybe_loadself;
  1254. end;
  1255. if delsource then
  1256. ungetiftemp(source);
  1257. end;
  1258. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;
  1259. destreg:Tregister;delloc:boolean);
  1260. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1261. var tai:Taicpu;
  1262. r:Preference;
  1263. begin
  1264. tai := nil;
  1265. case location.loc of
  1266. LOC_REGISTER,LOC_CREGISTER:
  1267. begin
  1268. case orddef.typ of
  1269. u8bit,uchar,bool8bit:
  1270. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_BL,location.register,destreg);
  1271. s8bit:
  1272. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_BL,location.register,destreg);
  1273. u16bit,uwidechar,bool16bit:
  1274. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_WL,location.register,destreg);
  1275. s16bit:
  1276. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_WL,location.register,destreg);
  1277. u32bit,bool32bit,s32bit:
  1278. if location.register <> destreg then
  1279. tai:=Taicpu.Op_reg_reg(A_MOV,S_L,location.register,destreg);
  1280. else
  1281. internalerror(330);
  1282. end;
  1283. if delloc then
  1284. ungetregister(location.register);
  1285. end;
  1286. LOC_MEM,
  1287. LOC_REFERENCE:
  1288. begin
  1289. if location.reference.is_immediate then
  1290. tai:=Taicpu.Op_const_reg(A_MOV,S_L,location.reference.offset,destreg)
  1291. else
  1292. begin
  1293. r:=newreference(location.reference);
  1294. case orddef.typ of
  1295. u8bit,uchar,bool8bit:
  1296. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_BL,r,destreg);
  1297. s8bit:
  1298. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_BL,r,destreg);
  1299. u16bit,uwidechar,bool16bit:
  1300. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_WL,r,destreg);
  1301. s16bit:
  1302. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_WL,r,destreg);
  1303. u32bit,bool32bit:
  1304. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1305. s32bit:
  1306. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1307. else
  1308. internalerror(330);
  1309. end;
  1310. end;
  1311. if delloc then
  1312. del_reference(location.reference);
  1313. end
  1314. else
  1315. internalerror(6);
  1316. end;
  1317. if assigned(tai) then
  1318. exprasmList.concat(tai);
  1319. end;
  1320. { if necessary ESI is reloaded after a call}
  1321. procedure maybe_loadself;
  1322. var
  1323. hp : preference;
  1324. p : pprocinfo;
  1325. i : longint;
  1326. begin
  1327. if assigned(procinfo^._class) then
  1328. begin
  1329. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1330. if lexlevel>normal_function_level then
  1331. begin
  1332. new(hp);
  1333. reset_reference(hp^);
  1334. hp^.offset:=procinfo^.framepointer_offset;
  1335. hp^.base:=procinfo^.framepointer;
  1336. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1337. p:=procinfo^.parent;
  1338. for i:=3 to lexlevel-1 do
  1339. begin
  1340. new(hp);
  1341. reset_reference(hp^);
  1342. hp^.offset:=p^.framepointer_offset;
  1343. hp^.base:=R_ESI;
  1344. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1345. p:=p^.parent;
  1346. end;
  1347. new(hp);
  1348. reset_reference(hp^);
  1349. hp^.offset:=p^.selfpointer_offset;
  1350. hp^.base:=R_ESI;
  1351. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1352. end
  1353. else
  1354. begin
  1355. new(hp);
  1356. reset_reference(hp^);
  1357. hp^.offset:=procinfo^.selfpointer_offset;
  1358. hp^.base:=procinfo^.framepointer;
  1359. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1360. end;
  1361. end;
  1362. end;
  1363. {*****************************************************************************
  1364. Entry/Exit Code Functions
  1365. *****************************************************************************}
  1366. procedure genprofilecode;
  1367. var
  1368. pl : tasmlabel;
  1369. begin
  1370. if (po_assembler in aktprocsym.definition.procoptions) then
  1371. exit;
  1372. case target_info.target of
  1373. target_i386_win32,
  1374. target_i386_freebsd,
  1375. target_i386_linux:
  1376. begin
  1377. getaddrlabel(pl);
  1378. emitinsertcall(target_info.Cprefix+'mcount');
  1379. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1380. exprasmList.insert(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1381. exprasmList.insert(Tai_section.Create(sec_code));
  1382. exprasmList.insert(Tai_const.Create_32bit(0));
  1383. exprasmList.insert(Tai_label.Create(pl));
  1384. exprasmList.insert(Tai_align.Create(4));
  1385. exprasmList.insert(Tai_section.Create(sec_data));
  1386. end;
  1387. target_i386_go32v2:
  1388. begin
  1389. emitinsertcall('MCOUNT');
  1390. end;
  1391. end;
  1392. end;
  1393. procedure generate_interrupt_stackframe_entry;
  1394. begin
  1395. { save the registers of an interrupt procedure }
  1396. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1397. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1398. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1399. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1400. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1401. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1402. { .... also the segment registers }
  1403. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1404. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1405. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1406. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1407. end;
  1408. procedure generate_interrupt_stackframe_exit;
  1409. begin
  1410. { restore the registers of an interrupt procedure }
  1411. { this was all with entrycode instead of exitcode !!}
  1412. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1413. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1414. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1415. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1416. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1417. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1418. { .... also the segment registers }
  1419. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1420. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1421. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1422. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1423. { this restores the flags }
  1424. procinfo^.aktexitcode.concat(Taicpu.Op_none(A_IRET,S_NO));
  1425. end;
  1426. { generates the code for threadvar initialisation }
  1427. procedure initialize_threadvar(p : tnamedindexitem);
  1428. var
  1429. hr : treference;
  1430. begin
  1431. if (tsym(p).typ=varsym) and
  1432. (vo_is_thread_var in tvarsym(p).varoptions) then
  1433. begin
  1434. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,tvarsym(p).getsize));
  1435. reset_reference(hr);
  1436. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1437. emitpushreferenceaddr(hr);
  1438. saveregvars($ff);
  1439. emitcall('FPC_INIT_THREADVAR');
  1440. end;
  1441. end;
  1442. { initilizes data of type t }
  1443. { if is_already_ref is true then the routines assumes }
  1444. { that r points to the data to initialize }
  1445. procedure initialize(t : tdef;const ref : treference;is_already_ref : boolean);
  1446. var
  1447. hr : treference;
  1448. begin
  1449. if is_ansistring(t) or
  1450. is_widestring(t) or
  1451. is_interfacecom(t) then
  1452. begin
  1453. emit_const_ref(A_MOV,S_L,0,
  1454. newreference(ref));
  1455. end
  1456. else
  1457. begin
  1458. reset_reference(hr);
  1459. hr.symbol:=tstoreddef(t).get_rtti_label(initrtti);
  1460. emitpushreferenceaddr(hr);
  1461. if is_already_ref then
  1462. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)))
  1463. else
  1464. emitpushreferenceaddr(ref);
  1465. emitcall('FPC_INITIALIZE');
  1466. end;
  1467. end;
  1468. { finalizes data of type t }
  1469. { if is_already_ref is true then the routines assumes }
  1470. { that r points to the data to finalizes }
  1471. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  1472. var
  1473. r : treference;
  1474. begin
  1475. if is_ansistring(t) or
  1476. is_widestring(t) then
  1477. begin
  1478. decrstringref(t,ref);
  1479. end
  1480. else if is_interfacecom(t) then
  1481. begin
  1482. decrcomintfref(t,ref);
  1483. end
  1484. else
  1485. begin
  1486. reset_reference(r);
  1487. r.symbol:=tstoreddef(t).get_rtti_label(initrtti);
  1488. emitpushreferenceaddr(r);
  1489. if is_already_ref then
  1490. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1491. newreference(ref)))
  1492. else
  1493. emitpushreferenceaddr(ref);
  1494. emitcall('FPC_FINALIZE');
  1495. end;
  1496. end;
  1497. { generates the code for initialisation of local data }
  1498. procedure initialize_data(p : tnamedindexitem);
  1499. var
  1500. hr : treference;
  1501. begin
  1502. if (tsym(p).typ=varsym) and
  1503. assigned(tvarsym(p).vartype.def) and
  1504. not(is_class(tvarsym(p).vartype.def)) and
  1505. tvarsym(p).vartype.def.needs_inittable then
  1506. begin
  1507. if assigned(procinfo) then
  1508. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1509. reset_reference(hr);
  1510. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  1511. begin
  1512. hr.base:=procinfo^.framepointer;
  1513. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1514. end
  1515. else
  1516. begin
  1517. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1518. end;
  1519. initialize(tvarsym(p).vartype.def,hr,false);
  1520. end;
  1521. end;
  1522. { generates the code for incrementing the reference count of parameters and
  1523. initialize out parameters }
  1524. procedure init_paras(p : tnamedindexitem);
  1525. var
  1526. hrv : treference;
  1527. hr: treference;
  1528. begin
  1529. if (tsym(p).typ=varsym) and
  1530. not is_class(tvarsym(p).vartype.def) and
  1531. tvarsym(p).vartype.def.needs_inittable then
  1532. begin
  1533. if (tvarsym(p).varspez=vs_value) then
  1534. begin
  1535. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1536. reset_reference(hrv);
  1537. hrv.base:=procinfo^.framepointer;
  1538. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1539. if is_ansistring(tvarsym(p).vartype.def) or
  1540. is_widestring(tvarsym(p).vartype.def) then
  1541. begin
  1542. incrstringref(tvarsym(p).vartype.def,hrv)
  1543. end
  1544. else if is_interfacecom(tvarsym(p).vartype.def) then
  1545. begin
  1546. incrcomintfref(tvarsym(p).vartype.def,hrv)
  1547. end
  1548. else
  1549. begin
  1550. reset_reference(hr);
  1551. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_rtti_label(initrtti);
  1552. emitpushreferenceaddr(hr);
  1553. emitpushreferenceaddr(hrv);
  1554. emitcall('FPC_ADDREF');
  1555. end;
  1556. end
  1557. else if (tvarsym(p).varspez=vs_out) then
  1558. begin
  1559. reset_reference(hrv);
  1560. hrv.base:=procinfo^.framepointer;
  1561. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1562. getexplicitregister32(R_EDI);
  1563. exprasmList.concat(Taicpu.Op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI));
  1564. reset_reference(hr);
  1565. hr.base:=R_EDI;
  1566. initialize(tvarsym(p).vartype.def,hr,false);
  1567. end;
  1568. end;
  1569. end;
  1570. { generates the code for decrementing the reference count of parameters }
  1571. procedure final_paras(p : tnamedindexitem);
  1572. var
  1573. hrv : treference;
  1574. hr: treference;
  1575. begin
  1576. if (tsym(p).typ=varsym) and
  1577. not is_class(tvarsym(p).vartype.def) and
  1578. tvarsym(p).vartype.def.needs_inittable then
  1579. begin
  1580. if (tvarsym(p).varspez=vs_value) then
  1581. begin
  1582. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1583. reset_reference(hrv);
  1584. hrv.base:=procinfo^.framepointer;
  1585. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1586. if is_ansistring(tvarsym(p).vartype.def) or
  1587. is_widestring(tvarsym(p).vartype.def) then
  1588. begin
  1589. decrstringref(tvarsym(p).vartype.def,hrv)
  1590. end
  1591. else if is_interfacecom(tvarsym(p).vartype.def) then
  1592. begin
  1593. decrcomintfref(tvarsym(p).vartype.def,hrv)
  1594. end
  1595. else
  1596. begin
  1597. reset_reference(hr);
  1598. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_rtti_label(initrtti);
  1599. emitpushreferenceaddr(hr);
  1600. emitpushreferenceaddr(hrv);
  1601. emitcall('FPC_DECREF');
  1602. end;
  1603. end;
  1604. end;
  1605. end;
  1606. { generates the code for finalisation of local data }
  1607. procedure finalize_data(p : tnamedindexitem);
  1608. var
  1609. hr : treference;
  1610. begin
  1611. if (tsym(p).typ=varsym) and
  1612. assigned(tvarsym(p).vartype.def) and
  1613. not(is_class(tvarsym(p).vartype.def)) and
  1614. tvarsym(p).vartype.def.needs_inittable then
  1615. begin
  1616. if assigned(procinfo) then
  1617. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1618. reset_reference(hr);
  1619. case tsym(p).owner.symtabletype of
  1620. localsymtable,inlinelocalsymtable:
  1621. begin
  1622. hr.base:=procinfo^.framepointer;
  1623. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1624. end;
  1625. else
  1626. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1627. end;
  1628. finalize(tvarsym(p).vartype.def,hr,false);
  1629. end;
  1630. end;
  1631. { generates the code to make local copies of the value parameters }
  1632. procedure copyvalueparas(p : tnamedindexitem);
  1633. var
  1634. href1,href2 : treference;
  1635. r : preference;
  1636. power,len : longint;
  1637. opsize : topsize;
  1638. {$ifndef NOTARGETWIN32}
  1639. again,ok : tasmlabel;
  1640. {$endif}
  1641. begin
  1642. if (tsym(p).typ=varsym) and
  1643. (tvarsym(p).varspez=vs_value) and
  1644. (push_addr_param(tvarsym(p).vartype.def)) then
  1645. begin
  1646. if is_open_array(tvarsym(p).vartype.def) or
  1647. is_array_of_const(tvarsym(p).vartype.def) then
  1648. begin
  1649. { get stack space }
  1650. new(r);
  1651. reset_reference(r^);
  1652. r^.base:=procinfo^.framepointer;
  1653. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1654. getexplicitregister32(R_EDI);
  1655. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1656. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1657. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1658. begin
  1659. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1660. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1661. else
  1662. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1663. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1664. end;
  1665. {$ifndef NOTARGETWIN32}
  1666. { windows guards only a few pages for stack growing, }
  1667. { so we have to access every page first }
  1668. if target_info.target=target_i386_win32 then
  1669. begin
  1670. getlabel(again);
  1671. getlabel(ok);
  1672. emitlab(again);
  1673. exprasmList.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1674. emitjmp(C_C,ok);
  1675. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1676. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1677. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1678. emitjmp(C_None,again);
  1679. emitlab(ok);
  1680. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1681. ungetregister32(R_EDI);
  1682. { now reload EDI }
  1683. new(r);
  1684. reset_reference(r^);
  1685. r^.base:=procinfo^.framepointer;
  1686. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1687. getexplicitregister32(R_EDI);
  1688. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1689. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1690. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1691. begin
  1692. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1693. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1694. else
  1695. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1696. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1697. end;
  1698. end
  1699. else
  1700. {$endif NOTARGETWIN32}
  1701. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1702. { load destination }
  1703. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1704. { don't destroy the registers! }
  1705. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1706. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1707. { load count }
  1708. new(r);
  1709. reset_reference(r^);
  1710. r^.base:=procinfo^.framepointer;
  1711. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1712. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ECX));
  1713. { load source }
  1714. new(r);
  1715. reset_reference(r^);
  1716. r^.base:=procinfo^.framepointer;
  1717. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1718. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ESI));
  1719. { scheduled .... }
  1720. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1721. { calculate size }
  1722. len:=tarraydef(tvarsym(p).vartype.def).elesize;
  1723. opsize:=S_B;
  1724. if (len and 3)=0 then
  1725. begin
  1726. opsize:=S_L;
  1727. len:=len shr 2;
  1728. end
  1729. else
  1730. if (len and 1)=0 then
  1731. begin
  1732. opsize:=S_W;
  1733. len:=len shr 1;
  1734. end;
  1735. if ispowerof2(len, power) then
  1736. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1737. else
  1738. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1739. exprasmList.concat(Taicpu.op_none(A_REP,S_NO));
  1740. case opsize of
  1741. S_B : exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1742. S_W : exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1743. S_L : exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1744. end;
  1745. ungetregister32(R_EDI);
  1746. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1747. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1748. { patch the new address }
  1749. new(r);
  1750. reset_reference(r^);
  1751. r^.base:=procinfo^.framepointer;
  1752. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1753. exprasmList.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,r));
  1754. end
  1755. else
  1756. if is_shortstring(tvarsym(p).vartype.def) then
  1757. begin
  1758. reset_reference(href1);
  1759. href1.base:=procinfo^.framepointer;
  1760. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1761. reset_reference(href2);
  1762. href2.base:=procinfo^.framepointer;
  1763. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1764. copyshortstring(href2,href1,tstringdef(tvarsym(p).vartype.def).len,true,false);
  1765. end
  1766. else
  1767. begin
  1768. reset_reference(href1);
  1769. href1.base:=procinfo^.framepointer;
  1770. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1771. reset_reference(href2);
  1772. href2.base:=procinfo^.framepointer;
  1773. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1774. concatcopy(href1,href2,tvarsym(p).vartype.def.size,true,true);
  1775. end;
  1776. end;
  1777. end;
  1778. procedure inittempvariables;
  1779. var
  1780. hp : ptemprecord;
  1781. r : preference;
  1782. begin
  1783. hp:=templist;
  1784. while assigned(hp) do
  1785. begin
  1786. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  1787. tt_widestring,tt_freewidestring,
  1788. tt_interfacecom] then
  1789. begin
  1790. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1791. new(r);
  1792. reset_reference(r^);
  1793. r^.base:=procinfo^.framepointer;
  1794. r^.offset:=hp^.pos;
  1795. emit_const_ref(A_MOV,S_L,0,r);
  1796. end;
  1797. hp:=hp^.next;
  1798. end;
  1799. end;
  1800. procedure finalizetempvariables;
  1801. var
  1802. hp : ptemprecord;
  1803. hr : treference;
  1804. begin
  1805. hp:=templist;
  1806. while assigned(hp) do
  1807. begin
  1808. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1809. begin
  1810. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1811. reset_reference(hr);
  1812. hr.base:=procinfo^.framepointer;
  1813. hr.offset:=hp^.pos;
  1814. emitpushreferenceaddr(hr);
  1815. emitcall('FPC_ANSISTR_DECR_REF');
  1816. end
  1817. else if hp^.temptype in [tt_widestring,tt_freewidestring] then
  1818. begin
  1819. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1820. reset_reference(hr);
  1821. hr.base:=procinfo^.framepointer;
  1822. hr.offset:=hp^.pos;
  1823. emitpushreferenceaddr(hr);
  1824. emitcall('FPC_WIDESTR_DECR_REF');
  1825. end
  1826. else if hp^.temptype=tt_interfacecom then
  1827. begin
  1828. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1829. reset_reference(hr);
  1830. hr.base:=procinfo^.framepointer;
  1831. hr.offset:=hp^.pos;
  1832. emitpushreferenceaddr(hr);
  1833. emitcall('FPC_INTF_DECR_REF');
  1834. end;
  1835. hp:=hp^.next;
  1836. end;
  1837. end;
  1838. {$ifdef dummy}
  1839. var
  1840. ls : longint;
  1841. procedure largest_size(p : tnamedindexitem);
  1842. begin
  1843. if (tsym(p).typ=varsym) and
  1844. (tvarsym(p).getvaluesize>ls) then
  1845. ls:=tvarsym(p).getvaluesize;
  1846. end;
  1847. {$endif dummy}
  1848. procedure alignstack(alist : TAAsmoutput);
  1849. begin
  1850. {$ifdef dummy}
  1851. if (cs_optimize in aktglobalswitches) and
  1852. (aktoptprocessor in [classp5,classp6]) then
  1853. begin
  1854. ls:=0;
  1855. aktprocsym.definition.localst.foreach({$ifndef TP}@{$endif}largest_size);
  1856. if ls>=8 then
  1857. aList.insert(Taicpu.Op_const_reg(A_AND,S_L,-8,R_ESP));
  1858. end;
  1859. {$endif dummy}
  1860. end;
  1861. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  1862. stackframe:longint;
  1863. var parasize:longint;var nostackframe:boolean;
  1864. inlined : boolean);
  1865. {
  1866. Generates the entry code for a procedure
  1867. }
  1868. var
  1869. hs : string;
  1870. {$ifdef GDB}
  1871. stab_function_name : tai_stab_function_name;
  1872. {$endif GDB}
  1873. hr : preference;
  1874. p : tsymtable;
  1875. r : treference;
  1876. oldlist,
  1877. oldexprasmlist : TAAsmoutput;
  1878. again : tasmlabel;
  1879. i : longint;
  1880. tempbuf,tempaddr : treference;
  1881. begin
  1882. oldexprasmlist:=exprasmlist;
  1883. exprasmlist:=alist;
  1884. if (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  1885. begin
  1886. emitinsertcall('FPC_INITIALIZEUNITS');
  1887. { initialize profiling for win32 }
  1888. if (target_info.target=target_I386_WIN32) and
  1889. (cs_profile in aktmoduleswitches) then
  1890. emitinsertcall('__monstartup');
  1891. { add threadvars }
  1892. oldlist:=exprasmlist;
  1893. exprasmlist:=TAAsmoutput.Create;
  1894. p:=symtablestack;
  1895. while assigned(p) do
  1896. begin
  1897. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar);
  1898. p:=p.next;
  1899. end;
  1900. oldList.insertlist(exprasmlist);
  1901. exprasmlist.free;
  1902. exprasmlist:=oldlist;
  1903. end;
  1904. {$ifdef GDB}
  1905. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1906. exprasmList.insert(Tai_force_line.Create);
  1907. {$endif GDB}
  1908. { a constructor needs a help procedure }
  1909. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  1910. begin
  1911. if is_class(procinfo^._class) then
  1912. begin
  1913. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1914. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1915. emitinsertcall('FPC_NEW_CLASS');
  1916. end
  1917. else if is_object(procinfo^._class) then
  1918. begin
  1919. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1920. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1921. getexplicitregister32(R_EDI);
  1922. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  1923. end
  1924. else
  1925. Internalerror(200006161);
  1926. end;
  1927. { don't load ESI, does the caller }
  1928. { we must do it for local function }
  1929. { that can be called from a foreach_static }
  1930. { of another object than self !! PM }
  1931. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1932. (lexlevel>normal_function_level) then
  1933. maybe_loadself;
  1934. { When message method contains self as a parameter,
  1935. we must load it into ESI }
  1936. If (po_containsself in aktprocsym.definition.procoptions) then
  1937. begin
  1938. new(hr);
  1939. reset_reference(hr^);
  1940. hr^.offset:=procinfo^.selfpointer_offset;
  1941. hr^.base:=procinfo^.framepointer;
  1942. exprasmList.insert(Taicpu.Op_ref_reg(A_MOV,S_L,hr,R_ESI));
  1943. exprasmList.insert(Tairegalloc.Alloc(R_ESI));
  1944. end;
  1945. { should we save edi,esi,ebx like C ? }
  1946. if (po_savestdregs in aktprocsym.definition.procoptions) then
  1947. begin
  1948. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1949. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1950. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1951. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1952. end;
  1953. { for the save all registers we can simply use a pusha,popa which
  1954. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1955. if (po_saveregisters in aktprocsym.definition.procoptions) then
  1956. begin
  1957. exprasmList.insert(Taicpu.Op_none(A_PUSHA,S_L));
  1958. end;
  1959. { omit stack frame ? }
  1960. if (not inlined) then
  1961. if (procinfo^.framepointer=stack_pointer) then
  1962. begin
  1963. CGMessage(cg_d_stackframe_omited);
  1964. nostackframe:=true;
  1965. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1966. parasize:=0
  1967. else
  1968. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-4;
  1969. if stackframe<>0 then
  1970. exprasmList.insert(Taicpu.op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  1971. end
  1972. else
  1973. begin
  1974. alignstack(alist);
  1975. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1976. parasize:=0
  1977. else
  1978. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-8;
  1979. nostackframe:=false;
  1980. if stackframe<>0 then
  1981. begin
  1982. {$ifndef NOTARGETWIN32}
  1983. { windows guards only a few pages for stack growing, }
  1984. { so we have to access every page first }
  1985. if (target_info.target=target_i386_win32) and
  1986. (stackframe>=winstackpagesize) then
  1987. begin
  1988. if stackframe div winstackpagesize<=5 then
  1989. begin
  1990. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe-4,R_ESP));
  1991. for i:=1 to stackframe div winstackpagesize do
  1992. begin
  1993. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  1994. exprasmList.concat(Taicpu.op_const_ref(A_MOV,S_L,0,hr));
  1995. end;
  1996. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1997. end
  1998. else
  1999. begin
  2000. getlabel(again);
  2001. getexplicitregister32(R_EDI);
  2002. exprasmList.concat(Taicpu.op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI));
  2003. emitlab(again);
  2004. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  2005. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2006. exprasmList.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  2007. emitjmp(C_NZ,again);
  2008. ungetregister32(R_EDI);
  2009. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP));
  2010. end
  2011. end
  2012. else
  2013. {$endif NOTARGETWIN32}
  2014. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  2015. if (cs_check_stack in aktlocalswitches) and
  2016. not(target_info.target in [target_i386_freebsd,target_i386_netbsd,
  2017. target_i386_linux,target_i386_win32]) then
  2018. begin
  2019. emitinsertcall('FPC_STACKCHECK');
  2020. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  2021. end;
  2022. if cs_profile in aktmoduleswitches then
  2023. genprofilecode;
  2024. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2025. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2026. end { endif stackframe <> 0 }
  2027. else
  2028. begin
  2029. if cs_profile in aktmoduleswitches then
  2030. genprofilecode;
  2031. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2032. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2033. end;
  2034. end;
  2035. if (po_interrupt in aktprocsym.definition.procoptions) then
  2036. generate_interrupt_stackframe_entry;
  2037. { initialize return value }
  2038. if (not is_void(aktprocsym.definition.rettype.def)) and
  2039. (aktprocsym.definition.rettype.def.needs_inittable) then
  2040. begin
  2041. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2042. reset_reference(r);
  2043. r.offset:=procinfo^.return_offset;
  2044. r.base:=procinfo^.framepointer;
  2045. initialize(aktprocsym.definition.rettype.def,r,ret_in_param(aktprocsym.definition.rettype.def));
  2046. end;
  2047. { initialisize local data like ansistrings }
  2048. case aktprocsym.definition.proctypeoption of
  2049. potype_unitinit:
  2050. begin
  2051. { using current_module.globalsymtable is hopefully }
  2052. { more robust than symtablestack and symtablestack.next }
  2053. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2054. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2055. end;
  2056. { units have seperate code for initilization and finalization }
  2057. potype_unitfinalize: ;
  2058. else
  2059. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}initialize_data);
  2060. end;
  2061. { initialisizes temp. ansi/wide string data }
  2062. inittempvariables;
  2063. { generate copies of call by value parameters }
  2064. if not(po_assembler in aktprocsym.definition.procoptions) and
  2065. (([pocall_cdecl,pocall_cppdecl]*aktprocsym.definition.proccalloptions)=[]) then
  2066. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas);
  2067. if assigned( aktprocsym.definition.parast) then
  2068. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}init_paras);
  2069. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2070. if not inlined and
  2071. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2072. { but it's useless in init/final code of units }
  2073. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2074. begin
  2075. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2076. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,36,R_ESP));
  2077. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  2078. reset_reference(tempaddr);
  2079. tempaddr.base:=R_EDI;
  2080. emitpushreferenceaddr(tempaddr);
  2081. reset_reference(tempbuf);
  2082. tempbuf.base:=R_EDI;
  2083. tempbuf.offset:=12;
  2084. emitpushreferenceaddr(tempbuf);
  2085. { Type of stack-frame must be pushed}
  2086. exprasmList.concat(Taicpu.op_const(A_PUSH,S_L,1));
  2087. emitcall('FPC_PUSHEXCEPTADDR');
  2088. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2089. emitcall('FPC_SETJMP');
  2090. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2091. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2092. emitjmp(C_NE,aktexitlabel);
  2093. { probably we've to reload self here }
  2094. maybe_loadself;
  2095. end;
  2096. if not inlined then
  2097. begin
  2098. if (cs_profile in aktmoduleswitches) or
  2099. (aktprocsym.definition.owner.symtabletype=globalsymtable) or
  2100. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  2101. make_global:=true;
  2102. hs:=aktprocsym.definition.aliasnames.getfirst;
  2103. {$ifdef GDB}
  2104. if (cs_debuginfo in aktmoduleswitches) and target_info.use_function_relative_addresses then
  2105. stab_function_name := Tai_stab_function_name.Create(strpnew(hs));
  2106. {$EndIf GDB}
  2107. while hs<>'' do
  2108. begin
  2109. if make_global then
  2110. exprasmList.insert(Tai_symbol.Createname_global(hs,0))
  2111. else
  2112. exprasmList.insert(Tai_symbol.Createname(hs,0));
  2113. {$ifdef GDB}
  2114. if (cs_debuginfo in aktmoduleswitches) and
  2115. target_info.use_function_relative_addresses then
  2116. exprasmList.insert(Tai_stab_function_name.Create(strpnew(hs)));
  2117. {$endif GDB}
  2118. hs:=aktprocsym.definition.aliasnames.getfirst;
  2119. end;
  2120. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2121. aktprocsym.is_global := True;
  2122. {$ifdef GDB}
  2123. if (cs_debuginfo in aktmoduleswitches) then
  2124. begin
  2125. if target_info.use_function_relative_addresses then
  2126. exprasmList.insert(stab_function_name);
  2127. exprasmList.insert(Tai_stabs.Create(aktprocsym.stabstring));
  2128. aktprocsym.isstabwritten:=true;
  2129. end;
  2130. {$endif GDB}
  2131. { Align, gprof uses 16 byte granularity }
  2132. if (cs_profile in aktmoduleswitches) then
  2133. exprasmList.insert(Tai_align.Create_op(16,$90))
  2134. else
  2135. exprasmList.insert(Tai_align.Create(aktalignment.procalign));
  2136. end;
  2137. if inlined then
  2138. load_regvars(exprasmlist,nil);
  2139. exprasmlist:=oldexprasmlist;
  2140. end;
  2141. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2142. var
  2143. hr : preference;
  2144. op : Tasmop;
  2145. s : Topsize;
  2146. begin
  2147. if not is_void(aktprocsym.definition.rettype.def) then
  2148. begin
  2149. {if ((procinfo^.flags and pi_operator)<>0) and
  2150. assigned(otsym) then
  2151. procinfo^.funcret_is_valid:=
  2152. procinfo^.funcret_is_valid or (otsym.refs>0);}
  2153. if (tfuncretsym(aktprocsym.definition.funcretsym).funcretstate<>vs_assigned) and not inlined { and
  2154. ((procinfo^.flags and pi_uses_asm)=0)} then
  2155. CGMessage(sym_w_function_result_not_set);
  2156. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2157. if (aktprocsym.definition.rettype.def.deftype in [orddef,enumdef]) then
  2158. begin
  2159. uses_eax:=true;
  2160. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2161. case aktprocsym.definition.rettype.def.size of
  2162. 8:
  2163. begin
  2164. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2165. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2166. exprasmList.concat(Tairegalloc.Alloc(R_EDX));
  2167. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2168. uses_edx:=true;
  2169. end;
  2170. 4:
  2171. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2172. 2:
  2173. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2174. 1:
  2175. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2176. end;
  2177. end
  2178. else
  2179. if ret_in_acc(aktprocsym.definition.rettype.def) then
  2180. begin
  2181. uses_eax:=true;
  2182. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2183. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2184. end
  2185. else
  2186. if (aktprocsym.definition.rettype.def.deftype=floatdef) then
  2187. begin
  2188. floatloadops(tfloatdef(aktprocsym.definition.rettype.def).typ,op,s);
  2189. exprasmList.concat(Taicpu.Op_ref(op,s,hr));
  2190. end
  2191. else
  2192. dispose(hr);
  2193. end
  2194. end;
  2195. procedure handle_fast_exit_return_value;
  2196. var
  2197. hr : preference;
  2198. op : Tasmop;
  2199. s : Topsize;
  2200. begin
  2201. if not is_void(aktprocsym.definition.rettype.def) then
  2202. begin
  2203. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2204. if (aktprocsym.definition.rettype.def.deftype in [orddef,enumdef]) then
  2205. begin
  2206. case aktprocsym.definition.rettype.def.size of
  2207. 8:
  2208. begin
  2209. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2210. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2211. emit_reg_ref(A_MOV,S_L,R_EDX,hr);
  2212. end;
  2213. 4:
  2214. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2215. 2:
  2216. emit_reg_ref(A_MOV,S_W,R_AX,hr);
  2217. 1:
  2218. emit_reg_ref(A_MOV,S_B,R_AL,hr);
  2219. end;
  2220. end
  2221. else
  2222. if ret_in_acc(aktprocsym.definition.rettype.def) then
  2223. begin
  2224. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2225. end
  2226. else
  2227. if (aktprocsym.definition.rettype.def.deftype=floatdef) then
  2228. begin
  2229. floatstoreops(tfloatdef(aktprocsym.definition.rettype.def).typ,op,s);
  2230. exprasmlist.concat(taicpu.op_ref(op,s,hr));
  2231. end
  2232. else
  2233. dispose(hr);
  2234. end
  2235. end;
  2236. procedure genexitcode(alist : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  2237. var
  2238. {$ifdef GDB}
  2239. mangled_length : longint;
  2240. p : pchar;
  2241. st : string[2];
  2242. {$endif GDB}
  2243. stabsendlabel,nofinal,okexitlabel,
  2244. noreraiselabel,nodestroycall : tasmlabel;
  2245. hr : treference;
  2246. uses_eax,uses_edx,uses_esi : boolean;
  2247. oldexprasmlist : TAAsmoutput;
  2248. ai : taicpu;
  2249. pd : tprocdef;
  2250. begin
  2251. oldexprasmlist:=exprasmlist;
  2252. exprasmlist:=alist;
  2253. if aktexit2label.is_used and
  2254. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  2255. begin
  2256. exprasmlist.concat(taicpu.op_sym(A_JMP,S_NO,aktexitlabel));
  2257. exprasmlist.concat(tai_label.create(aktexit2label));
  2258. handle_fast_exit_return_value;
  2259. end;
  2260. if aktexitlabel.is_used then
  2261. exprasmList.concat(Tai_label.Create(aktexitlabel));
  2262. { call the destructor help procedure }
  2263. if (aktprocsym.definition.proctypeoption=potype_destructor) and
  2264. assigned(procinfo^._class) then
  2265. begin
  2266. if is_class(procinfo^._class) then
  2267. begin
  2268. emitinsertcall('FPC_DISPOSE_CLASS');
  2269. end
  2270. else if is_object(procinfo^._class) then
  2271. begin
  2272. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2273. getexplicitregister32(R_EDI);
  2274. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  2275. { must the object be finalized ? }
  2276. if procinfo^._class.needs_inittable then
  2277. begin
  2278. getlabel(nofinal);
  2279. exprasmList.insert(Tai_label.Create(nofinal));
  2280. emitinsertcall('FPC_FINALIZE');
  2281. ungetregister32(R_EDI);
  2282. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  2283. exprasmList.insert(Taicpu.Op_sym(A_PUSH,S_L,procinfo^._class.get_rtti_label(initrtti)));
  2284. ai:=Taicpu.Op_sym(A_Jcc,S_NO,nofinal);
  2285. ai.SetCondition(C_Z);
  2286. exprasmList.insert(ai);
  2287. reset_reference(hr);
  2288. hr.base:=R_EBP;
  2289. hr.offset:=8;
  2290. exprasmList.insert(Taicpu.Op_const_ref(A_CMP,S_L,0,newreference(hr)));
  2291. end;
  2292. end
  2293. else
  2294. begin
  2295. Internalerror(200006161);
  2296. end;
  2297. end;
  2298. { finalize temporary data }
  2299. finalizetempvariables;
  2300. { finalize local data like ansistrings}
  2301. case aktprocsym.definition.proctypeoption of
  2302. potype_unitfinalize:
  2303. begin
  2304. { using current_module.globalsymtable is hopefully }
  2305. { more robust than symtablestack and symtablestack.next }
  2306. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2307. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2308. end;
  2309. { units have seperate code for initialization and finalization }
  2310. potype_unitinit: ;
  2311. else
  2312. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}finalize_data);
  2313. end;
  2314. { finalize paras data }
  2315. if assigned(aktprocsym.definition.parast) then
  2316. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}final_paras);
  2317. { do we need to handle exceptions because of ansi/widestrings ? }
  2318. if not inlined and
  2319. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2320. { but it's useless in init/final code of units }
  2321. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2322. begin
  2323. { the exception helper routines modify all registers }
  2324. aktprocsym.definition.usedregisters:=$ff;
  2325. getlabel(noreraiselabel);
  2326. emitcall('FPC_POPADDRSTACK');
  2327. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2328. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  2329. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2330. ungetregister32(R_EAX);
  2331. emitjmp(C_E,noreraiselabel);
  2332. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  2333. begin
  2334. if assigned(procinfo^._class) then
  2335. begin
  2336. pd:=procinfo^._class.searchdestructor;
  2337. if assigned(pd) then
  2338. begin
  2339. getlabel(nodestroycall);
  2340. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2341. procinfo^.selfpointer_offset));
  2342. emitjmp(C_E,nodestroycall);
  2343. if is_class(procinfo^._class) then
  2344. begin
  2345. emit_const(A_PUSH,S_L,1);
  2346. emit_reg(A_PUSH,S_L,R_ESI);
  2347. end
  2348. else if is_object(procinfo^._class) then
  2349. begin
  2350. emit_reg(A_PUSH,S_L,R_ESI);
  2351. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class.vmt_mangledname));
  2352. end
  2353. else
  2354. begin
  2355. Internalerror(200006161);
  2356. end;
  2357. if (po_virtualmethod in pd.procoptions) then
  2358. begin
  2359. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2360. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class.vmtmethodoffset(pd.extnumber)));
  2361. end
  2362. else
  2363. emitcall(pd.mangledname);
  2364. { not necessary because the result is never assigned in the
  2365. case of an exception (FK)
  2366. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2367. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2368. }
  2369. emitlab(nodestroycall);
  2370. end;
  2371. end
  2372. end
  2373. else
  2374. { must be the return value finalized before reraising the exception? }
  2375. if (not is_void(aktprocsym.definition.rettype.def)) and
  2376. (aktprocsym.definition.rettype.def.needs_inittable) and
  2377. ((aktprocsym.definition.rettype.def.deftype<>objectdef) or
  2378. not is_class(aktprocsym.definition.rettype.def)) then
  2379. begin
  2380. reset_reference(hr);
  2381. hr.offset:=procinfo^.return_offset;
  2382. hr.base:=procinfo^.framepointer;
  2383. finalize(aktprocsym.definition.rettype.def,hr,ret_in_param(aktprocsym.definition.rettype.def));
  2384. end;
  2385. emitcall('FPC_RERAISE');
  2386. emitlab(noreraiselabel);
  2387. end;
  2388. { call __EXIT for main program }
  2389. if (not DLLsource) and (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  2390. begin
  2391. emitcall('FPC_DO_EXIT');
  2392. end;
  2393. { handle return value }
  2394. uses_eax:=false;
  2395. uses_edx:=false;
  2396. uses_esi:=false;
  2397. if not(po_assembler in aktprocsym.definition.procoptions) then
  2398. if (aktprocsym.definition.proctypeoption<>potype_constructor) then
  2399. handle_return_value(inlined,uses_eax,uses_edx)
  2400. else
  2401. begin
  2402. { successful constructor deletes the zero flag }
  2403. { and returns self in eax }
  2404. { eax must be set to zero if the allocation failed !!! }
  2405. getlabel(okexitlabel);
  2406. emitjmp(C_NONE,okexitlabel);
  2407. emitlab(faillabel);
  2408. if is_class(procinfo^._class) then
  2409. begin
  2410. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2411. emitcall('FPC_HELP_FAIL_CLASS');
  2412. end
  2413. else if is_object(procinfo^._class) then
  2414. begin
  2415. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2416. getexplicitregister32(R_EDI);
  2417. emit_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI);
  2418. emitcall('FPC_HELP_FAIL');
  2419. ungetregister32(R_EDI);
  2420. end
  2421. else
  2422. Internalerror(200006161);
  2423. emitlab(okexitlabel);
  2424. { for classes this is done after the call to }
  2425. { AfterConstruction }
  2426. if is_object(procinfo^._class) then
  2427. begin
  2428. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2429. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2430. uses_eax:=true;
  2431. end;
  2432. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2433. uses_esi:=true;
  2434. end;
  2435. if aktexit2label.is_used and not aktexit2label.is_set then
  2436. emitlab(aktexit2label);
  2437. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2438. begin
  2439. getlabel(stabsendlabel);
  2440. emitlab(stabsendlabel);
  2441. end;
  2442. { gives problems for long mangled names }
  2443. {List.concat(Tai_symbol.Create(aktprocsym.definition.mangledname+'_end'));}
  2444. { should we restore edi ? }
  2445. { for all i386 gcc implementations }
  2446. if (po_savestdregs in aktprocsym.definition.procoptions) then
  2447. begin
  2448. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2449. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  2450. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  2451. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  2452. { here we could reset R_EBX
  2453. but that is risky because it only works
  2454. if genexitcode is called after genentrycode
  2455. so lets skip this for the moment PM
  2456. aktprocsym.definition.usedregisters:=
  2457. aktprocsym.definition.usedregisters or not ($80 shr byte(R_EBX));
  2458. }
  2459. end;
  2460. { for the save all registers we can simply use a pusha,popa which
  2461. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2462. if (po_saveregisters in aktprocsym.definition.procoptions) then
  2463. begin
  2464. if uses_esi then
  2465. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4)));
  2466. if uses_edx then
  2467. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20)));
  2468. if uses_eax then
  2469. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28)));
  2470. exprasmList.concat(Taicpu.Op_none(A_POPA,S_L));
  2471. { We add a NOP because of the 386DX CPU bugs with POPAD }
  2472. exprasmlist.concat(taicpu.op_none(A_NOP,S_L));
  2473. end;
  2474. if not(nostackframe) then
  2475. begin
  2476. if not inlined then
  2477. exprasmList.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  2478. end
  2479. else
  2480. begin
  2481. if (gettempsize<>0) and not inlined then
  2482. exprasmList.insert(Taicpu.op_const_reg(A_ADD,S_L,gettempsize,R_ESP));
  2483. end;
  2484. { parameters are limited to 65535 bytes because }
  2485. { ret allows only imm16 }
  2486. if (parasize>65535) and not(pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2487. CGMessage(cg_e_parasize_too_big);
  2488. { at last, the return is generated }
  2489. if not inlined then
  2490. if (po_interrupt in aktprocsym.definition.procoptions) then
  2491. begin
  2492. if uses_esi then
  2493. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16)));
  2494. if uses_edx then
  2495. begin
  2496. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2497. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12)));
  2498. end;
  2499. if uses_eax then
  2500. begin
  2501. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2502. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0)));
  2503. end;
  2504. generate_interrupt_stackframe_exit;
  2505. end
  2506. else
  2507. begin
  2508. {Routines with the poclearstack flag set use only a ret.}
  2509. { also routines with parasize=0 }
  2510. if (pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2511. begin
  2512. {$ifndef OLD_C_STACK}
  2513. { complex return values are removed from stack in C code PM }
  2514. if ret_in_param(aktprocsym.definition.rettype.def) then
  2515. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,4))
  2516. else
  2517. {$endif not OLD_C_STACK}
  2518. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2519. end
  2520. else if (parasize=0) then
  2521. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO))
  2522. else
  2523. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  2524. end;
  2525. if not inlined then
  2526. exprasmList.concat(Tai_symbol_end.Createname(aktprocsym.definition.mangledname));
  2527. {$ifdef GDB}
  2528. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2529. begin
  2530. aktprocsym.concatstabto(exprasmlist);
  2531. if assigned(procinfo^._class) then
  2532. if (not assigned(procinfo^.parent) or
  2533. not assigned(procinfo^.parent^._class)) then
  2534. begin
  2535. if (po_classmethod in aktprocsym.definition.procoptions) or
  2536. ((po_virtualmethod in aktprocsym.definition.procoptions) and
  2537. (potype_constructor=aktprocsym.definition.proctypeoption)) or
  2538. (po_staticmethod in aktprocsym.definition.procoptions) then
  2539. begin
  2540. exprasmList.concat(Tai_stabs.Create(strpnew(
  2541. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  2542. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2543. end
  2544. else
  2545. begin
  2546. if not(is_class(procinfo^._class)) then
  2547. st:='v'
  2548. else
  2549. st:='p';
  2550. exprasmList.concat(Tai_stabs.Create(strpnew(
  2551. '"$t:'+st+procinfo^._class.numberstring+'",'+
  2552. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2553. end;
  2554. end
  2555. else
  2556. begin
  2557. if not is_class(procinfo^._class) then
  2558. st:='*'
  2559. else
  2560. st:='';
  2561. exprasmList.concat(Tai_stabs.Create(strpnew(
  2562. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  2563. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI]))));
  2564. end;
  2565. { define calling EBP as pseudo local var PM }
  2566. { this enables test if the function is a local one !! }
  2567. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2568. exprasmList.concat(Tai_stabs.Create(strpnew(
  2569. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  2570. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  2571. if (not is_void(aktprocsym.definition.rettype.def)) then
  2572. begin
  2573. if ret_in_param(aktprocsym.definition.rettype.def) then
  2574. exprasmList.concat(Tai_stabs.Create(strpnew(
  2575. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2576. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2577. else
  2578. exprasmList.concat(Tai_stabs.Create(strpnew(
  2579. '"'+aktprocsym.name+':X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2580. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2581. if (m_result in aktmodeswitches) then
  2582. if ret_in_param(aktprocsym.definition.rettype.def) then
  2583. exprasmList.concat(Tai_stabs.Create(strpnew(
  2584. '"RESULT:X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2585. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2586. else
  2587. exprasmList.concat(Tai_stabs.Create(strpnew(
  2588. '"RESULT:X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2589. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2590. end;
  2591. mangled_length:=length(aktprocsym.definition.mangledname);
  2592. getmem(p,2*mangled_length+50);
  2593. strpcopy(p,'192,0,0,');
  2594. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2595. if (target_info.use_function_relative_addresses) then
  2596. begin
  2597. strpcopy(strend(p),'-');
  2598. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2599. end;
  2600. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2601. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  2602. +aktprocsym.definition.mangledname))));
  2603. p[0]:='2';p[1]:='2';p[2]:='4';
  2604. strpcopy(strend(p),'_end');}
  2605. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  2606. if (target_info.use_function_relative_addresses) then
  2607. begin
  2608. strpcopy(strend(p),'-');
  2609. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2610. end;
  2611. exprasmList.concatlist(withdebuglist);
  2612. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2613. { strpnew('224,0,0,'
  2614. +aktprocsym.definition.mangledname+'_end'))));}
  2615. freemem(p,2*mangled_length+50);
  2616. end;
  2617. {$endif GDB}
  2618. if inlined then
  2619. cleanup_regvars(exprasmlist);
  2620. exprasmlist:=oldexprasmlist;
  2621. end;
  2622. procedure genimplicitunitfinal(alist : TAAsmoutput);
  2623. begin
  2624. { using current_module.globalsymtable is hopefully }
  2625. { more robust than symtablestack and symtablestack.next }
  2626. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2627. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2628. exprasmList.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  2629. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  2630. {$ifdef GDB}
  2631. if (cs_debuginfo in aktmoduleswitches) and
  2632. target_info.use_function_relative_addresses then
  2633. exprasmList.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  2634. {$endif GDB}
  2635. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2636. aList.concatlist(exprasmlist);
  2637. end;
  2638. procedure genimplicitunitinit(alist : TAAsmoutput);
  2639. begin
  2640. { using current_module.globalsymtable is hopefully }
  2641. { more robust than symtablestack and symtablestack.next }
  2642. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2643. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2644. exprasmList.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  2645. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  2646. {$ifdef GDB}
  2647. if (cs_debuginfo in aktmoduleswitches) and
  2648. target_info.use_function_relative_addresses then
  2649. exprasmList.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  2650. {$endif GDB}
  2651. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2652. aList.concatlist(exprasmlist);
  2653. end;
  2654. {$ifdef test_dest_loc}
  2655. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2656. begin
  2657. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2658. begin
  2659. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2660. set_location(p^.location,dest_loc);
  2661. in_dest_loc:=true;
  2662. end
  2663. else
  2664. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2665. begin
  2666. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference)));
  2667. set_location(p^.location,dest_loc);
  2668. in_dest_loc:=true;
  2669. end
  2670. else
  2671. internalerror(20080);
  2672. end;
  2673. {$endif test_dest_loc}
  2674. end.
  2675. {
  2676. $Log$
  2677. Revision 1.5 2001-09-30 21:28:34 peter
  2678. * int64->boolean fixed
  2679. Revision 1.4 2001/08/30 20:13:57 peter
  2680. * rtti/init table updates
  2681. * rttisym for reusable global rtti/init info
  2682. * support published for interfaces
  2683. Revision 1.3 2001/08/29 12:01:47 jonas
  2684. + support for int64 LOC_REGISTERS in remove_non_regvars_from_loc
  2685. Revision 1.2 2001/08/26 13:36:52 florian
  2686. * some cg reorganisation
  2687. * some PPC updates
  2688. Revision 1.29 2001/08/12 20:23:02 peter
  2689. * netbsd doesn't use stackchecking
  2690. Revision 1.28 2001/08/07 18:47:13 peter
  2691. * merged netbsd start
  2692. * profile for win32
  2693. Revision 1.27 2001/08/06 21:40:49 peter
  2694. * funcret moved from tprocinfo to tprocdef
  2695. Revision 1.26 2001/07/30 20:59:28 peter
  2696. * m68k updates from v10 merged
  2697. Revision 1.25 2001/07/01 20:16:18 peter
  2698. * alignmentinfo record added
  2699. * -Oa argument supports more alignment settings that can be specified
  2700. per type: PROC,LOOP,VARMIN,VARMAX,CONSTMIN,CONSTMAX,RECORDMIN
  2701. RECORDMAX,LOCALMIN,LOCALMAX. It is possible to set the mimimum
  2702. required alignment and the maximum usefull alignment. The final
  2703. alignment will be choosen per variable size dependent on these
  2704. settings
  2705. Revision 1.24 2001/05/27 14:30:55 florian
  2706. + some widestring stuff added
  2707. Revision 1.23 2001/04/21 13:33:16 peter
  2708. * move winstackpagesize const to cgai386 to remove uses t_win32
  2709. Revision 1.22 2001/04/21 12:05:32 peter
  2710. * add nop after popa (merged)
  2711. Revision 1.21 2001/04/18 22:02:00 peter
  2712. * registration of targets and assemblers
  2713. Revision 1.20 2001/04/13 01:22:17 peter
  2714. * symtable change to classes
  2715. * range check generation and errors fixed, make cycle DEBUG=1 works
  2716. * memory leaks fixed
  2717. Revision 1.19 2001/04/05 21:33:07 peter
  2718. * fast exit fix merged
  2719. Revision 1.18 2001/04/02 21:20:35 peter
  2720. * resulttype rewrite
  2721. Revision 1.17 2001/01/05 17:36:58 florian
  2722. * the info about exception frames is stored now on the stack
  2723. instead on the heap
  2724. Revision 1.16 2000/12/25 00:07:31 peter
  2725. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2726. tlinkedlist objects)
  2727. Revision 1.15 2000/12/05 11:44:32 jonas
  2728. + new integer regvar handling, should be much more efficient
  2729. Revision 1.14 2000/11/29 00:30:43 florian
  2730. * unused units removed from uses clause
  2731. * some changes for widestrings
  2732. Revision 1.13 2000/11/28 00:28:07 pierre
  2733. * stabs fixing
  2734. Revision 1.12 2000/11/22 15:12:06 jonas
  2735. * fixed inline-related problems (partially "merges")
  2736. Revision 1.11 2000/11/17 10:30:24 florian
  2737. * passing interfaces as parameters fixed
  2738. Revision 1.10 2000/11/07 23:40:48 florian
  2739. + AfterConstruction and BeforeDestruction impemented
  2740. Revision 1.9 2000/11/06 23:49:20 florian
  2741. * fixed init_paras call
  2742. Revision 1.8 2000/11/06 23:15:01 peter
  2743. * added copyvaluepara call again
  2744. Revision 1.7 2000/11/04 14:25:23 florian
  2745. + merged Attila's changes for interfaces, not tested yet
  2746. Revision 1.6 2000/10/31 22:02:55 peter
  2747. * symtable splitted, no real code changes
  2748. Revision 1.5 2000/10/24 22:23:04 peter
  2749. * emitcall -> emitinsertcall for profiling (merged)
  2750. Revision 1.4 2000/10/24 12:47:45 jonas
  2751. * allocate registers which hold function result
  2752. Revision 1.3 2000/10/24 08:54:25 michael
  2753. + Extra patch from peter
  2754. Revision 1.2 2000/10/24 07:20:03 pierre
  2755. * fix for bug 1193 (merged)
  2756. Revision 1.1 2000/10/15 09:47:42 peter
  2757. * moved to i386/
  2758. Revision 1.19 2000/10/14 10:14:46 peter
  2759. * moehrendorf oct 2000 rewrite
  2760. Revision 1.18 2000/10/10 14:55:28 jonas
  2761. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2762. Revision 1.17 2000/10/01 19:48:23 peter
  2763. * lot of compile updates for cg11
  2764. Revision 1.16 2000/09/30 16:08:45 peter
  2765. * more cg11 updates
  2766. Revision 1.15 2000/09/24 15:06:12 peter
  2767. * use defines.inc
  2768. Revision 1.14 2000/09/16 12:22:52 peter
  2769. * freebsd support merged
  2770. Revision 1.13 2000/08/27 16:11:49 peter
  2771. * moved some util functions from globals,cobjects to cutils
  2772. * splitted files into finput,fmodule
  2773. Revision 1.12 2000/08/24 19:07:54 peter
  2774. * don't initialize if localvarsym is set because that varsym will
  2775. already be initialized
  2776. * first initialize local data before copy of value para's (merged)
  2777. Revision 1.11 2000/08/19 20:09:33 peter
  2778. * check size after checking openarray in push_value_para (merged)
  2779. Revision 1.10 2000/08/16 13:06:06 florian
  2780. + support of 64 bit integer constants
  2781. Revision 1.9 2000/08/10 18:42:03 peter
  2782. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2783. Revision 1.8 2000/08/07 11:29:40 jonas
  2784. + emit_push_mem_size() which pushes a value in memory of a certain size
  2785. * pushsetelement() and pushvaluepara() use this new procedure, because
  2786. otherwise they could sometimes try to push data past the end of the
  2787. heap, causing a crash
  2788. (merged from fixes branch)
  2789. Revision 1.7 2000/08/03 13:17:25 jonas
  2790. + allow regvars to be used inside inlined procs, which required the
  2791. following changes:
  2792. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2793. * moved all regvar related code to new regvars unit
  2794. + added pregvarinfo type to hcodegen
  2795. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2796. * deallocate the regvars of the caller in secondprocinline before
  2797. inlining the called procedure and reallocate them afterwards
  2798. Revision 1.6 2000/08/02 08:05:04 jonas
  2799. * fixed web bug1087
  2800. * allocate R_ECX explicitely if it's used
  2801. (merged from fixes branch)
  2802. Revision 1.5 2000/07/27 09:25:05 jonas
  2803. * moved locflags2reg() procedure from cg386add to cgai386
  2804. + added locjump2reg() procedure to cgai386
  2805. * fixed internalerror(2002) when the result of a case expression has
  2806. LOC_JUMP
  2807. (all merged from fixes branch)
  2808. Revision 1.4 2000/07/21 15:14:02 jonas
  2809. + added is_addr field for labels, if they are only used for getting the address
  2810. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2811. Revision 1.3 2000/07/13 12:08:25 michael
  2812. + patched to 1.1.0 with former 1.09patch from peter
  2813. Revision 1.2 2000/07/13 11:32:37 michael
  2814. + removed logs
  2815. }