cgcpu.pas 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,symtype,symdef,
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcg)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : tcgpara);override;
  36. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  37. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);override;
  38. procedure a_call_name(list : taasmoutput;const s : string);override;
  39. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  40. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  41. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  42. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  43. size: tcgsize; a: aint; src, dst: tregister); override;
  44. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  45. size: tcgsize; src1, src2, dst: tregister); override;
  46. { move instructions }
  47. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aint;reg : tregister);override;
  48. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  49. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  50. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  51. { fpu move instructions }
  52. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  53. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  54. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  55. { comparison operations }
  56. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  57. l : tasmlabel);override;
  58. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_name(list : taasmoutput;const s : string); override;
  60. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  62. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  63. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  64. procedure g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean); override;
  65. procedure g_save_standard_registers(list:Taasmoutput); override;
  66. procedure g_restore_standard_registers(list:Taasmoutput); override;
  67. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  68. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);override;
  69. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  70. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  71. { that's the case, we can use rlwinm to do an AND operation }
  72. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  73. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  74. procedure g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  75. function g_darwin_indirect_sym_load(list: taasmoutput; const symname: string): tregister;
  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 : aint);
  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. function save_regs(list : taasmoutput):longint;
  99. procedure restore_regs(list : taasmoutput);
  100. function get_darwin_call_stub(const s: string): tasmsymbol;
  101. end;
  102. tcg64fppc = class(tcg64f32)
  103. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  104. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  105. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  106. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  107. end;
  108. const
  109. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  110. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  111. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  112. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  113. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  114. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  115. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  116. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  117. implementation
  118. uses
  119. globals,verbose,systems,cutils,
  120. symconst,symsym,fmodule,
  121. rgobj,tgobj,cpupi,procinfo,paramgr;
  122. procedure tcgppc.init_register_allocators;
  123. begin
  124. inherited init_register_allocators;
  125. if target_info.system=system_powerpc_darwin then
  126. begin
  127. {
  128. if pi_needs_got in current_procinfo.flags then
  129. begin
  130. current_procinfo.got:=NR_R31;
  131. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  132. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  133. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  134. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  135. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  136. RS_R14,RS_R13],first_int_imreg,[]);
  137. end
  138. else}
  139. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  140. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  141. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  142. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  143. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  144. RS_R14,RS_R13],first_int_imreg,[]);
  145. end
  146. else
  147. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  148. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  149. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  150. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  151. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  152. RS_R14,RS_R13],first_int_imreg,[]);
  153. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  154. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  155. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  156. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  157. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  158. {$warning FIX ME}
  159. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  160. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  161. end;
  162. procedure tcgppc.done_register_allocators;
  163. begin
  164. rg[R_INTREGISTER].free;
  165. rg[R_FPUREGISTER].free;
  166. rg[R_MMREGISTER].free;
  167. inherited done_register_allocators;
  168. end;
  169. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : tcgpara);
  170. var
  171. ref: treference;
  172. begin
  173. paraloc.check_simple_location;
  174. case paraloc.location^.loc of
  175. LOC_REGISTER,LOC_CREGISTER:
  176. a_load_const_reg(list,size,a,paraloc.location^.register);
  177. LOC_REFERENCE:
  178. begin
  179. reference_reset(ref);
  180. ref.base:=paraloc.location^.reference.index;
  181. ref.offset:=paraloc.location^.reference.offset;
  182. a_load_const_ref(list,size,a,ref);
  183. end;
  184. else
  185. internalerror(2002081101);
  186. end;
  187. end;
  188. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : tcgpara);
  189. var
  190. tmpref, ref: treference;
  191. location: pcgparalocation;
  192. sizeleft: aint;
  193. begin
  194. location := paraloc.location;
  195. tmpref := r;
  196. sizeleft := paraloc.intsize;
  197. while assigned(location) do
  198. begin
  199. case location^.loc of
  200. LOC_REGISTER,LOC_CREGISTER:
  201. begin
  202. {$ifndef cpu64bit}
  203. if (sizeleft <> 3) then
  204. begin
  205. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  206. { the following is only for AIX abi systems, but the }
  207. { conditions should never be true for SYSV (if they }
  208. { are, there is a bug in cpupara) }
  209. { update: this doesn't work yet (we have to shift }
  210. { right again in ncgutil when storing the parameters, }
  211. { and additionally Apple's documentation seems to be }
  212. { wrong, in that these values are always kept in the }
  213. { lower bytes of the registers }
  214. {
  215. if (paraloc.composite) and
  216. (sizeleft <= 2) and
  217. ((paraloc.intsize > 4) or
  218. (target_info.system <> system_powerpc_darwin)) then
  219. begin
  220. case sizeleft of
  221. 1:
  222. a_op_const_reg(list,OP_SHL,OS_INT,24,location^.register);
  223. 2:
  224. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  225. else
  226. internalerror(2005010910);
  227. end;
  228. end;
  229. }
  230. end
  231. else
  232. begin
  233. a_load_ref_reg(list,OS_16,OS_16,tmpref,location^.register);
  234. a_reg_alloc(list,NR_R0);
  235. inc(tmpref.offset,2);
  236. a_load_ref_reg(list,OS_8,OS_8,tmpref,newreg(R_INTREGISTER,RS_R0,R_SUBNONE));
  237. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  238. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location^.register,newreg(R_INTREGISTER,RS_R0,R_SUBNONE),8,16,31-8));
  239. a_reg_dealloc(list,NR_R0);
  240. dec(tmpref.offset,2);
  241. end;
  242. {$else not cpu64bit}
  243. {$error add 64 bit support for non power of 2 loads in a_param_ref}
  244. {$endif not cpu64bit}
  245. end;
  246. LOC_REFERENCE:
  247. begin
  248. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  249. g_concatcopy(list,tmpref,ref,sizeleft);
  250. if assigned(location^.next) then
  251. internalerror(2005010710);
  252. end;
  253. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  254. case location^.size of
  255. OS_F32, OS_F64:
  256. a_loadfpu_ref_reg(list,location^.size,tmpref,location^.register);
  257. else
  258. internalerror(2002072801);
  259. end;
  260. LOC_VOID:
  261. begin
  262. // nothing to do
  263. end;
  264. else
  265. internalerror(2002081103);
  266. end;
  267. inc(tmpref.offset,tcgsize2size[location^.size]);
  268. dec(sizeleft,tcgsize2size[location^.size]);
  269. location := location^.next;
  270. end;
  271. end;
  272. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);
  273. var
  274. ref: treference;
  275. tmpreg: tregister;
  276. begin
  277. paraloc.check_simple_location;
  278. case paraloc.location^.loc of
  279. LOC_REGISTER,LOC_CREGISTER:
  280. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  281. LOC_REFERENCE:
  282. begin
  283. reference_reset(ref);
  284. ref.base := paraloc.location^.reference.index;
  285. ref.offset := paraloc.location^.reference.offset;
  286. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  287. a_loadaddr_ref_reg(list,r,tmpreg);
  288. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  289. end;
  290. else
  291. internalerror(2002080701);
  292. end;
  293. end;
  294. function tcgppc.get_darwin_call_stub(const s: string): tasmsymbol;
  295. var
  296. stubname: string;
  297. href: treference;
  298. l1: tasmsymbol;
  299. begin
  300. { function declared in the current unit? }
  301. { doesn't work correctly, because this will also return a hit if we }
  302. { previously took the address of an external procedure. It doesn't }
  303. { really matter, the linker will remove all unnecessary stubs. }
  304. { result := objectlibrary.getasmsymbol(s);
  305. if not(assigned(result)) then
  306. begin }
  307. stubname := 'L'+s+'$stub';
  308. result := objectlibrary.getasmsymbol(stubname);
  309. { end; }
  310. if assigned(result) then
  311. exit;
  312. if asmlist[al_imports]=nil then
  313. asmlist[al_imports]:=TAAsmoutput.create;
  314. asmlist[al_imports].concat(Tai_section.Create(sec_data,'',0));
  315. asmlist[al_imports].concat(Tai_section.create(sec_stub,'',0));
  316. asmlist[al_imports].concat(Tai_align.Create(4));
  317. result := objectlibrary.newasmsymbol(stubname,AB_EXTERNAL,AT_FUNCTION);
  318. asmlist[al_imports].concat(Tai_symbol.Create(result,0));
  319. asmlist[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  320. l1 := objectlibrary.newasmsymbol('L'+s+'$lazy_ptr',AB_EXTERNAL,AT_FUNCTION);
  321. reference_reset_symbol(href,l1,0);
  322. href.refaddr := addr_hi;
  323. asmlist[al_imports].concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  324. href.refaddr := addr_lo;
  325. href.base := NR_R11;
  326. asmlist[al_imports].concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  327. asmlist[al_imports].concat(taicpu.op_reg(A_MTCTR,NR_R12));
  328. asmlist[al_imports].concat(taicpu.op_none(A_BCTR));
  329. asmlist[al_imports].concat(Tai_section.Create(sec_data,'',0));
  330. asmlist[al_imports].concat(tai_directive.create(asd_lazy_symbol_pointer,''));
  331. asmlist[al_imports].concat(Tai_symbol.Create(l1,0));
  332. asmlist[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  333. asmlist[al_imports].concat(tai_const.createname(strpnew('dyld_stub_binding_helper'),AT_FUNCTION,0));
  334. end;
  335. { calling a procedure by name }
  336. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  337. begin
  338. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  339. if it is a cross-TOC call. If so, it also replaces the NOP
  340. with some restore code.}
  341. if (target_info.system <> system_powerpc_darwin) then
  342. begin
  343. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  344. if target_info.system=system_powerpc_macos then
  345. list.concat(taicpu.op_none(A_NOP));
  346. end
  347. else
  348. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  349. {
  350. the compiler does not properly set this flag anymore in pass 1, and
  351. for now we only need it after pass 2 (I hope) (JM)
  352. if not(pi_do_call in current_procinfo.flags) then
  353. internalerror(2003060703);
  354. }
  355. include(current_procinfo.flags,pi_do_call);
  356. end;
  357. { calling a procedure by address }
  358. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  359. var
  360. tmpreg : tregister;
  361. tmpref : treference;
  362. begin
  363. if target_info.system=system_powerpc_macos then
  364. begin
  365. {Generate instruction to load the procedure address from
  366. the transition vector.}
  367. //TODO: Support cross-TOC calls.
  368. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  369. reference_reset(tmpref);
  370. tmpref.offset := 0;
  371. //tmpref.symaddr := refs_full;
  372. tmpref.base:= reg;
  373. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  374. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  375. end
  376. else
  377. list.concat(taicpu.op_reg(A_MTCTR,reg));
  378. list.concat(taicpu.op_none(A_BCTRL));
  379. //if target_info.system=system_powerpc_macos then
  380. // //NOP is not needed here.
  381. // list.concat(taicpu.op_none(A_NOP));
  382. include(current_procinfo.flags,pi_do_call);
  383. {
  384. if not(pi_do_call in current_procinfo.flags) then
  385. internalerror(2003060704);
  386. }
  387. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  388. end;
  389. {********************** load instructions ********************}
  390. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aint; reg : TRegister);
  391. begin
  392. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  393. internalerror(2002090902);
  394. if (a >= low(smallint)) and
  395. (a <= high(smallint)) then
  396. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  397. else if ((a and $ffff) <> 0) then
  398. begin
  399. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  400. if ((a shr 16) <> 0) or
  401. (smallint(a and $ffff) < 0) then
  402. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  403. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  404. end
  405. else
  406. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  407. end;
  408. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  409. const
  410. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  411. { indexed? updating?}
  412. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  413. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  414. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  415. var
  416. op: TAsmOp;
  417. ref2: TReference;
  418. begin
  419. ref2 := ref;
  420. fixref(list,ref2);
  421. if tosize in [OS_S8..OS_S16] then
  422. { storing is the same for signed and unsigned values }
  423. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  424. { 64 bit stuff should be handled separately }
  425. if tosize in [OS_64,OS_S64] then
  426. internalerror(200109236);
  427. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  428. a_load_store(list,op,reg,ref2);
  429. End;
  430. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  431. const
  432. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  433. { indexed? updating?}
  434. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  435. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  436. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  437. { 64bit stuff should be handled separately }
  438. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  439. { 128bit stuff too }
  440. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  441. { there's no load-byte-with-sign-extend :( }
  442. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  443. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  444. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  445. var
  446. op: tasmop;
  447. ref2: treference;
  448. begin
  449. { TODO: optimize/take into consideration fromsize/tosize. Will }
  450. { probably only matter for OS_S8 loads though }
  451. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  452. internalerror(2002090902);
  453. ref2 := ref;
  454. fixref(list,ref2);
  455. { the caller is expected to have adjusted the reference already }
  456. { in this case }
  457. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  458. fromsize := tosize;
  459. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  460. a_load_store(list,op,reg,ref2);
  461. { sign extend shortint if necessary, since there is no }
  462. { load instruction that does that automatically (JM) }
  463. if fromsize = OS_S8 then
  464. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  465. end;
  466. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  467. var
  468. instr: taicpu;
  469. begin
  470. case tosize of
  471. OS_8:
  472. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  473. reg2,reg1,0,31-8+1,31);
  474. OS_S8:
  475. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  476. OS_16:
  477. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  478. reg2,reg1,0,31-16+1,31);
  479. OS_S16:
  480. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  481. OS_32,OS_S32:
  482. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  483. else internalerror(2002090901);
  484. end;
  485. list.concat(instr);
  486. rg[R_INTREGISTER].add_move_instruction(instr);
  487. end;
  488. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  489. var
  490. instr: taicpu;
  491. begin
  492. instr := taicpu.op_reg_reg(A_FMR,reg2,reg1);
  493. list.concat(instr);
  494. rg[R_FPUREGISTER].add_move_instruction(instr);
  495. end;
  496. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  497. const
  498. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  499. { indexed? updating?}
  500. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  501. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  502. var
  503. op: tasmop;
  504. ref2: treference;
  505. begin
  506. { several functions call this procedure with OS_32 or OS_64 }
  507. { so this makes life easier (FK) }
  508. case size of
  509. OS_32,OS_F32:
  510. size:=OS_F32;
  511. OS_64,OS_F64,OS_C64:
  512. size:=OS_F64;
  513. else
  514. internalerror(200201121);
  515. end;
  516. ref2 := ref;
  517. fixref(list,ref2);
  518. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  519. a_load_store(list,op,reg,ref2);
  520. end;
  521. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  522. const
  523. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  524. { indexed? updating?}
  525. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  526. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  527. var
  528. op: tasmop;
  529. ref2: treference;
  530. begin
  531. if not(size in [OS_F32,OS_F64]) then
  532. internalerror(200201122);
  533. ref2 := ref;
  534. fixref(list,ref2);
  535. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  536. a_load_store(list,op,reg,ref2);
  537. end;
  538. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  539. begin
  540. a_op_const_reg_reg(list,op,size,a,reg,reg);
  541. end;
  542. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  543. begin
  544. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  545. end;
  546. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  547. size: tcgsize; a: aint; src, dst: tregister);
  548. var
  549. l1,l2: longint;
  550. oplo, ophi: tasmop;
  551. scratchreg: tregister;
  552. useReg, gotrlwi: boolean;
  553. procedure do_lo_hi;
  554. begin
  555. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  556. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  557. end;
  558. begin
  559. if op = OP_SUB then
  560. begin
  561. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  562. exit;
  563. end;
  564. ophi := TOpCG2AsmOpConstHi[op];
  565. oplo := TOpCG2AsmOpConstLo[op];
  566. gotrlwi := get_rlwi_const(a,l1,l2);
  567. if (op in [OP_AND,OP_OR,OP_XOR]) then
  568. begin
  569. if (a = 0) then
  570. begin
  571. if op = OP_AND then
  572. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  573. else
  574. a_load_reg_reg(list,size,size,src,dst);
  575. exit;
  576. end
  577. else if (a = -1) then
  578. begin
  579. case op of
  580. OP_OR:
  581. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  582. OP_XOR:
  583. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  584. OP_AND:
  585. a_load_reg_reg(list,size,size,src,dst);
  586. end;
  587. exit;
  588. end
  589. else if (aword(a) <= high(word)) and
  590. ((op <> OP_AND) or
  591. not gotrlwi) then
  592. begin
  593. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  594. exit;
  595. end;
  596. { all basic constant instructions also have a shifted form that }
  597. { works only on the highest 16bits, so if lo(a) is 0, we can }
  598. { use that one }
  599. if (word(a) = 0) and
  600. (not(op = OP_AND) or
  601. not gotrlwi) then
  602. begin
  603. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  604. exit;
  605. end;
  606. end
  607. else if (op = OP_ADD) then
  608. if a = 0 then
  609. begin
  610. a_load_reg_reg(list,size,size,src,dst);
  611. exit
  612. end
  613. else if (a >= low(smallint)) and
  614. (a <= high(smallint)) then
  615. begin
  616. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  617. exit;
  618. end;
  619. { otherwise, the instructions we can generate depend on the }
  620. { operation }
  621. useReg := false;
  622. case op of
  623. OP_DIV,OP_IDIV:
  624. if (a = 0) then
  625. internalerror(200208103)
  626. else if (a = 1) then
  627. begin
  628. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  629. exit
  630. end
  631. else if ispowerof2(a,l1) then
  632. begin
  633. case op of
  634. OP_DIV:
  635. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  636. OP_IDIV:
  637. begin
  638. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  639. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  640. end;
  641. end;
  642. exit;
  643. end
  644. else
  645. usereg := true;
  646. OP_IMUL, OP_MUL:
  647. if (a = 0) then
  648. begin
  649. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  650. exit
  651. end
  652. else if (a = 1) then
  653. begin
  654. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  655. exit
  656. end
  657. else if ispowerof2(a,l1) then
  658. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  659. else if (longint(a) >= low(smallint)) and
  660. (longint(a) <= high(smallint)) then
  661. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  662. else
  663. usereg := true;
  664. OP_ADD:
  665. begin
  666. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  667. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  668. smallint((a shr 16) + ord(smallint(a) < 0))));
  669. end;
  670. OP_OR:
  671. { try to use rlwimi }
  672. if gotrlwi and
  673. (src = dst) then
  674. begin
  675. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  676. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  677. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  678. scratchreg,0,l1,l2));
  679. end
  680. else
  681. do_lo_hi;
  682. OP_AND:
  683. { try to use rlwinm }
  684. if gotrlwi then
  685. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  686. src,0,l1,l2))
  687. else
  688. useReg := true;
  689. OP_XOR:
  690. do_lo_hi;
  691. OP_SHL,OP_SHR,OP_SAR:
  692. begin
  693. if (a and 31) <> 0 Then
  694. list.concat(taicpu.op_reg_reg_const(
  695. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  696. else
  697. a_load_reg_reg(list,size,size,src,dst);
  698. if (a shr 5) <> 0 then
  699. internalError(68991);
  700. end
  701. else
  702. internalerror(200109091);
  703. end;
  704. { if all else failed, load the constant in a register and then }
  705. { perform the operation }
  706. if useReg then
  707. begin
  708. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  709. a_load_const_reg(list,OS_32,a,scratchreg);
  710. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  711. end;
  712. end;
  713. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  714. size: tcgsize; src1, src2, dst: tregister);
  715. const
  716. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  717. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  718. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  719. begin
  720. case op of
  721. OP_NEG,OP_NOT:
  722. begin
  723. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  724. if (op = OP_NOT) and
  725. not(size in [OS_32,OS_S32]) then
  726. { zero/sign extend result again }
  727. a_load_reg_reg(list,OS_32,size,dst,dst);
  728. end;
  729. else
  730. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  731. end;
  732. end;
  733. {*************** compare instructructions ****************}
  734. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  735. l : tasmlabel);
  736. var
  737. scratch_register: TRegister;
  738. signed: boolean;
  739. begin
  740. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  741. { in the following case, we generate more efficient code when }
  742. { signed is true }
  743. if (cmp_op in [OC_EQ,OC_NE]) and
  744. (aword(a) > $ffff) then
  745. signed := true;
  746. if signed then
  747. if (a >= low(smallint)) and (a <= high(smallint)) Then
  748. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  749. else
  750. begin
  751. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  752. a_load_const_reg(list,OS_32,a,scratch_register);
  753. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  754. end
  755. else
  756. if (aword(a) <= $ffff) then
  757. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  758. else
  759. begin
  760. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  761. a_load_const_reg(list,OS_32,a,scratch_register);
  762. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  763. end;
  764. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  765. end;
  766. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  767. reg1,reg2 : tregister;l : tasmlabel);
  768. var
  769. op: tasmop;
  770. begin
  771. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  772. op := A_CMPW
  773. else
  774. op := A_CMPLW;
  775. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  776. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  777. end;
  778. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  779. begin
  780. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  781. end;
  782. procedure tcgppc.a_jmp_name(list : taasmoutput;const s : string);
  783. var
  784. p : taicpu;
  785. begin
  786. if (target_info.system = system_powerpc_darwin) then
  787. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  788. else
  789. p := taicpu.op_sym(A_B,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION));
  790. p.is_jmp := true;
  791. list.concat(p)
  792. end;
  793. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  794. begin
  795. a_jmp(list,A_B,C_None,0,l);
  796. end;
  797. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  798. var
  799. c: tasmcond;
  800. begin
  801. c := flags_to_cond(f);
  802. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  803. end;
  804. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  805. var
  806. testbit: byte;
  807. bitvalue: boolean;
  808. begin
  809. { get the bit to extract from the conditional register + its }
  810. { requested value (0 or 1) }
  811. testbit := ((f.cr-RS_CR0) * 4);
  812. case f.flag of
  813. F_EQ,F_NE:
  814. begin
  815. inc(testbit,2);
  816. bitvalue := f.flag = F_EQ;
  817. end;
  818. F_LT,F_GE:
  819. begin
  820. bitvalue := f.flag = F_LT;
  821. end;
  822. F_GT,F_LE:
  823. begin
  824. inc(testbit);
  825. bitvalue := f.flag = F_GT;
  826. end;
  827. else
  828. internalerror(200112261);
  829. end;
  830. { load the conditional register in the destination reg }
  831. list.concat(taicpu.op_reg(A_MFCR,reg));
  832. { we will move the bit that has to be tested to bit 0 by rotating }
  833. { left }
  834. testbit := (testbit + 1) and 31;
  835. { extract bit }
  836. list.concat(taicpu.op_reg_reg_const_const_const(
  837. A_RLWINM,reg,reg,testbit,31,31));
  838. { if we need the inverse, xor with 1 }
  839. if not bitvalue then
  840. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  841. end;
  842. (*
  843. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  844. var
  845. testbit: byte;
  846. bitvalue: boolean;
  847. begin
  848. { get the bit to extract from the conditional register + its }
  849. { requested value (0 or 1) }
  850. case f.simple of
  851. false:
  852. begin
  853. { we don't generate this in the compiler }
  854. internalerror(200109062);
  855. end;
  856. true:
  857. case f.cond of
  858. C_None:
  859. internalerror(200109063);
  860. C_LT..C_NU:
  861. begin
  862. testbit := (ord(f.cr) - ord(R_CR0))*4;
  863. inc(testbit,AsmCondFlag2BI[f.cond]);
  864. bitvalue := AsmCondFlagTF[f.cond];
  865. end;
  866. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  867. begin
  868. testbit := f.crbit
  869. bitvalue := AsmCondFlagTF[f.cond];
  870. end;
  871. else
  872. internalerror(200109064);
  873. end;
  874. end;
  875. { load the conditional register in the destination reg }
  876. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  877. { we will move the bit that has to be tested to bit 31 -> rotate }
  878. { left by bitpos+1 (remember, this is big-endian!) }
  879. if bitpos <> 31 then
  880. inc(bitpos)
  881. else
  882. bitpos := 0;
  883. { extract bit }
  884. list.concat(taicpu.op_reg_reg_const_const_const(
  885. A_RLWINM,reg,reg,bitpos,31,31));
  886. { if we need the inverse, xor with 1 }
  887. if not bitvalue then
  888. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  889. end;
  890. *)
  891. { *********** entry/exit code and address loading ************ }
  892. procedure tcgppc.g_save_standard_registers(list:Taasmoutput);
  893. begin
  894. { this work is done in g_proc_entry }
  895. end;
  896. procedure tcgppc.g_restore_standard_registers(list:Taasmoutput);
  897. begin
  898. { this work is done in g_proc_exit }
  899. end;
  900. procedure tcgppc.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  901. { generated the entry code of a procedure/function. Note: localsize is the }
  902. { sum of the size necessary for local variables and the maximum possible }
  903. { combined size of ALL the parameters of a procedure called by the current }
  904. { one. }
  905. { This procedure may be called before, as well as after g_return_from_proc }
  906. { is called. NOTE registers are not to be allocated through the register }
  907. { allocator here, because the register colouring has already occured !! }
  908. var regcounter,firstregfpu,firstregint: TSuperRegister;
  909. href : treference;
  910. usesfpr,usesgpr,gotgot : boolean;
  911. cond : tasmcond;
  912. instr : taicpu;
  913. begin
  914. { CR and LR only have to be saved in case they are modified by the current }
  915. { procedure, but currently this isn't checked, so save them always }
  916. { following is the entry code as described in "Altivec Programming }
  917. { Interface Manual", bar the saving of AltiVec registers }
  918. a_reg_alloc(list,NR_STACK_POINTER_REG);
  919. usesgpr := false;
  920. usesfpr := false;
  921. if not(po_assembler in current_procinfo.procdef.procoptions) then
  922. begin
  923. { save link register? }
  924. if (pi_do_call in current_procinfo.flags) or
  925. ([cs_lineinfo,cs_debuginfo] * aktmoduleswitches <> []) then
  926. begin
  927. a_reg_alloc(list,NR_R0);
  928. { save return address... }
  929. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  930. { ... in caller's frame }
  931. case target_info.abi of
  932. abi_powerpc_aix:
  933. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  934. abi_powerpc_sysv:
  935. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  936. end;
  937. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  938. a_reg_dealloc(list,NR_R0);
  939. end;
  940. (*
  941. { save the CR if necessary in callers frame. }
  942. if target_info.abi = abi_powerpc_aix then
  943. if false then { Not needed at the moment. }
  944. begin
  945. a_reg_alloc(list,NR_R0);
  946. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  947. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  948. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  949. a_reg_dealloc(list,NR_R0);
  950. end;
  951. *)
  952. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  953. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  954. usesgpr := firstregint <> 32;
  955. usesfpr := firstregfpu <> 32;
  956. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  957. begin
  958. a_reg_alloc(list,NR_R12);
  959. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  960. end;
  961. end;
  962. { no GOT pointer loaded yet }
  963. gotgot:=false;
  964. if usesfpr then
  965. begin
  966. { save floating-point registers
  967. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  968. begin
  969. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  970. gotgot:=true;
  971. end
  972. else
  973. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14),AB_EXTERNAL,AT_FUNCTION));
  974. }
  975. reference_reset_base(href,NR_R1,-8);
  976. for regcounter:=firstregfpu to RS_F31 do
  977. begin
  978. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  979. dec(href.offset,8);
  980. end;
  981. { compute start of gpr save area }
  982. inc(href.offset,4);
  983. end
  984. else
  985. { compute start of gpr save area }
  986. reference_reset_base(href,NR_R1,-4);
  987. { save gprs and fetch GOT pointer }
  988. if usesgpr then
  989. begin
  990. {
  991. if cs_create_pic in aktmoduleswitches then
  992. begin
  993. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  994. gotgot:=true;
  995. end
  996. else
  997. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14),AB_EXTERNAL,AT_FUNCTION))
  998. }
  999. if (firstregint <= RS_R22) or
  1000. ((cs_littlesize in aktglobalswitches) and
  1001. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1002. (firstregint <= RS_R29)) then
  1003. begin
  1004. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1005. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1006. end
  1007. else
  1008. for regcounter:=firstregint to RS_R31 do
  1009. begin
  1010. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  1011. dec(href.offset,4);
  1012. end;
  1013. end;
  1014. { done in ncgutil because it may only be released after the parameters }
  1015. { have been moved to their final resting place }
  1016. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  1017. { a_reg_dealloc(list,NR_R12); }
  1018. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1019. (*
  1020. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1021. case target_info.system of
  1022. system_powerpc_darwin:
  1023. begin
  1024. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1025. fillchar(cond,sizeof(cond),0);
  1026. cond.simple:=false;
  1027. cond.bo:=20;
  1028. cond.bi:=31;
  1029. instr:=taicpu.op_sym(A_BCL,current_procinfo.gotlabel);
  1030. instr.setcondition(cond);
  1031. list.concat(instr);
  1032. a_label(list,current_procinfo.gotlabel);
  1033. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1034. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1035. end;
  1036. else
  1037. begin
  1038. a_reg_alloc(list,NR_R31);
  1039. { place GOT ptr in r31 }
  1040. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1041. end;
  1042. end;
  1043. *)
  1044. if (not nostackframe) and
  1045. (localsize <> 0) then
  1046. begin
  1047. if (localsize <= high(smallint)) then
  1048. begin
  1049. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1050. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1051. end
  1052. else
  1053. begin
  1054. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1055. { can't use getregisterint here, the register colouring }
  1056. { is already done when we get here }
  1057. href.index := NR_R11;
  1058. a_reg_alloc(list,href.index);
  1059. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1060. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1061. a_reg_dealloc(list,href.index);
  1062. end;
  1063. end;
  1064. { save the CR if necessary ( !!! never done currently ) }
  1065. { still need to find out where this has to be done for SystemV
  1066. a_reg_alloc(list,R_0);
  1067. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1068. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1069. new_reference(STACK_POINTER_REG,LA_CR)));
  1070. a_reg_dealloc(list,R_0);
  1071. }
  1072. { now comes the AltiVec context save, not yet implemented !!! }
  1073. end;
  1074. procedure tcgppc.g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean);
  1075. { This procedure may be called before, as well as after g_stackframe_entry }
  1076. { is called. NOTE registers are not to be allocated through the register }
  1077. { allocator here, because the register colouring has already occured !! }
  1078. var
  1079. regcounter,firstregfpu,firstregint: TsuperRegister;
  1080. href : treference;
  1081. usesfpr,usesgpr,genret : boolean;
  1082. localsize: aint;
  1083. begin
  1084. { AltiVec context restore, not yet implemented !!! }
  1085. usesfpr:=false;
  1086. usesgpr:=false;
  1087. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1088. begin
  1089. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  1090. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  1091. usesgpr := firstregint <> 32;
  1092. usesfpr := firstregfpu <> 32;
  1093. end;
  1094. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1095. { adjust r1 }
  1096. { (register allocator is no longer valid at this time and an add of 0 }
  1097. { is translated into a move, which is then registered with the register }
  1098. { allocator, causing a crash }
  1099. if (not nostackframe) and
  1100. (localsize <> 0) then
  1101. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1102. { no return (blr) generated yet }
  1103. genret:=true;
  1104. if usesfpr then
  1105. begin
  1106. reference_reset_base(href,NR_R1,-8);
  1107. for regcounter := firstregfpu to RS_F31 do
  1108. begin
  1109. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1110. dec(href.offset,8);
  1111. end;
  1112. inc(href.offset,4);
  1113. end
  1114. else
  1115. reference_reset_base(href,NR_R1,-4);
  1116. if (usesgpr) then
  1117. begin
  1118. if (firstregint <= RS_R22) or
  1119. ((cs_littlesize in aktglobalswitches) and
  1120. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1121. (firstregint <= RS_R29)) then
  1122. begin
  1123. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1124. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1125. end
  1126. else
  1127. for regcounter:=firstregint to RS_R31 do
  1128. begin
  1129. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  1130. dec(href.offset,4);
  1131. end;
  1132. end;
  1133. (*
  1134. { restore fprs and return }
  1135. if usesfpr then
  1136. begin
  1137. { address of fpr save area to r11 }
  1138. r:=NR_R12;
  1139. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1140. {
  1141. if (pi_do_call in current_procinfo.flags) then
  1142. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1143. '_x',AB_EXTERNAL,AT_FUNCTION))
  1144. else
  1145. { leaf node => lr haven't to be restored }
  1146. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1147. '_l');
  1148. genret:=false;
  1149. }
  1150. end;
  1151. *)
  1152. { if we didn't generate the return code, we've to do it now }
  1153. if genret then
  1154. begin
  1155. { load link register? }
  1156. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1157. begin
  1158. if (pi_do_call in current_procinfo.flags) then
  1159. begin
  1160. case target_info.abi of
  1161. abi_powerpc_aix:
  1162. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1163. abi_powerpc_sysv:
  1164. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1165. end;
  1166. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1167. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1168. end;
  1169. (*
  1170. { restore the CR if necessary from callers frame}
  1171. if target_info.abi = abi_powerpc_aix then
  1172. if false then { Not needed at the moment. }
  1173. begin
  1174. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1175. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1176. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1177. a_reg_dealloc(list,NR_R0);
  1178. end;
  1179. *)
  1180. end;
  1181. list.concat(taicpu.op_none(A_BLR));
  1182. end;
  1183. end;
  1184. function tcgppc.save_regs(list : taasmoutput):longint;
  1185. {Generates code which saves used non-volatile registers in
  1186. the save area right below the address the stackpointer point to.
  1187. Returns the actual used save area size.}
  1188. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1189. usesfpr,usesgpr: boolean;
  1190. href : treference;
  1191. offset: aint;
  1192. regcounter2, firstfpureg: Tsuperregister;
  1193. begin
  1194. usesfpr:=false;
  1195. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1196. begin
  1197. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1198. case target_info.abi of
  1199. abi_powerpc_aix:
  1200. firstfpureg := RS_F14;
  1201. abi_powerpc_sysv:
  1202. firstfpureg := RS_F9;
  1203. else
  1204. internalerror(2003122903);
  1205. end;
  1206. for regcounter:=firstfpureg to RS_F31 do
  1207. begin
  1208. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1209. begin
  1210. usesfpr:=true;
  1211. firstregfpu:=regcounter;
  1212. break;
  1213. end;
  1214. end;
  1215. end;
  1216. usesgpr:=false;
  1217. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1218. for regcounter2:=RS_R13 to RS_R31 do
  1219. begin
  1220. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1221. begin
  1222. usesgpr:=true;
  1223. firstreggpr:=regcounter2;
  1224. break;
  1225. end;
  1226. end;
  1227. offset:= 0;
  1228. { save floating-point registers }
  1229. if usesfpr then
  1230. for regcounter := firstregfpu to RS_F31 do
  1231. begin
  1232. offset:= offset - 8;
  1233. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1234. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1235. end;
  1236. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1237. { save gprs in gpr save area }
  1238. if usesgpr then
  1239. if firstreggpr < RS_R30 then
  1240. begin
  1241. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1242. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1243. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1244. {STMW stores multiple registers}
  1245. end
  1246. else
  1247. begin
  1248. for regcounter := firstreggpr to RS_R31 do
  1249. begin
  1250. offset:= offset - 4;
  1251. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1252. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1253. end;
  1254. end;
  1255. { now comes the AltiVec context save, not yet implemented !!! }
  1256. save_regs:= -offset;
  1257. end;
  1258. procedure tcgppc.restore_regs(list : taasmoutput);
  1259. {Generates code which restores used non-volatile registers from
  1260. the save area right below the address the stackpointer point to.}
  1261. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1262. usesfpr,usesgpr: boolean;
  1263. href : treference;
  1264. offset: integer;
  1265. regcounter2, firstfpureg: Tsuperregister;
  1266. begin
  1267. usesfpr:=false;
  1268. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1269. begin
  1270. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1271. case target_info.abi of
  1272. abi_powerpc_aix:
  1273. firstfpureg := RS_F14;
  1274. abi_powerpc_sysv:
  1275. firstfpureg := RS_F9;
  1276. else
  1277. internalerror(2003122903);
  1278. end;
  1279. for regcounter:=firstfpureg to RS_F31 do
  1280. begin
  1281. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1282. begin
  1283. usesfpr:=true;
  1284. firstregfpu:=regcounter;
  1285. break;
  1286. end;
  1287. end;
  1288. end;
  1289. usesgpr:=false;
  1290. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1291. for regcounter2:=RS_R13 to RS_R31 do
  1292. begin
  1293. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1294. begin
  1295. usesgpr:=true;
  1296. firstreggpr:=regcounter2;
  1297. break;
  1298. end;
  1299. end;
  1300. offset:= 0;
  1301. { restore fp registers }
  1302. if usesfpr then
  1303. for regcounter := firstregfpu to RS_F31 do
  1304. begin
  1305. offset:= offset - 8;
  1306. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1307. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1308. end;
  1309. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1310. { restore gprs }
  1311. if usesgpr then
  1312. if firstreggpr < RS_R30 then
  1313. begin
  1314. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1315. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1316. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1317. {LMW loads multiple registers}
  1318. end
  1319. else
  1320. begin
  1321. for regcounter := firstreggpr to RS_R31 do
  1322. begin
  1323. offset:= offset - 4;
  1324. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1325. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1326. end;
  1327. end;
  1328. { now comes the AltiVec context restore, not yet implemented !!! }
  1329. end;
  1330. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1331. (* NOT IN USE *)
  1332. { generated the entry code of a procedure/function. Note: localsize is the }
  1333. { sum of the size necessary for local variables and the maximum possible }
  1334. { combined size of ALL the parameters of a procedure called by the current }
  1335. { one }
  1336. const
  1337. macosLinkageAreaSize = 24;
  1338. var
  1339. href : treference;
  1340. registerSaveAreaSize : longint;
  1341. begin
  1342. if (localsize mod 8) <> 0 then
  1343. internalerror(58991);
  1344. { CR and LR only have to be saved in case they are modified by the current }
  1345. { procedure, but currently this isn't checked, so save them always }
  1346. { following is the entry code as described in "Altivec Programming }
  1347. { Interface Manual", bar the saving of AltiVec registers }
  1348. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1349. a_reg_alloc(list,NR_R0);
  1350. { save return address in callers frame}
  1351. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1352. { ... in caller's frame }
  1353. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1354. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1355. a_reg_dealloc(list,NR_R0);
  1356. { save non-volatile registers in callers frame}
  1357. registerSaveAreaSize:= save_regs(list);
  1358. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1359. a_reg_alloc(list,NR_R0);
  1360. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1361. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1362. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1363. a_reg_dealloc(list,NR_R0);
  1364. (*
  1365. { save pointer to incoming arguments }
  1366. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1367. *)
  1368. (*
  1369. a_reg_alloc(list,R_12);
  1370. { 0 or 8 based on SP alignment }
  1371. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1372. R_12,STACK_POINTER_REG,0,28,28));
  1373. { add in stack length }
  1374. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1375. -localsize));
  1376. { establish new alignment }
  1377. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1378. a_reg_dealloc(list,R_12);
  1379. *)
  1380. { allocate stack frame }
  1381. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1382. inc(localsize,tg.lasttemp);
  1383. localsize:=align(localsize,16);
  1384. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1385. if (localsize <> 0) then
  1386. begin
  1387. if (localsize <= high(smallint)) then
  1388. begin
  1389. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1390. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1391. end
  1392. else
  1393. begin
  1394. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1395. href.index := NR_R11;
  1396. a_reg_alloc(list,href.index);
  1397. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1398. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1399. a_reg_dealloc(list,href.index);
  1400. end;
  1401. end;
  1402. end;
  1403. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  1404. (* NOT IN USE *)
  1405. var
  1406. href : treference;
  1407. begin
  1408. a_reg_alloc(list,NR_R0);
  1409. { restore stack pointer }
  1410. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1411. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1412. (*
  1413. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1414. *)
  1415. { restore the CR if necessary from callers frame
  1416. ( !!! always done currently ) }
  1417. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1418. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1419. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1420. a_reg_dealloc(list,NR_R0);
  1421. (*
  1422. { restore return address from callers frame }
  1423. reference_reset_base(href,STACK_POINTER_REG,8);
  1424. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1425. *)
  1426. { restore non-volatile registers from callers frame }
  1427. restore_regs(list);
  1428. (*
  1429. { return to caller }
  1430. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1431. list.concat(taicpu.op_none(A_BLR));
  1432. *)
  1433. { restore return address from callers frame }
  1434. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1435. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1436. { return to caller }
  1437. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1438. list.concat(taicpu.op_none(A_BLR));
  1439. end;
  1440. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1441. var
  1442. ref2, tmpref: treference;
  1443. begin
  1444. ref2 := ref;
  1445. fixref(list,ref2);
  1446. if assigned(ref2.symbol) then
  1447. begin
  1448. if target_info.system = system_powerpc_macos then
  1449. begin
  1450. if macos_direct_globals then
  1451. begin
  1452. reference_reset(tmpref);
  1453. tmpref.offset := ref2.offset;
  1454. tmpref.symbol := ref2.symbol;
  1455. tmpref.base := NR_NO;
  1456. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1457. end
  1458. else
  1459. begin
  1460. reference_reset(tmpref);
  1461. tmpref.symbol := ref2.symbol;
  1462. tmpref.offset := 0;
  1463. tmpref.base := NR_RTOC;
  1464. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1465. if ref2.offset <> 0 then
  1466. begin
  1467. reference_reset(tmpref);
  1468. tmpref.offset := ref2.offset;
  1469. tmpref.base:= r;
  1470. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1471. end;
  1472. end;
  1473. if ref2.base <> NR_NO then
  1474. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1475. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1476. end
  1477. else
  1478. begin
  1479. { add the symbol's value to the base of the reference, and if the }
  1480. { reference doesn't have a base, create one }
  1481. reference_reset(tmpref);
  1482. tmpref.offset := ref2.offset;
  1483. tmpref.symbol := ref2.symbol;
  1484. tmpref.relsymbol := ref2.relsymbol;
  1485. tmpref.refaddr := addr_hi;
  1486. if ref2.base<> NR_NO then
  1487. begin
  1488. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1489. ref2.base,tmpref));
  1490. end
  1491. else
  1492. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1493. tmpref.base := NR_NO;
  1494. tmpref.refaddr := addr_lo;
  1495. { can be folded with one of the next instructions by the }
  1496. { optimizer probably }
  1497. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1498. end
  1499. end
  1500. else if ref2.offset <> 0 Then
  1501. if ref2.base <> NR_NO then
  1502. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1503. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1504. { occurs, so now only ref.offset has to be loaded }
  1505. else
  1506. a_load_const_reg(list,OS_32,ref2.offset,r)
  1507. else if ref2.index <> NR_NO Then
  1508. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1509. else if (ref2.base <> NR_NO) and
  1510. (r <> ref2.base) then
  1511. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1512. else
  1513. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1514. end;
  1515. { ************* concatcopy ************ }
  1516. {$ifndef ppc603}
  1517. const
  1518. maxmoveunit = 8;
  1519. {$else ppc603}
  1520. const
  1521. maxmoveunit = 4;
  1522. {$endif ppc603}
  1523. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);
  1524. var
  1525. countreg: TRegister;
  1526. src, dst: TReference;
  1527. lab: tasmlabel;
  1528. count, count2: aint;
  1529. size: tcgsize;
  1530. begin
  1531. {$ifdef extdebug}
  1532. if len > high(longint) then
  1533. internalerror(2002072704);
  1534. {$endif extdebug}
  1535. { make sure short loads are handled as optimally as possible }
  1536. if (len <= maxmoveunit) and
  1537. (byte(len) in [1,2,4,8]) then
  1538. begin
  1539. if len < 8 then
  1540. begin
  1541. size := int_cgsize(len);
  1542. a_load_ref_ref(list,size,size,source,dest);
  1543. end
  1544. else
  1545. begin
  1546. a_reg_alloc(list,NR_F0);
  1547. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1548. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1549. a_reg_dealloc(list,NR_F0);
  1550. end;
  1551. exit;
  1552. end;
  1553. count := len div maxmoveunit;
  1554. reference_reset(src);
  1555. reference_reset(dst);
  1556. { load the address of source into src.base }
  1557. if (count > 4) or
  1558. not issimpleref(source) or
  1559. ((source.index <> NR_NO) and
  1560. ((source.offset + longint(len)) > high(smallint))) then
  1561. begin
  1562. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1563. a_loadaddr_ref_reg(list,source,src.base);
  1564. end
  1565. else
  1566. begin
  1567. src := source;
  1568. end;
  1569. { load the address of dest into dst.base }
  1570. if (count > 4) or
  1571. not issimpleref(dest) or
  1572. ((dest.index <> NR_NO) and
  1573. ((dest.offset + longint(len)) > high(smallint))) then
  1574. begin
  1575. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1576. a_loadaddr_ref_reg(list,dest,dst.base);
  1577. end
  1578. else
  1579. begin
  1580. dst := dest;
  1581. end;
  1582. {$ifndef ppc603}
  1583. if count > 4 then
  1584. { generate a loop }
  1585. begin
  1586. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1587. { have to be set to 8. I put an Inc there so debugging may be }
  1588. { easier (should offset be different from zero here, it will be }
  1589. { easy to notice in the generated assembler }
  1590. inc(dst.offset,8);
  1591. inc(src.offset,8);
  1592. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1593. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1594. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1595. a_load_const_reg(list,OS_32,count,countreg);
  1596. { explicitely allocate R_0 since it can be used safely here }
  1597. { (for holding date that's being copied) }
  1598. a_reg_alloc(list,NR_F0);
  1599. objectlibrary.getjumplabel(lab);
  1600. a_label(list, lab);
  1601. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1602. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1603. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1604. a_jmp(list,A_BC,C_NE,0,lab);
  1605. a_reg_dealloc(list,NR_F0);
  1606. len := len mod 8;
  1607. end;
  1608. count := len div 8;
  1609. if count > 0 then
  1610. { unrolled loop }
  1611. begin
  1612. a_reg_alloc(list,NR_F0);
  1613. for count2 := 1 to count do
  1614. begin
  1615. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1616. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1617. inc(src.offset,8);
  1618. inc(dst.offset,8);
  1619. end;
  1620. a_reg_dealloc(list,NR_F0);
  1621. len := len mod 8;
  1622. end;
  1623. if (len and 4) <> 0 then
  1624. begin
  1625. a_reg_alloc(list,NR_R0);
  1626. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1627. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1628. inc(src.offset,4);
  1629. inc(dst.offset,4);
  1630. a_reg_dealloc(list,NR_R0);
  1631. end;
  1632. {$else not ppc603}
  1633. if count > 4 then
  1634. { generate a loop }
  1635. begin
  1636. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1637. { have to be set to 4. I put an Inc there so debugging may be }
  1638. { easier (should offset be different from zero here, it will be }
  1639. { easy to notice in the generated assembler }
  1640. inc(dst.offset,4);
  1641. inc(src.offset,4);
  1642. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1643. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1644. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1645. a_load_const_reg(list,OS_32,count,countreg);
  1646. { explicitely allocate R_0 since it can be used safely here }
  1647. { (for holding date that's being copied) }
  1648. a_reg_alloc(list,NR_R0);
  1649. objectlibrary.getjumplabel(lab);
  1650. a_label(list, lab);
  1651. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1652. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1653. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1654. a_jmp(list,A_BC,C_NE,0,lab);
  1655. a_reg_dealloc(list,NR_R0);
  1656. len := len mod 4;
  1657. end;
  1658. count := len div 4;
  1659. if count > 0 then
  1660. { unrolled loop }
  1661. begin
  1662. a_reg_alloc(list,NR_R0);
  1663. for count2 := 1 to count do
  1664. begin
  1665. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1666. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1667. inc(src.offset,4);
  1668. inc(dst.offset,4);
  1669. end;
  1670. a_reg_dealloc(list,NR_R0);
  1671. len := len mod 4;
  1672. end;
  1673. {$endif not ppc603}
  1674. { copy the leftovers }
  1675. if (len and 2) <> 0 then
  1676. begin
  1677. a_reg_alloc(list,NR_R0);
  1678. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1679. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1680. inc(src.offset,2);
  1681. inc(dst.offset,2);
  1682. a_reg_dealloc(list,NR_R0);
  1683. end;
  1684. if (len and 1) <> 0 then
  1685. begin
  1686. a_reg_alloc(list,NR_R0);
  1687. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1688. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1689. a_reg_dealloc(list,NR_R0);
  1690. end;
  1691. end;
  1692. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1693. var
  1694. hl : tasmlabel;
  1695. begin
  1696. if not(cs_check_overflow in aktlocalswitches) then
  1697. exit;
  1698. objectlibrary.getjumplabel(hl);
  1699. if not ((def.deftype=pointerdef) or
  1700. ((def.deftype=orddef) and
  1701. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1702. bool8bit,bool16bit,bool32bit]))) then
  1703. begin
  1704. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1705. a_jmp(list,A_BC,C_NO,7,hl)
  1706. end
  1707. else
  1708. a_jmp_cond(list,OC_AE,hl);
  1709. a_call_name(list,'FPC_OVERFLOW');
  1710. a_label(list,hl);
  1711. end;
  1712. procedure tcgppc.g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);
  1713. procedure loadvmttor11;
  1714. var
  1715. href : treference;
  1716. begin
  1717. reference_reset_base(href,NR_R3,0);
  1718. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  1719. end;
  1720. procedure op_onr11methodaddr;
  1721. var
  1722. href : treference;
  1723. begin
  1724. if (procdef.extnumber=$ffff) then
  1725. Internalerror(200006139);
  1726. { call/jmp vmtoffs(%eax) ; method offs }
  1727. reference_reset_base(href,NR_R11,procdef._class.vmtmethodoffset(procdef.extnumber));
  1728. if not((longint(href.offset) >= low(smallint)) and
  1729. (longint(href.offset) <= high(smallint))) then
  1730. begin
  1731. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  1732. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  1733. href.offset := smallint(href.offset and $ffff);
  1734. end;
  1735. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  1736. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  1737. list.concat(taicpu.op_none(A_BCTR));
  1738. end;
  1739. var
  1740. make_global : boolean;
  1741. begin
  1742. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1743. Internalerror(200006137);
  1744. if not assigned(procdef._class) or
  1745. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1746. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1747. Internalerror(200006138);
  1748. if procdef.owner.symtabletype<>objectsymtable then
  1749. Internalerror(200109191);
  1750. make_global:=false;
  1751. if (not current_module.is_unit) or
  1752. (cs_create_smart in aktmoduleswitches) or
  1753. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1754. make_global:=true;
  1755. if make_global then
  1756. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1757. else
  1758. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1759. { set param1 interface to self }
  1760. g_adjust_self_value(list,procdef,ioffset);
  1761. { case 4 }
  1762. if po_virtualmethod in procdef.procoptions then
  1763. begin
  1764. loadvmttor11;
  1765. op_onr11methodaddr;
  1766. end
  1767. { case 0 }
  1768. else
  1769. list.concat(taicpu.op_sym(A_B,objectlibrary.newasmsymbol(procdef.mangledname,AB_EXTERNAL,AT_FUNCTION)));
  1770. List.concat(Tai_symbol_end.Createname(labelname));
  1771. end;
  1772. {***************** This is private property, keep out! :) *****************}
  1773. function tcgppc.issimpleref(const ref: treference): boolean;
  1774. begin
  1775. if (ref.base = NR_NO) and
  1776. (ref.index <> NR_NO) then
  1777. internalerror(200208101);
  1778. result :=
  1779. not(assigned(ref.symbol)) and
  1780. (((ref.index = NR_NO) and
  1781. (ref.offset >= low(smallint)) and
  1782. (ref.offset <= high(smallint))) or
  1783. ((ref.index <> NR_NO) and
  1784. (ref.offset = 0)));
  1785. end;
  1786. function tcgppc.g_darwin_indirect_sym_load(list: taasmoutput; const symname: string): tregister;
  1787. var
  1788. l: tasmsymbol;
  1789. ref: treference;
  1790. begin
  1791. l:=objectlibrary.getasmsymbol('L'+symname+'$non_lazy_ptr');
  1792. if not(assigned(l)) then
  1793. begin
  1794. l:=objectlibrary.newasmsymbol('L'+symname+'$non_lazy_ptr',AB_COMMON,AT_DATA);
  1795. asmlist[al_picdata].concat(tai_symbol.create(l,0));
  1796. asmlist[al_picdata].concat(tai_const.create_indirect_sym(objectlibrary.newasmsymbol(symname,AB_EXTERNAL,AT_DATA)));
  1797. asmlist[al_picdata].concat(tai_const.create_32bit(0));
  1798. end;
  1799. reference_reset_symbol(ref,l,0);
  1800. { ref.base:=current_procinfo.got;
  1801. ref.relsymbol:=current_procinfo.gotlabel;}
  1802. result := cg.getaddressregister(exprasmlist);
  1803. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  1804. end;
  1805. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1806. var
  1807. tmpreg: tregister;
  1808. begin
  1809. result := false;
  1810. if (target_info.system = system_powerpc_darwin) and
  1811. assigned(ref.symbol) and
  1812. (ref.symbol.defbind = AB_EXTERNAL) then
  1813. begin
  1814. tmpreg := g_darwin_indirect_sym_load(list,ref.symbol.name);
  1815. if (ref.base = NR_NO) then
  1816. ref.base := tmpreg
  1817. else if (ref.index = NR_NO) then
  1818. ref.index := tmpreg
  1819. else
  1820. list.concat(taicpu.op_reg_reg_reg(A_ADD,ref.base,ref.base,tmpreg));
  1821. ref.symbol := nil;
  1822. end;
  1823. if (ref.base = NR_NO) then
  1824. begin
  1825. ref.base := ref.index;
  1826. ref.index := NR_NO;
  1827. end;
  1828. if (ref.base <> NR_NO) then
  1829. begin
  1830. if (ref.index <> NR_NO) and
  1831. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1832. begin
  1833. result := true;
  1834. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1835. list.concat(taicpu.op_reg_reg_reg(
  1836. A_ADD,tmpreg,ref.base,ref.index));
  1837. ref.index := NR_NO;
  1838. ref.base := tmpreg;
  1839. end
  1840. end
  1841. else
  1842. if ref.index <> NR_NO then
  1843. internalerror(200208102);
  1844. end;
  1845. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1846. { that's the case, we can use rlwinm to do an AND operation }
  1847. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1848. var
  1849. temp : longint;
  1850. testbit : aint;
  1851. compare: boolean;
  1852. begin
  1853. get_rlwi_const := false;
  1854. if (a = 0) or (a = -1) then
  1855. exit;
  1856. { start with the lowest bit }
  1857. testbit := 1;
  1858. { check its value }
  1859. compare := boolean(a and testbit);
  1860. { find out how long the run of bits with this value is }
  1861. { (it's impossible that all bits are 1 or 0, because in that case }
  1862. { this function wouldn't have been called) }
  1863. l1 := 31;
  1864. while (((a and testbit) <> 0) = compare) do
  1865. begin
  1866. testbit := testbit shl 1;
  1867. dec(l1);
  1868. end;
  1869. { check the length of the run of bits that comes next }
  1870. compare := not compare;
  1871. l2 := l1;
  1872. while (((a and testbit) <> 0) = compare) and
  1873. (l2 >= 0) do
  1874. begin
  1875. testbit := testbit shl 1;
  1876. dec(l2);
  1877. end;
  1878. { and finally the check whether the rest of the bits all have the }
  1879. { same value }
  1880. compare := not compare;
  1881. temp := l2;
  1882. if temp >= 0 then
  1883. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1884. exit;
  1885. { we have done "not(not(compare))", so compare is back to its }
  1886. { initial value. If the lowest bit was 0, a is of the form }
  1887. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1888. { because l2 now contains the position of the last zero of the }
  1889. { first run instead of that of the first 1) so switch l1 and l2 }
  1890. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1891. if not compare then
  1892. begin
  1893. temp := l1;
  1894. l1 := l2+1;
  1895. l2 := temp;
  1896. end
  1897. else
  1898. { otherwise, l1 currently contains the position of the last }
  1899. { zero instead of that of the first 1 of the second run -> +1 }
  1900. inc(l1);
  1901. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1902. l1 := l1 and 31;
  1903. l2 := l2 and 31;
  1904. get_rlwi_const := true;
  1905. end;
  1906. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1907. ref: treference);
  1908. var
  1909. tmpreg: tregister;
  1910. tmpref: treference;
  1911. largeOffset: Boolean;
  1912. begin
  1913. tmpreg := NR_NO;
  1914. if target_info.system = system_powerpc_macos then
  1915. begin
  1916. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1917. high(smallint)-low(smallint));
  1918. if assigned(ref.symbol) then
  1919. begin {Load symbol's value}
  1920. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1921. reference_reset(tmpref);
  1922. tmpref.symbol := ref.symbol;
  1923. tmpref.base := NR_RTOC;
  1924. if macos_direct_globals then
  1925. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1926. else
  1927. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1928. end;
  1929. if largeOffset then
  1930. begin {Add hi part of offset}
  1931. reference_reset(tmpref);
  1932. if Smallint(Lo(ref.offset)) < 0 then
  1933. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1934. else
  1935. tmpref.offset := Hi(ref.offset);
  1936. if (tmpreg <> NR_NO) then
  1937. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1938. else
  1939. begin
  1940. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1941. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1942. end;
  1943. end;
  1944. if (tmpreg <> NR_NO) then
  1945. begin
  1946. {Add content of base register}
  1947. if ref.base <> NR_NO then
  1948. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1949. ref.base,tmpreg));
  1950. {Make ref ready to be used by op}
  1951. ref.symbol:= nil;
  1952. ref.base:= tmpreg;
  1953. if largeOffset then
  1954. ref.offset := Smallint(Lo(ref.offset));
  1955. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1956. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1957. end
  1958. else
  1959. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1960. end
  1961. else {if target_info.system <> system_powerpc_macos}
  1962. begin
  1963. if assigned(ref.symbol) or
  1964. (cardinal(ref.offset-low(smallint)) >
  1965. high(smallint)-low(smallint)) then
  1966. begin
  1967. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1968. reference_reset(tmpref);
  1969. tmpref.symbol := ref.symbol;
  1970. tmpref.relsymbol := ref.relsymbol;
  1971. tmpref.offset := ref.offset;
  1972. tmpref.refaddr := addr_hi;
  1973. if ref.base <> NR_NO then
  1974. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1975. ref.base,tmpref))
  1976. else
  1977. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1978. ref.base := tmpreg;
  1979. ref.refaddr := addr_lo;
  1980. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1981. end
  1982. else
  1983. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1984. end;
  1985. end;
  1986. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1987. crval: longint; l: tasmlabel);
  1988. var
  1989. p: taicpu;
  1990. begin
  1991. p := taicpu.op_sym(op,l);
  1992. if op <> A_B then
  1993. create_cond_norm(c,crval,p.condition);
  1994. p.is_jmp := true;
  1995. list.concat(p)
  1996. end;
  1997. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1998. begin
  1999. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  2000. end;
  2001. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2002. begin
  2003. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  2004. end;
  2005. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2006. begin
  2007. case op of
  2008. OP_AND,OP_OR,OP_XOR:
  2009. begin
  2010. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2011. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2012. end;
  2013. OP_ADD:
  2014. begin
  2015. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2016. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2017. end;
  2018. OP_SUB:
  2019. begin
  2020. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2021. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2022. end;
  2023. else
  2024. internalerror(2002072801);
  2025. end;
  2026. end;
  2027. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  2028. const
  2029. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2030. (A_SUBIC,A_SUBC,A_ADDME));
  2031. var
  2032. tmpreg: tregister;
  2033. tmpreg64: tregister64;
  2034. issub: boolean;
  2035. begin
  2036. case op of
  2037. OP_AND,OP_OR,OP_XOR:
  2038. begin
  2039. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  2040. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2041. regdst.reghi);
  2042. end;
  2043. OP_ADD, OP_SUB:
  2044. begin
  2045. if (value < 0) then
  2046. begin
  2047. if op = OP_ADD then
  2048. op := OP_SUB
  2049. else
  2050. op := OP_ADD;
  2051. value := -value;
  2052. end;
  2053. if (longint(value) <> 0) then
  2054. begin
  2055. issub := op = OP_SUB;
  2056. if (value > 0) and
  2057. (value-ord(issub) <= 32767) then
  2058. begin
  2059. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2060. regdst.reglo,regsrc.reglo,longint(value)));
  2061. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2062. regdst.reghi,regsrc.reghi));
  2063. end
  2064. else if ((value shr 32) = 0) then
  2065. begin
  2066. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2067. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2068. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2069. regdst.reglo,regsrc.reglo,tmpreg));
  2070. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2071. regdst.reghi,regsrc.reghi));
  2072. end
  2073. else
  2074. begin
  2075. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2076. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2077. a_load64_const_reg(list,value,tmpreg64);
  2078. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  2079. end
  2080. end
  2081. else
  2082. begin
  2083. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2084. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2085. regdst.reghi);
  2086. end;
  2087. end;
  2088. else
  2089. internalerror(2002072802);
  2090. end;
  2091. end;
  2092. begin
  2093. cg := tcgppc.create;
  2094. cg64 :=tcg64fppc.create;
  2095. end.