cgcpu.pas 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,node,cg64f32,cginfo;
  25. type
  26. tcgppc = class(tcg)
  27. { passing parameters, per default the parameter is pushed }
  28. { nr gives the number of the parameter (enumerated from }
  29. { left to right), this allows to move the parameter to }
  30. { register, if the cpu supports register calling }
  31. { conventions }
  32. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  33. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  34. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  35. procedure a_call_name(list : taasmoutput;const s : string);override;
  36. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  37. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  38. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  39. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  40. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  41. size: tcgsize; a: aword; src, dst: tregister); override;
  42. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  43. size: tcgsize; src1, src2, dst: tregister); override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  46. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  47. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  48. procedure a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  49. { fpu move instructions }
  50. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  51. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  52. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  53. { comparison operations }
  54. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  55. l : tasmlabel);override;
  56. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  57. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  58. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  59. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  60. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  61. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  62. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  63. procedure g_restore_frame_pointer(list : taasmoutput);override;
  64. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  65. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  66. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  67. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  68. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  69. { that's the case, we can use rlwinm to do an AND operation }
  70. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  71. procedure g_save_standard_registers(list : taasmoutput; usedinproc : tregisterset);override;
  72. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : tregisterset);override;
  73. procedure g_save_all_registers(list : taasmoutput);override;
  74. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  75. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  76. private
  77. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  78. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  79. { Make sure ref is a valid reference for the PowerPC and sets the }
  80. { base to the value of the index if (base = R_NO). }
  81. { Returns true if the reference contained a base, index and an }
  82. { offset or symbol, in which case the base will have been changed }
  83. { to a tempreg (which has to be freed by the caller) containing }
  84. { the sum of part of the original reference }
  85. function fixref(list: taasmoutput; var ref: treference): boolean;
  86. { returns whether a reference can be used immediately in a powerpc }
  87. { instruction }
  88. function issimpleref(const ref: treference): boolean;
  89. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  90. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  91. ref: treference);
  92. { creates the correct branch instruction for a given combination }
  93. { of asmcondflags and destination addressing mode }
  94. procedure a_jmp(list: taasmoutput; op: tasmop;
  95. c: tasmcondflag; crval: longint; l: tasmlabel);
  96. end;
  97. tcg64fppc = class(tcg64f32)
  98. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  99. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  100. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  101. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  102. end;
  103. const
  104. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  105. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  106. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  107. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  108. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  109. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  110. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  111. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  112. implementation
  113. uses
  114. globtype,globals,verbose,systems,cutils,symconst,symdef,rgobj,tgobj,cpupi;
  115. { parameter passing... Still needs extra support from the processor }
  116. { independent code generator }
  117. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  118. var
  119. ref: treference;
  120. begin
  121. case locpara.loc of
  122. LOC_REGISTER,LOC_CREGISTER:
  123. a_load_const_reg(list,size,a,locpara.register);
  124. LOC_REFERENCE:
  125. begin
  126. reference_reset(ref);
  127. ref.base:=locpara.reference.index;
  128. ref.offset:=locpara.reference.offset;
  129. a_load_const_ref(list,size,a,ref);
  130. end;
  131. else
  132. internalerror(2002081101);
  133. end;
  134. if locpara.sp_fixup<>0 then
  135. internalerror(2002081102);
  136. end;
  137. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  138. var
  139. ref: treference;
  140. tmpreg: tregister;
  141. begin
  142. case locpara.loc of
  143. LOC_REGISTER,LOC_CREGISTER:
  144. a_load_ref_reg(list,size,r,locpara.register);
  145. LOC_REFERENCE:
  146. begin
  147. reference_reset(ref);
  148. ref.base:=locpara.reference.index;
  149. ref.offset:=locpara.reference.offset;
  150. tmpreg := get_scratch_reg_int(list);
  151. a_load_ref_reg(list,size,r,tmpreg);
  152. a_load_reg_ref(list,size,tmpreg,ref);
  153. free_scratch_reg(list,tmpreg);
  154. end;
  155. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  156. case size of
  157. OS_32:
  158. a_loadfpu_ref_reg(list,OS_F32,r,locpara.register);
  159. OS_64:
  160. a_loadfpu_ref_reg(list,OS_F64,r,locpara.register);
  161. else
  162. internalerror(2002072801);
  163. end;
  164. else
  165. internalerror(2002081103);
  166. end;
  167. if locpara.sp_fixup<>0 then
  168. internalerror(2002081104);
  169. end;
  170. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  171. var
  172. ref: treference;
  173. tmpreg: tregister;
  174. begin
  175. case locpara.loc of
  176. LOC_REGISTER,LOC_CREGISTER:
  177. a_loadaddr_ref_reg(list,r,locpara.register);
  178. LOC_REFERENCE:
  179. begin
  180. reference_reset(ref);
  181. ref.base := locpara.reference.index;
  182. ref.offset := locpara.reference.offset;
  183. tmpreg := get_scratch_reg_address(list);
  184. a_loadaddr_ref_reg(list,r,tmpreg);
  185. a_load_reg_ref(list,OS_ADDR,tmpreg,ref);
  186. free_scratch_reg(list,tmpreg);
  187. end;
  188. else
  189. internalerror(2002080701);
  190. end;
  191. end;
  192. { calling a code fragment by name }
  193. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  194. var
  195. href : treference;
  196. begin
  197. if target_info.system=system_powerpc_macos then
  198. begin
  199. { save our RTOC register value. Only necessary when doing pointer based }
  200. { calls or cross TOC calls, but currently done always }
  201. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  202. list.concat(taicpu.op_reg_ref(A_STW,R_TOC,href));
  203. end;
  204. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  205. if target_info.system=system_powerpc_macos then
  206. list.concat(taicpu.op_reg_ref(A_LWZ,R_TOC,href));
  207. procinfo.flags:=procinfo.flags or pi_do_call;
  208. end;
  209. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  210. var
  211. href : treference;
  212. begin
  213. list.concat(taicpu.op_reg(A_MTCTR,reg));
  214. if target_info.system=system_powerpc_macos then
  215. begin
  216. { save our RTOC register value. Only necessary when doing pointer based }
  217. { calls or cross TOC calls, but currently done always }
  218. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  219. list.concat(taicpu.op_reg_ref(A_STW,R_TOC,href));
  220. end;
  221. list.concat(taicpu.op_none(A_BCCTRL));
  222. if target_info.system=system_powerpc_macos then
  223. list.concat(taicpu.op_reg_ref(A_LWZ,R_TOC,href));
  224. procinfo.flags:=procinfo.flags or pi_do_call;
  225. end;
  226. { calling a code fragment through a reference }
  227. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  228. var
  229. href : treference;
  230. tmpreg : tregister;
  231. begin
  232. if target_info.system=system_powerpc_macos then
  233. begin
  234. { save our RTOC register value. Only necessary when doing pointer based }
  235. { calls or cross TOC calls, but currently done always }
  236. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  237. list.concat(taicpu.op_reg_ref(A_STW,R_TOC,href));
  238. end;
  239. tmpreg := get_scratch_reg_int(list);
  240. a_load_ref_reg(list,OS_ADDR,ref,tmpreg);
  241. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  242. free_scratch_reg(list,tmpreg);
  243. list.concat(taicpu.op_none(A_BCCTRL));
  244. if target_info.system=system_powerpc_macos then
  245. list.concat(taicpu.op_reg_ref(A_LWZ,R_TOC,href));
  246. procinfo.flags:=procinfo.flags or pi_do_call;
  247. end;
  248. {********************** load instructions ********************}
  249. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  250. begin
  251. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  252. internalerror(2002090902);
  253. if (longint(a) >= low(smallint)) and
  254. (longint(a) <= high(smallint)) then
  255. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  256. else if ((a and $ffff) <> 0) then
  257. begin
  258. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  259. if ((a shr 16) <> 0) or
  260. (smallint(a and $ffff) < 0) then
  261. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  262. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  263. end
  264. else
  265. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  266. end;
  267. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  268. const
  269. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  270. { indexed? updating?}
  271. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  272. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  273. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  274. var
  275. op: TAsmOp;
  276. ref2: TReference;
  277. freereg: boolean;
  278. begin
  279. ref2 := ref;
  280. freereg := fixref(list,ref2);
  281. if size in [OS_S8..OS_S16] then
  282. { storing is the same for signed and unsigned values }
  283. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  284. { 64 bit stuff should be handled separately }
  285. if size in [OS_64,OS_S64] then
  286. internalerror(200109236);
  287. op := storeinstr[tcgsize2unsigned[size],ref2.index<>R_NO,false];
  288. a_load_store(list,op,reg,ref2);
  289. if freereg then
  290. cg.free_scratch_reg(list,ref2.base);
  291. End;
  292. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  293. const
  294. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  295. { indexed? updating?}
  296. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  297. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  298. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  299. { 64bit stuff should be handled separately }
  300. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  301. { there's no load-byte-with-sign-extend :( }
  302. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  303. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  304. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  305. var
  306. op: tasmop;
  307. tmpreg: tregister;
  308. ref2, tmpref: treference;
  309. freereg: boolean;
  310. begin
  311. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  312. internalerror(2002090902);
  313. ref2 := ref;
  314. freereg := fixref(list,ref2);
  315. op := loadinstr[size,ref2.index<>R_NO,false];
  316. a_load_store(list,op,reg,ref2);
  317. if freereg then
  318. free_scratch_reg(list,ref2.base);
  319. { sign extend shortint if necessary, since there is no }
  320. { load instruction that does that automatically (JM) }
  321. if size = OS_S8 then
  322. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  323. end;
  324. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  325. begin
  326. if (reg1 <> reg2) or
  327. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  328. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  329. (tosize <> fromsize) and
  330. not(fromsize in [OS_32,OS_S32])) then
  331. begin
  332. case fromsize of
  333. OS_8:
  334. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  335. reg2,reg1,0,31-8+1,31));
  336. OS_S8:
  337. list.concat(taicpu.op_reg_reg(A_EXTSB,reg2,reg1));
  338. OS_16:
  339. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  340. reg2,reg1,0,31-16+1,31));
  341. OS_S16:
  342. list.concat(taicpu.op_reg_reg(A_EXTSH,reg2,reg1));
  343. OS_32,OS_S32:
  344. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  345. else internalerror(2002090901);
  346. end;
  347. end;
  348. end;
  349. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  350. begin
  351. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  352. end;
  353. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  354. const
  355. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  356. { indexed? updating?}
  357. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  358. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  359. var
  360. op: tasmop;
  361. ref2: treference;
  362. freereg: boolean;
  363. begin
  364. { several functions call this procedure with OS_32 or OS_64 }
  365. { so this makes life easier (FK) }
  366. case size of
  367. OS_32,OS_F32:
  368. size:=OS_F32;
  369. OS_64,OS_F64:
  370. size:=OS_F64;
  371. else
  372. internalerror(200201121);
  373. end;
  374. ref2 := ref;
  375. freereg := fixref(list,ref2);
  376. op := fpuloadinstr[size,ref2.index <> R_NO,false];
  377. a_load_store(list,op,reg,ref2);
  378. if freereg then
  379. cg.free_scratch_reg(list,ref2.base);
  380. end;
  381. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  382. const
  383. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  384. { indexed? updating?}
  385. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  386. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  387. var
  388. op: tasmop;
  389. ref2: treference;
  390. freereg: boolean;
  391. begin
  392. if not(size in [OS_F32,OS_F64]) then
  393. internalerror(200201122);
  394. ref2 := ref;
  395. freereg := fixref(list,ref2);
  396. op := fpustoreinstr[size,ref2.index <> R_NO,false];
  397. a_load_store(list,op,reg,ref2);
  398. if freereg then
  399. cg.free_scratch_reg(list,ref2.base);
  400. end;
  401. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  402. var
  403. scratch_register: TRegister;
  404. begin
  405. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  406. end;
  407. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  408. begin
  409. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  410. end;
  411. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  412. size: tcgsize; a: aword; src, dst: tregister);
  413. var
  414. l1,l2: longint;
  415. oplo, ophi: tasmop;
  416. scratchreg: tregister;
  417. useReg, gotrlwi: boolean;
  418. procedure do_lo_hi;
  419. begin
  420. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  421. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  422. end;
  423. begin
  424. if op = OP_SUB then
  425. begin
  426. {$ifopt q+}
  427. {$q-}
  428. {$define overflowon}
  429. {$endif}
  430. a_op_const_reg_reg(list,OP_ADD,size,aword(-a),src,dst);
  431. {$ifdef overflowon}
  432. {$q+}
  433. {$undef overflowon}
  434. {$endif}
  435. exit;
  436. end;
  437. ophi := TOpCG2AsmOpConstHi[op];
  438. oplo := TOpCG2AsmOpConstLo[op];
  439. gotrlwi := get_rlwi_const(a,l1,l2);
  440. if (op in [OP_AND,OP_OR,OP_XOR]) then
  441. begin
  442. if (a = 0) then
  443. begin
  444. if op = OP_AND then
  445. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  446. exit;
  447. end
  448. else if (a = high(aword)) then
  449. begin
  450. case op of
  451. OP_OR:
  452. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  453. OP_XOR:
  454. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  455. end;
  456. exit;
  457. end
  458. else if (a <= high(word)) and
  459. ((op <> OP_AND) or
  460. not gotrlwi) then
  461. begin
  462. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  463. exit;
  464. end;
  465. { all basic constant instructions also have a shifted form that }
  466. { works only on the highest 16bits, so if lo(a) is 0, we can }
  467. { use that one }
  468. if (word(a) = 0) and
  469. (not(op = OP_AND) or
  470. not gotrlwi) then
  471. begin
  472. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  473. exit;
  474. end;
  475. end
  476. else if (op = OP_ADD) then
  477. if a = 0 then
  478. exit
  479. else if (longint(a) >= low(smallint)) and
  480. (longint(a) <= high(smallint)) then
  481. begin
  482. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  483. exit;
  484. end;
  485. { otherwise, the instructions we can generate depend on the }
  486. { operation }
  487. useReg := false;
  488. case op of
  489. OP_DIV,OP_IDIV:
  490. if (a = 0) then
  491. internalerror(200208103)
  492. else if (a = 1) then
  493. begin
  494. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  495. exit
  496. end
  497. else if ispowerof2(a,l1) then
  498. begin
  499. case op of
  500. OP_DIV:
  501. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  502. OP_IDIV:
  503. begin
  504. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  505. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  506. end;
  507. end;
  508. exit;
  509. end
  510. else
  511. usereg := true;
  512. OP_IMUL, OP_MUL:
  513. if (a = 0) then
  514. begin
  515. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  516. exit
  517. end
  518. else if (a = 1) then
  519. begin
  520. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  521. exit
  522. end
  523. else if ispowerof2(a,l1) then
  524. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  525. else if (longint(a) >= low(smallint)) and
  526. (longint(a) <= high(smallint)) then
  527. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  528. else
  529. usereg := true;
  530. OP_ADD:
  531. begin
  532. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  533. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  534. smallint((a shr 16) + ord(smallint(a) < 0))));
  535. end;
  536. OP_OR:
  537. { try to use rlwimi }
  538. if gotrlwi and
  539. (src = dst) then
  540. begin
  541. scratchreg := get_scratch_reg_int(list);
  542. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  543. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  544. scratchreg,0,l1,l2));
  545. free_scratch_reg(list,scratchreg);
  546. end
  547. else
  548. do_lo_hi;
  549. OP_AND:
  550. { try to use rlwinm }
  551. if gotrlwi then
  552. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  553. src,0,l1,l2))
  554. else
  555. useReg := true;
  556. OP_XOR:
  557. do_lo_hi;
  558. OP_SHL,OP_SHR,OP_SAR:
  559. begin
  560. if (a and 31) <> 0 Then
  561. list.concat(taicpu.op_reg_reg_const(
  562. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  563. if (a shr 5) <> 0 then
  564. internalError(68991);
  565. end
  566. else
  567. internalerror(200109091);
  568. end;
  569. { if all else failed, load the constant in a register and then }
  570. { perform the operation }
  571. if useReg then
  572. begin
  573. scratchreg := get_scratch_reg_int(list);
  574. a_load_const_reg(list,OS_32,a,scratchreg);
  575. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  576. free_scratch_reg(list,scratchreg);
  577. end;
  578. end;
  579. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  580. size: tcgsize; src1, src2, dst: tregister);
  581. const
  582. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  583. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  584. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  585. begin
  586. case op of
  587. OP_NEG,OP_NOT:
  588. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  589. else
  590. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  591. end;
  592. end;
  593. {*************** compare instructructions ****************}
  594. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  595. l : tasmlabel);
  596. var
  597. p: taicpu;
  598. scratch_register: TRegister;
  599. signed: boolean;
  600. begin
  601. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  602. { in the following case, we generate more efficient code when }
  603. { signed is true }
  604. if (cmp_op in [OC_EQ,OC_NE]) and
  605. (a > $ffff) then
  606. signed := true;
  607. if signed then
  608. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  609. list.concat(taicpu.op_reg_reg_const(A_CMPWI,R_CR0,reg,longint(a)))
  610. else
  611. begin
  612. scratch_register := get_scratch_reg_int(list);
  613. a_load_const_reg(list,OS_32,a,scratch_register);
  614. list.concat(taicpu.op_reg_reg_reg(A_CMPW,R_CR0,reg,scratch_register));
  615. free_scratch_reg(list,scratch_register);
  616. end
  617. else
  618. if (a <= $ffff) then
  619. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,R_CR0,reg,a))
  620. else
  621. begin
  622. scratch_register := get_scratch_reg_int(list);
  623. a_load_const_reg(list,OS_32,a,scratch_register);
  624. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,R_CR0,reg,scratch_register));
  625. free_scratch_reg(list,scratch_register);
  626. end;
  627. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  628. end;
  629. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  630. reg1,reg2 : tregister;l : tasmlabel);
  631. var
  632. p: taicpu;
  633. op: tasmop;
  634. begin
  635. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  636. op := A_CMPW
  637. else op := A_CMPLW;
  638. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  639. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  640. end;
  641. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : tregisterset);
  642. begin
  643. {$warning FIX ME}
  644. end;
  645. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : tregisterset);
  646. begin
  647. {$warning FIX ME}
  648. end;
  649. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  650. begin
  651. {$warning FIX ME}
  652. end;
  653. procedure tcgppc.g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);
  654. begin
  655. {$warning FIX ME}
  656. end;
  657. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  658. begin
  659. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  660. end;
  661. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  662. begin
  663. a_jmp(list,A_B,C_None,0,l);
  664. end;
  665. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  666. var
  667. c: tasmcond;
  668. begin
  669. c := flags_to_cond(f);
  670. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(R_CR0),l);
  671. end;
  672. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  673. var
  674. testbit: byte;
  675. bitvalue: boolean;
  676. begin
  677. { get the bit to extract from the conditional register + its }
  678. { requested value (0 or 1) }
  679. testbit := ((ord(f.cr)-ord(R_CR0)) * 4);
  680. case f.flag of
  681. F_EQ,F_NE:
  682. bitvalue := f.flag = F_EQ;
  683. F_LT,F_GE:
  684. begin
  685. inc(testbit);
  686. bitvalue := f.flag = F_LT;
  687. end;
  688. F_GT,F_LE:
  689. begin
  690. inc(testbit,2);
  691. bitvalue := f.flag = F_GT;
  692. end;
  693. else
  694. internalerror(200112261);
  695. end;
  696. { load the conditional register in the destination reg }
  697. list.concat(taicpu.op_reg(A_MFCR,reg));
  698. { we will move the bit that has to be tested to bit 0 by rotating }
  699. { left }
  700. testbit := (32 - testbit) and 31;
  701. { extract bit }
  702. list.concat(taicpu.op_reg_reg_const_const_const(
  703. A_RLWINM,reg,reg,testbit,31,31));
  704. { if we need the inverse, xor with 1 }
  705. if not bitvalue then
  706. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  707. end;
  708. (*
  709. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  710. var
  711. testbit: byte;
  712. bitvalue: boolean;
  713. begin
  714. { get the bit to extract from the conditional register + its }
  715. { requested value (0 or 1) }
  716. case f.simple of
  717. false:
  718. begin
  719. { we don't generate this in the compiler }
  720. internalerror(200109062);
  721. end;
  722. true:
  723. case f.cond of
  724. C_None:
  725. internalerror(200109063);
  726. C_LT..C_NU:
  727. begin
  728. testbit := (ord(f.cr) - ord(R_CR0))*4;
  729. inc(testbit,AsmCondFlag2BI[f.cond]);
  730. bitvalue := AsmCondFlagTF[f.cond];
  731. end;
  732. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  733. begin
  734. testbit := f.crbit
  735. bitvalue := AsmCondFlagTF[f.cond];
  736. end;
  737. else
  738. internalerror(200109064);
  739. end;
  740. end;
  741. { load the conditional register in the destination reg }
  742. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  743. { we will move the bit that has to be tested to bit 31 -> rotate }
  744. { left by bitpos+1 (remember, this is big-endian!) }
  745. if bitpos <> 31 then
  746. inc(bitpos)
  747. else
  748. bitpos := 0;
  749. { extract bit }
  750. list.concat(taicpu.op_reg_reg_const_const_const(
  751. A_RLWINM,reg,reg,bitpos,31,31));
  752. { if we need the inverse, xor with 1 }
  753. if not bitvalue then
  754. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  755. end;
  756. *)
  757. { *********** entry/exit code and address loading ************ }
  758. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  759. begin
  760. case target_info.system of
  761. system_powerpc_macos:
  762. g_stackframe_entry_mac(list,localsize);
  763. system_powerpc_linux:
  764. g_stackframe_entry_sysv(list,localsize)
  765. else
  766. internalerror(2204001);
  767. end;
  768. end;
  769. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  770. { generated the entry code of a procedure/function. Note: localsize is the }
  771. { sum of the size necessary for local variables and the maximum possible }
  772. { combined size of ALL the parameters of a procedure called by the current }
  773. { one }
  774. var regcounter,firstregfpu,firstreggpr : TRegister;
  775. href : treference;
  776. usesfpr,usesgpr,gotgot : boolean;
  777. parastart : aword;
  778. offset : aword;
  779. begin
  780. { we do our own localsize calculation }
  781. localsize:=0;
  782. { CR and LR only have to be saved in case they are modified by the current }
  783. { procedure, but currently this isn't checked, so save them always }
  784. { following is the entry code as described in "Altivec Programming }
  785. { Interface Manual", bar the saving of AltiVec registers }
  786. a_reg_alloc(list,STACK_POINTER_REG);
  787. a_reg_alloc(list,R_0);
  788. { allocate registers containing reg parameters }
  789. for regcounter := R_3 to R_10 do
  790. a_reg_alloc(list,regcounter);
  791. usesfpr:=false;
  792. for regcounter:=R_F14 to R_F31 do
  793. if regcounter in rg.usedbyproc then
  794. begin
  795. usesfpr:=true;
  796. firstregfpu:=regcounter;
  797. break;
  798. end;
  799. usesgpr:=false;
  800. for regcounter:=R_14 to R_31 do
  801. if regcounter in rg.usedbyproc then
  802. begin
  803. usesgpr:=true;
  804. firstreggpr:=regcounter;
  805. break;
  806. end;
  807. { save link register? }
  808. if (procinfo.flags and pi_do_call)<>0 then
  809. begin
  810. { save return address... }
  811. list.concat(taicpu.op_reg(A_MFLR,R_0));
  812. { ... in caller's rframe }
  813. reference_reset_base(href,STACK_POINTER_REG,4);
  814. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  815. a_reg_dealloc(list,R_0);
  816. end;
  817. if usesfpr or usesgpr then
  818. begin
  819. a_reg_alloc(list,R_11);
  820. { save end of fpr save area }
  821. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER_REG,0));
  822. end;
  823. { calculate the size of the locals }
  824. if usesgpr then
  825. inc(localsize,(ord(R_31)-ord(firstreggpr)+1)*4);
  826. if usesfpr then
  827. inc(localsize,(ord(R_F31)-ord(firstregfpu)+1)*8);
  828. { align to 16 bytes }
  829. localsize:=align(localsize,16);
  830. inc(localsize,tg.lasttemp);
  831. localsize:=align(localsize,16);
  832. tppcprocinfo(procinfo).localsize:=localsize;
  833. reference_reset_base(href,R_1,-localsize);
  834. a_load_store(list,A_STWU,R_1,href);
  835. { no GOT pointer loaded yet }
  836. gotgot:=false;
  837. if usesfpr then
  838. begin
  839. { save floating-point registers
  840. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  841. begin
  842. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g'),0));
  843. gotgot:=true;
  844. end
  845. else
  846. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)),0));
  847. }
  848. for regcounter:=firstregfpu to R_F31 do
  849. if regcounter in rg.usedbyproc then
  850. begin
  851. { reference_reset_base(href,R_1,-localsize);
  852. a_load_store(list,A_STWU,R_1,href);
  853. }
  854. end;
  855. { compute end of gpr save area }
  856. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-(ord(R_F31)-ord(firstregfpu)+1)*8));
  857. end;
  858. { save gprs and fetch GOT pointer }
  859. if usesgpr then
  860. begin
  861. {
  862. if cs_create_pic in aktmoduleswitches then
  863. begin
  864. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g'),0));
  865. gotgot:=true;
  866. end
  867. else
  868. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)),0))
  869. }
  870. reference_reset_base(href,R_11,-(ord(R_31)-ord(firstreggpr)+1)*4);
  871. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  872. end;
  873. if usesfpr or usesgpr then
  874. a_reg_dealloc(list,R_11);
  875. { PIC code support, }
  876. if cs_create_pic in aktmoduleswitches then
  877. begin
  878. { if we didn't get the GOT pointer till now, we've to calculate it now }
  879. if not(gotgot) then
  880. begin
  881. {!!!!!!!!!!!!!}
  882. end;
  883. a_reg_alloc(list,R_31);
  884. { place GOT ptr in r31 }
  885. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  886. end;
  887. { save the CR if necessary ( !!! always done currently ) }
  888. { still need to find out where this has to be done for SystemV
  889. a_reg_alloc(list,R_0);
  890. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  891. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  892. new_reference(STACK_POINTER_REG,LA_CR)));
  893. a_reg_dealloc(list,R_0); }
  894. { now comes the AltiVec context save, not yet implemented !!! }
  895. end;
  896. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  897. var
  898. regcounter,firstregfpu,firstreggpr : TRegister;
  899. href : treference;
  900. usesfpr,usesgpr,genret : boolean;
  901. begin
  902. { release parameter registers }
  903. for regcounter := R_3 to R_10 do
  904. a_reg_dealloc(list,regcounter);
  905. { AltiVec context restore, not yet implemented !!! }
  906. usesfpr:=false;
  907. for regcounter:=R_F14 to R_F31 do
  908. if regcounter in rg.usedbyproc then
  909. begin
  910. usesfpr:=true;
  911. firstregfpu:=regcounter;
  912. break;
  913. end;
  914. usesgpr:=false;
  915. for regcounter:=R_14 to R_30 do
  916. if regcounter in rg.usedbyproc then
  917. begin
  918. usesgpr:=true;
  919. firstreggpr:=regcounter;
  920. break;
  921. end;
  922. { no return (blr) generated yet }
  923. genret:=true;
  924. if usesgpr then
  925. begin
  926. { address of gpr save area to r11 }
  927. if usesfpr then
  928. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_1,tppcprocinfo(procinfo).localsize-(ord(R_F31)-ord(firstregfpu)+1)*8))
  929. else
  930. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_1,tppcprocinfo(procinfo).localsize));
  931. { restore gprs }
  932. { at least for now we use LMW }
  933. {
  934. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_restgpr_14'),0));
  935. }
  936. reference_reset_base(href,R_11,-(ord(R_31)-ord(firstreggpr)+1)*4);
  937. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  938. end;
  939. { restore fprs and return }
  940. if usesfpr then
  941. begin
  942. { address of fpr save area to r11 }
  943. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,(ord(R_F31)-ord(firstregfpu)+1)*8));
  944. {
  945. if (procinfo.flags and pi_do_call)<>0 then
  946. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  947. '_x'),0))
  948. else
  949. { leaf node => lr haven't to be restored }
  950. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  951. '_l'),0));
  952. genret:=false;
  953. }
  954. end;
  955. { if we didn't generate the return code, we've to do it now }
  956. if genret then
  957. begin
  958. { adjust r1 }
  959. a_op_const_reg(list,OP_ADD,tppcprocinfo(procinfo).localsize,R_1);
  960. { load link register? }
  961. if (procinfo.flags and pi_do_call)<>0 then
  962. begin
  963. reference_reset_base(href,STACK_POINTER_REG,4);
  964. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  965. list.concat(taicpu.op_reg(A_MTLR,R_0));
  966. end;
  967. list.concat(taicpu.op_none(A_BLR));
  968. end;
  969. end;
  970. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  971. { generated the entry code of a procedure/function. Note: localsize is the }
  972. { sum of the size necessary for local variables and the maximum possible }
  973. { combined size of ALL the parameters of a procedure called by the current }
  974. { one }
  975. var regcounter: TRegister;
  976. href : treference;
  977. begin
  978. if (localsize mod 8) <> 0 then internalerror(58991);
  979. { CR and LR only have to be saved in case they are modified by the current }
  980. { procedure, but currently this isn't checked, so save them always }
  981. { following is the entry code as described in "Altivec Programming }
  982. { Interface Manual", bar the saving of AltiVec registers }
  983. a_reg_alloc(list,STACK_POINTER_REG);
  984. a_reg_alloc(list,R_0);
  985. { allocate registers containing reg parameters }
  986. for regcounter := R_3 to R_10 do
  987. a_reg_alloc(list,regcounter);
  988. { save return address... }
  989. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  990. { ... in caller's frame }
  991. reference_reset_base(href,STACK_POINTER_REG,8);
  992. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  993. a_reg_dealloc(list,R_0);
  994. { save floating-point registers }
  995. { !!! has to be optimized: only save registers that are used }
  996. list.concat(taicpu.op_sym_ofs(A_BL,objectlibrary.newasmsymbol('_savef14'),0));
  997. { save gprs in gpr save area }
  998. { !!! has to be optimized: only save registers that are used }
  999. reference_reset_base(href,STACK_POINTER_REG,-220);
  1000. list.concat(taicpu.op_reg_ref(A_STMW,R_13,href));
  1001. { save the CR if necessary ( !!! always done currently ) }
  1002. a_reg_alloc(list,R_0);
  1003. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  1004. reference_reset_base(href,stack_pointer_reg,LA_CR);
  1005. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  1006. a_reg_dealloc(list,R_0);
  1007. { save pointer to incoming arguments }
  1008. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1009. a_reg_alloc(list,R_12);
  1010. { 0 or 8 based on SP alignment }
  1011. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1012. R_12,STACK_POINTER_REG,0,28,28));
  1013. { add in stack length }
  1014. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1015. -localsize));
  1016. { establish new alignment }
  1017. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1018. a_reg_dealloc(list,R_12);
  1019. { now comes the AltiVec context save, not yet implemented !!! }
  1020. end;
  1021. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1022. begin
  1023. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1024. end;
  1025. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  1026. begin
  1027. case target_info.system of
  1028. system_powerpc_macos:
  1029. g_return_from_proc_mac(list,parasize);
  1030. system_powerpc_linux:
  1031. g_return_from_proc_sysv(list,parasize)
  1032. else
  1033. internalerror(2204001);
  1034. end;
  1035. end;
  1036. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1037. var
  1038. ref2, tmpref: treference;
  1039. freereg: boolean;
  1040. begin
  1041. ref2 := ref;
  1042. freereg := fixref(list,ref2);
  1043. if assigned(ref2.symbol) then
  1044. { add the symbol's value to the base of the reference, and if the }
  1045. { reference doesn't have a base, create one }
  1046. begin
  1047. reference_reset(tmpref);
  1048. tmpref.offset := ref2.offset;
  1049. tmpref.symbol := ref2.symbol;
  1050. tmpref.symaddr := refs_ha;
  1051. if ref2.base <> R_NO then
  1052. begin
  1053. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1054. ref2.base,tmpref));
  1055. if freereg then
  1056. begin
  1057. cg.free_scratch_reg(list,ref2.base);
  1058. freereg := false;
  1059. end;
  1060. end
  1061. else
  1062. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1063. tmpref.base := R_NO;
  1064. tmpref.symaddr := refs_l;
  1065. { can be folded with one of the next instructions by the }
  1066. { optimizer probably }
  1067. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1068. end
  1069. else if ref2.offset <> 0 Then
  1070. if ref2.base <> R_NO then
  1071. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1072. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1073. { occurs, so now only ref.offset has to be loaded }
  1074. else a_load_const_reg(list,OS_32,ref2.offset,r)
  1075. else if ref.index <> R_NO Then
  1076. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1077. else if (ref2.base <> R_NO) and
  1078. (r <> ref2.base) then
  1079. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1080. if freereg then
  1081. cg.free_scratch_reg(list,ref2.base);
  1082. end;
  1083. { ************* concatcopy ************ }
  1084. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1085. var
  1086. countreg: TRegister;
  1087. src, dst: TReference;
  1088. lab: tasmlabel;
  1089. count, count2: aword;
  1090. orgsrc, orgdst: boolean;
  1091. begin
  1092. {$ifdef extdebug}
  1093. if len > high(longint) then
  1094. internalerror(2002072704);
  1095. {$endif extdebug}
  1096. { make sure short loads are handled as optimally as possible }
  1097. if not loadref then
  1098. if (len <= 8) and
  1099. (byte(len) in [1,2,4,8]) then
  1100. begin
  1101. if len < 8 then
  1102. begin
  1103. a_load_ref_ref(list,int_cgsize(len),source,dest);
  1104. if delsource then
  1105. reference_release(list,source);
  1106. end
  1107. else
  1108. begin
  1109. a_reg_alloc(list,R_F0);
  1110. a_loadfpu_ref_reg(list,OS_F64,source,R_F0);
  1111. if delsource then
  1112. reference_release(list,source);
  1113. a_loadfpu_reg_ref(list,OS_F64,R_F0,dest);
  1114. a_reg_dealloc(list,R_F0);
  1115. end;
  1116. exit;
  1117. end;
  1118. reference_reset(src);
  1119. reference_reset(dst);
  1120. { load the address of source into src.base }
  1121. if loadref then
  1122. begin
  1123. src.base := get_scratch_reg_address(list);
  1124. a_load_ref_reg(list,OS_32,source,src.base);
  1125. orgsrc := false;
  1126. end
  1127. else if not issimpleref(source) or
  1128. ((source.index <> R_NO) and
  1129. ((source.offset + longint(len)) > high(smallint))) then
  1130. begin
  1131. src.base := get_scratch_reg_address(list);
  1132. a_loadaddr_ref_reg(list,source,src.base);
  1133. orgsrc := false;
  1134. end
  1135. else
  1136. begin
  1137. src := source;
  1138. orgsrc := true;
  1139. end;
  1140. if not orgsrc and delsource then
  1141. reference_release(list,source);
  1142. { load the address of dest into dst.base }
  1143. if not issimpleref(dest) or
  1144. ((dest.index <> R_NO) and
  1145. ((dest.offset + longint(len)) > high(smallint))) then
  1146. begin
  1147. dst.base := get_scratch_reg_address(list);
  1148. a_loadaddr_ref_reg(list,dest,dst.base);
  1149. orgdst := false;
  1150. end
  1151. else
  1152. begin
  1153. dst := dest;
  1154. orgdst := true;
  1155. end;
  1156. count := len div 8;
  1157. if count > 4 then
  1158. { generate a loop }
  1159. begin
  1160. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1161. { have to be set to 8. I put an Inc there so debugging may be }
  1162. { easier (should offset be different from zero here, it will be }
  1163. { easy to notice in the generated assembler }
  1164. inc(dst.offset,8);
  1165. inc(src.offset,8);
  1166. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1167. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1168. countreg := get_scratch_reg_int(list);
  1169. a_load_const_reg(list,OS_32,count,countreg);
  1170. { explicitely allocate R_0 since it can be used safely here }
  1171. { (for holding date that's being copied) }
  1172. a_reg_alloc(list,R_F0);
  1173. objectlibrary.getlabel(lab);
  1174. a_label(list, lab);
  1175. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1176. list.concat(taicpu.op_reg_ref(A_LFDU,R_F0,src));
  1177. list.concat(taicpu.op_reg_ref(A_STFDU,R_F0,dst));
  1178. a_jmp(list,A_BC,C_NE,0,lab);
  1179. free_scratch_reg(list,countreg);
  1180. a_reg_dealloc(list,R_F0);
  1181. len := len mod 8;
  1182. end;
  1183. count := len div 8;
  1184. if count > 0 then
  1185. { unrolled loop }
  1186. begin
  1187. a_reg_alloc(list,R_F0);
  1188. for count2 := 1 to count do
  1189. begin
  1190. a_loadfpu_ref_reg(list,OS_F64,src,R_F0);
  1191. a_loadfpu_reg_ref(list,OS_F64,R_F0,dst);
  1192. inc(src.offset,8);
  1193. inc(dst.offset,8);
  1194. end;
  1195. a_reg_dealloc(list,R_F0);
  1196. len := len mod 8;
  1197. end;
  1198. if (len and 4) <> 0 then
  1199. begin
  1200. a_reg_alloc(list,R_0);
  1201. a_load_ref_reg(list,OS_32,src,R_0);
  1202. a_load_reg_ref(list,OS_32,R_0,dst);
  1203. inc(src.offset,4);
  1204. inc(dst.offset,4);
  1205. a_reg_dealloc(list,R_0);
  1206. end;
  1207. { copy the leftovers }
  1208. if (len and 2) <> 0 then
  1209. begin
  1210. a_reg_alloc(list,R_0);
  1211. a_load_ref_reg(list,OS_16,src,R_0);
  1212. a_load_reg_ref(list,OS_16,R_0,dst);
  1213. inc(src.offset,2);
  1214. inc(dst.offset,2);
  1215. a_reg_dealloc(list,R_0);
  1216. end;
  1217. if (len and 1) <> 0 then
  1218. begin
  1219. a_reg_alloc(list,R_0);
  1220. a_load_ref_reg(list,OS_8,src,R_0);
  1221. a_load_reg_ref(list,OS_8,R_0,dst);
  1222. a_reg_dealloc(list,R_0);
  1223. end;
  1224. if orgsrc then
  1225. begin
  1226. if delsource then
  1227. reference_release(list,source);
  1228. end
  1229. else
  1230. free_scratch_reg(list,src.base);
  1231. if not orgdst then
  1232. free_scratch_reg(list,dst.base);
  1233. end;
  1234. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  1235. var
  1236. hl : tasmlabel;
  1237. begin
  1238. if not(cs_check_overflow in aktlocalswitches) then
  1239. exit;
  1240. objectlibrary.getlabel(hl);
  1241. if not ((p.resulttype.def.deftype=pointerdef) or
  1242. ((p.resulttype.def.deftype=orddef) and
  1243. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1244. bool8bit,bool16bit,bool32bit]))) then
  1245. begin
  1246. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  1247. a_jmp(list,A_BC,C_OV,7,hl)
  1248. end
  1249. else
  1250. a_jmp_cond(list,OC_AE,hl);
  1251. a_call_name(list,'FPC_OVERFLOW');
  1252. a_label(list,hl);
  1253. end;
  1254. {***************** This is private property, keep out! :) *****************}
  1255. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1256. var
  1257. regcounter: TRegister;
  1258. href : treference;
  1259. begin
  1260. { release parameter registers }
  1261. for regcounter := R_3 to R_10 do
  1262. a_reg_dealloc(list,regcounter);
  1263. { AltiVec context restore, not yet implemented !!! }
  1264. { restore SP }
  1265. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER_REG,R_31,0));
  1266. { restore gprs }
  1267. reference_reset_base(href,STACK_POINTER_REG,-220);
  1268. list.concat(taicpu.op_reg_ref(A_LMW,R_13,href));
  1269. { restore return address ... }
  1270. reference_reset_base(href,STACK_POINTER_REG,8);
  1271. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1272. { ... and return from _restf14 }
  1273. list.concat(taicpu.op_sym_ofs(A_B,objectlibrary.newasmsymbol('_restf14'),0));
  1274. end;
  1275. function tcgppc.issimpleref(const ref: treference): boolean;
  1276. begin
  1277. if (ref.base = R_NO) and
  1278. (ref.index <> R_NO) then
  1279. internalerror(200208101);
  1280. result :=
  1281. not(assigned(ref.symbol)) and
  1282. (((ref.index = R_NO) and
  1283. (ref.offset >= low(smallint)) and
  1284. (ref.offset <= high(smallint))) or
  1285. ((ref.index <> R_NO) and
  1286. (ref.offset = 0)));
  1287. end;
  1288. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1289. var
  1290. tmpreg: tregister;
  1291. begin
  1292. result := false;
  1293. if (ref.base <> R_NO) then
  1294. begin
  1295. if (ref.index <> R_NO) and
  1296. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1297. begin
  1298. result := true;
  1299. tmpreg := cg.get_scratch_reg_int(list);
  1300. if not assigned(ref.symbol) and
  1301. (cardinal(ref.offset-low(smallint)) <=
  1302. high(smallint)-low(smallint)) then
  1303. begin
  1304. list.concat(taicpu.op_reg_reg_const(
  1305. A_ADDI,tmpreg,ref.base,ref.offset));
  1306. ref.offset := 0;
  1307. end
  1308. else
  1309. begin
  1310. list.concat(taicpu.op_reg_reg_reg(
  1311. A_ADD,tmpreg,ref.base,ref.index));
  1312. ref.index := R_NO;
  1313. end;
  1314. ref.base := tmpreg;
  1315. end
  1316. end
  1317. else
  1318. if ref.index <> R_NO then
  1319. internalerror(200208102);
  1320. end;
  1321. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1322. { that's the case, we can use rlwinm to do an AND operation }
  1323. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1324. var
  1325. temp, testbit: longint;
  1326. compare: boolean;
  1327. begin
  1328. get_rlwi_const := false;
  1329. if (a = 0) or (a = $ffffffff) then
  1330. exit;
  1331. { start with the lowest bit }
  1332. testbit := 1;
  1333. { check its value }
  1334. compare := boolean(a and testbit);
  1335. { find out how long the run of bits with this value is }
  1336. { (it's impossible that all bits are 1 or 0, because in that case }
  1337. { this function wouldn't have been called) }
  1338. l1 := 31;
  1339. while (((a and testbit) <> 0) = compare) do
  1340. begin
  1341. testbit := testbit shl 1;
  1342. dec(l1);
  1343. end;
  1344. { check the length of the run of bits that comes next }
  1345. compare := not compare;
  1346. l2 := l1;
  1347. while (((a and testbit) <> 0) = compare) and
  1348. (l2 >= 0) do
  1349. begin
  1350. testbit := testbit shl 1;
  1351. dec(l2);
  1352. end;
  1353. { and finally the check whether the rest of the bits all have the }
  1354. { same value }
  1355. compare := not compare;
  1356. temp := l2;
  1357. if temp >= 0 then
  1358. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1359. exit;
  1360. { we have done "not(not(compare))", so compare is back to its }
  1361. { initial value. If the lowest bit was 0, a is of the form }
  1362. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1363. { because l2 now contains the position of the last zero of the }
  1364. { first run instead of that of the first 1) so switch l1 and l2 }
  1365. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1366. if not compare then
  1367. begin
  1368. temp := l1;
  1369. l1 := l2+1;
  1370. l2 := temp;
  1371. end
  1372. else
  1373. { otherwise, l1 currently contains the position of the last }
  1374. { zero instead of that of the first 1 of the second run -> +1 }
  1375. inc(l1);
  1376. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1377. l1 := l1 and 31;
  1378. l2 := l2 and 31;
  1379. get_rlwi_const := true;
  1380. end;
  1381. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1382. ref: treference);
  1383. var
  1384. tmpreg: tregister;
  1385. tmpref: treference;
  1386. begin
  1387. tmpreg := R_NO;
  1388. if assigned(ref.symbol) or
  1389. (cardinal(ref.offset-low(smallint)) >
  1390. high(smallint)-low(smallint)) then
  1391. begin
  1392. tmpreg := get_scratch_reg_address(list);
  1393. reference_reset(tmpref);
  1394. tmpref.symbol := ref.symbol;
  1395. tmpref.offset := ref.offset;
  1396. tmpref.symaddr := refs_ha;
  1397. if ref.base <> R_NO then
  1398. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1399. ref.base,tmpref))
  1400. else
  1401. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1402. ref.base := tmpreg;
  1403. ref.symaddr := refs_l;
  1404. end;
  1405. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1406. if (tmpreg <> R_NO) then
  1407. free_scratch_reg(list,tmpreg);
  1408. end;
  1409. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1410. crval: longint; l: tasmlabel);
  1411. var
  1412. p: taicpu;
  1413. begin
  1414. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  1415. if op <> A_B then
  1416. create_cond_norm(c,crval,p.condition);
  1417. p.is_jmp := true;
  1418. list.concat(p)
  1419. end;
  1420. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1421. begin
  1422. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1423. end;
  1424. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1425. begin
  1426. a_op64_const_reg_reg(list,op,value,reg,reg);
  1427. end;
  1428. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1429. begin
  1430. case op of
  1431. OP_AND,OP_OR,OP_XOR:
  1432. begin
  1433. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1434. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1435. end;
  1436. OP_ADD:
  1437. begin
  1438. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1439. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1440. end;
  1441. OP_SUB:
  1442. begin
  1443. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1444. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1445. end;
  1446. else
  1447. internalerror(2002072801);
  1448. end;
  1449. end;
  1450. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  1451. const
  1452. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1453. (A_SUBIC,A_SUBC,A_ADDME));
  1454. var
  1455. tmpreg: tregister;
  1456. tmpreg64: tregister64;
  1457. issub: boolean;
  1458. begin
  1459. case op of
  1460. OP_AND,OP_OR,OP_XOR:
  1461. begin
  1462. cg.a_op_const_reg_reg(list,op,OS_32,cardinal(value),regsrc.reglo,regdst.reglo);
  1463. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1464. regdst.reghi);
  1465. end;
  1466. OP_ADD, OP_SUB:
  1467. begin
  1468. if (longint(value) <> 0) then
  1469. begin
  1470. issub := op = OP_SUB;
  1471. if (longint(value)-ord(issub) >= -32768) and
  1472. (longint(value)-ord(issub) <= 32767) then
  1473. begin
  1474. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1475. regdst.reglo,regsrc.reglo,longint(value)));
  1476. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1477. regdst.reghi,regsrc.reghi));
  1478. end
  1479. else if ((value shr 32) = 0) then
  1480. begin
  1481. tmpreg := cg.get_scratch_reg_int(list);
  1482. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  1483. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1484. regdst.reglo,regsrc.reglo,tmpreg));
  1485. cg.free_scratch_reg(list,tmpreg);
  1486. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1487. regdst.reghi,regsrc.reghi));
  1488. end
  1489. else
  1490. begin
  1491. tmpreg64.reglo := cg.get_scratch_reg_int(list);
  1492. tmpreg64.reghi := cg.get_scratch_reg_int(list);
  1493. a_load64_const_reg(list,value,tmpreg64);
  1494. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  1495. cg.free_scratch_reg(list,tmpreg64.reghi);
  1496. cg.free_scratch_reg(list,tmpreg64.reglo);
  1497. end
  1498. end
  1499. else
  1500. begin
  1501. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1502. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1503. regdst.reghi);
  1504. end;
  1505. end;
  1506. else
  1507. internalerror(2002072802);
  1508. end;
  1509. end;
  1510. begin
  1511. cg := tcgppc.create;
  1512. cg64 :=tcg64fppc.create;
  1513. end.
  1514. {
  1515. $Log$
  1516. Revision 1.58 2002-09-17 18:54:06 jonas
  1517. * a_load_reg_reg() now has two size parameters: source and dest. This
  1518. allows some optimizations on architectures that don't encode the
  1519. register size in the register name.
  1520. Revision 1.57 2002/09/10 21:22:25 jonas
  1521. + added some internal errors
  1522. * fixed bug in sysv exit code
  1523. Revision 1.56 2002/09/08 20:11:56 jonas
  1524. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  1525. Revision 1.55 2002/09/08 13:03:26 jonas
  1526. * several large offset-related fixes
  1527. Revision 1.54 2002/09/07 17:54:58 florian
  1528. * first part of PowerPC fixes
  1529. Revision 1.53 2002/09/07 15:25:14 peter
  1530. * old logs removed and tabs fixed
  1531. Revision 1.52 2002/09/02 10:14:51 jonas
  1532. + a_call_reg()
  1533. * small fix in a_call_ref()
  1534. Revision 1.51 2002/09/02 06:09:02 jonas
  1535. * fixed range error
  1536. Revision 1.50 2002/09/01 21:04:49 florian
  1537. * several powerpc related stuff fixed
  1538. Revision 1.49 2002/09/01 12:09:27 peter
  1539. + a_call_reg, a_call_loc added
  1540. * removed exprasmlist references
  1541. Revision 1.48 2002/08/31 21:38:02 jonas
  1542. * fixed a_call_ref (it should load ctr, not lr)
  1543. Revision 1.47 2002/08/31 21:30:45 florian
  1544. * fixed several problems caused by Jonas' commit :)
  1545. Revision 1.46 2002/08/31 19:25:50 jonas
  1546. + implemented a_call_ref()
  1547. Revision 1.45 2002/08/18 22:16:14 florian
  1548. + the ppc gas assembler writer adds now registers aliases
  1549. to the assembler file
  1550. Revision 1.44 2002/08/17 18:23:53 florian
  1551. * some assembler writer bugs fixed
  1552. Revision 1.43 2002/08/17 09:23:49 florian
  1553. * first part of procinfo rewrite
  1554. Revision 1.42 2002/08/16 14:24:59 carl
  1555. * issameref() to test if two references are the same (then emit no opcodes)
  1556. + ret_in_reg to replace ret_in_acc
  1557. (fix some register allocation bugs at the same time)
  1558. + save_std_register now has an extra parameter which is the
  1559. usedinproc registers
  1560. Revision 1.41 2002/08/15 08:13:54 carl
  1561. - a_load_sym_ofs_reg removed
  1562. * loadvmt now calls loadaddr_ref_reg instead
  1563. Revision 1.40 2002/08/11 14:32:32 peter
  1564. * renamed current_library to objectlibrary
  1565. Revision 1.39 2002/08/11 13:24:18 peter
  1566. * saving of asmsymbols in ppu supported
  1567. * asmsymbollist global is removed and moved into a new class
  1568. tasmlibrarydata that will hold the info of a .a file which
  1569. corresponds with a single module. Added librarydata to tmodule
  1570. to keep the library info stored for the module. In the future the
  1571. objectfiles will also be stored to the tasmlibrarydata class
  1572. * all getlabel/newasmsymbol and friends are moved to the new class
  1573. Revision 1.38 2002/08/11 11:39:31 jonas
  1574. + powerpc-specific genlinearlist
  1575. Revision 1.37 2002/08/10 17:15:31 jonas
  1576. * various fixes and optimizations
  1577. Revision 1.36 2002/08/06 20:55:23 florian
  1578. * first part of ppc calling conventions fix
  1579. Revision 1.35 2002/08/06 07:12:05 jonas
  1580. * fixed bug in g_flags2reg()
  1581. * and yet more constant operation fixes :)
  1582. Revision 1.34 2002/08/05 08:58:53 jonas
  1583. * fixed compilation problems
  1584. Revision 1.33 2002/08/04 12:57:55 jonas
  1585. * more misc. fixes, mostly constant-related
  1586. }