cgcpu.pas 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  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 cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,node,cg64f32,cginfo;
  26. type
  27. tcgppc = class(tcg)
  28. { passing parameters, per default the parameter is pushed }
  29. { nr gives the number of the parameter (enumerated from }
  30. { left to right), this allows to move the parameter to }
  31. { register, if the cpu supports register calling }
  32. { conventions }
  33. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  34. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  35. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  36. procedure a_call_name(list : taasmoutput;const s : string);override;
  37. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  38. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  39. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  40. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  41. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  42. size: tcgsize; a: aword; src, dst: tregister); override;
  43. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  44. size: tcgsize; src1, src2, dst: tregister); override;
  45. { move instructions }
  46. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  47. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  48. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  49. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  50. { fpu move instructions }
  51. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  52. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  53. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  54. { comparison operations }
  55. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  56. l : tasmlabel);override;
  57. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  58. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  59. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  60. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  61. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  62. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  63. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  64. procedure g_restore_frame_pointer(list : taasmoutput);override;
  65. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  66. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  67. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  68. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  69. { that's the case, we can use rlwinm to do an AND operation }
  70. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  71. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  72. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  73. procedure g_save_all_registers(list : taasmoutput);override;
  74. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  75. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  76. private
  77. (* NOT IN USE: *)
  78. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  79. (* NOT IN USE: *)
  80. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  81. { Make sure ref is a valid reference for the PowerPC and sets the }
  82. { base to the value of the index if (base = R_NO). }
  83. { Returns true if the reference contained a base, index and an }
  84. { offset or symbol, in which case the base will have been changed }
  85. { to a tempreg (which has to be freed by the caller) containing }
  86. { the sum of part of the original reference }
  87. function fixref(list: taasmoutput; var ref: treference): boolean;
  88. { returns whether a reference can be used immediately in a powerpc }
  89. { instruction }
  90. function issimpleref(const ref: treference): boolean;
  91. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  92. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  93. ref: treference);
  94. { creates the correct branch instruction for a given combination }
  95. { of asmcondflags and destination addressing mode }
  96. procedure a_jmp(list: taasmoutput; op: tasmop;
  97. c: tasmcondflag; crval: longint; l: tasmlabel);
  98. end;
  99. tcg64fppc = class(tcg64f32)
  100. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  101. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  102. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  103. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  104. end;
  105. const
  106. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  107. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  108. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  109. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  110. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  111. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  112. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  113. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  114. implementation
  115. uses
  116. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  117. { parameter passing... Still needs extra support from the processor }
  118. { independent code generator }
  119. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  120. var
  121. ref: treference;
  122. begin
  123. case locpara.loc of
  124. LOC_REGISTER,LOC_CREGISTER:
  125. a_load_const_reg(list,size,a,locpara.register);
  126. LOC_REFERENCE:
  127. begin
  128. reference_reset(ref);
  129. ref.base:=locpara.reference.index;
  130. ref.offset:=locpara.reference.offset;
  131. a_load_const_ref(list,size,a,ref);
  132. end;
  133. else
  134. internalerror(2002081101);
  135. end;
  136. if locpara.sp_fixup<>0 then
  137. internalerror(2002081102);
  138. end;
  139. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  140. var
  141. ref: treference;
  142. tmpreg: tregister;
  143. begin
  144. case locpara.loc of
  145. LOC_REGISTER,LOC_CREGISTER:
  146. a_load_ref_reg(list,size,size,r,locpara.register);
  147. LOC_REFERENCE:
  148. begin
  149. reference_reset(ref);
  150. ref.base:=locpara.reference.index;
  151. ref.offset:=locpara.reference.offset;
  152. tmpreg := rg.getregisterint(list,size);
  153. a_load_ref_reg(list,size,size,r,tmpreg);
  154. a_load_reg_ref(list,size,size,tmpreg,ref);
  155. rg.ungetregisterint(list,tmpreg);
  156. end;
  157. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  158. case size of
  159. OS_F32, OS_F64:
  160. a_loadfpu_ref_reg(list,size,r,locpara.register);
  161. else
  162. internalerror(2002072801);
  163. end;
  164. else
  165. internalerror(2002081103);
  166. end;
  167. if locpara.sp_fixup<>0 then
  168. internalerror(2002081104);
  169. end;
  170. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  171. var
  172. ref: treference;
  173. tmpreg: tregister;
  174. begin
  175. case locpara.loc of
  176. LOC_REGISTER,LOC_CREGISTER:
  177. a_loadaddr_ref_reg(list,r,locpara.register);
  178. LOC_REFERENCE:
  179. begin
  180. reference_reset(ref);
  181. ref.base := locpara.reference.index;
  182. ref.offset := locpara.reference.offset;
  183. tmpreg := rg.getregisterint(list,OS_ADDR);
  184. a_loadaddr_ref_reg(list,r,tmpreg);
  185. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  186. rg.ungetregisterint(list,tmpreg);
  187. end;
  188. else
  189. internalerror(2002080701);
  190. end;
  191. end;
  192. { calling a procedure by name }
  193. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  194. var
  195. href : treference;
  196. begin
  197. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  198. if it is a cross-TOC call. If so, it also replaces the NOP
  199. with some restore code.}
  200. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  201. if target_info.system=system_powerpc_macos then
  202. list.concat(taicpu.op_none(A_NOP));
  203. if not(pi_do_call in current_procinfo.flags) then
  204. internalerror(2003060703);
  205. end;
  206. { calling a procedure by address }
  207. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  208. var
  209. tmpreg : tregister;
  210. tmpref : treference;
  211. begin
  212. if target_info.system=system_powerpc_macos then
  213. begin
  214. {Generate instruction to load the procedure address from
  215. the transition vector.}
  216. //TODO: Support cross-TOC calls.
  217. tmpreg := rg.getregisterint(list,OS_INT);
  218. reference_reset(tmpref);
  219. tmpref.offset := 0;
  220. //tmpref.symaddr := refs_full;
  221. tmpref.base:= reg;
  222. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  223. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  224. rg.ungetregisterint(list,tmpreg);
  225. end
  226. else
  227. list.concat(taicpu.op_reg(A_MTCTR,reg));
  228. list.concat(taicpu.op_none(A_BCTRL));
  229. //if target_info.system=system_powerpc_macos then
  230. // //NOP is not needed here.
  231. // list.concat(taicpu.op_none(A_NOP));
  232. if not(pi_do_call in current_procinfo.flags) then
  233. internalerror(2003060704);
  234. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  235. end;
  236. { calling a procedure by address }
  237. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  238. var
  239. tmpreg : tregister;
  240. tmpref : treference;
  241. begin
  242. tmpreg := rg.getregisterint(list,OS_ADDR);
  243. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,tmpreg);
  244. if target_info.system=system_powerpc_macos then
  245. begin
  246. {Generate instruction to load the procedure address from
  247. the transition vector.}
  248. //TODO: Support cross-TOC calls.
  249. reference_reset(tmpref);
  250. tmpref.offset := 0;
  251. //tmpref.symaddr := refs_full;
  252. tmpref.base:= tmpreg;
  253. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  254. end;
  255. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  256. rg.ungetregisterint(list,tmpreg);
  257. list.concat(taicpu.op_none(A_BCTRL));
  258. //if target_info.system=system_powerpc_macos then
  259. // //NOP is not needed here.
  260. // list.concat(taicpu.op_none(A_NOP));
  261. if not(pi_do_call in current_procinfo.flags) then
  262. internalerror(2003060705);
  263. //list.concat(tai_comment.create(strpnew('***** a_call_ref')));
  264. end;
  265. {********************** load instructions ********************}
  266. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  267. begin
  268. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  269. internalerror(2002090902);
  270. if (longint(a) >= low(smallint)) and
  271. (longint(a) <= high(smallint)) then
  272. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  273. else if ((a and $ffff) <> 0) then
  274. begin
  275. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  276. if ((a shr 16) <> 0) or
  277. (smallint(a and $ffff) < 0) then
  278. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  279. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  280. end
  281. else
  282. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  283. end;
  284. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  285. const
  286. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  287. { indexed? updating?}
  288. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  289. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  290. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  291. var
  292. op: TAsmOp;
  293. ref2: TReference;
  294. freereg: boolean;
  295. begin
  296. ref2 := ref;
  297. freereg := fixref(list,ref2);
  298. if tosize in [OS_S8..OS_S16] then
  299. { storing is the same for signed and unsigned values }
  300. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  301. { 64 bit stuff should be handled separately }
  302. if tosize in [OS_64,OS_S64] then
  303. internalerror(200109236);
  304. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  305. a_load_store(list,op,reg,ref2);
  306. if freereg then
  307. rg.ungetregisterint(list,ref2.base);
  308. End;
  309. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  310. const
  311. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  312. { indexed? updating?}
  313. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  314. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  315. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  316. { 64bit stuff should be handled separately }
  317. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  318. { there's no load-byte-with-sign-extend :( }
  319. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  320. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  321. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  322. var
  323. op: tasmop;
  324. tmpreg: tregister;
  325. ref2, tmpref: treference;
  326. freereg: boolean;
  327. begin
  328. { TODO: optimize/take into consideration fromsize/tosize. Will }
  329. { probably only matter for OS_S8 loads though }
  330. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  331. internalerror(2002090902);
  332. ref2 := ref;
  333. freereg := fixref(list,ref2);
  334. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  335. a_load_store(list,op,reg,ref2);
  336. if freereg then
  337. rg.ungetregisterint(list,ref2.base);
  338. { sign extend shortint if necessary, since there is no }
  339. { load instruction that does that automatically (JM) }
  340. if fromsize = OS_S8 then
  341. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  342. end;
  343. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  344. var
  345. instr: taicpu;
  346. begin
  347. if (reg1<>reg2) or
  348. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  349. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  350. (tosize <> fromsize) and
  351. not(fromsize in [OS_32,OS_S32])) then
  352. begin
  353. case tosize of
  354. OS_8:
  355. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  356. reg2,reg1,0,31-8+1,31);
  357. OS_S8:
  358. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  359. OS_16:
  360. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  361. reg2,reg1,0,31-16+1,31);
  362. OS_S16:
  363. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  364. OS_32,OS_S32:
  365. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  366. else internalerror(2002090901);
  367. end;
  368. list.concat(instr);
  369. rg.add_move_instruction(instr);
  370. end;
  371. end;
  372. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  373. begin
  374. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  375. end;
  376. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  377. const
  378. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  379. { indexed? updating?}
  380. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  381. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  382. var
  383. op: tasmop;
  384. ref2: treference;
  385. freereg: boolean;
  386. begin
  387. { several functions call this procedure with OS_32 or OS_64 }
  388. { so this makes life easier (FK) }
  389. case size of
  390. OS_32,OS_F32:
  391. size:=OS_F32;
  392. OS_64,OS_F64,OS_C64:
  393. size:=OS_F64;
  394. else
  395. internalerror(200201121);
  396. end;
  397. ref2 := ref;
  398. freereg := fixref(list,ref2);
  399. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  400. a_load_store(list,op,reg,ref2);
  401. if freereg then
  402. rg.ungetregisterint(list,ref2.base);
  403. end;
  404. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  405. const
  406. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  407. { indexed? updating?}
  408. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  409. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  410. var
  411. op: tasmop;
  412. ref2: treference;
  413. freereg: boolean;
  414. begin
  415. if not(size in [OS_F32,OS_F64]) then
  416. internalerror(200201122);
  417. ref2 := ref;
  418. freereg := fixref(list,ref2);
  419. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  420. a_load_store(list,op,reg,ref2);
  421. if freereg then
  422. rg.ungetregisterint(list,ref2.base);
  423. end;
  424. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  425. begin
  426. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  427. end;
  428. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  429. begin
  430. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  431. end;
  432. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  433. size: tcgsize; a: aword; src, dst: tregister);
  434. var
  435. l1,l2: longint;
  436. oplo, ophi: tasmop;
  437. scratchreg: tregister;
  438. useReg, gotrlwi: boolean;
  439. procedure do_lo_hi;
  440. begin
  441. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  442. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  443. end;
  444. begin
  445. if src.enum<>R_INTREGISTER then
  446. internalerror(200303102);
  447. if op = OP_SUB then
  448. begin
  449. {$ifopt q+}
  450. {$q-}
  451. {$define overflowon}
  452. {$endif}
  453. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  454. {$ifdef overflowon}
  455. {$q+}
  456. {$undef overflowon}
  457. {$endif}
  458. exit;
  459. end;
  460. ophi := TOpCG2AsmOpConstHi[op];
  461. oplo := TOpCG2AsmOpConstLo[op];
  462. gotrlwi := get_rlwi_const(a,l1,l2);
  463. if (op in [OP_AND,OP_OR,OP_XOR]) then
  464. begin
  465. if (a = 0) then
  466. begin
  467. if op = OP_AND then
  468. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  469. else
  470. a_load_reg_reg(list,size,size,src,dst);
  471. exit;
  472. end
  473. else if (a = high(aword)) then
  474. begin
  475. case op of
  476. OP_OR:
  477. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  478. OP_XOR:
  479. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  480. OP_AND:
  481. a_load_reg_reg(list,size,size,src,dst);
  482. end;
  483. exit;
  484. end
  485. else if (a <= high(word)) and
  486. ((op <> OP_AND) or
  487. not gotrlwi) then
  488. begin
  489. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  490. exit;
  491. end;
  492. { all basic constant instructions also have a shifted form that }
  493. { works only on the highest 16bits, so if lo(a) is 0, we can }
  494. { use that one }
  495. if (word(a) = 0) and
  496. (not(op = OP_AND) or
  497. not gotrlwi) then
  498. begin
  499. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  500. exit;
  501. end;
  502. end
  503. else if (op = OP_ADD) then
  504. if a = 0 then
  505. exit
  506. else if (longint(a) >= low(smallint)) and
  507. (longint(a) <= high(smallint)) then
  508. begin
  509. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  510. exit;
  511. end;
  512. { otherwise, the instructions we can generate depend on the }
  513. { operation }
  514. useReg := false;
  515. case op of
  516. OP_DIV,OP_IDIV:
  517. if (a = 0) then
  518. internalerror(200208103)
  519. else if (a = 1) then
  520. begin
  521. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  522. exit
  523. end
  524. else if ispowerof2(a,l1) then
  525. begin
  526. case op of
  527. OP_DIV:
  528. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  529. OP_IDIV:
  530. begin
  531. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  532. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  533. end;
  534. end;
  535. exit;
  536. end
  537. else
  538. usereg := true;
  539. OP_IMUL, OP_MUL:
  540. if (a = 0) then
  541. begin
  542. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  543. exit
  544. end
  545. else if (a = 1) then
  546. begin
  547. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  548. exit
  549. end
  550. else if ispowerof2(a,l1) then
  551. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  552. else if (longint(a) >= low(smallint)) and
  553. (longint(a) <= high(smallint)) then
  554. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  555. else
  556. usereg := true;
  557. OP_ADD:
  558. begin
  559. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  560. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  561. smallint((a shr 16) + ord(smallint(a) < 0))));
  562. end;
  563. OP_OR:
  564. { try to use rlwimi }
  565. if gotrlwi and
  566. (src = dst) then
  567. begin
  568. scratchreg := rg.getregisterint(list,OS_INT);
  569. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  570. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  571. scratchreg,0,l1,l2));
  572. rg.ungetregisterint(list,scratchreg);
  573. end
  574. else
  575. do_lo_hi;
  576. OP_AND:
  577. { try to use rlwinm }
  578. if gotrlwi then
  579. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  580. src,0,l1,l2))
  581. else
  582. useReg := true;
  583. OP_XOR:
  584. do_lo_hi;
  585. OP_SHL,OP_SHR,OP_SAR:
  586. begin
  587. if (a and 31) <> 0 Then
  588. list.concat(taicpu.op_reg_reg_const(
  589. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  590. else
  591. a_load_reg_reg(list,size,size,src,dst);
  592. if (a shr 5) <> 0 then
  593. internalError(68991);
  594. end
  595. else
  596. internalerror(200109091);
  597. end;
  598. { if all else failed, load the constant in a register and then }
  599. { perform the operation }
  600. if useReg then
  601. begin
  602. scratchreg := rg.getregisterint(list,OS_INT);
  603. a_load_const_reg(list,OS_32,a,scratchreg);
  604. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  605. rg.ungetregisterint(list,scratchreg);
  606. end;
  607. end;
  608. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  609. size: tcgsize; src1, src2, dst: tregister);
  610. const
  611. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  612. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  613. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  614. begin
  615. case op of
  616. OP_NEG,OP_NOT:
  617. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  618. else
  619. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  620. end;
  621. end;
  622. {*************** compare instructructions ****************}
  623. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  624. l : tasmlabel);
  625. var
  626. p: taicpu;
  627. scratch_register: TRegister;
  628. signed: boolean;
  629. r:Tregister;
  630. begin
  631. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  632. { in the following case, we generate more efficient code when }
  633. { signed is true }
  634. if (cmp_op in [OC_EQ,OC_NE]) and
  635. (a > $ffff) then
  636. signed := true;
  637. r.enum:=R_CR0;
  638. if signed then
  639. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  640. list.concat(taicpu.op_reg_reg_const(A_CMPWI,r,reg,longint(a)))
  641. else
  642. begin
  643. scratch_register := rg.getregisterint(list,OS_INT);
  644. a_load_const_reg(list,OS_32,a,scratch_register);
  645. list.concat(taicpu.op_reg_reg_reg(A_CMPW,r,reg,scratch_register));
  646. rg.ungetregisterint(list,scratch_register);
  647. end
  648. else
  649. if (a <= $ffff) then
  650. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,r,reg,a))
  651. else
  652. begin
  653. scratch_register := rg.getregisterint(list,OS_INT);
  654. a_load_const_reg(list,OS_32,a,scratch_register);
  655. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,r,reg,scratch_register));
  656. rg.ungetregisterint(list,scratch_register);
  657. end;
  658. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  659. end;
  660. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  661. reg1,reg2 : tregister;l : tasmlabel);
  662. var
  663. p: taicpu;
  664. op: tasmop;
  665. r:Tregister;
  666. begin
  667. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  668. op := A_CMPW
  669. else op := A_CMPLW;
  670. r.enum:=R_CR0;
  671. list.concat(taicpu.op_reg_reg_reg(op,r,reg2,reg1));
  672. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  673. end;
  674. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  675. begin
  676. {$warning FIX ME}
  677. end;
  678. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  679. begin
  680. {$warning FIX ME}
  681. end;
  682. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  683. begin
  684. {$warning FIX ME}
  685. end;
  686. procedure tcgppc.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  687. begin
  688. {$warning FIX ME}
  689. end;
  690. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  691. begin
  692. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  693. end;
  694. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  695. begin
  696. a_jmp(list,A_B,C_None,0,l);
  697. end;
  698. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  699. var
  700. c: tasmcond;
  701. r:Tregister;
  702. begin
  703. c := flags_to_cond(f);
  704. r.enum:=R_CR0;
  705. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(r.enum),l);
  706. end;
  707. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  708. var
  709. testbit: byte;
  710. bitvalue: boolean;
  711. begin
  712. { get the bit to extract from the conditional register + its }
  713. { requested value (0 or 1) }
  714. testbit := ((ord(f.cr)-ord(R_CR0)) * 4);
  715. case f.flag of
  716. F_EQ,F_NE:
  717. begin
  718. inc(testbit,2);
  719. bitvalue := f.flag = F_EQ;
  720. end;
  721. F_LT,F_GE:
  722. begin
  723. bitvalue := f.flag = F_LT;
  724. end;
  725. F_GT,F_LE:
  726. begin
  727. inc(testbit);
  728. bitvalue := f.flag = F_GT;
  729. end;
  730. else
  731. internalerror(200112261);
  732. end;
  733. { load the conditional register in the destination reg }
  734. list.concat(taicpu.op_reg(A_MFCR,reg));
  735. { we will move the bit that has to be tested to bit 0 by rotating }
  736. { left }
  737. testbit := (testbit + 1) and 31;
  738. { extract bit }
  739. list.concat(taicpu.op_reg_reg_const_const_const(
  740. A_RLWINM,reg,reg,testbit,31,31));
  741. { if we need the inverse, xor with 1 }
  742. if not bitvalue then
  743. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  744. end;
  745. (*
  746. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  747. var
  748. testbit: byte;
  749. bitvalue: boolean;
  750. begin
  751. { get the bit to extract from the conditional register + its }
  752. { requested value (0 or 1) }
  753. case f.simple of
  754. false:
  755. begin
  756. { we don't generate this in the compiler }
  757. internalerror(200109062);
  758. end;
  759. true:
  760. case f.cond of
  761. C_None:
  762. internalerror(200109063);
  763. C_LT..C_NU:
  764. begin
  765. testbit := (ord(f.cr) - ord(R_CR0))*4;
  766. inc(testbit,AsmCondFlag2BI[f.cond]);
  767. bitvalue := AsmCondFlagTF[f.cond];
  768. end;
  769. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  770. begin
  771. testbit := f.crbit
  772. bitvalue := AsmCondFlagTF[f.cond];
  773. end;
  774. else
  775. internalerror(200109064);
  776. end;
  777. end;
  778. { load the conditional register in the destination reg }
  779. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  780. { we will move the bit that has to be tested to bit 31 -> rotate }
  781. { left by bitpos+1 (remember, this is big-endian!) }
  782. if bitpos <> 31 then
  783. inc(bitpos)
  784. else
  785. bitpos := 0;
  786. { extract bit }
  787. list.concat(taicpu.op_reg_reg_const_const_const(
  788. A_RLWINM,reg,reg,bitpos,31,31));
  789. { if we need the inverse, xor with 1 }
  790. if not bitvalue then
  791. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  792. end;
  793. *)
  794. { *********** entry/exit code and address loading ************ }
  795. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  796. { generated the entry code of a procedure/function. Note: localsize is the }
  797. { sum of the size necessary for local variables and the maximum possible }
  798. { combined size of ALL the parameters of a procedure called by the current }
  799. { one. }
  800. { This procedure may be called before, as well as after
  801. g_return_from_proc is called.}
  802. var regcounter,firstregfpu,firstreggpr: TRegister;
  803. href,href2 : treference;
  804. usesfpr,usesgpr,gotgot : boolean;
  805. parastart : aword;
  806. offset : aword;
  807. r,r2,rsp:Tregister;
  808. regcounter2: Tsuperregister;
  809. hp: tparaitem;
  810. begin
  811. { CR and LR only have to be saved in case they are modified by the current }
  812. { procedure, but currently this isn't checked, so save them always }
  813. { following is the entry code as described in "Altivec Programming }
  814. { Interface Manual", bar the saving of AltiVec registers }
  815. a_reg_alloc(list,NR_STACK_POINTER_REG);
  816. a_reg_alloc(list,NR_R0);
  817. if current_procinfo.procdef.parast.symtablelevel>1 then
  818. a_reg_alloc(list,NR_R11);
  819. usesfpr:=false;
  820. if not (po_assembler in current_procinfo.procdef.procoptions) then
  821. {$warning FIXME!!}
  822. { FIXME: has to be R_F8 instad of R_F14 for SYSV abi }
  823. for regcounter.enum:=R_F14 to R_F31 do
  824. if regcounter.enum in rg.used_in_proc_other then
  825. begin
  826. usesfpr:= true;
  827. firstregfpu:=regcounter;
  828. break;
  829. end;
  830. usesgpr:=false;
  831. if not (po_assembler in current_procinfo.procdef.procoptions) then
  832. for regcounter2:=firstsaveintreg to RS_R31 do
  833. begin
  834. if regcounter2 in rg.used_in_proc_int then
  835. begin
  836. usesgpr:=true;
  837. firstreggpr:=newreg(R_INTREGISTER,regcounter2,R_SUBNONE);
  838. break;
  839. end;
  840. end;
  841. { save link register? }
  842. if not (po_assembler in current_procinfo.procdef.procoptions) then
  843. if (pi_do_call in current_procinfo.flags) then
  844. begin
  845. { save return address... }
  846. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  847. { ... in caller's frame }
  848. case target_info.abi of
  849. abi_powerpc_aix:
  850. reference_reset_base(href,rsp,LA_LR_AIX);
  851. abi_powerpc_sysv:
  852. reference_reset_base(href,rsp,LA_LR_SYSV);
  853. end;
  854. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  855. a_reg_dealloc(list,NR_R0);
  856. end;
  857. { save the CR if necessary in callers frame. }
  858. if not (po_assembler in current_procinfo.procdef.procoptions) then
  859. if target_info.abi = abi_powerpc_aix then
  860. if false then { Not needed at the moment. }
  861. begin
  862. a_reg_alloc(list,NR_R0);
  863. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  864. reference_reset_base(href,rsp,LA_CR_AIX);
  865. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  866. a_reg_dealloc(list,NR_R0);
  867. end;
  868. { !!! always allocate space for all registers for now !!! }
  869. if not (po_assembler in current_procinfo.procdef.procoptions) then
  870. { if usesfpr or usesgpr then }
  871. begin
  872. a_reg_alloc(list,NR_R12);
  873. { save end of fpr save area }
  874. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  875. end;
  876. if (localsize <> 0) then
  877. begin
  878. if (localsize <= high(smallint)) then
  879. begin
  880. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  881. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  882. end
  883. else
  884. begin
  885. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  886. { can't use getregisterint here, the register colouring }
  887. { is already done when we get here }
  888. href.index := NR_R11;
  889. a_reg_alloc(list,href.index);
  890. a_load_const_reg(list,OS_S32,-localsize,href.index);
  891. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  892. a_reg_dealloc(list,href.index);
  893. end;
  894. end;
  895. { no GOT pointer loaded yet }
  896. gotgot:=false;
  897. r := NR_R12;
  898. if usesfpr then
  899. begin
  900. { save floating-point registers
  901. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  902. begin
  903. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  904. gotgot:=true;
  905. end
  906. else
  907. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  908. }
  909. reference_reset_base(href,r,-8);
  910. for regcounter.enum:=firstregfpu.enum to R_F31 do
  911. if regcounter.enum in rg.used_in_proc_other then
  912. begin
  913. a_loadfpu_reg_ref(list,OS_F64,regcounter,href);
  914. dec(href.offset,8);
  915. end;
  916. { compute end of gpr save area }
  917. a_op_const_reg(list,OP_ADD,OS_ADDR,aword(href.offset+8),r);
  918. end;
  919. { save gprs and fetch GOT pointer }
  920. if usesgpr then
  921. begin
  922. {
  923. if cs_create_pic in aktmoduleswitches then
  924. begin
  925. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  926. gotgot:=true;
  927. end
  928. else
  929. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  930. }
  931. reference_reset_base(href,r,-4);
  932. for regcounter2:=firstsaveintreg to RS_R31 do
  933. begin
  934. if regcounter2 in rg.used_in_proc_int then
  935. begin
  936. usesgpr:=true;
  937. r:=newreg(R_INTREGISTER,regcounter2,R_SUBNONE);
  938. a_load_reg_ref(list,OS_INT,OS_INT,r,href);
  939. dec(href.offset,4);
  940. end;
  941. end;
  942. {
  943. r.enum:=R_INTREGISTER;
  944. r.number:=NR_R12;
  945. reference_reset_base(href,r,-((NR_R31-firstreggpr.number) shr 8+1)*4);
  946. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  947. }
  948. end;
  949. if assigned(current_procinfo.procdef.parast) then
  950. begin
  951. if not (po_assembler in current_procinfo.procdef.procoptions) then
  952. begin
  953. { copy memory parameters to local parast }
  954. r:=NR_R12;
  955. hp:=tparaitem(current_procinfo.procdef.para.first);
  956. while assigned(hp) do
  957. begin
  958. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  959. begin
  960. reference_reset_base(href,current_procinfo.framepointer,tvarsym(hp.parasym).adjusted_address);
  961. reference_reset_base(href2,r,hp.paraloc[callerside].reference.offset);
  962. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  963. end
  964. {$ifdef newra2}
  965. else if (hp.calleeparaloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  966. begin
  967. rg.getexplicitregisterint(list,hp.calleeparaloc.register);
  968. end
  969. {$endif newra}
  970. ;
  971. hp := tparaitem(hp.next);
  972. end;
  973. end;
  974. end;
  975. if usesfpr or usesgpr then
  976. a_reg_dealloc(list,NR_R12);
  977. { PIC code support, }
  978. if cs_create_pic in aktmoduleswitches then
  979. begin
  980. { if we didn't get the GOT pointer till now, we've to calculate it now }
  981. if not(gotgot) then
  982. begin
  983. {!!!!!!!!!!!!!}
  984. end;
  985. a_reg_alloc(list,NR_R31);
  986. { place GOT ptr in r31 }
  987. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  988. end;
  989. { save the CR if necessary ( !!! always done currently ) }
  990. { still need to find out where this has to be done for SystemV
  991. a_reg_alloc(list,R_0);
  992. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  993. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  994. new_reference(STACK_POINTER_REG,LA_CR)));
  995. a_reg_dealloc(list,R_0); }
  996. { now comes the AltiVec context save, not yet implemented !!! }
  997. { if we're in a nested procedure, we've to save R11 }
  998. if current_procinfo.procdef.parast.symtablelevel>2 then
  999. begin
  1000. reference_reset_base(href,rsp,PARENT_FRAMEPOINTER_OFFSET);
  1001. list.concat(taicpu.op_reg_ref(A_STW,NR_R11,href));
  1002. end;
  1003. end;
  1004. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  1005. { This procedure may be called before, as well as after
  1006. g_stackframe_entry is called.}
  1007. var
  1008. regcounter,firstregfpu,firstreggpr: TRegister;
  1009. href : treference;
  1010. usesfpr,usesgpr,genret : boolean;
  1011. r,r2:Tregister;
  1012. regcounter2:Tsuperregister;
  1013. localsize: aword;
  1014. begin
  1015. { AltiVec context restore, not yet implemented !!! }
  1016. usesfpr:=false;
  1017. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1018. for regcounter.enum:=R_F14 to R_F31 do
  1019. if regcounter.enum in rg.used_in_proc_other then
  1020. begin
  1021. usesfpr:=true;
  1022. firstregfpu:=regcounter;
  1023. break;
  1024. end;
  1025. usesgpr:=false;
  1026. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1027. for regcounter2:=firstsaveintreg to RS_R31 do
  1028. begin
  1029. if regcounter2 in rg.used_in_proc_int then
  1030. begin
  1031. usesgpr:=true;
  1032. firstreggpr:=newreg(R_INTREGISTER,regcounter2,R_SUBNONE);
  1033. break;
  1034. end;
  1035. end;
  1036. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1037. { no return (blr) generated yet }
  1038. genret:=true;
  1039. if usesgpr or usesfpr then
  1040. begin
  1041. { address of gpr save area to r11 }
  1042. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12);
  1043. if usesfpr then
  1044. begin
  1045. reference_reset_base(href,NR_R12,-8);
  1046. for regcounter.enum := firstregfpu.enum to R_F31 do
  1047. if (regcounter.enum in rg.used_in_proc_other) then
  1048. begin
  1049. a_loadfpu_ref_reg(list,OS_F64,href,regcounter);
  1050. dec(href.offset,8);
  1051. end;
  1052. inc(href.offset,4);
  1053. end
  1054. else
  1055. reference_reset_base(href,NR_R12,-4);
  1056. for regcounter2:=firstsaveintreg to RS_R31 do
  1057. begin
  1058. if regcounter2 in rg.used_in_proc_int then
  1059. begin
  1060. usesgpr:=true;
  1061. r:=newreg(R_INTREGISTER,regcounter2,R_SUBNONE);
  1062. a_load_ref_reg(list,OS_INT,OS_INT,href,r);
  1063. dec(href.offset,4);
  1064. end;
  1065. end;
  1066. (*
  1067. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr.number)) shr 8+1)*4);
  1068. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1069. *)
  1070. end;
  1071. (*
  1072. { restore fprs and return }
  1073. if usesfpr then
  1074. begin
  1075. { address of fpr save area to r11 }
  1076. r.enum:=R_INTREGISTER;
  1077. r.number:=NR_R12;
  1078. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1079. {
  1080. if (pi_do_call in current_procinfo.flags) then
  1081. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1082. '_x')
  1083. else
  1084. { leaf node => lr haven't to be restored }
  1085. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1086. '_l');
  1087. genret:=false;
  1088. }
  1089. end;
  1090. *)
  1091. { if we didn't generate the return code, we've to do it now }
  1092. if genret then
  1093. begin
  1094. { adjust r1 }
  1095. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1096. { load link register? }
  1097. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1098. begin
  1099. if (pi_do_call in current_procinfo.flags) then
  1100. begin
  1101. case target_info.abi of
  1102. abi_powerpc_aix:
  1103. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1104. abi_powerpc_sysv:
  1105. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1106. end;
  1107. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1108. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1109. end;
  1110. { restore the CR if necessary from callers frame}
  1111. if target_info.abi = abi_powerpc_aix then
  1112. if false then { Not needed at the moment. }
  1113. begin
  1114. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1115. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1116. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1117. a_reg_dealloc(list,NR_R0);
  1118. end;
  1119. end;
  1120. list.concat(taicpu.op_none(A_BLR));
  1121. end;
  1122. end;
  1123. function save_regs(list : taasmoutput):longint;
  1124. {Generates code which saves used non-volatile registers in
  1125. the save area right below the address the stackpointer point to.
  1126. Returns the actual used save area size.}
  1127. var regcounter,firstregfpu,firstreggpr: TRegister;
  1128. usesfpr,usesgpr: boolean;
  1129. href : treference;
  1130. offset: integer;
  1131. r,r2:Tregister;
  1132. regcounter2: Tsuperregister;
  1133. begin
  1134. usesfpr:=false;
  1135. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1136. for regcounter.enum:=R_F14 to R_F31 do
  1137. if regcounter.enum in rg.used_in_proc_other then
  1138. begin
  1139. usesfpr:=true;
  1140. firstregfpu:=regcounter;
  1141. break;
  1142. end;
  1143. usesgpr:=false;
  1144. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1145. for regcounter2:=firstsaveintreg to RS_R31 do
  1146. begin
  1147. if regcounter2 in rg.used_in_proc_int then
  1148. begin
  1149. usesgpr:=true;
  1150. firstreggpr:=newreg(R_INTREGISTER,regcounter2,R_SUBNONE);
  1151. break;
  1152. end;
  1153. end;
  1154. offset:= 0;
  1155. { save floating-point registers }
  1156. if usesfpr then
  1157. for regcounter.enum := firstregfpu.enum to R_F31 do
  1158. begin
  1159. offset:= offset - 8;
  1160. r.enum:=R_INTREGISTER;
  1161. r.number:=NR_STACK_POINTER_REG;
  1162. reference_reset_base(href, r, offset);
  1163. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1164. end;
  1165. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1166. { save gprs in gpr save area }
  1167. if usesgpr then
  1168. if firstreggpr.enum < R_30 then
  1169. begin
  1170. offset:= offset - 4 * (ord(R_31) - ord(firstreggpr.enum) + 1);
  1171. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1172. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1173. {STMW stores multiple registers}
  1174. end
  1175. else
  1176. begin
  1177. r2 := firstreggpr;
  1178. convert_register_to_enum(firstreggpr);
  1179. for regcounter.enum := firstreggpr.enum to R_31 do
  1180. begin
  1181. offset:= offset - 4;
  1182. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1183. list.concat(taicpu.op_reg_ref(A_STW, r2, href));
  1184. inc(r2.number,NR_R1-NR_R0);
  1185. end;
  1186. end;
  1187. { now comes the AltiVec context save, not yet implemented !!! }
  1188. save_regs:= -offset;
  1189. end;
  1190. procedure restore_regs(list : taasmoutput);
  1191. {Generates code which restores used non-volatile registers from
  1192. the save area right below the address the stackpointer point to.}
  1193. var regcounter,firstregfpu,firstreggpr: TRegister;
  1194. usesfpr,usesgpr: boolean;
  1195. href : treference;
  1196. offset: integer;
  1197. r,r2:Tregister;
  1198. regcounter2: Tsuperregister;
  1199. begin
  1200. usesfpr:=false;
  1201. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1202. for regcounter.enum:=R_F14 to R_F31 do
  1203. if regcounter.enum in rg.used_in_proc_other then
  1204. begin
  1205. usesfpr:=true;
  1206. firstregfpu:=regcounter;
  1207. break;
  1208. end;
  1209. usesgpr:=false;
  1210. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1211. for regcounter2:=RS_R13 to RS_R31 do
  1212. begin
  1213. if regcounter2 in rg.used_in_proc_int then
  1214. begin
  1215. usesgpr:=true;
  1216. firstreggpr.enum:=R_INTREGISTER;
  1217. firstreggpr.number:=regcounter2 shl 8;
  1218. break;
  1219. end;
  1220. end;
  1221. offset:= 0;
  1222. { restore fp registers }
  1223. if usesfpr then
  1224. for regcounter.enum := firstregfpu.enum to R_F31 do
  1225. begin
  1226. offset:= offset - 8;
  1227. r.enum:=R_INTREGISTER;
  1228. r.number:=NR_STACK_POINTER_REG;
  1229. reference_reset_base(href, r, offset);
  1230. list.concat(taicpu.op_reg_ref(A_LFD, regcounter, href));
  1231. end;
  1232. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1233. { restore gprs }
  1234. if usesgpr then
  1235. if firstreggpr.enum < R_30 then
  1236. begin
  1237. offset:= offset - 4 * (ord(R_31) - ord(firstreggpr.enum) + 1);
  1238. r.enum:=R_INTREGISTER;
  1239. r.number:=NR_STACK_POINTER_REG;
  1240. reference_reset_base(href,r,offset); //-220
  1241. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1242. {LMW loads multiple registers}
  1243. end
  1244. else
  1245. begin
  1246. r.enum:=R_INTREGISTER;
  1247. r.number:=NR_STACK_POINTER_REG;
  1248. r2 := firstreggpr;
  1249. convert_register_to_enum(firstreggpr);
  1250. for regcounter.enum := firstreggpr.enum to R_31 do
  1251. begin
  1252. offset:= offset - 4;
  1253. reference_reset_base(href, r, offset);
  1254. list.concat(taicpu.op_reg_ref(A_LWZ, r2, href));
  1255. inc(r2.number,NR_R1-NR_R0);
  1256. end;
  1257. end;
  1258. { now comes the AltiVec context restore, not yet implemented !!! }
  1259. end;
  1260. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1261. (* NOT IN USE *)
  1262. { generated the entry code of a procedure/function. Note: localsize is the }
  1263. { sum of the size necessary for local variables and the maximum possible }
  1264. { combined size of ALL the parameters of a procedure called by the current }
  1265. { one }
  1266. const
  1267. macosLinkageAreaSize = 24;
  1268. var regcounter: TRegister;
  1269. href : treference;
  1270. registerSaveAreaSize : longint;
  1271. r,r2,rsp:Tregister;
  1272. regcounter2: Tsuperregister;
  1273. begin
  1274. if (localsize mod 8) <> 0 then internalerror(58991);
  1275. { CR and LR only have to be saved in case they are modified by the current }
  1276. { procedure, but currently this isn't checked, so save them always }
  1277. { following is the entry code as described in "Altivec Programming }
  1278. { Interface Manual", bar the saving of AltiVec registers }
  1279. r.enum:=R_INTREGISTER;
  1280. r.number:=NR_R0;
  1281. rsp.enum:=R_INTREGISTER;
  1282. rsp.number:=NR_STACK_POINTER_REG;
  1283. a_reg_alloc(list,rsp);
  1284. a_reg_alloc(list,r);
  1285. { save return address in callers frame}
  1286. r2.enum:=R_LR;
  1287. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1288. { ... in caller's frame }
  1289. reference_reset_base(href,rsp,8);
  1290. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1291. a_reg_dealloc(list,r);
  1292. { save non-volatile registers in callers frame}
  1293. registerSaveAreaSize:= save_regs(list);
  1294. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1295. a_reg_alloc(list,r);
  1296. r2.enum:=R_CR;
  1297. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1298. reference_reset_base(href,rsp,LA_CR_AIX);
  1299. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1300. a_reg_dealloc(list,r);
  1301. (*
  1302. { save pointer to incoming arguments }
  1303. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1304. *)
  1305. (*
  1306. a_reg_alloc(list,R_12);
  1307. { 0 or 8 based on SP alignment }
  1308. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1309. R_12,STACK_POINTER_REG,0,28,28));
  1310. { add in stack length }
  1311. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1312. -localsize));
  1313. { establish new alignment }
  1314. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1315. a_reg_dealloc(list,R_12);
  1316. *)
  1317. { allocate stack frame }
  1318. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1319. inc(localsize,tg.lasttemp);
  1320. localsize:=align(localsize,16);
  1321. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1322. if (localsize <> 0) then
  1323. begin
  1324. r.enum:=R_INTREGISTER;
  1325. r.number:=NR_STACK_POINTER_REG;
  1326. if (localsize <= high(smallint)) then
  1327. begin
  1328. reference_reset_base(href,r,-localsize);
  1329. a_load_store(list,A_STWU,r,href);
  1330. end
  1331. else
  1332. begin
  1333. reference_reset_base(href,r,0);
  1334. href.index.enum := R_INTREGISTER;
  1335. href.index.number := NR_R11;
  1336. a_reg_alloc(list,href.index);
  1337. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1338. a_load_store(list,A_STWUX,r,href);
  1339. a_reg_dealloc(list,href.index);
  1340. end;
  1341. end;
  1342. end;
  1343. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1344. (* NOT IN USE *)
  1345. var
  1346. regcounter: TRegister;
  1347. href : treference;
  1348. r,r2,rsp:Tregister;
  1349. regcounter2: Tsuperregister;
  1350. begin
  1351. r.enum:=R_INTREGISTER;
  1352. r.number:=NR_R0;
  1353. rsp.enum:=R_INTREGISTER;
  1354. rsp.number:=NR_STACK_POINTER_REG;
  1355. a_reg_alloc(list,r);
  1356. { restore stack pointer }
  1357. reference_reset_base(href,rsp,LA_SP);
  1358. list.concat(taicpu.op_reg_ref(A_LWZ,rsp,href));
  1359. (*
  1360. list.concat(taicpu.op_reg_reg_const(A_ORI,rsp,R_31,0));
  1361. *)
  1362. { restore the CR if necessary from callers frame
  1363. ( !!! always done currently ) }
  1364. reference_reset_base(href,rsp,LA_CR_AIX);
  1365. r.enum:=R_INTREGISTER;
  1366. r.number:=NR_R0;
  1367. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1368. r2.enum:=R_CR;
  1369. list.concat(taicpu.op_reg_reg(A_MTSPR,r,r2));
  1370. a_reg_dealloc(list,r);
  1371. (*
  1372. { restore return address from callers frame }
  1373. reference_reset_base(href,STACK_POINTER_REG,8);
  1374. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1375. *)
  1376. { restore non-volatile registers from callers frame }
  1377. restore_regs(list);
  1378. (*
  1379. { return to caller }
  1380. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1381. list.concat(taicpu.op_none(A_BLR));
  1382. *)
  1383. { restore return address from callers frame }
  1384. r.enum:=R_INTREGISTER;
  1385. r.number:=NR_R0;
  1386. r2.enum:=R_LR;
  1387. reference_reset_base(href,rsp,8);
  1388. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1389. { return to caller }
  1390. list.concat(taicpu.op_reg_reg(A_MTSPR,r,r2));
  1391. list.concat(taicpu.op_none(A_BLR));
  1392. end;
  1393. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1394. begin
  1395. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1396. end;
  1397. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1398. var
  1399. ref2, tmpref: treference;
  1400. freereg: boolean;
  1401. r2,tmpreg:Tregister;
  1402. begin
  1403. ref2 := ref;
  1404. freereg := fixref(list,ref2);
  1405. if assigned(ref2.symbol) then
  1406. begin
  1407. if target_info.system = system_powerpc_macos then
  1408. begin
  1409. if macos_direct_globals then
  1410. begin
  1411. reference_reset(tmpref);
  1412. tmpref.offset := ref2.offset;
  1413. tmpref.symbol := ref2.symbol;
  1414. tmpref.base.number := NR_NO;
  1415. r2.enum:=R_INTREGISTER;
  1416. r2.number:=NR_RTOC;
  1417. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r2,tmpref));
  1418. end
  1419. else
  1420. begin
  1421. reference_reset(tmpref);
  1422. tmpref.symbol := ref2.symbol;
  1423. tmpref.offset := 0;
  1424. tmpref.base.enum := R_INTREGISTER;
  1425. tmpref.base.number := NR_RTOC;
  1426. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1427. if ref2.offset <> 0 then
  1428. begin
  1429. reference_reset(tmpref);
  1430. tmpref.offset := ref2.offset;
  1431. tmpref.base:= r;
  1432. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1433. end;
  1434. end;
  1435. if ref2.base.number <> NR_NO then
  1436. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1437. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1438. end
  1439. else
  1440. begin
  1441. { add the symbol's value to the base of the reference, and if the }
  1442. { reference doesn't have a base, create one }
  1443. reference_reset(tmpref);
  1444. tmpref.offset := ref2.offset;
  1445. tmpref.symbol := ref2.symbol;
  1446. tmpref.symaddr := refs_ha;
  1447. if ref2.base.number<> NR_NO then
  1448. begin
  1449. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1450. ref2.base,tmpref));
  1451. if freereg then
  1452. begin
  1453. rg.ungetregisterint(list,ref2.base);
  1454. freereg := false;
  1455. end;
  1456. end
  1457. else
  1458. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1459. tmpref.base.number := NR_NO;
  1460. tmpref.symaddr := refs_l;
  1461. { can be folded with one of the next instructions by the }
  1462. { optimizer probably }
  1463. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1464. end
  1465. end
  1466. else if ref2.offset <> 0 Then
  1467. if ref2.base.number <> NR_NO then
  1468. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1469. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1470. { occurs, so now only ref.offset has to be loaded }
  1471. else
  1472. a_load_const_reg(list,OS_32,ref2.offset,r)
  1473. else if ref.index.number <> NR_NO Then
  1474. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1475. else if (ref2.base.number <> NR_NO) and
  1476. (r.number <> ref2.base.number) then
  1477. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1478. if freereg then
  1479. rg.ungetregisterint(list,ref2.base);
  1480. end;
  1481. { ************* concatcopy ************ }
  1482. {$ifndef ppc603}
  1483. const
  1484. maxmoveunit = 8;
  1485. {$else ppc603}
  1486. const
  1487. maxmoveunit = 4;
  1488. {$endif ppc603}
  1489. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1490. var
  1491. countreg: TRegister;
  1492. src, dst: TReference;
  1493. lab: tasmlabel;
  1494. count, count2: aword;
  1495. orgsrc, orgdst: boolean;
  1496. r:Tregister;
  1497. size: tcgsize;
  1498. begin
  1499. {$ifdef extdebug}
  1500. if len > high(longint) then
  1501. internalerror(2002072704);
  1502. {$endif extdebug}
  1503. { make sure short loads are handled as optimally as possible }
  1504. if not loadref then
  1505. if (len <= maxmoveunit) and
  1506. (byte(len) in [1,2,4,8]) then
  1507. begin
  1508. if len < 8 then
  1509. begin
  1510. size := int_cgsize(len);
  1511. a_load_ref_ref(list,size,size,source,dest);
  1512. if delsource then
  1513. begin
  1514. reference_release(list,source);
  1515. tg.ungetiftemp(list,source);
  1516. end;
  1517. end
  1518. else
  1519. begin
  1520. r.enum:=R_F0;
  1521. a_reg_alloc(list,r);
  1522. a_loadfpu_ref_reg(list,OS_F64,source,r);
  1523. if delsource then
  1524. begin
  1525. reference_release(list,source);
  1526. tg.ungetiftemp(list,source);
  1527. end;
  1528. a_loadfpu_reg_ref(list,OS_F64,r,dest);
  1529. a_reg_dealloc(list,r);
  1530. end;
  1531. exit;
  1532. end;
  1533. count := len div maxmoveunit;
  1534. reference_reset(src);
  1535. reference_reset(dst);
  1536. { load the address of source into src.base }
  1537. if loadref then
  1538. begin
  1539. src.base := rg.getregisterint(list,OS_ADDR);
  1540. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1541. orgsrc := false;
  1542. end
  1543. else if (count > 4) or
  1544. not issimpleref(source) or
  1545. ((source.index <> NR_NO) and
  1546. ((source.offset + longint(len)) > high(smallint))) then
  1547. begin
  1548. src.base := rg.getregisterint(list,OS_ADDR);
  1549. a_loadaddr_ref_reg(list,source,src.base);
  1550. orgsrc := false;
  1551. end
  1552. else
  1553. begin
  1554. src := source;
  1555. orgsrc := true;
  1556. end;
  1557. if not orgsrc and delsource then
  1558. reference_release(list,source);
  1559. { load the address of dest into dst.base }
  1560. if (count > 4) or
  1561. not issimpleref(dest) or
  1562. ((dest.index <> NR_NO) and
  1563. ((dest.offset + longint(len)) > high(smallint))) then
  1564. begin
  1565. dst.base := rg.getregisterint(list,OS_ADDR);
  1566. a_loadaddr_ref_reg(list,dest,dst.base);
  1567. orgdst := false;
  1568. end
  1569. else
  1570. begin
  1571. dst := dest;
  1572. orgdst := true;
  1573. end;
  1574. {$ifndef ppc603}
  1575. if count > 4 then
  1576. { generate a loop }
  1577. begin
  1578. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1579. { have to be set to 8. I put an Inc there so debugging may be }
  1580. { easier (should offset be different from zero here, it will be }
  1581. { easy to notice in the generated assembler }
  1582. inc(dst.offset,8);
  1583. inc(src.offset,8);
  1584. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1585. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1586. countreg := rg.getregisterint(list,OS_INT);
  1587. a_load_const_reg(list,OS_32,count,countreg);
  1588. { explicitely allocate R_0 since it can be used safely here }
  1589. { (for holding date that's being copied) }
  1590. r.enum:=R_F0;
  1591. a_reg_alloc(list,r);
  1592. objectlibrary.getlabel(lab);
  1593. a_label(list, lab);
  1594. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1595. r.enum:=R_F0;
  1596. list.concat(taicpu.op_reg_ref(A_LFDU,r,src));
  1597. list.concat(taicpu.op_reg_ref(A_STFDU,r,dst));
  1598. a_jmp(list,A_BC,C_NE,0,lab);
  1599. rg.ungetregisterint(list,countreg);
  1600. a_reg_dealloc(list,r);
  1601. len := len mod 8;
  1602. end;
  1603. count := len div 8;
  1604. if count > 0 then
  1605. { unrolled loop }
  1606. begin
  1607. r.enum:=R_F0;
  1608. a_reg_alloc(list,r);
  1609. for count2 := 1 to count do
  1610. begin
  1611. a_loadfpu_ref_reg(list,OS_F64,src,r);
  1612. a_loadfpu_reg_ref(list,OS_F64,r,dst);
  1613. inc(src.offset,8);
  1614. inc(dst.offset,8);
  1615. end;
  1616. a_reg_dealloc(list,r);
  1617. len := len mod 8;
  1618. end;
  1619. if (len and 4) <> 0 then
  1620. begin
  1621. a_reg_alloc(list,NR_R0);
  1622. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1623. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1624. inc(src.offset,4);
  1625. inc(dst.offset,4);
  1626. a_reg_dealloc(list,NR_R0);
  1627. end;
  1628. {$else not ppc603}
  1629. if count > 4 then
  1630. { generate a loop }
  1631. begin
  1632. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1633. { have to be set to 4. I put an Inc there so debugging may be }
  1634. { easier (should offset be different from zero here, it will be }
  1635. { easy to notice in the generated assembler }
  1636. inc(dst.offset,4);
  1637. inc(src.offset,4);
  1638. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1639. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1640. countreg := rg.getregisterint(list,OS_INT);
  1641. a_load_const_reg(list,OS_32,count,countreg);
  1642. { explicitely allocate R_0 since it can be used safely here }
  1643. { (for holding date that's being copied) }
  1644. a_reg_alloc(list,NR_R0);
  1645. objectlibrary.getlabel(lab);
  1646. a_label(list, lab);
  1647. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1648. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1649. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1650. a_jmp(list,A_BC,C_NE,0,lab);
  1651. rg.ungetregisterint(list,countreg);
  1652. a_reg_dealloc(list,NR_R0);
  1653. len := len mod 4;
  1654. end;
  1655. count := len div 4;
  1656. if count > 0 then
  1657. { unrolled loop }
  1658. begin
  1659. a_reg_alloc(list,NR_R0);
  1660. for count2 := 1 to count do
  1661. begin
  1662. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1663. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1664. inc(src.offset,4);
  1665. inc(dst.offset,4);
  1666. end;
  1667. a_reg_dealloc(list,r);
  1668. len := len mod 4;
  1669. end;
  1670. {$endif not ppc603}
  1671. { copy the leftovers }
  1672. if (len and 2) <> 0 then
  1673. begin
  1674. a_reg_alloc(list,NR_R0);
  1675. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1676. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1677. inc(src.offset,2);
  1678. inc(dst.offset,2);
  1679. a_reg_dealloc(list,NR_R0);
  1680. end;
  1681. if (len and 1) <> 0 then
  1682. begin
  1683. a_reg_alloc(list,NR_R0);
  1684. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1685. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1686. a_reg_dealloc(list,NR_R0);
  1687. end;
  1688. if orgsrc then
  1689. begin
  1690. if delsource then
  1691. reference_release(list,source);
  1692. end
  1693. else
  1694. rg.ungetregisterint(list,src.base);
  1695. if not orgdst then
  1696. rg.ungetregisterint(list,dst.base);
  1697. if delsource then
  1698. tg.ungetiftemp(list,source);
  1699. end;
  1700. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  1701. var
  1702. power,len : longint;
  1703. {$ifndef __NOWINPECOFF__}
  1704. again,ok : tasmlabel;
  1705. {$endif}
  1706. r,r2,rsp:Tregister;
  1707. begin
  1708. {$warning !!!! FIX ME !!!!}
  1709. internalerror(200305231);
  1710. (* !!!!
  1711. lenref:=ref;
  1712. inc(lenref.offset,4);
  1713. { get stack space }
  1714. r.enum:=R_INTREGISTER;
  1715. r.number:=NR_EDI;
  1716. rsp.enum:=R_INTREGISTER;
  1717. rsp.number:=NR_ESP;
  1718. r2.enum:=R_INTREGISTER;
  1719. rg.getexplicitregisterint(list,NR_EDI);
  1720. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1721. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1722. if (elesize<>1) then
  1723. begin
  1724. if ispowerof2(elesize, power) then
  1725. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1726. else
  1727. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1728. end;
  1729. {$ifndef __NOWINPECOFF__}
  1730. { windows guards only a few pages for stack growing, }
  1731. { so we have to access every page first }
  1732. if target_info.system=system_i386_win32 then
  1733. begin
  1734. objectlibrary.getlabel(again);
  1735. objectlibrary.getlabel(ok);
  1736. a_label(list,again);
  1737. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1738. a_jmp_cond(list,OC_B,ok);
  1739. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1740. r2.number:=NR_EAX;
  1741. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1742. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1743. a_jmp_always(list,again);
  1744. a_label(list,ok);
  1745. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1746. rg.ungetregisterint(list,r);
  1747. { now reload EDI }
  1748. rg.getexplicitregisterint(list,NR_EDI);
  1749. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1750. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1751. if (elesize<>1) then
  1752. begin
  1753. if ispowerof2(elesize, power) then
  1754. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1755. else
  1756. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1757. end;
  1758. end
  1759. else
  1760. {$endif __NOWINPECOFF__}
  1761. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1762. { align stack on 4 bytes }
  1763. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1764. { load destination }
  1765. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1766. { don't destroy the registers! }
  1767. r2.number:=NR_ECX;
  1768. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1769. r2.number:=NR_ESI;
  1770. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1771. { load count }
  1772. r2.number:=NR_ECX;
  1773. a_load_ref_reg(list,OS_INT,lenref,r2);
  1774. { load source }
  1775. r2.number:=NR_ESI;
  1776. a_load_ref_reg(list,OS_INT,ref,r2);
  1777. { scheduled .... }
  1778. r2.number:=NR_ECX;
  1779. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1780. { calculate size }
  1781. len:=elesize;
  1782. opsize:=S_B;
  1783. if (len and 3)=0 then
  1784. begin
  1785. opsize:=S_L;
  1786. len:=len shr 2;
  1787. end
  1788. else
  1789. if (len and 1)=0 then
  1790. begin
  1791. opsize:=S_W;
  1792. len:=len shr 1;
  1793. end;
  1794. if ispowerof2(len, power) then
  1795. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1796. else
  1797. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1798. list.concat(Taicpu.op_none(A_REP,S_NO));
  1799. case opsize of
  1800. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1801. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1802. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1803. end;
  1804. rg.ungetregisterint(list,r);
  1805. r2.number:=NR_ESI;
  1806. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1807. r2.number:=NR_ECX;
  1808. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1809. { patch the new address }
  1810. a_load_reg_ref(list,OS_INT,rsp,ref);
  1811. !!!! *)
  1812. end;
  1813. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1814. var
  1815. hl : tasmlabel;
  1816. r:Tregister;
  1817. begin
  1818. if not(cs_check_overflow in aktlocalswitches) then
  1819. exit;
  1820. objectlibrary.getlabel(hl);
  1821. if not ((def.deftype=pointerdef) or
  1822. ((def.deftype=orddef) and
  1823. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1824. bool8bit,bool16bit,bool32bit]))) then
  1825. begin
  1826. r.enum:=R_CR7;
  1827. list.concat(taicpu.op_reg(A_MCRXR,r));
  1828. a_jmp(list,A_BC,C_OV,7,hl)
  1829. end
  1830. else
  1831. a_jmp_cond(list,OC_AE,hl);
  1832. a_call_name(list,'FPC_OVERFLOW');
  1833. a_label(list,hl);
  1834. end;
  1835. {***************** This is private property, keep out! :) *****************}
  1836. function tcgppc.issimpleref(const ref: treference): boolean;
  1837. begin
  1838. if (ref.base = NR_NO) and
  1839. (ref.index <> NR_NO) then
  1840. internalerror(200208101);
  1841. result :=
  1842. not(assigned(ref.symbol)) and
  1843. (((ref.index = NR_NO) and
  1844. (ref.offset >= low(smallint)) and
  1845. (ref.offset <= high(smallint))) or
  1846. ((ref.index <> NR_NO) and
  1847. (ref.offset = 0)));
  1848. end;
  1849. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1850. var
  1851. tmpreg: tregister;
  1852. orgindex: tregister;
  1853. freeindex: boolean;
  1854. begin
  1855. result := false;
  1856. if (ref.base = NR_NO) then
  1857. begin
  1858. ref.base := ref.index;
  1859. ref.base := NR_NO;
  1860. end;
  1861. if (ref.base <> NR_NO) then
  1862. begin
  1863. if (ref.index <> NR_NO) and
  1864. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1865. begin
  1866. result := true;
  1867. { references are often freed before they are used. Since we allocate }
  1868. { a register here, we must first reallocate the index register, since }
  1869. { otherwise it may be overwritten (and it's still used afterwards) }
  1870. freeindex := false;
  1871. if (getsupreg(ref.index) >= first_supreg) and
  1872. (getsupreg(ref.index) in rg.unusedregsint) then
  1873. begin
  1874. rg.getexplicitregisterint(list,ref.index);
  1875. orgindex := ref.index;
  1876. freeindex := true;
  1877. end;
  1878. tmpreg := rg.getregisterint(list,OS_ADDR);
  1879. {$endif newra}
  1880. if not assigned(ref.symbol) and
  1881. (cardinal(ref.offset-low(smallint)) <=
  1882. high(smallint)-low(smallint)) then
  1883. begin
  1884. list.concat(taicpu.op_reg_reg_const(
  1885. A_ADDI,tmpreg,ref.base,ref.offset));
  1886. ref.offset := 0;
  1887. end
  1888. else
  1889. begin
  1890. list.concat(taicpu.op_reg_reg_reg(
  1891. A_ADD,tmpreg,ref.base,ref.index));
  1892. ref.index := NR_NO;
  1893. end;
  1894. ref.base := tmpreg;
  1895. if freeindex then
  1896. rg.ungetregisterint(list,orgindex);
  1897. end
  1898. end
  1899. else
  1900. if ref.index <> NR_NO then
  1901. internalerror(200208102);
  1902. end;
  1903. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1904. { that's the case, we can use rlwinm to do an AND operation }
  1905. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1906. var
  1907. temp : longint;
  1908. testbit : aword;
  1909. compare: boolean;
  1910. begin
  1911. get_rlwi_const := false;
  1912. if (a = 0) or (a = $ffffffff) then
  1913. exit;
  1914. { start with the lowest bit }
  1915. testbit := 1;
  1916. { check its value }
  1917. compare := boolean(a and testbit);
  1918. { find out how long the run of bits with this value is }
  1919. { (it's impossible that all bits are 1 or 0, because in that case }
  1920. { this function wouldn't have been called) }
  1921. l1 := 31;
  1922. while (((a and testbit) <> 0) = compare) do
  1923. begin
  1924. testbit := testbit shl 1;
  1925. dec(l1);
  1926. end;
  1927. { check the length of the run of bits that comes next }
  1928. compare := not compare;
  1929. l2 := l1;
  1930. while (((a and testbit) <> 0) = compare) and
  1931. (l2 >= 0) do
  1932. begin
  1933. testbit := testbit shl 1;
  1934. dec(l2);
  1935. end;
  1936. { and finally the check whether the rest of the bits all have the }
  1937. { same value }
  1938. compare := not compare;
  1939. temp := l2;
  1940. if temp >= 0 then
  1941. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1942. exit;
  1943. { we have done "not(not(compare))", so compare is back to its }
  1944. { initial value. If the lowest bit was 0, a is of the form }
  1945. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1946. { because l2 now contains the position of the last zero of the }
  1947. { first run instead of that of the first 1) so switch l1 and l2 }
  1948. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1949. if not compare then
  1950. begin
  1951. temp := l1;
  1952. l1 := l2+1;
  1953. l2 := temp;
  1954. end
  1955. else
  1956. { otherwise, l1 currently contains the position of the last }
  1957. { zero instead of that of the first 1 of the second run -> +1 }
  1958. inc(l1);
  1959. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1960. l1 := l1 and 31;
  1961. l2 := l2 and 31;
  1962. get_rlwi_const := true;
  1963. end;
  1964. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1965. ref: treference);
  1966. var
  1967. tmpreg: tregister;
  1968. tmpregUsed: Boolean;
  1969. tmpref: treference;
  1970. largeOffset: Boolean;
  1971. begin
  1972. tmpreg := NR_NO;
  1973. if target_info.system = system_powerpc_macos then
  1974. begin
  1975. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1976. high(smallint)-low(smallint));
  1977. tmpreg := rg.getregisterint(list,OS_ADDR);
  1978. tmpregUsed:= false;
  1979. if assigned(ref.symbol) then
  1980. begin //Load symbol's value
  1981. reference_reset(tmpref);
  1982. tmpref.symbol := ref.symbol;
  1983. tmpref.base := NR_RTOC;
  1984. if macos_direct_globals then
  1985. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1986. else
  1987. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1988. tmpregUsed:= true;
  1989. end;
  1990. if largeOffset then
  1991. begin //Add hi part of offset
  1992. reference_reset(tmpref);
  1993. tmpref.offset := Hi(ref.offset);
  1994. if tmpregUsed then
  1995. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1996. tmpreg,tmpref))
  1997. else
  1998. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1999. tmpregUsed:= true;
  2000. end;
  2001. if tmpregUsed then
  2002. begin
  2003. //Add content of base register
  2004. if ref.base <> NR_NO then
  2005. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  2006. ref.base,tmpreg));
  2007. //Make ref ready to be used by op
  2008. ref.symbol:= nil;
  2009. ref.base:= tmpreg;
  2010. if largeOffset then
  2011. ref.offset := Lo(ref.offset);
  2012. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2013. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  2014. end
  2015. else
  2016. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2017. end
  2018. else {if target_info.system <> system_powerpc_macos}
  2019. begin
  2020. if assigned(ref.symbol) or
  2021. (cardinal(ref.offset-low(smallint)) >
  2022. high(smallint)-low(smallint)) then
  2023. begin
  2024. tmpreg := rg.getregisterint(list,OS_ADDR);
  2025. reference_reset(tmpref);
  2026. tmpref.symbol := ref.symbol;
  2027. tmpref.offset := ref.offset;
  2028. tmpref.symaddr := refs_ha;
  2029. if ref.base <> NR_NO then
  2030. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  2031. ref.base,tmpref))
  2032. else
  2033. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2034. ref.base := tmpreg;
  2035. ref.symaddr := refs_l;
  2036. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2037. end
  2038. else
  2039. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2040. end;
  2041. if (tmpreg <> NR_NO) then
  2042. rg.ungetregisterint(list,tmpreg);
  2043. end;
  2044. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  2045. crval: longint; l: tasmlabel);
  2046. var
  2047. p: taicpu;
  2048. begin
  2049. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  2050. if op <> A_B then
  2051. create_cond_norm(c,crval,p.condition);
  2052. p.is_jmp := true;
  2053. list.concat(p)
  2054. end;
  2055. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2056. begin
  2057. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2058. end;
  2059. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  2060. begin
  2061. a_op64_const_reg_reg(list,op,value,reg,reg);
  2062. end;
  2063. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2064. begin
  2065. case op of
  2066. OP_AND,OP_OR,OP_XOR:
  2067. begin
  2068. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2069. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2070. end;
  2071. OP_ADD:
  2072. begin
  2073. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2074. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2075. end;
  2076. OP_SUB:
  2077. begin
  2078. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2079. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2080. end;
  2081. else
  2082. internalerror(2002072801);
  2083. end;
  2084. end;
  2085. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2086. const
  2087. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2088. (A_SUBIC,A_SUBC,A_ADDME));
  2089. var
  2090. tmpreg: tregister;
  2091. tmpreg64: tregister64;
  2092. newop: TOpCG;
  2093. issub: boolean;
  2094. begin
  2095. case op of
  2096. OP_AND,OP_OR,OP_XOR:
  2097. begin
  2098. cg.a_op_const_reg_reg(list,op,OS_32,aword(value),regsrc.reglo,regdst.reglo);
  2099. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2100. regdst.reghi);
  2101. end;
  2102. OP_ADD, OP_SUB:
  2103. begin
  2104. if (int64(value) < 0) then
  2105. begin
  2106. if op = OP_ADD then
  2107. op := OP_SUB
  2108. else
  2109. op := OP_ADD;
  2110. int64(value) := -int64(value);
  2111. end;
  2112. if (longint(value) <> 0) then
  2113. begin
  2114. issub := op = OP_SUB;
  2115. if (int64(value) > 0) and
  2116. (int64(value)-ord(issub) <= 32767) then
  2117. begin
  2118. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2119. regdst.reglo,regsrc.reglo,longint(value)));
  2120. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2121. regdst.reghi,regsrc.reghi));
  2122. end
  2123. else if ((value shr 32) = 0) then
  2124. begin
  2125. tmpreg := rg.getregisterint(list,OS_32);
  2126. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2127. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2128. regdst.reglo,regsrc.reglo,tmpreg));
  2129. rg.ungetregisterint(list,tmpreg);
  2130. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2131. regdst.reghi,regsrc.reghi));
  2132. end
  2133. else
  2134. begin
  2135. tmpreg64.reglo := rg.getregisterint(list,OS_32);
  2136. tmpreg64.reghi := rg.getregisterint(list,OS_32);
  2137. a_load64_const_reg(list,value,tmpreg64);
  2138. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2139. rg.ungetregisterint(list,tmpreg64.reglo);
  2140. rg.ungetregisterint(list,tmpreg64.reghi);
  2141. end
  2142. end
  2143. else
  2144. begin
  2145. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2146. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2147. regdst.reghi);
  2148. end;
  2149. end;
  2150. else
  2151. internalerror(2002072802);
  2152. end;
  2153. end;
  2154. begin
  2155. cg := tcgppc.create;
  2156. cg64 :=tcg64fppc.create;
  2157. end.
  2158. {
  2159. $Log$
  2160. Revision 1.123 2003-09-03 15:55:01 peter
  2161. * NEWRA branch merged
  2162. Revision 1.122.2.1 2003/08/31 21:08:16 peter
  2163. * first batch of sparc fixes
  2164. Revision 1.122 2003/08/18 21:27:00 jonas
  2165. * some newra optimizations (eliminate lots of moves between registers)
  2166. Revision 1.121 2003/08/18 11:50:55 olle
  2167. + cleaning up in proc entry and exit, now calc_stack_frame always is used.
  2168. Revision 1.120 2003/08/17 16:59:20 jonas
  2169. * fixed regvars so they work with newra (at least for ppc)
  2170. * fixed some volatile register bugs
  2171. + -dnotranslation option for -dnewra, which causes the registers not to
  2172. be translated from virtual to normal registers. Requires support in
  2173. the assembler writer as well, which is only implemented in aggas/
  2174. agppcgas currently
  2175. Revision 1.119 2003/08/11 21:18:20 peter
  2176. * start of sparc support for newra
  2177. Revision 1.118 2003/08/08 15:50:45 olle
  2178. * merged macos entry/exit code generation into the general one.
  2179. Revision 1.117 2002/10/01 05:24:28 olle
  2180. * made a_load_store more robust and to accept large offsets and cleaned up code
  2181. Revision 1.116 2003/07/23 11:02:23 jonas
  2182. * don't use rg.getregisterint() anymore in g_stackframe_entry_*, because
  2183. the register colouring has already occurred then, use a hard-coded
  2184. register instead
  2185. Revision 1.115 2003/07/20 20:39:20 jonas
  2186. * fixed newra bug due to the fact that we sometimes need a temp reg
  2187. when loading/storing to memory (base+index+offset is not possible)
  2188. and because a reference is often freed before it is last used, this
  2189. temp register was soemtimes the same as one of the reference regs
  2190. Revision 1.114 2003/07/20 16:15:58 jonas
  2191. * fixed bug in g_concatcopy with -dnewra
  2192. Revision 1.113 2003/07/06 20:25:03 jonas
  2193. * fixed ppc compiler
  2194. Revision 1.112 2003/07/05 20:11:42 jonas
  2195. * create_paraloc_info() is now called separately for the caller and
  2196. callee info
  2197. * fixed ppc cycle
  2198. Revision 1.111 2003/07/02 22:18:04 peter
  2199. * paraloc splitted in callerparaloc,calleeparaloc
  2200. * sparc calling convention updates
  2201. Revision 1.110 2003/06/18 10:12:36 olle
  2202. * macos: fixes of loading-code
  2203. Revision 1.109 2003/06/14 22:32:43 jonas
  2204. * ppc compiles with -dnewra, haven't tried to compile anything with it
  2205. yet though
  2206. Revision 1.108 2003/06/13 21:19:31 peter
  2207. * current_procdef removed, use current_procinfo.procdef instead
  2208. Revision 1.107 2003/06/09 14:54:26 jonas
  2209. * (de)allocation of registers for parameters is now performed properly
  2210. (and checked on the ppc)
  2211. - removed obsolete allocation of all parameter registers at the start
  2212. of a procedure (and deallocation at the end)
  2213. Revision 1.106 2003/06/08 18:19:27 jonas
  2214. - removed duplicate identifier
  2215. Revision 1.105 2003/06/07 18:57:04 jonas
  2216. + added freeintparaloc
  2217. * ppc get/freeintparaloc now check whether the parameter regs are
  2218. properly allocated/deallocated (and get an extra list para)
  2219. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  2220. * fixed lot of missing pi_do_call's
  2221. Revision 1.104 2003/06/04 11:58:58 jonas
  2222. * calculate localsize also in g_return_from_proc since it's now called
  2223. before g_stackframe_entry (still have to fix macos)
  2224. * compilation fixes (cycle doesn't work yet though)
  2225. Revision 1.103 2003/06/01 21:38:06 peter
  2226. * getregisterfpu size parameter added
  2227. * op_const_reg size parameter added
  2228. * sparc updates
  2229. Revision 1.102 2003/06/01 13:42:18 jonas
  2230. * fix for bug in fixref that Peter found during the Sparc conversion
  2231. Revision 1.101 2003/05/30 18:52:10 jonas
  2232. * fixed bug with intregvars
  2233. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2234. rcgppc.a_param_ref, which previously got bogus size values
  2235. Revision 1.100 2003/05/29 21:17:27 jonas
  2236. * compile with -dppc603 to not use unaligned float loads in move() and
  2237. g_concatcopy, because the 603 and 604 take an exception for those
  2238. (and netbsd doesn't even handle those in the kernel). There are
  2239. still some of those left that could cause problems though (e.g.
  2240. in the set helpers)
  2241. Revision 1.99 2003/05/29 10:06:09 jonas
  2242. * also free temps in g_concatcopy if delsource is true
  2243. Revision 1.98 2003/05/28 23:58:18 jonas
  2244. * added missing initialization of rg.usedint{in,by}proc
  2245. * ppc now also saves/restores used fpu registers
  2246. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  2247. i386
  2248. Revision 1.97 2003/05/28 23:18:31 florian
  2249. * started to fix and clean up the sparc port
  2250. Revision 1.96 2003/05/24 11:59:42 jonas
  2251. * fixed integer typeconversion problems
  2252. Revision 1.95 2003/05/23 18:51:26 jonas
  2253. * fixed support for nested procedures and more parameters than those
  2254. which fit in registers (untested/probably not working: calling a
  2255. nested procedure from a deeper nested procedure)
  2256. Revision 1.94 2003/05/20 23:54:00 florian
  2257. + basic darwin support added
  2258. Revision 1.93 2003/05/15 22:14:42 florian
  2259. * fixed last commit, changing lastsaveintreg to r31 caused some strange problems
  2260. Revision 1.92 2003/05/15 21:37:00 florian
  2261. * sysv entry code saves r13 now as well
  2262. Revision 1.91 2003/05/15 19:39:09 florian
  2263. * fixed ppc compiler which was broken by Peter's changes
  2264. Revision 1.90 2003/05/12 18:43:50 jonas
  2265. * fixed g_concatcopy
  2266. Revision 1.89 2003/05/11 20:59:23 jonas
  2267. * fixed bug with large offsets in entrycode
  2268. Revision 1.88 2003/05/11 11:45:08 jonas
  2269. * fixed shifts
  2270. Revision 1.87 2003/05/11 11:07:33 jonas
  2271. * fixed optimizations in a_op_const_reg_reg()
  2272. Revision 1.86 2003/04/27 11:21:36 peter
  2273. * aktprocdef renamed to current_procinfo.procdef
  2274. * procinfo renamed to current_procinfo
  2275. * procinfo will now be stored in current_module so it can be
  2276. cleaned up properly
  2277. * gen_main_procsym changed to create_main_proc and release_main_proc
  2278. to also generate a tprocinfo structure
  2279. * fixed unit implicit initfinal
  2280. Revision 1.85 2003/04/26 22:56:11 jonas
  2281. * fix to a_op64_const_reg_reg
  2282. Revision 1.84 2003/04/26 16:08:41 jonas
  2283. * fixed g_flags2reg
  2284. Revision 1.83 2003/04/26 15:25:29 florian
  2285. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2286. Revision 1.82 2003/04/25 20:55:34 florian
  2287. * stack frame calculations are now completly done using the code generator
  2288. routines instead of generating directly assembler so also large stack frames
  2289. are handle properly
  2290. Revision 1.81 2003/04/24 11:24:00 florian
  2291. * fixed several issues with nested procedures
  2292. Revision 1.80 2003/04/23 22:18:01 peter
  2293. * fixes to get rtl compiled
  2294. Revision 1.79 2003/04/23 12:35:35 florian
  2295. * fixed several issues with powerpc
  2296. + applied a patch from Jonas for nested function calls (PowerPC only)
  2297. * ...
  2298. Revision 1.78 2003/04/16 09:26:55 jonas
  2299. * assembler procedures now again get a stackframe if they have local
  2300. variables. No space is reserved for a function result however.
  2301. Also, the register parameters aren't automatically saved on the stack
  2302. anymore in assembler procedures.
  2303. Revision 1.77 2003/04/06 16:39:11 jonas
  2304. * don't generate entry/exit code for assembler procedures
  2305. Revision 1.76 2003/03/22 18:01:13 jonas
  2306. * fixed linux entry/exit code generation
  2307. Revision 1.75 2003/03/19 14:26:26 jonas
  2308. * fixed R_TOC bugs introduced by new register allocator conversion
  2309. Revision 1.74 2003/03/13 22:57:45 olle
  2310. * change in a_loadaddr_ref_reg
  2311. Revision 1.73 2003/03/12 22:43:38 jonas
  2312. * more powerpc and generic fixes related to the new register allocator
  2313. Revision 1.72 2003/03/11 21:46:24 jonas
  2314. * lots of new regallocator fixes, both in generic and ppc-specific code
  2315. (ppc compiler still can't compile the linux system unit though)
  2316. Revision 1.71 2003/02/19 22:00:16 daniel
  2317. * Code generator converted to new register notation
  2318. - Horribily outdated todo.txt removed
  2319. Revision 1.70 2003/01/13 17:17:50 olle
  2320. * changed global var access, TOC now contain pointers to globals
  2321. * fixed handling of function pointers
  2322. Revision 1.69 2003/01/09 22:00:53 florian
  2323. * fixed some PowerPC issues
  2324. Revision 1.68 2003/01/08 18:43:58 daniel
  2325. * Tregister changed into a record
  2326. Revision 1.67 2002/12/15 19:22:01 florian
  2327. * fixed some crashes and a rte 201
  2328. Revision 1.66 2002/11/28 10:55:16 olle
  2329. * macos: changing code gen for references to globals
  2330. Revision 1.65 2002/11/07 15:50:23 jonas
  2331. * fixed bctr(l) problems
  2332. Revision 1.64 2002/11/04 18:24:19 olle
  2333. * macos: globals are located in TOC and relative r2, instead of absolute
  2334. Revision 1.63 2002/10/28 22:24:28 olle
  2335. * macos entry/exit: only used registers are saved
  2336. - macos entry/exit: stackptr not saved in r31 anymore
  2337. * macos entry/exit: misc fixes
  2338. Revision 1.62 2002/10/19 23:51:48 olle
  2339. * macos stack frame size computing updated
  2340. + macos epilogue: control register now restored
  2341. * macos prologue and epilogue: fp reg now saved and restored
  2342. Revision 1.61 2002/10/19 12:50:36 olle
  2343. * reorganized prologue and epilogue routines
  2344. Revision 1.60 2002/10/02 21:49:51 florian
  2345. * all A_BL instructions replaced by calls to a_call_name
  2346. Revision 1.59 2002/10/02 13:24:58 jonas
  2347. * changed a_call_* so that no superfluous code is generated anymore
  2348. Revision 1.58 2002/09/17 18:54:06 jonas
  2349. * a_load_reg_reg() now has two size parameters: source and dest. This
  2350. allows some optimizations on architectures that don't encode the
  2351. register size in the register name.
  2352. Revision 1.57 2002/09/10 21:22:25 jonas
  2353. + added some internal errors
  2354. * fixed bug in sysv exit code
  2355. Revision 1.56 2002/09/08 20:11:56 jonas
  2356. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2357. Revision 1.55 2002/09/08 13:03:26 jonas
  2358. * several large offset-related fixes
  2359. Revision 1.54 2002/09/07 17:54:58 florian
  2360. * first part of PowerPC fixes
  2361. Revision 1.53 2002/09/07 15:25:14 peter
  2362. * old logs removed and tabs fixed
  2363. Revision 1.52 2002/09/02 10:14:51 jonas
  2364. + a_call_reg()
  2365. * small fix in a_call_ref()
  2366. Revision 1.51 2002/09/02 06:09:02 jonas
  2367. * fixed range error
  2368. Revision 1.50 2002/09/01 21:04:49 florian
  2369. * several powerpc related stuff fixed
  2370. Revision 1.49 2002/09/01 12:09:27 peter
  2371. + a_call_reg, a_call_loc added
  2372. * removed exprasmlist references
  2373. Revision 1.48 2002/08/31 21:38:02 jonas
  2374. * fixed a_call_ref (it should load ctr, not lr)
  2375. Revision 1.47 2002/08/31 21:30:45 florian
  2376. * fixed several problems caused by Jonas' commit :)
  2377. Revision 1.46 2002/08/31 19:25:50 jonas
  2378. + implemented a_call_ref()
  2379. Revision 1.45 2002/08/18 22:16:14 florian
  2380. + the ppc gas assembler writer adds now registers aliases
  2381. to the assembler file
  2382. Revision 1.44 2002/08/17 18:23:53 florian
  2383. * some assembler writer bugs fixed
  2384. Revision 1.43 2002/08/17 09:23:49 florian
  2385. * first part of procinfo rewrite
  2386. Revision 1.42 2002/08/16 14:24:59 carl
  2387. * issameref() to test if two references are the same (then emit no opcodes)
  2388. + ret_in_reg to replace ret_in_acc
  2389. (fix some register allocation bugs at the same time)
  2390. + save_std_register now has an extra parameter which is the
  2391. usedinproc registers
  2392. Revision 1.41 2002/08/15 08:13:54 carl
  2393. - a_load_sym_ofs_reg removed
  2394. * loadvmt now calls loadaddr_ref_reg instead
  2395. Revision 1.40 2002/08/11 14:32:32 peter
  2396. * renamed current_library to objectlibrary
  2397. Revision 1.39 2002/08/11 13:24:18 peter
  2398. * saving of asmsymbols in ppu supported
  2399. * asmsymbollist global is removed and moved into a new class
  2400. tasmlibrarydata that will hold the info of a .a file which
  2401. corresponds with a single module. Added librarydata to tmodule
  2402. to keep the library info stored for the module. In the future the
  2403. objectfiles will also be stored to the tasmlibrarydata class
  2404. * all getlabel/newasmsymbol and friends are moved to the new class
  2405. Revision 1.38 2002/08/11 11:39:31 jonas
  2406. + powerpc-specific genlinearlist
  2407. Revision 1.37 2002/08/10 17:15:31 jonas
  2408. * various fixes and optimizations
  2409. Revision 1.36 2002/08/06 20:55:23 florian
  2410. * first part of ppc calling conventions fix
  2411. Revision 1.35 2002/08/06 07:12:05 jonas
  2412. * fixed bug in g_flags2reg()
  2413. * and yet more constant operation fixes :)
  2414. Revision 1.34 2002/08/05 08:58:53 jonas
  2415. * fixed compilation problems
  2416. Revision 1.33 2002/08/04 12:57:55 jonas
  2417. * more misc. fixes, mostly constant-related
  2418. }