cgcpu.pas 70 KB

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