cgcpu.pas 99 KB

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