cgcpu.pas 90 KB

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