cgcpu.pas 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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_BCTRL));
  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_BCTRL));
  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. begin
  1182. if target_info.system = system_powerpc_macos then
  1183. begin
  1184. if ref2.base <> R_NO then
  1185. internalerror(2002103102); //TODO: Implement this if needed
  1186. reference_reset(tmpref);
  1187. tmpref.offset := ref2.offset;
  1188. tmpref.symbol := ref2.symbol;
  1189. tmpref.symaddr := refs_full;
  1190. tmpref.base := R_NO;
  1191. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,R_TOC,tmpref));
  1192. end
  1193. else
  1194. begin
  1195. { add the symbol's value to the base of the reference, and if the }
  1196. { reference doesn't have a base, create one }
  1197. reference_reset(tmpref);
  1198. tmpref.offset := ref2.offset;
  1199. tmpref.symbol := ref2.symbol;
  1200. tmpref.symaddr := refs_ha;
  1201. if ref2.base <> R_NO then
  1202. begin
  1203. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1204. ref2.base,tmpref));
  1205. if freereg then
  1206. begin
  1207. cg.free_scratch_reg(list,ref2.base);
  1208. freereg := false;
  1209. end;
  1210. end
  1211. else
  1212. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1213. tmpref.base := R_NO;
  1214. tmpref.symaddr := refs_l;
  1215. { can be folded with one of the next instructions by the }
  1216. { optimizer probably }
  1217. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1218. end
  1219. end
  1220. else if ref2.offset <> 0 Then
  1221. if ref2.base <> R_NO then
  1222. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1223. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1224. { occurs, so now only ref.offset has to be loaded }
  1225. else
  1226. a_load_const_reg(list,OS_32,ref2.offset,r)
  1227. else if ref.index <> R_NO Then
  1228. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1229. else if (ref2.base <> R_NO) and
  1230. (r <> ref2.base) then
  1231. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1232. if freereg then
  1233. cg.free_scratch_reg(list,ref2.base);
  1234. end;
  1235. { ************* concatcopy ************ }
  1236. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1237. var
  1238. countreg: TRegister;
  1239. src, dst: TReference;
  1240. lab: tasmlabel;
  1241. count, count2: aword;
  1242. orgsrc, orgdst: boolean;
  1243. begin
  1244. {$ifdef extdebug}
  1245. if len > high(longint) then
  1246. internalerror(2002072704);
  1247. {$endif extdebug}
  1248. { make sure short loads are handled as optimally as possible }
  1249. if not loadref then
  1250. if (len <= 8) and
  1251. (byte(len) in [1,2,4,8]) then
  1252. begin
  1253. if len < 8 then
  1254. begin
  1255. a_load_ref_ref(list,int_cgsize(len),source,dest);
  1256. if delsource then
  1257. reference_release(list,source);
  1258. end
  1259. else
  1260. begin
  1261. a_reg_alloc(list,R_F0);
  1262. a_loadfpu_ref_reg(list,OS_F64,source,R_F0);
  1263. if delsource then
  1264. reference_release(list,source);
  1265. a_loadfpu_reg_ref(list,OS_F64,R_F0,dest);
  1266. a_reg_dealloc(list,R_F0);
  1267. end;
  1268. exit;
  1269. end;
  1270. reference_reset(src);
  1271. reference_reset(dst);
  1272. { load the address of source into src.base }
  1273. if loadref then
  1274. begin
  1275. src.base := get_scratch_reg_address(list);
  1276. a_load_ref_reg(list,OS_32,source,src.base);
  1277. orgsrc := false;
  1278. end
  1279. else if not issimpleref(source) or
  1280. ((source.index <> R_NO) and
  1281. ((source.offset + longint(len)) > high(smallint))) then
  1282. begin
  1283. src.base := get_scratch_reg_address(list);
  1284. a_loadaddr_ref_reg(list,source,src.base);
  1285. orgsrc := false;
  1286. end
  1287. else
  1288. begin
  1289. src := source;
  1290. orgsrc := true;
  1291. end;
  1292. if not orgsrc and delsource then
  1293. reference_release(list,source);
  1294. { load the address of dest into dst.base }
  1295. if not issimpleref(dest) or
  1296. ((dest.index <> R_NO) and
  1297. ((dest.offset + longint(len)) > high(smallint))) then
  1298. begin
  1299. dst.base := get_scratch_reg_address(list);
  1300. a_loadaddr_ref_reg(list,dest,dst.base);
  1301. orgdst := false;
  1302. end
  1303. else
  1304. begin
  1305. dst := dest;
  1306. orgdst := true;
  1307. end;
  1308. count := len div 8;
  1309. if count > 4 then
  1310. { generate a loop }
  1311. begin
  1312. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1313. { have to be set to 8. I put an Inc there so debugging may be }
  1314. { easier (should offset be different from zero here, it will be }
  1315. { easy to notice in the generated assembler }
  1316. inc(dst.offset,8);
  1317. inc(src.offset,8);
  1318. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1319. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1320. countreg := get_scratch_reg_int(list);
  1321. a_load_const_reg(list,OS_32,count,countreg);
  1322. { explicitely allocate R_0 since it can be used safely here }
  1323. { (for holding date that's being copied) }
  1324. a_reg_alloc(list,R_F0);
  1325. objectlibrary.getlabel(lab);
  1326. a_label(list, lab);
  1327. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1328. list.concat(taicpu.op_reg_ref(A_LFDU,R_F0,src));
  1329. list.concat(taicpu.op_reg_ref(A_STFDU,R_F0,dst));
  1330. a_jmp(list,A_BC,C_NE,0,lab);
  1331. free_scratch_reg(list,countreg);
  1332. a_reg_dealloc(list,R_F0);
  1333. len := len mod 8;
  1334. end;
  1335. count := len div 8;
  1336. if count > 0 then
  1337. { unrolled loop }
  1338. begin
  1339. a_reg_alloc(list,R_F0);
  1340. for count2 := 1 to count do
  1341. begin
  1342. a_loadfpu_ref_reg(list,OS_F64,src,R_F0);
  1343. a_loadfpu_reg_ref(list,OS_F64,R_F0,dst);
  1344. inc(src.offset,8);
  1345. inc(dst.offset,8);
  1346. end;
  1347. a_reg_dealloc(list,R_F0);
  1348. len := len mod 8;
  1349. end;
  1350. if (len and 4) <> 0 then
  1351. begin
  1352. a_reg_alloc(list,R_0);
  1353. a_load_ref_reg(list,OS_32,src,R_0);
  1354. a_load_reg_ref(list,OS_32,R_0,dst);
  1355. inc(src.offset,4);
  1356. inc(dst.offset,4);
  1357. a_reg_dealloc(list,R_0);
  1358. end;
  1359. { copy the leftovers }
  1360. if (len and 2) <> 0 then
  1361. begin
  1362. a_reg_alloc(list,R_0);
  1363. a_load_ref_reg(list,OS_16,src,R_0);
  1364. a_load_reg_ref(list,OS_16,R_0,dst);
  1365. inc(src.offset,2);
  1366. inc(dst.offset,2);
  1367. a_reg_dealloc(list,R_0);
  1368. end;
  1369. if (len and 1) <> 0 then
  1370. begin
  1371. a_reg_alloc(list,R_0);
  1372. a_load_ref_reg(list,OS_8,src,R_0);
  1373. a_load_reg_ref(list,OS_8,R_0,dst);
  1374. a_reg_dealloc(list,R_0);
  1375. end;
  1376. if orgsrc then
  1377. begin
  1378. if delsource then
  1379. reference_release(list,source);
  1380. end
  1381. else
  1382. free_scratch_reg(list,src.base);
  1383. if not orgdst then
  1384. free_scratch_reg(list,dst.base);
  1385. end;
  1386. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  1387. var
  1388. hl : tasmlabel;
  1389. begin
  1390. if not(cs_check_overflow in aktlocalswitches) then
  1391. exit;
  1392. objectlibrary.getlabel(hl);
  1393. if not ((p.resulttype.def.deftype=pointerdef) or
  1394. ((p.resulttype.def.deftype=orddef) and
  1395. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1396. bool8bit,bool16bit,bool32bit]))) then
  1397. begin
  1398. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  1399. a_jmp(list,A_BC,C_OV,7,hl)
  1400. end
  1401. else
  1402. a_jmp_cond(list,OC_AE,hl);
  1403. a_call_name(list,'FPC_OVERFLOW');
  1404. a_label(list,hl);
  1405. end;
  1406. {***************** This is private property, keep out! :) *****************}
  1407. function tcgppc.issimpleref(const ref: treference): boolean;
  1408. begin
  1409. if (ref.base = R_NO) and
  1410. (ref.index <> R_NO) then
  1411. internalerror(200208101);
  1412. result :=
  1413. not(assigned(ref.symbol)) and
  1414. (((ref.index = R_NO) and
  1415. (ref.offset >= low(smallint)) and
  1416. (ref.offset <= high(smallint))) or
  1417. ((ref.index <> R_NO) and
  1418. (ref.offset = 0)));
  1419. end;
  1420. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1421. var
  1422. tmpreg: tregister;
  1423. begin
  1424. result := false;
  1425. if (ref.base <> R_NO) then
  1426. begin
  1427. if (ref.index <> R_NO) and
  1428. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1429. begin
  1430. result := true;
  1431. tmpreg := cg.get_scratch_reg_int(list);
  1432. if not assigned(ref.symbol) and
  1433. (cardinal(ref.offset-low(smallint)) <=
  1434. high(smallint)-low(smallint)) then
  1435. begin
  1436. list.concat(taicpu.op_reg_reg_const(
  1437. A_ADDI,tmpreg,ref.base,ref.offset));
  1438. ref.offset := 0;
  1439. end
  1440. else
  1441. begin
  1442. list.concat(taicpu.op_reg_reg_reg(
  1443. A_ADD,tmpreg,ref.base,ref.index));
  1444. ref.index := R_NO;
  1445. end;
  1446. ref.base := tmpreg;
  1447. end
  1448. end
  1449. else
  1450. if ref.index <> R_NO then
  1451. internalerror(200208102);
  1452. end;
  1453. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1454. { that's the case, we can use rlwinm to do an AND operation }
  1455. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1456. var
  1457. temp : longint;
  1458. testbit : aword;
  1459. compare: boolean;
  1460. begin
  1461. get_rlwi_const := false;
  1462. if (a = 0) or (a = $ffffffff) then
  1463. exit;
  1464. { start with the lowest bit }
  1465. testbit := 1;
  1466. { check its value }
  1467. compare := boolean(a and testbit);
  1468. { find out how long the run of bits with this value is }
  1469. { (it's impossible that all bits are 1 or 0, because in that case }
  1470. { this function wouldn't have been called) }
  1471. l1 := 31;
  1472. while (((a and testbit) <> 0) = compare) do
  1473. begin
  1474. testbit := testbit shl 1;
  1475. dec(l1);
  1476. end;
  1477. { check the length of the run of bits that comes next }
  1478. compare := not compare;
  1479. l2 := l1;
  1480. while (((a and testbit) <> 0) = compare) and
  1481. (l2 >= 0) do
  1482. begin
  1483. testbit := testbit shl 1;
  1484. dec(l2);
  1485. end;
  1486. { and finally the check whether the rest of the bits all have the }
  1487. { same value }
  1488. compare := not compare;
  1489. temp := l2;
  1490. if temp >= 0 then
  1491. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1492. exit;
  1493. { we have done "not(not(compare))", so compare is back to its }
  1494. { initial value. If the lowest bit was 0, a is of the form }
  1495. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1496. { because l2 now contains the position of the last zero of the }
  1497. { first run instead of that of the first 1) so switch l1 and l2 }
  1498. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1499. if not compare then
  1500. begin
  1501. temp := l1;
  1502. l1 := l2+1;
  1503. l2 := temp;
  1504. end
  1505. else
  1506. { otherwise, l1 currently contains the position of the last }
  1507. { zero instead of that of the first 1 of the second run -> +1 }
  1508. inc(l1);
  1509. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1510. l1 := l1 and 31;
  1511. l2 := l2 and 31;
  1512. get_rlwi_const := true;
  1513. end;
  1514. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1515. ref: treference);
  1516. var
  1517. tmpreg: tregister;
  1518. tmpref: treference;
  1519. begin
  1520. tmpreg := R_NO;
  1521. if assigned(ref.symbol) or
  1522. (cardinal(ref.offset-low(smallint)) >
  1523. high(smallint)-low(smallint)) then
  1524. begin
  1525. if target_info.system = system_powerpc_macos then
  1526. begin
  1527. if ref.base <> R_NO then
  1528. begin
  1529. {Generates
  1530. add tempreg, ref.base, RTOC
  1531. op reg, symbolplusoffset, tempreg
  1532. which is eqvivalent to the more comprehensive
  1533. addi tempreg, RTOC, symbolplusoffset
  1534. add tempreg, ref.base, RTOC
  1535. op reg, tempreg
  1536. but which saves one instruction.}
  1537. tmpreg := get_scratch_reg_address(list);
  1538. reference_reset(tmpref);
  1539. tmpref.symbol := ref.symbol;
  1540. tmpref.offset := ref.offset;
  1541. tmpref.symaddr := refs_full;
  1542. tmpref.base:= tmpreg;
  1543. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1544. ref.base,R_TOC));
  1545. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  1546. end
  1547. else
  1548. begin
  1549. reference_reset(tmpref);
  1550. tmpref.symbol := ref.symbol;
  1551. tmpref.offset := ref.offset;
  1552. tmpref.symaddr := refs_full;
  1553. tmpref.base:= R_TOC;
  1554. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  1555. end;
  1556. end
  1557. else
  1558. begin
  1559. tmpreg := get_scratch_reg_address(list);
  1560. reference_reset(tmpref);
  1561. tmpref.symbol := ref.symbol;
  1562. tmpref.offset := ref.offset;
  1563. tmpref.symaddr := refs_ha;
  1564. if ref.base <> R_NO then
  1565. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1566. ref.base,tmpref))
  1567. else
  1568. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1569. ref.base := tmpreg;
  1570. ref.symaddr := refs_l;
  1571. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1572. end
  1573. end
  1574. else
  1575. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1576. if (tmpreg <> R_NO) then
  1577. free_scratch_reg(list,tmpreg);
  1578. end;
  1579. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1580. crval: longint; l: tasmlabel);
  1581. var
  1582. p: taicpu;
  1583. begin
  1584. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  1585. if op <> A_B then
  1586. create_cond_norm(c,crval,p.condition);
  1587. p.is_jmp := true;
  1588. list.concat(p)
  1589. end;
  1590. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1591. begin
  1592. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1593. end;
  1594. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1595. begin
  1596. a_op64_const_reg_reg(list,op,value,reg,reg);
  1597. end;
  1598. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1599. begin
  1600. case op of
  1601. OP_AND,OP_OR,OP_XOR:
  1602. begin
  1603. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1604. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1605. end;
  1606. OP_ADD:
  1607. begin
  1608. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1609. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1610. end;
  1611. OP_SUB:
  1612. begin
  1613. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1614. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1615. end;
  1616. else
  1617. internalerror(2002072801);
  1618. end;
  1619. end;
  1620. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  1621. const
  1622. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1623. (A_SUBIC,A_SUBC,A_ADDME));
  1624. var
  1625. tmpreg: tregister;
  1626. tmpreg64: tregister64;
  1627. issub: boolean;
  1628. begin
  1629. case op of
  1630. OP_AND,OP_OR,OP_XOR:
  1631. begin
  1632. cg.a_op_const_reg_reg(list,op,OS_32,cardinal(value),regsrc.reglo,regdst.reglo);
  1633. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1634. regdst.reghi);
  1635. end;
  1636. OP_ADD, OP_SUB:
  1637. begin
  1638. if (longint(value) <> 0) then
  1639. begin
  1640. issub := op = OP_SUB;
  1641. if (longint(value)-ord(issub) >= -32768) and
  1642. (longint(value)-ord(issub) <= 32767) then
  1643. begin
  1644. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1645. regdst.reglo,regsrc.reglo,longint(value)));
  1646. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1647. regdst.reghi,regsrc.reghi));
  1648. end
  1649. else if ((value shr 32) = 0) then
  1650. begin
  1651. tmpreg := cg.get_scratch_reg_int(list);
  1652. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  1653. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1654. regdst.reglo,regsrc.reglo,tmpreg));
  1655. cg.free_scratch_reg(list,tmpreg);
  1656. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1657. regdst.reghi,regsrc.reghi));
  1658. end
  1659. else
  1660. begin
  1661. tmpreg64.reglo := cg.get_scratch_reg_int(list);
  1662. tmpreg64.reghi := cg.get_scratch_reg_int(list);
  1663. a_load64_const_reg(list,value,tmpreg64);
  1664. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  1665. cg.free_scratch_reg(list,tmpreg64.reghi);
  1666. cg.free_scratch_reg(list,tmpreg64.reglo);
  1667. end
  1668. end
  1669. else
  1670. begin
  1671. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1672. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1673. regdst.reghi);
  1674. end;
  1675. end;
  1676. else
  1677. internalerror(2002072802);
  1678. end;
  1679. end;
  1680. begin
  1681. cg := tcgppc.create;
  1682. cg64 :=tcg64fppc.create;
  1683. end.
  1684. {
  1685. $Log$
  1686. Revision 1.67 2002-12-15 19:22:01 florian
  1687. * fixed some crashes and a rte 201
  1688. Revision 1.66 2002/11/28 10:55:16 olle
  1689. * macos: changing code gen for references to globals
  1690. Revision 1.65 2002/11/07 15:50:23 jonas
  1691. * fixed bctr(l) problems
  1692. Revision 1.64 2002/11/04 18:24:19 olle
  1693. * macos: globals are located in TOC and relative r2, instead of absolute
  1694. Revision 1.63 2002/10/28 22:24:28 olle
  1695. * macos entry/exit: only used registers are saved
  1696. - macos entry/exit: stackptr not saved in r31 anymore
  1697. * macos entry/exit: misc fixes
  1698. Revision 1.62 2002/10/19 23:51:48 olle
  1699. * macos stack frame size computing updated
  1700. + macos epilogue: control register now restored
  1701. * macos prologue and epilogue: fp reg now saved and restored
  1702. Revision 1.61 2002/10/19 12:50:36 olle
  1703. * reorganized prologue and epilogue routines
  1704. Revision 1.60 2002/10/02 21:49:51 florian
  1705. * all A_BL instructions replaced by calls to a_call_name
  1706. Revision 1.59 2002/10/02 13:24:58 jonas
  1707. * changed a_call_* so that no superfluous code is generated anymore
  1708. Revision 1.58 2002/09/17 18:54:06 jonas
  1709. * a_load_reg_reg() now has two size parameters: source and dest. This
  1710. allows some optimizations on architectures that don't encode the
  1711. register size in the register name.
  1712. Revision 1.57 2002/09/10 21:22:25 jonas
  1713. + added some internal errors
  1714. * fixed bug in sysv exit code
  1715. Revision 1.56 2002/09/08 20:11:56 jonas
  1716. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  1717. Revision 1.55 2002/09/08 13:03:26 jonas
  1718. * several large offset-related fixes
  1719. Revision 1.54 2002/09/07 17:54:58 florian
  1720. * first part of PowerPC fixes
  1721. Revision 1.53 2002/09/07 15:25:14 peter
  1722. * old logs removed and tabs fixed
  1723. Revision 1.52 2002/09/02 10:14:51 jonas
  1724. + a_call_reg()
  1725. * small fix in a_call_ref()
  1726. Revision 1.51 2002/09/02 06:09:02 jonas
  1727. * fixed range error
  1728. Revision 1.50 2002/09/01 21:04:49 florian
  1729. * several powerpc related stuff fixed
  1730. Revision 1.49 2002/09/01 12:09:27 peter
  1731. + a_call_reg, a_call_loc added
  1732. * removed exprasmlist references
  1733. Revision 1.48 2002/08/31 21:38:02 jonas
  1734. * fixed a_call_ref (it should load ctr, not lr)
  1735. Revision 1.47 2002/08/31 21:30:45 florian
  1736. * fixed several problems caused by Jonas' commit :)
  1737. Revision 1.46 2002/08/31 19:25:50 jonas
  1738. + implemented a_call_ref()
  1739. Revision 1.45 2002/08/18 22:16:14 florian
  1740. + the ppc gas assembler writer adds now registers aliases
  1741. to the assembler file
  1742. Revision 1.44 2002/08/17 18:23:53 florian
  1743. * some assembler writer bugs fixed
  1744. Revision 1.43 2002/08/17 09:23:49 florian
  1745. * first part of procinfo rewrite
  1746. Revision 1.42 2002/08/16 14:24:59 carl
  1747. * issameref() to test if two references are the same (then emit no opcodes)
  1748. + ret_in_reg to replace ret_in_acc
  1749. (fix some register allocation bugs at the same time)
  1750. + save_std_register now has an extra parameter which is the
  1751. usedinproc registers
  1752. Revision 1.41 2002/08/15 08:13:54 carl
  1753. - a_load_sym_ofs_reg removed
  1754. * loadvmt now calls loadaddr_ref_reg instead
  1755. Revision 1.40 2002/08/11 14:32:32 peter
  1756. * renamed current_library to objectlibrary
  1757. Revision 1.39 2002/08/11 13:24:18 peter
  1758. * saving of asmsymbols in ppu supported
  1759. * asmsymbollist global is removed and moved into a new class
  1760. tasmlibrarydata that will hold the info of a .a file which
  1761. corresponds with a single module. Added librarydata to tmodule
  1762. to keep the library info stored for the module. In the future the
  1763. objectfiles will also be stored to the tasmlibrarydata class
  1764. * all getlabel/newasmsymbol and friends are moved to the new class
  1765. Revision 1.38 2002/08/11 11:39:31 jonas
  1766. + powerpc-specific genlinearlist
  1767. Revision 1.37 2002/08/10 17:15:31 jonas
  1768. * various fixes and optimizations
  1769. Revision 1.36 2002/08/06 20:55:23 florian
  1770. * first part of ppc calling conventions fix
  1771. Revision 1.35 2002/08/06 07:12:05 jonas
  1772. * fixed bug in g_flags2reg()
  1773. * and yet more constant operation fixes :)
  1774. Revision 1.34 2002/08/05 08:58:53 jonas
  1775. * fixed compilation problems
  1776. Revision 1.33 2002/08/04 12:57:55 jonas
  1777. * more misc. fixes, mostly constant-related
  1778. }