cgcpu.pas 66 KB

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