cgcpu.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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,aasm,cpuasm,cpubase,cpuinfo,node,cg64f32,cginfo;
  23. type
  24. tcgppc = class(tcg64f32)
  25. { passing parameters, per default the parameter is pushed }
  26. { nr gives the number of the parameter (enumerated from }
  27. { left to right), this allows to move the parameter to }
  28. { register, if the cpu supports register calling }
  29. { conventions }
  30. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);override;
  31. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);override;
  32. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);override;
  33. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);override;
  34. procedure a_call_name(list : taasmoutput;const s : string);override;
  35. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  36. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  37. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  38. size: tcgsize; a: aword; src, dst: tregister); override;
  39. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  40. size: tcgsize; src1, src2, dst: tregister); override;
  41. { move instructions }
  42. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  43. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  44. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  45. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  46. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister); override;
  47. { fpu move instructions }
  48. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  49. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  50. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  51. { comparison operations }
  52. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  53. l : tasmlabel);override;
  54. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  56. procedure g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister); override;
  57. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  58. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  59. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  60. procedure g_restore_frame_pointer(list : taasmoutput);override;
  61. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  62. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  63. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  64. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  65. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  66. { that's the case, we can use rlwinm to do an AND operation }
  67. function get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  68. private
  69. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  70. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  71. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  72. { Make sure ref is a valid reference for the PowerPC and sets the }
  73. { base to the value of the index if (base = R_NO). }
  74. procedure fixref(list: taasmoutput; var ref: treference);
  75. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  76. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  77. ref: treference);
  78. { creates the correct branch instruction for a given combination }
  79. { of asmcondflags and destination addressing mode }
  80. procedure a_jmp(list: taasmoutput; op: tasmop;
  81. c: tasmcondflag; crval: longint; l: tasmlabel);
  82. end;
  83. const
  84. {
  85. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIVWU,
  86. A_DIVW,A_MULLW, A_MULLW, A_NEG,A_NOT,A_OR,
  87. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  88. }
  89. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  90. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  91. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  92. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  93. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  94. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  95. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  96. C_LT,C_GE,C_LE,C_NE,C_LE,C_NG,C_GE,C_NL);
  97. implementation
  98. uses
  99. globtype,globals,verbose,systems,cutils,symconst,symdef,rgobj;
  100. { parameter passing... Still needs extra support from the processor }
  101. { independent code generator }
  102. procedure tcgppc.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);
  103. var
  104. ref: treference;
  105. begin
  106. {$ifdef para_sizes_known}
  107. if (nr <= max_param_regs_int) then
  108. a_load_reg_reg(list,size,r,param_regs_int[nr])
  109. else
  110. begin
  111. reset_reference(ref);
  112. ref.base := STACK_POINTER_REG;
  113. ref.offset := LinkageAreaSize+para_size_till_now;
  114. a_load_reg_ref(list,size,reg,ref);
  115. end;
  116. {$endif para_sizes_known}
  117. end;
  118. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);
  119. var
  120. ref: treference;
  121. begin
  122. {$ifdef para_sizes_known}
  123. if (nr <= max_param_regs_int) then
  124. a_load_const_reg(list,size,a,param_regs_int[nr])
  125. else
  126. begin
  127. reset_reference(ref);
  128. ref.base := STACK_POINTER_REG;
  129. ref.offset := LinkageAreaSize+para_size_till_now;
  130. a_load_const_ref(list,size,a,ref);
  131. end;
  132. {$endif para_sizes_known}
  133. end;
  134. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  135. var
  136. ref: treference;
  137. tmpreg: tregister;
  138. begin
  139. {$ifdef para_sizes_known}
  140. if (nr <= max_param_regs_int) then
  141. a_load_ref_reg(list,size,r,param_regs_int[nr])
  142. else
  143. begin
  144. reset_reference(ref);
  145. ref.base := STACK_POINTER_REG;
  146. ref.offset := LinkageAreaSize+para_size_till_now;
  147. tmpreg := get_scratch_reg(list);
  148. a_load_ref_reg(list,size,r,tmpreg);
  149. a_load_reg_ref(list,size,tmpreg,ref);
  150. free_scratch_reg(list,tmpreg);
  151. end;
  152. {$endif para_sizes_known}
  153. end;
  154. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  155. var
  156. ref: treference;
  157. tmpreg: tregister;
  158. begin
  159. {$ifdef para_sizes_known}
  160. if (nr <= max_param_regs_int) then
  161. a_loadaddr_ref_reg(list,size,r,param_regs_int[nr])
  162. else
  163. begin
  164. reset_reference(ref);
  165. ref.base := STACK_POINTER_REG;
  166. ref.offset := LinkageAreaSize+para_size_till_now;
  167. tmpreg := get_scratch_reg(list);
  168. a_loadaddr_ref_reg(list,size,r,tmpreg);
  169. a_load_reg_ref(list,size,tmpreg,ref);
  170. free_scratch_reg(list,tmpreg);
  171. end;
  172. {$endif para_sizes_known}
  173. end;
  174. { calling a code fragment by name }
  175. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  176. begin
  177. { save our RTOC register value. Only necessary when doing pointer based }
  178. { calls or cross TOC calls, but currently done always }
  179. list.concat(taicpu.op_reg_ref(A_STW,R_RTOC,
  180. new_reference(STACK_POINTER_REG,LA_RTOC)));
  181. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  182. list.concat(taicpu.op_reg_ref(A_LWZ,R_RTOC,
  183. new_reference(STACK_POINTER_REG,LA_RTOC)));
  184. end;
  185. {********************** load instructions ********************}
  186. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  187. begin
  188. if (a and $ffff) <> 0 Then
  189. begin
  190. list.concat(taicpu.op_reg_const(A_LI,reg,a and $ffff));
  191. if (longint(a) < low(smallint)) or
  192. (longint(a) > high(smallint)) then
  193. list.concat(taicpu.op_reg_const(A_ADDIS,reg,
  194. (a shr 16)+ord(smallint(a and $ffff) < 0)))
  195. end
  196. else
  197. list.concat(taicpu.op_reg_const(A_LIS,reg,a shr 16));
  198. end;
  199. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  200. const
  201. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  202. { indexed? updating?}
  203. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  204. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  205. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  206. var
  207. op: TAsmOp;
  208. ref2: TReference;
  209. begin
  210. ref2 := ref;
  211. FixRef(list,ref2);
  212. if size in [OS_S8..OS_S16] then
  213. { storing is the same for signed and unsigned values }
  214. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  215. { 64 bit stuff should be handled separately }
  216. if size in [OS_64,OS_S64] then
  217. internalerror(200109236);
  218. op := storeinstr[size,ref2.index<>R_NO,false];
  219. a_load_store(list,op,reg,ref2);
  220. End;
  221. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  222. const
  223. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  224. { indexed? updating?}
  225. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  226. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  227. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  228. { 64bit stuff should be handled separately }
  229. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  230. { there's no load-byte-with-sign-extend :( }
  231. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  232. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  233. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  234. var
  235. op: tasmop;
  236. tmpreg: tregister;
  237. ref2, tmpref: treference;
  238. begin
  239. if ref.is_immediate then
  240. a_load_const_reg(list,size,ref.offset,reg)
  241. else
  242. begin
  243. ref2 := ref;
  244. fixref(list,ref2);
  245. op := loadinstr[size,ref2.index<>R_NO,false];
  246. a_load_store(list,op,reg,ref2);
  247. { sign extend shortint if necessary, since there is no }
  248. { load instruction that does that automatically (JM) }
  249. if size = OS_S8 then
  250. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  251. end;
  252. end;
  253. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  254. begin
  255. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  256. end;
  257. procedure tcgppc.a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);
  258. begin
  259. { can't use op_sym_ofs_reg because sym+ofs can be > 32767!! }
  260. internalerror(200112293);
  261. end;
  262. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  263. begin
  264. list.concat(taicpu.op_reg_reg(A_FMR,reg1,reg2));
  265. end;
  266. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  267. const
  268. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  269. { indexed? updating?}
  270. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  271. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  272. var
  273. op: tasmop;
  274. ref2: treference;
  275. begin
  276. if not(size in [OS_F32,OS_F64]) then
  277. internalerror(200201121);
  278. ref2 := ref;
  279. fixref(list,ref2);
  280. op := fpuloadinstr[size,ref2.index <> R_NO,false];
  281. a_load_store(list,op,reg,ref2);
  282. end;
  283. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  284. const
  285. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  286. { indexed? updating?}
  287. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  288. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  289. var
  290. op: tasmop;
  291. ref2: treference;
  292. begin
  293. if not(size in [OS_F32,OS_F64]) then
  294. internalerror(200201122);
  295. ref2 := ref;
  296. fixref(list,ref2);
  297. op := fpustoreinstr[size,ref2.index <> R_NO,false];
  298. a_load_store(list,op,reg,ref2);
  299. end;
  300. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  301. var
  302. scratch_register: TRegister;
  303. begin
  304. case op of
  305. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL, OP_ADD, OP_AND, OP_OR, OP_SUB,
  306. OP_XOR:
  307. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  308. OP_SHL,OP_SHR,OP_SAR:
  309. begin
  310. if (a and 31) <> 0 then
  311. list.concat(taicpu.op_reg_reg_const(
  312. TOpCG2AsmOpConstLo[op],reg,reg,a and 31));
  313. if (a shr 5) <> 0 then
  314. internalError(68991);
  315. end
  316. else internalError(68992);
  317. end;
  318. end;
  319. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  320. begin
  321. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  322. end;
  323. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  324. size: tcgsize; a: aword; src, dst: tregister);
  325. var
  326. l1,l2: longint;
  327. var
  328. oplo, ophi: tasmop;
  329. scratchreg: tregister;
  330. useReg: boolean;
  331. begin
  332. ophi := TOpCG2AsmOpConstHi[op];
  333. oplo := TOpCG2AsmOpConstLo[op];
  334. { constants in a PPC instruction are always interpreted as signed }
  335. { 16bit values, so if the value is between low(smallint) and }
  336. { high(smallint), it's easy }
  337. if (op in [OP_ADD,OP_SUB,OP_AND,OP_OR,OP_XOR]) then
  338. begin
  339. if (longint(a) >= low(smallint)) and
  340. (longint(a) <= high(smallint)) then
  341. begin
  342. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,a));
  343. exit;
  344. end;
  345. { all basic constant instructions also have a shifted form that }
  346. { works only on the highest 16bits, so if low(a) is 0, we can }
  347. { use that one }
  348. if (lo(a) = 0) then
  349. begin
  350. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,hi(a)));
  351. exit;
  352. end;
  353. end;
  354. { otherwise, the instructions we can generate depend on the }
  355. { operation }
  356. useReg := false;
  357. case op of
  358. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL:
  359. if (Op = OP_IMUL) and (longint(a) >= -32768) and
  360. (longint(a) <= 32767) then
  361. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,a))
  362. else
  363. usereg := true;
  364. OP_ADD,OP_SUB:
  365. begin
  366. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,low(a)));
  367. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  368. high(a) + ord(smallint(a) < 0)));
  369. end;
  370. OP_OR:
  371. { try to use rlwimi }
  372. if get_rlwi_const(a,l1,l2) then
  373. begin
  374. if src <> dst then
  375. list.concat(taicpu.op_reg_reg(A_MR,dst,src));
  376. scratchreg := get_scratch_reg(list);
  377. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  378. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  379. scratchreg,0,l1,l2));
  380. free_scratch_reg(list,scratchreg);
  381. end
  382. else
  383. useReg := true;
  384. OP_AND:
  385. { try to use rlwinm }
  386. if get_rlwi_const(a,l1,l2) then
  387. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  388. src,0,l1,l2))
  389. else
  390. useReg := true;
  391. OP_XOR:
  392. useReg := true;
  393. OP_SHL,OP_SHR,OP_SAR:
  394. begin
  395. if (a and 31) <> 0 Then
  396. list.concat(taicpu.op_reg_reg_const(
  397. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  398. if (a shr 5) <> 0 then
  399. internalError(68991);
  400. end
  401. else
  402. internalerror(200109091);
  403. end;
  404. { if all else failed, load the constant in a register and then }
  405. { perform the operation }
  406. if useReg then
  407. begin
  408. scratchreg := get_scratch_reg(list);
  409. a_load_const_reg(list,OS_32,a,scratchreg);
  410. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  411. free_scratch_reg(list,scratchreg);
  412. end;
  413. end;
  414. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  415. size: tcgsize; src1, src2, dst: tregister);
  416. const
  417. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  418. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  419. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  420. begin
  421. case op of
  422. OP_NEG,OP_NOT:
  423. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  424. else
  425. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  426. end;
  427. end;
  428. {*************** compare instructructions ****************}
  429. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  430. l : tasmlabel);
  431. var
  432. p: taicpu;
  433. scratch_register: TRegister;
  434. signed: boolean;
  435. begin
  436. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  437. if signed then
  438. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  439. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,reg,a))
  440. else
  441. begin
  442. scratch_register := get_scratch_reg(list);
  443. a_load_const_reg(list,OS_32,a,scratch_register);
  444. list.concat(taicpu.op_reg_reg_reg(A_CMP,R_CR0,reg,scratch_register));
  445. free_scratch_reg(list,scratch_register);
  446. end
  447. else
  448. if (a <= $ffff) then
  449. list.concat(taicpu.op_reg_reg_const(A_CMPLI,R_CR0,reg,a))
  450. else
  451. begin
  452. scratch_register := get_scratch_reg(list);
  453. a_load_const_reg(list,OS_32,a,scratch_register);
  454. list.concat(taicpu.op_reg_reg_reg(A_CMPL,R_CR0,reg,scratch_register));
  455. free_scratch_reg(list,scratch_register);
  456. end;
  457. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  458. end;
  459. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  460. reg1,reg2 : tregister;l : tasmlabel);
  461. var
  462. p: taicpu;
  463. op: tasmop;
  464. begin
  465. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  466. op := A_CMP
  467. else op := A_CMPL;
  468. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  469. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  470. end;
  471. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  472. begin
  473. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  474. end;
  475. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  476. var
  477. c: tasmcond;
  478. begin
  479. c := flags_to_cond(f);
  480. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(R_CR0),l);
  481. end;
  482. procedure tcgppc.g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister);
  483. var
  484. testbit: byte;
  485. bitvalue: boolean;
  486. begin
  487. { get the bit to extract from the conditional register + its }
  488. { requested value (0 or 1) }
  489. testbit := (ord(f.cr) * 4);
  490. case f.flag of
  491. F_EQ,F_NE:
  492. bitvalue := f.flag = F_EQ;
  493. F_LT,F_GE:
  494. begin
  495. inc(testbit);
  496. bitvalue := f.flag = F_LT;
  497. end;
  498. F_GT,F_LE:
  499. begin
  500. inc(testbit,2);
  501. bitvalue := f.flag = F_GT;
  502. end;
  503. else
  504. internalerror(200112261);
  505. end;
  506. { load the conditional register in the destination reg }
  507. list.concat(taicpu.op_reg(A_MFCR,reg));
  508. { we will move the bit that has to be tested to bit 31 -> rotate }
  509. { left by bitpos+1 (remember, this is big-endian!) }
  510. testbit := (testbit + 1) and 31;
  511. { extract bit }
  512. list.concat(taicpu.op_reg_reg_const_const_const(
  513. A_RLWINM,reg,reg,testbit,31,31));
  514. { if we need the inverse, xor with 1 }
  515. if not bitvalue then
  516. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  517. end;
  518. (*
  519. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  520. var
  521. testbit: byte;
  522. bitvalue: boolean;
  523. begin
  524. { get the bit to extract from the conditional register + its }
  525. { requested value (0 or 1) }
  526. case f.simple of
  527. false:
  528. begin
  529. { we don't generate this in the compiler }
  530. internalerror(200109062);
  531. end;
  532. true:
  533. case f.cond of
  534. C_None:
  535. internalerror(200109063);
  536. C_LT..C_NU:
  537. begin
  538. testbit := (ord(f.cr) - ord(R_CR0))*4;
  539. inc(testbit,AsmCondFlag2BI[f.cond]);
  540. bitvalue := AsmCondFlagTF[f.cond];
  541. end;
  542. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  543. begin
  544. testbit := f.crbit
  545. bitvalue := AsmCondFlagTF[f.cond];
  546. end;
  547. else
  548. internalerror(200109064);
  549. end;
  550. end;
  551. { load the conditional register in the destination reg }
  552. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  553. { we will move the bit that has to be tested to bit 31 -> rotate }
  554. { left by bitpos+1 (remember, this is big-endian!) }
  555. if bitpos <> 31 then
  556. inc(bitpos)
  557. else
  558. bitpos := 0;
  559. { extract bit }
  560. list.concat(taicpu.op_reg_reg_const_const_const(
  561. A_RLWINM,reg,reg,bitpos,31,31));
  562. { if we need the inverse, xor with 1 }
  563. if not bitvalue then
  564. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  565. end;
  566. *)
  567. { *********** entry/exit code and address loading ************ }
  568. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  569. begin
  570. case target_info.target of
  571. target_powerpc_macos:
  572. g_stackframe_entry_mac(list,localsize);
  573. target_powerpc_linux:
  574. g_stackframe_entry_sysv(list,localsize)
  575. else
  576. internalerror(2204001);
  577. end;
  578. end;
  579. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  580. { generated the entry code of a procedure/function. Note: localsize is the }
  581. { sum of the size necessary for local variables and the maximum possible }
  582. { combined size of ALL the parameters of a procedure called by the current }
  583. { one }
  584. var regcounter: TRegister;
  585. begin
  586. if (localsize mod 8) <> 0 then internalerror(58991);
  587. { CR and LR only have to be saved in case they are modified by the current }
  588. { procedure, but currently this isn't checked, so save them always }
  589. { following is the entry code as described in "Altivec Programming }
  590. { Interface Manual", bar the saving of AltiVec registers }
  591. a_reg_alloc(list,STACK_POINTER_REG);
  592. a_reg_alloc(list,R_0);
  593. { allocate registers containing reg parameters }
  594. for regcounter := R_3 to R_10 do
  595. a_reg_alloc(list,regcounter);
  596. { save return address... }
  597. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  598. { ... in caller's frame }
  599. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER_REG,4)));
  600. a_reg_dealloc(list,R_0);
  601. a_reg_alloc(list,R_11);
  602. { save end of fpr save area }
  603. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER_REG,0));
  604. a_reg_alloc(list,R_12);
  605. { 0 or 8 based on SP alignment }
  606. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  607. R_12,STACK_POINTER_REG,0,28,28));
  608. { add in stack length }
  609. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  610. -localsize));
  611. { establish new alignment }
  612. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  613. a_reg_dealloc(list,R_12);
  614. { save floating-point registers }
  615. { !!! has to be optimized: only save registers that are used }
  616. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  617. { compute end of gpr save area }
  618. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  619. { save gprs and fetch GOT pointer }
  620. { !!! has to be optimized: only save registers that are used }
  621. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  622. a_reg_alloc(list,R_31);
  623. { place GOT ptr in r31 }
  624. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  625. { save the CR if necessary ( !!! always done currently ) }
  626. { still need to find out where this has to be done for SystemV
  627. a_reg_alloc(list,R_0);
  628. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  629. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  630. new_reference(STACK_POINTER_REG,LA_CR)));
  631. a_reg_dealloc(list,R_0); }
  632. { save pointer to incoming arguments }
  633. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  634. { now comes the AltiVec context save, not yet implemented !!! }
  635. end;
  636. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  637. { generated the entry code of a procedure/function. Note: localsize is the }
  638. { sum of the size necessary for local variables and the maximum possible }
  639. { combined size of ALL the parameters of a procedure called by the current }
  640. { one }
  641. var regcounter: TRegister;
  642. begin
  643. if (localsize mod 8) <> 0 then internalerror(58991);
  644. { CR and LR only have to be saved in case they are modified by the current }
  645. { procedure, but currently this isn't checked, so save them always }
  646. { following is the entry code as described in "Altivec Programming }
  647. { Interface Manual", bar the saving of AltiVec registers }
  648. a_reg_alloc(list,STACK_POINTER_REG);
  649. a_reg_alloc(list,R_0);
  650. { allocate registers containing reg parameters }
  651. for regcounter := R_3 to R_10 do
  652. a_reg_alloc(list,regcounter);
  653. { save return address... }
  654. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  655. { ... in caller's frame }
  656. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER_REG,8)));
  657. a_reg_dealloc(list,R_0);
  658. { save floating-point registers }
  659. { !!! has to be optimized: only save registers that are used }
  660. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  661. { save gprs in gpr save area }
  662. { !!! has to be optimized: only save registers that are used }
  663. list.concat(taicpu.op_reg_ref(A_STMW,R_13,new_reference(STACK_POINTER_REG,-220)));
  664. { save the CR if necessary ( !!! always done currently ) }
  665. a_reg_alloc(list,R_0);
  666. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  667. list.concat(taicpu.op_reg_ref(A_STW,R_0,
  668. new_reference(stack_pointer_reg,LA_CR)));
  669. a_reg_dealloc(list,R_0);
  670. { save pointer to incoming arguments }
  671. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  672. a_reg_alloc(list,R_12);
  673. { 0 or 8 based on SP alignment }
  674. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  675. R_12,STACK_POINTER_REG,0,28,28));
  676. { add in stack length }
  677. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  678. -localsize));
  679. { establish new alignment }
  680. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  681. a_reg_dealloc(list,R_12);
  682. { now comes the AltiVec context save, not yet implemented !!! }
  683. end;
  684. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  685. begin
  686. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  687. end;
  688. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  689. begin
  690. case target_info.target of
  691. target_powerpc_macos:
  692. g_return_from_proc_mac(list,parasize);
  693. target_powerpc_linux:
  694. g_return_from_proc_sysv(list,parasize)
  695. else
  696. internalerror(2204001);
  697. end;
  698. end;
  699. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  700. var tmpreg: tregister;
  701. ref2, tmpref: treference;
  702. begin
  703. ref2 := ref;
  704. FixRef(list,ref2);
  705. if assigned(ref2.symbol) then
  706. { add the symbol's value to the base of the reference, and if the }
  707. { reference doesn't have a base, create one }
  708. begin
  709. tmpreg := get_scratch_reg(list);
  710. reset_reference(tmpref);
  711. tmpref.symbol := ref2.symbol;
  712. tmpref.symaddr := refs_ha;
  713. tmpref.is_immediate := true;
  714. if ref2.base <> R_NO then
  715. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  716. ref2.base,newreference(tmpref)))
  717. else
  718. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  719. newreference(tmpref)));
  720. ref2.base := tmpreg;
  721. ref2.symaddr := refs_l;
  722. { can be folded with one of the next instructions by the }
  723. { optimizer probably }
  724. list.concat(taicpu.op_reg_reg_ref(A_ADDI,tmpreg,tmpreg,
  725. newreference(tmpref)));
  726. end;
  727. if ref2.offset <> 0 Then
  728. if ref2.base <> R_NO then
  729. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  730. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  731. { occurs, so now only ref.offset has to be loaded }
  732. else a_load_const_reg(list,OS_32,ref2.offset,r)
  733. else
  734. if ref.index <> R_NO Then
  735. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  736. else
  737. if r <> ref2.base then
  738. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  739. if assigned(ref2.symbol) then
  740. free_scratch_reg(list,tmpreg);
  741. end;
  742. { ************* concatcopy ************ }
  743. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  744. var
  745. t: taicpu;
  746. countreg, tempreg: TRegister;
  747. src, dst: TReference;
  748. lab: tasmlabel;
  749. count, count2: aword;
  750. begin
  751. { make sure source and dest are valid }
  752. src := source;
  753. fixref(list,src);
  754. dst := dest;
  755. fixref(list,dst);
  756. reset_reference(src);
  757. reset_reference(dst);
  758. { load the address of source into src.base }
  759. src.base := get_scratch_reg(list);
  760. if loadref then
  761. a_load_ref_reg(list,OS_32,source,src.base)
  762. else a_loadaddr_ref_reg(list,source,src.base);
  763. if delsource then
  764. reference_release(exprasmlist,source);
  765. { load the address of dest into dst.base }
  766. dst.base := get_scratch_reg(list);
  767. a_loadaddr_ref_reg(list,dest,dst.base);
  768. count := len div 4;
  769. if count > 3 then
  770. { generate a loop }
  771. begin
  772. { the offsets are zero after the a_loadaddress_ref_reg and just }
  773. { have to be set to 4. I put an Inc there so debugging may be }
  774. { easier (should offset be different from zero here, it will be }
  775. { easy to notice in the genreated assembler }
  776. Inc(dst.offset,4);
  777. Inc(src.offset,4);
  778. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  779. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  780. countreg := get_scratch_reg(list);
  781. a_load_const_reg(list,OS_32,count-1,countreg);
  782. { explicitely allocate R_0 since it can be used safely here }
  783. { (for holding date that's being copied) }
  784. tempreg := R_0;
  785. a_reg_alloc(list,R_0);
  786. getlabel(lab);
  787. a_label(list, lab);
  788. list.concat(taicpu.op_reg_ref(A_LWZU,tempreg,
  789. newreference(src)));
  790. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,countreg,0));
  791. list.concat(taicpu.op_reg_ref(A_STWU,tempreg,newreference(dst)));
  792. list.concat(taicpu.op_reg_reg_const(A_SUBI,countreg,countreg,1));
  793. a_jmp(list,A_BC,C_NE,0,lab);
  794. free_scratch_reg(list,countreg);
  795. end
  796. else
  797. { unrolled loop }
  798. begin
  799. tempreg := get_scratch_reg(list);
  800. for count2 := 1 to count do
  801. begin
  802. a_load_ref_reg(list,OS_32,src,tempreg);
  803. a_load_reg_ref(list,OS_32,tempreg,dst);
  804. inc(src.offset,4);
  805. inc(dst.offset,4);
  806. end
  807. end;
  808. { copy the leftovers }
  809. if (len and 2) <> 0 then
  810. begin
  811. a_load_ref_reg(list,OS_16,src,tempreg);
  812. a_load_reg_ref(list,OS_16,tempreg,dst);
  813. inc(src.offset,2);
  814. inc(dst.offset,2);
  815. end;
  816. if (len and 1) <> 0 then
  817. begin
  818. a_load_ref_reg(list,OS_8,src,tempreg);
  819. a_load_reg_ref(list,OS_8,tempreg,dst);
  820. end;
  821. a_reg_dealloc(list,tempreg);
  822. free_scratch_reg(list,src.base);
  823. free_scratch_reg(list,dst.base);
  824. end;
  825. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  826. var
  827. hl : tasmlabel;
  828. begin
  829. if not(cs_check_overflow in aktlocalswitches) then
  830. exit;
  831. getlabel(hl);
  832. if not ((p.resulttype.def.deftype=pointerdef) or
  833. ((p.resulttype.def.deftype=orddef) and
  834. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  835. bool8bit,bool16bit,bool32bit]))) then
  836. begin
  837. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  838. a_jmp(list,A_BC,C_OV,7,hl)
  839. end
  840. else
  841. a_jmp_cond(list,OC_AE,hl);
  842. a_call_name(list,'FPC_OVERFLOW');
  843. a_label(list,hl);
  844. end;
  845. {***************** This is private property, keep out! :) *****************}
  846. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  847. var
  848. regcounter: TRegister;
  849. begin
  850. { release parameter registers }
  851. for regcounter := R_3 to R_10 do
  852. a_reg_dealloc(list,regcounter);
  853. { AltiVec context restore, not yet implemented !!! }
  854. { address of gpr save area to r11 }
  855. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  856. { restore gprs }
  857. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  858. { address of fpr save area to r11 }
  859. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  860. { restore fprs and return }
  861. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  862. end;
  863. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  864. var
  865. regcounter: TRegister;
  866. begin
  867. { release parameter registers }
  868. for regcounter := R_3 to R_10 do
  869. a_reg_dealloc(list,regcounter);
  870. { AltiVec context restore, not yet implemented !!! }
  871. { restore SP }
  872. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER_REG,R_31,0));
  873. { restore gprs }
  874. list.concat(taicpu.op_reg_ref(A_LMW,R_13,new_reference(STACK_POINTER_REG,-220)));
  875. { restore return address ... }
  876. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,new_reference(STACK_POINTER_REG,8)));
  877. { ... and return from _restf14 }
  878. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  879. end;
  880. procedure tcgppc.fixref(list: taasmoutput; var ref: treference);
  881. begin
  882. If (ref.base <> R_NO) then
  883. begin
  884. if (ref.index <> R_NO) and
  885. ((ref.offset <> 0) or assigned(ref.symbol)) then
  886. begin
  887. if not assigned(ref.symbol) and
  888. (cardinal(ref.offset-low(smallint)) <=
  889. high(smallint)-low(smallint)) then
  890. begin
  891. list.concat(taicpu.op_reg_reg_const(
  892. A_ADDI,ref.base,ref.base,ref.offset));
  893. ref.offset := 0;
  894. end
  895. else
  896. begin
  897. list.concat(taicpu.op_reg_reg_reg(
  898. A_ADD,ref.base,ref.base,ref.index));
  899. ref.index := R_NO;
  900. end;
  901. end
  902. end
  903. else
  904. begin
  905. ref.base := ref.index;
  906. ref.index := R_NO
  907. end
  908. end;
  909. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  910. { that's the case, we can use rlwinm to do an AND operation }
  911. function tcgppc.get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  912. var
  913. temp, testbit: longint;
  914. compare: boolean;
  915. begin
  916. get_rlwi_const := false;
  917. { start with the lowest bit }
  918. testbit := 1;
  919. { check its value }
  920. compare := boolean(a and testbit);
  921. { find out how long the run of bits with this value is }
  922. { (it's impossible that all bits are 1 or 0, because in that case }
  923. { this function wouldn't have been called) }
  924. l1 := 31;
  925. while (((a and testbit) <> 0) = compare) do
  926. begin
  927. testbit := testbit shl 1;
  928. dec(l1);
  929. end;
  930. { check the length of the run of bits that comes next }
  931. compare := not compare;
  932. l2 := l1;
  933. while (((a and testbit) <> 0) = compare) and
  934. (l2 >= 0) do
  935. begin
  936. testbit := testbit shl 1;
  937. dec(l2);
  938. end;
  939. { and finally the check whether the rest of the bits all have the }
  940. { same value }
  941. compare := not compare;
  942. temp := l2;
  943. if temp >= 0 then
  944. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  945. exit;
  946. { we have done "not(not(compare))", so compare is back to its }
  947. { initial value. If the lowest bit was 0, a is of the form }
  948. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  949. { because l2 now contains the position of the last zero of the }
  950. { first run instead of that of the first 1) so switch l1 and l2 }
  951. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  952. if not compare then
  953. begin
  954. temp := l1;
  955. l1 := l2+1;
  956. l2 := temp;
  957. end
  958. else
  959. { otherwise, l1 currently contains the position of the last }
  960. { zero instead of that of the first 1 of the second run -> +1 }
  961. inc(l1);
  962. { the following is the same as "if l1 = -1 then l1 := 31;" }
  963. l1 := l1 and 31;
  964. l2 := l2 and 31;
  965. get_rlwi_const := true;
  966. end;
  967. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  968. ref: treference);
  969. var
  970. tmpreg: tregister;
  971. tmpref: treference;
  972. begin
  973. if assigned(ref.symbol) then
  974. begin
  975. tmpreg := get_scratch_reg(list);
  976. reset_reference(tmpref);
  977. tmpref.symbol := ref.symbol;
  978. tmpref.symaddr := refs_ha;
  979. tmpref.is_immediate := true;
  980. if ref.base <> R_NO then
  981. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  982. ref.base,newreference(tmpref)))
  983. else
  984. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  985. newreference(tmpref)));
  986. ref.base := tmpreg;
  987. ref.symaddr := refs_l;
  988. end;
  989. list.concat(taicpu.op_reg_ref(op,reg,newreference(ref)));
  990. if assigned(ref.symbol) then
  991. free_scratch_reg(list,tmpreg);
  992. end;
  993. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  994. crval: longint; l: tasmlabel);
  995. var
  996. p: taicpu;
  997. begin
  998. p := taicpu.op_sym(op,newasmsymbol(l.name));
  999. create_cond_norm(c,crval,p.condition);
  1000. p.is_jmp := true;
  1001. list.concat(p)
  1002. end;
  1003. begin
  1004. cg := tcgppc.create;
  1005. end.
  1006. {
  1007. $Log$
  1008. Revision 1.17 2002-05-16 19:46:53 carl
  1009. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1010. + try to fix temp allocation (still in ifdef)
  1011. + generic constructor calls
  1012. + start of tassembler / tmodulebase class cleanup
  1013. Revision 1.14 2002/05/13 19:52:46 peter
  1014. * a ppcppc can be build again
  1015. Revision 1.13 2002/04/20 21:41:51 carl
  1016. * renamed some constants
  1017. Revision 1.12 2002/04/06 18:13:01 jonas
  1018. * several powerpc-related additions and fixes
  1019. Revision 1.11 2002/01/02 14:53:04 jonas
  1020. * fixed small bug in a_jmp_flags
  1021. Revision 1.10 2001/12/30 17:24:48 jonas
  1022. * range checking is now processor independent (part in cgobj, part in
  1023. cg64f32) and should work correctly again (it needed some changes after
  1024. the changes of the low and high of tordef's to int64)
  1025. * maketojumpbool() is now processor independent (in ncgutil)
  1026. * getregister32 is now called getregisterint
  1027. Revision 1.9 2001/12/29 15:28:58 jonas
  1028. * powerpc/cgcpu.pas compiles :)
  1029. * several powerpc-related fixes
  1030. * cpuasm unit is now based on common tainst unit
  1031. + nppcmat unit for powerpc (almost complete)
  1032. Revision 1.8 2001/10/28 14:16:49 jonas
  1033. * small fixes
  1034. Revision 1.7 2001/09/29 21:33:30 jonas
  1035. * small optimization
  1036. Revision 1.6 2001/09/28 20:40:05 jonas
  1037. * several additions, almost complete (only some problems with resflags left)
  1038. Revision 1.5 2001/09/16 10:33:21 jonas
  1039. * some fixes to operations with constants
  1040. Revision 1.3 2001/09/06 15:25:55 jonas
  1041. * changed type of tcg from object to class -> abstract methods are now
  1042. a lot cleaner :)
  1043. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  1044. (if possible with generic implementation and necessary ppc
  1045. implementations)
  1046. * worked a bit further on cgflw, now working on exitnode
  1047. Revision 1.2 2001/09/05 20:21:03 jonas
  1048. * new cgflow based on n386flw with all nodes until forn "translated"
  1049. + a_cmp_loc_*_label methods for tcg
  1050. + base implementatino for a_cmp_ref_*_label methods
  1051. * small bugfixes to powerpc cg
  1052. Revision 1.1 2001/08/26 13:31:04 florian
  1053. * some cg reorganisation
  1054. * some PPC updates
  1055. Revision 1.2 2001/08/26 13:29:33 florian
  1056. * some cg reorganisation
  1057. * some PPC updates
  1058. Revision 1.1 2000/07/13 06:30:12 michael
  1059. + Initial import
  1060. Revision 1.12 2000/04/22 14:25:04 jonas
  1061. * aasm.pas: pai_align instead of pai_align_abstract if cpu <> i386
  1062. + systems.pas: info for macos/ppc
  1063. * new/cgobj.pas: compiles again without newst define
  1064. * new/powerpc/cgcpu: generate different entry/exit code depending on
  1065. whether target_os is MacOs or Linux
  1066. Revision 1.11 2000/01/07 01:14:57 peter
  1067. * updated copyright to 2000
  1068. Revision 1.10 1999/12/24 22:48:10 jonas
  1069. * compiles again
  1070. Revision 1.9 1999/11/05 07:05:56 jonas
  1071. + a_jmp_cond()
  1072. Revision 1.8 1999/10/24 09:22:18 jonas
  1073. + entry/exitcode for SystemV (Linux) and AIX/Mac from the Altivec
  1074. PIM (no AltiVec support yet though)
  1075. * small fix to the a_cmp_* methods
  1076. Revision 1.7 1999/10/20 12:23:24 jonas
  1077. * fixed a_loadaddress_ref_reg (mentioned as ToDo in rev. 1.5)
  1078. * small bugfix in a_load_store
  1079. Revision 1.6 1999/09/15 20:35:47 florian
  1080. * small fix to operator overloading when in MMX mode
  1081. + the compiler uses now fldz and fld1 if possible
  1082. + some fixes to floating point registers
  1083. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1084. * .... ???
  1085. Revision 1.5 1999/09/03 13:14:11 jonas
  1086. + implemented some parameter passing methods, but they require
  1087. some more helper routines
  1088. * fix for loading symbol addresses (still needs to be done in a_loadaddress)
  1089. * several changes to the way conditional branches are handled
  1090. Revision 1.4 1999/08/26 14:53:41 jonas
  1091. * first implementation of concatcopy (requires 4 scratch regs)
  1092. Revision 1.3 1999/08/25 12:00:23 jonas
  1093. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1094. Revision 1.2 1999/08/18 17:05:57 florian
  1095. + implemented initilizing of data for the new code generator
  1096. so it should compile now simple programs
  1097. Revision 1.1 1999/08/06 16:41:11 jonas
  1098. * PowerPC compiles again, several routines implemented in cgcpu.pas
  1099. * added constant to cpubase of alpha and powerpc for maximum
  1100. number of operands
  1101. }