cgcpu.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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. const
  958. macosLinkageAreaSize = 24;
  959. registerSaveAreaMaxSize = 19*4 + 18*8;
  960. procedure save_fp_regs(list : taasmoutput);
  961. var regcounter: TRegister;
  962. href : treference;
  963. offset: integer;
  964. begin
  965. offset:= 0;
  966. for regcounter := R_F14 to R_F31 do
  967. begin
  968. offset:= offset - 8;
  969. reference_reset_base(href, STACK_POINTER_REG, offset);
  970. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  971. end;
  972. end;
  973. procedure restore_fp_regs(list : taasmoutput);
  974. var regcounter: TRegister;
  975. href : treference;
  976. offset: integer;
  977. begin
  978. offset:= 0;
  979. for regcounter := R_F14 to R_F31 do
  980. begin
  981. offset:= offset - 8;
  982. reference_reset_base(href, STACK_POINTER_REG, offset);
  983. list.concat(taicpu.op_reg_ref(A_LFD, regcounter, href));
  984. end;
  985. end;
  986. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  987. { generated the entry code of a procedure/function. Note: localsize is the }
  988. { sum of the size necessary for local variables and the maximum possible }
  989. { combined size of ALL the parameters of a procedure called by the current }
  990. { one }
  991. var regcounter: TRegister;
  992. href : treference;
  993. begin
  994. if (localsize mod 8) <> 0 then internalerror(58991);
  995. { CR and LR only have to be saved in case they are modified by the current }
  996. { procedure, but currently this isn't checked, so save them always }
  997. { following is the entry code as described in "Altivec Programming }
  998. { Interface Manual", bar the saving of AltiVec registers }
  999. a_reg_alloc(list,STACK_POINTER_REG);
  1000. a_reg_alloc(list,R_0);
  1001. { allocate registers containing reg parameters }
  1002. for regcounter := R_3 to R_10 do
  1003. a_reg_alloc(list,regcounter);
  1004. { save return address... }
  1005. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  1006. { ... in caller's frame }
  1007. reference_reset_base(href,STACK_POINTER_REG,8);
  1008. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  1009. a_reg_dealloc(list,R_0);
  1010. { save floating-point registers }
  1011. { !!! has to be optimized: only save registers that are used }
  1012. save_fp_regs(list);
  1013. (* a_call_name(list,'_savef14'); *)
  1014. { save gprs in gpr save area }
  1015. { !!! has to be optimized: only save registers that are used }
  1016. reference_reset_base(href,STACK_POINTER_REG,-220);
  1017. list.concat(taicpu.op_reg_ref(A_STMW,R_13,href));
  1018. { save the CR if necessary ( !!! always done currently ) }
  1019. a_reg_alloc(list,R_0);
  1020. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  1021. reference_reset_base(href,stack_pointer_reg,LA_CR);
  1022. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  1023. a_reg_dealloc(list,R_0);
  1024. { save pointer to incoming arguments }
  1025. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1026. (*
  1027. a_reg_alloc(list,R_12);
  1028. { 0 or 8 based on SP alignment }
  1029. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1030. R_12,STACK_POINTER_REG,0,28,28));
  1031. { add in stack length }
  1032. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1033. -localsize));
  1034. { establish new alignment }
  1035. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1036. a_reg_dealloc(list,R_12);
  1037. *)
  1038. { allocate stack frame }
  1039. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaMaxSize, 16);
  1040. reference_reset_base(href,R_1,-localsize);
  1041. a_load_store(list,A_STWU,R_1,href);
  1042. { now comes the AltiVec context save, not yet implemented !!! }
  1043. end;
  1044. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1045. var
  1046. regcounter: TRegister;
  1047. href : treference;
  1048. begin
  1049. { release parameter registers }
  1050. for regcounter := R_3 to R_10 do
  1051. a_reg_dealloc(list,regcounter);
  1052. { AltiVec context restore, not yet implemented !!! }
  1053. { restore SP }
  1054. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER_REG,R_31,0));
  1055. { restore the CR if necessary ( !!! always done currently ) }
  1056. a_reg_alloc(list,R_0);
  1057. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_CR));
  1058. reference_reset_base(href,stack_pointer_reg,LA_CR);
  1059. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1060. a_reg_dealloc(list,R_0);
  1061. { restore gprs }
  1062. reference_reset_base(href,STACK_POINTER_REG,-220);
  1063. list.concat(taicpu.op_reg_ref(A_LMW,R_13,href));
  1064. { restore return address ... }
  1065. reference_reset_base(href,STACK_POINTER_REG,8);
  1066. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1067. (*
  1068. { ... and return from _restf14 }
  1069. list.concat(taicpu.op_sym_ofs(A_B,objectlibrary.newasmsymbol('_restf14'),0));
  1070. *)
  1071. { restore fp registers }
  1072. restore_fp_regs(list);
  1073. { return to caller }
  1074. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1075. list.concat(taicpu.op_none(A_BLR));
  1076. end;
  1077. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1078. begin
  1079. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1080. end;
  1081. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1082. var
  1083. ref2, tmpref: treference;
  1084. freereg: boolean;
  1085. begin
  1086. ref2 := ref;
  1087. freereg := fixref(list,ref2);
  1088. if assigned(ref2.symbol) then
  1089. { add the symbol's value to the base of the reference, and if the }
  1090. { reference doesn't have a base, create one }
  1091. begin
  1092. reference_reset(tmpref);
  1093. tmpref.offset := ref2.offset;
  1094. tmpref.symbol := ref2.symbol;
  1095. tmpref.symaddr := refs_ha;
  1096. if ref2.base <> R_NO then
  1097. begin
  1098. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1099. ref2.base,tmpref));
  1100. if freereg then
  1101. begin
  1102. cg.free_scratch_reg(list,ref2.base);
  1103. freereg := false;
  1104. end;
  1105. end
  1106. else
  1107. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1108. tmpref.base := R_NO;
  1109. tmpref.symaddr := refs_l;
  1110. { can be folded with one of the next instructions by the }
  1111. { optimizer probably }
  1112. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1113. end
  1114. else if ref2.offset <> 0 Then
  1115. if ref2.base <> R_NO then
  1116. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1117. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1118. { occurs, so now only ref.offset has to be loaded }
  1119. else a_load_const_reg(list,OS_32,ref2.offset,r)
  1120. else if ref.index <> R_NO Then
  1121. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1122. else if (ref2.base <> R_NO) and
  1123. (r <> ref2.base) then
  1124. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1125. if freereg then
  1126. cg.free_scratch_reg(list,ref2.base);
  1127. end;
  1128. { ************* concatcopy ************ }
  1129. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1130. var
  1131. countreg: TRegister;
  1132. src, dst: TReference;
  1133. lab: tasmlabel;
  1134. count, count2: aword;
  1135. orgsrc, orgdst: boolean;
  1136. begin
  1137. {$ifdef extdebug}
  1138. if len > high(longint) then
  1139. internalerror(2002072704);
  1140. {$endif extdebug}
  1141. { make sure short loads are handled as optimally as possible }
  1142. if not loadref then
  1143. if (len <= 8) and
  1144. (byte(len) in [1,2,4,8]) then
  1145. begin
  1146. if len < 8 then
  1147. begin
  1148. a_load_ref_ref(list,int_cgsize(len),source,dest);
  1149. if delsource then
  1150. reference_release(list,source);
  1151. end
  1152. else
  1153. begin
  1154. a_reg_alloc(list,R_F0);
  1155. a_loadfpu_ref_reg(list,OS_F64,source,R_F0);
  1156. if delsource then
  1157. reference_release(list,source);
  1158. a_loadfpu_reg_ref(list,OS_F64,R_F0,dest);
  1159. a_reg_dealloc(list,R_F0);
  1160. end;
  1161. exit;
  1162. end;
  1163. reference_reset(src);
  1164. reference_reset(dst);
  1165. { load the address of source into src.base }
  1166. if loadref then
  1167. begin
  1168. src.base := get_scratch_reg_address(list);
  1169. a_load_ref_reg(list,OS_32,source,src.base);
  1170. orgsrc := false;
  1171. end
  1172. else if not issimpleref(source) or
  1173. ((source.index <> R_NO) and
  1174. ((source.offset + longint(len)) > high(smallint))) then
  1175. begin
  1176. src.base := get_scratch_reg_address(list);
  1177. a_loadaddr_ref_reg(list,source,src.base);
  1178. orgsrc := false;
  1179. end
  1180. else
  1181. begin
  1182. src := source;
  1183. orgsrc := true;
  1184. end;
  1185. if not orgsrc and delsource then
  1186. reference_release(list,source);
  1187. { load the address of dest into dst.base }
  1188. if not issimpleref(dest) or
  1189. ((dest.index <> R_NO) and
  1190. ((dest.offset + longint(len)) > high(smallint))) then
  1191. begin
  1192. dst.base := get_scratch_reg_address(list);
  1193. a_loadaddr_ref_reg(list,dest,dst.base);
  1194. orgdst := false;
  1195. end
  1196. else
  1197. begin
  1198. dst := dest;
  1199. orgdst := true;
  1200. end;
  1201. count := len div 8;
  1202. if count > 4 then
  1203. { generate a loop }
  1204. begin
  1205. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1206. { have to be set to 8. I put an Inc there so debugging may be }
  1207. { easier (should offset be different from zero here, it will be }
  1208. { easy to notice in the generated assembler }
  1209. inc(dst.offset,8);
  1210. inc(src.offset,8);
  1211. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1212. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1213. countreg := get_scratch_reg_int(list);
  1214. a_load_const_reg(list,OS_32,count,countreg);
  1215. { explicitely allocate R_0 since it can be used safely here }
  1216. { (for holding date that's being copied) }
  1217. a_reg_alloc(list,R_F0);
  1218. objectlibrary.getlabel(lab);
  1219. a_label(list, lab);
  1220. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1221. list.concat(taicpu.op_reg_ref(A_LFDU,R_F0,src));
  1222. list.concat(taicpu.op_reg_ref(A_STFDU,R_F0,dst));
  1223. a_jmp(list,A_BC,C_NE,0,lab);
  1224. free_scratch_reg(list,countreg);
  1225. a_reg_dealloc(list,R_F0);
  1226. len := len mod 8;
  1227. end;
  1228. count := len div 8;
  1229. if count > 0 then
  1230. { unrolled loop }
  1231. begin
  1232. a_reg_alloc(list,R_F0);
  1233. for count2 := 1 to count do
  1234. begin
  1235. a_loadfpu_ref_reg(list,OS_F64,src,R_F0);
  1236. a_loadfpu_reg_ref(list,OS_F64,R_F0,dst);
  1237. inc(src.offset,8);
  1238. inc(dst.offset,8);
  1239. end;
  1240. a_reg_dealloc(list,R_F0);
  1241. len := len mod 8;
  1242. end;
  1243. if (len and 4) <> 0 then
  1244. begin
  1245. a_reg_alloc(list,R_0);
  1246. a_load_ref_reg(list,OS_32,src,R_0);
  1247. a_load_reg_ref(list,OS_32,R_0,dst);
  1248. inc(src.offset,4);
  1249. inc(dst.offset,4);
  1250. a_reg_dealloc(list,R_0);
  1251. end;
  1252. { copy the leftovers }
  1253. if (len and 2) <> 0 then
  1254. begin
  1255. a_reg_alloc(list,R_0);
  1256. a_load_ref_reg(list,OS_16,src,R_0);
  1257. a_load_reg_ref(list,OS_16,R_0,dst);
  1258. inc(src.offset,2);
  1259. inc(dst.offset,2);
  1260. a_reg_dealloc(list,R_0);
  1261. end;
  1262. if (len and 1) <> 0 then
  1263. begin
  1264. a_reg_alloc(list,R_0);
  1265. a_load_ref_reg(list,OS_8,src,R_0);
  1266. a_load_reg_ref(list,OS_8,R_0,dst);
  1267. a_reg_dealloc(list,R_0);
  1268. end;
  1269. if orgsrc then
  1270. begin
  1271. if delsource then
  1272. reference_release(list,source);
  1273. end
  1274. else
  1275. free_scratch_reg(list,src.base);
  1276. if not orgdst then
  1277. free_scratch_reg(list,dst.base);
  1278. end;
  1279. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  1280. var
  1281. hl : tasmlabel;
  1282. begin
  1283. if not(cs_check_overflow in aktlocalswitches) then
  1284. exit;
  1285. objectlibrary.getlabel(hl);
  1286. if not ((p.resulttype.def.deftype=pointerdef) or
  1287. ((p.resulttype.def.deftype=orddef) and
  1288. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1289. bool8bit,bool16bit,bool32bit]))) then
  1290. begin
  1291. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  1292. a_jmp(list,A_BC,C_OV,7,hl)
  1293. end
  1294. else
  1295. a_jmp_cond(list,OC_AE,hl);
  1296. a_call_name(list,'FPC_OVERFLOW');
  1297. a_label(list,hl);
  1298. end;
  1299. {***************** This is private property, keep out! :) *****************}
  1300. function tcgppc.issimpleref(const ref: treference): boolean;
  1301. begin
  1302. if (ref.base = R_NO) and
  1303. (ref.index <> R_NO) then
  1304. internalerror(200208101);
  1305. result :=
  1306. not(assigned(ref.symbol)) and
  1307. (((ref.index = R_NO) and
  1308. (ref.offset >= low(smallint)) and
  1309. (ref.offset <= high(smallint))) or
  1310. ((ref.index <> R_NO) and
  1311. (ref.offset = 0)));
  1312. end;
  1313. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1314. var
  1315. tmpreg: tregister;
  1316. begin
  1317. result := false;
  1318. if (ref.base <> R_NO) then
  1319. begin
  1320. if (ref.index <> R_NO) and
  1321. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1322. begin
  1323. result := true;
  1324. tmpreg := cg.get_scratch_reg_int(list);
  1325. if not assigned(ref.symbol) and
  1326. (cardinal(ref.offset-low(smallint)) <=
  1327. high(smallint)-low(smallint)) then
  1328. begin
  1329. list.concat(taicpu.op_reg_reg_const(
  1330. A_ADDI,tmpreg,ref.base,ref.offset));
  1331. ref.offset := 0;
  1332. end
  1333. else
  1334. begin
  1335. list.concat(taicpu.op_reg_reg_reg(
  1336. A_ADD,tmpreg,ref.base,ref.index));
  1337. ref.index := R_NO;
  1338. end;
  1339. ref.base := tmpreg;
  1340. end
  1341. end
  1342. else
  1343. if ref.index <> R_NO then
  1344. internalerror(200208102);
  1345. end;
  1346. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1347. { that's the case, we can use rlwinm to do an AND operation }
  1348. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1349. var
  1350. temp, testbit: longint;
  1351. compare: boolean;
  1352. begin
  1353. get_rlwi_const := false;
  1354. if (a = 0) or (a = $ffffffff) then
  1355. exit;
  1356. { start with the lowest bit }
  1357. testbit := 1;
  1358. { check its value }
  1359. compare := boolean(a and testbit);
  1360. { find out how long the run of bits with this value is }
  1361. { (it's impossible that all bits are 1 or 0, because in that case }
  1362. { this function wouldn't have been called) }
  1363. l1 := 31;
  1364. while (((a and testbit) <> 0) = compare) do
  1365. begin
  1366. testbit := testbit shl 1;
  1367. dec(l1);
  1368. end;
  1369. { check the length of the run of bits that comes next }
  1370. compare := not compare;
  1371. l2 := l1;
  1372. while (((a and testbit) <> 0) = compare) and
  1373. (l2 >= 0) do
  1374. begin
  1375. testbit := testbit shl 1;
  1376. dec(l2);
  1377. end;
  1378. { and finally the check whether the rest of the bits all have the }
  1379. { same value }
  1380. compare := not compare;
  1381. temp := l2;
  1382. if temp >= 0 then
  1383. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1384. exit;
  1385. { we have done "not(not(compare))", so compare is back to its }
  1386. { initial value. If the lowest bit was 0, a is of the form }
  1387. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1388. { because l2 now contains the position of the last zero of the }
  1389. { first run instead of that of the first 1) so switch l1 and l2 }
  1390. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1391. if not compare then
  1392. begin
  1393. temp := l1;
  1394. l1 := l2+1;
  1395. l2 := temp;
  1396. end
  1397. else
  1398. { otherwise, l1 currently contains the position of the last }
  1399. { zero instead of that of the first 1 of the second run -> +1 }
  1400. inc(l1);
  1401. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1402. l1 := l1 and 31;
  1403. l2 := l2 and 31;
  1404. get_rlwi_const := true;
  1405. end;
  1406. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1407. ref: treference);
  1408. var
  1409. tmpreg: tregister;
  1410. tmpref: treference;
  1411. begin
  1412. tmpreg := R_NO;
  1413. if assigned(ref.symbol) or
  1414. (cardinal(ref.offset-low(smallint)) >
  1415. high(smallint)-low(smallint)) then
  1416. begin
  1417. tmpreg := get_scratch_reg_address(list);
  1418. reference_reset(tmpref);
  1419. tmpref.symbol := ref.symbol;
  1420. tmpref.offset := ref.offset;
  1421. tmpref.symaddr := refs_ha;
  1422. if ref.base <> R_NO then
  1423. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1424. ref.base,tmpref))
  1425. else
  1426. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1427. ref.base := tmpreg;
  1428. ref.symaddr := refs_l;
  1429. end;
  1430. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1431. if (tmpreg <> R_NO) then
  1432. free_scratch_reg(list,tmpreg);
  1433. end;
  1434. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1435. crval: longint; l: tasmlabel);
  1436. var
  1437. p: taicpu;
  1438. begin
  1439. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  1440. if op <> A_B then
  1441. create_cond_norm(c,crval,p.condition);
  1442. p.is_jmp := true;
  1443. list.concat(p)
  1444. end;
  1445. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1446. begin
  1447. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1448. end;
  1449. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1450. begin
  1451. a_op64_const_reg_reg(list,op,value,reg,reg);
  1452. end;
  1453. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1454. begin
  1455. case op of
  1456. OP_AND,OP_OR,OP_XOR:
  1457. begin
  1458. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1459. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1460. end;
  1461. OP_ADD:
  1462. begin
  1463. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1464. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1465. end;
  1466. OP_SUB:
  1467. begin
  1468. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1469. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1470. end;
  1471. else
  1472. internalerror(2002072801);
  1473. end;
  1474. end;
  1475. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  1476. const
  1477. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1478. (A_SUBIC,A_SUBC,A_ADDME));
  1479. var
  1480. tmpreg: tregister;
  1481. tmpreg64: tregister64;
  1482. issub: boolean;
  1483. begin
  1484. case op of
  1485. OP_AND,OP_OR,OP_XOR:
  1486. begin
  1487. cg.a_op_const_reg_reg(list,op,OS_32,cardinal(value),regsrc.reglo,regdst.reglo);
  1488. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1489. regdst.reghi);
  1490. end;
  1491. OP_ADD, OP_SUB:
  1492. begin
  1493. if (longint(value) <> 0) then
  1494. begin
  1495. issub := op = OP_SUB;
  1496. if (longint(value)-ord(issub) >= -32768) and
  1497. (longint(value)-ord(issub) <= 32767) then
  1498. begin
  1499. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1500. regdst.reglo,regsrc.reglo,longint(value)));
  1501. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1502. regdst.reghi,regsrc.reghi));
  1503. end
  1504. else if ((value shr 32) = 0) then
  1505. begin
  1506. tmpreg := cg.get_scratch_reg_int(list);
  1507. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  1508. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1509. regdst.reglo,regsrc.reglo,tmpreg));
  1510. cg.free_scratch_reg(list,tmpreg);
  1511. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1512. regdst.reghi,regsrc.reghi));
  1513. end
  1514. else
  1515. begin
  1516. tmpreg64.reglo := cg.get_scratch_reg_int(list);
  1517. tmpreg64.reghi := cg.get_scratch_reg_int(list);
  1518. a_load64_const_reg(list,value,tmpreg64);
  1519. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  1520. cg.free_scratch_reg(list,tmpreg64.reghi);
  1521. cg.free_scratch_reg(list,tmpreg64.reglo);
  1522. end
  1523. end
  1524. else
  1525. begin
  1526. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1527. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1528. regdst.reghi);
  1529. end;
  1530. end;
  1531. else
  1532. internalerror(2002072802);
  1533. end;
  1534. end;
  1535. begin
  1536. cg := tcgppc.create;
  1537. cg64 :=tcg64fppc.create;
  1538. end.
  1539. {
  1540. $Log$
  1541. Revision 1.62 2002-10-19 23:51:48 olle
  1542. * macos stack frame size computing updated
  1543. + macos epilogue: control register now restored
  1544. * macos prologue and epilogue: fp reg now saved and restored
  1545. Revision 1.61 2002/10/19 12:50:36 olle
  1546. * reorganized prologue and epilogue routines
  1547. Revision 1.60 2002/10/02 21:49:51 florian
  1548. * all A_BL instructions replaced by calls to a_call_name
  1549. Revision 1.59 2002/10/02 13:24:58 jonas
  1550. * changed a_call_* so that no superfluous code is generated anymore
  1551. Revision 1.58 2002/09/17 18:54:06 jonas
  1552. * a_load_reg_reg() now has two size parameters: source and dest. This
  1553. allows some optimizations on architectures that don't encode the
  1554. register size in the register name.
  1555. Revision 1.57 2002/09/10 21:22:25 jonas
  1556. + added some internal errors
  1557. * fixed bug in sysv exit code
  1558. Revision 1.56 2002/09/08 20:11:56 jonas
  1559. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  1560. Revision 1.55 2002/09/08 13:03:26 jonas
  1561. * several large offset-related fixes
  1562. Revision 1.54 2002/09/07 17:54:58 florian
  1563. * first part of PowerPC fixes
  1564. Revision 1.53 2002/09/07 15:25:14 peter
  1565. * old logs removed and tabs fixed
  1566. Revision 1.52 2002/09/02 10:14:51 jonas
  1567. + a_call_reg()
  1568. * small fix in a_call_ref()
  1569. Revision 1.51 2002/09/02 06:09:02 jonas
  1570. * fixed range error
  1571. Revision 1.50 2002/09/01 21:04:49 florian
  1572. * several powerpc related stuff fixed
  1573. Revision 1.49 2002/09/01 12:09:27 peter
  1574. + a_call_reg, a_call_loc added
  1575. * removed exprasmlist references
  1576. Revision 1.48 2002/08/31 21:38:02 jonas
  1577. * fixed a_call_ref (it should load ctr, not lr)
  1578. Revision 1.47 2002/08/31 21:30:45 florian
  1579. * fixed several problems caused by Jonas' commit :)
  1580. Revision 1.46 2002/08/31 19:25:50 jonas
  1581. + implemented a_call_ref()
  1582. Revision 1.45 2002/08/18 22:16:14 florian
  1583. + the ppc gas assembler writer adds now registers aliases
  1584. to the assembler file
  1585. Revision 1.44 2002/08/17 18:23:53 florian
  1586. * some assembler writer bugs fixed
  1587. Revision 1.43 2002/08/17 09:23:49 florian
  1588. * first part of procinfo rewrite
  1589. Revision 1.42 2002/08/16 14:24:59 carl
  1590. * issameref() to test if two references are the same (then emit no opcodes)
  1591. + ret_in_reg to replace ret_in_acc
  1592. (fix some register allocation bugs at the same time)
  1593. + save_std_register now has an extra parameter which is the
  1594. usedinproc registers
  1595. Revision 1.41 2002/08/15 08:13:54 carl
  1596. - a_load_sym_ofs_reg removed
  1597. * loadvmt now calls loadaddr_ref_reg instead
  1598. Revision 1.40 2002/08/11 14:32:32 peter
  1599. * renamed current_library to objectlibrary
  1600. Revision 1.39 2002/08/11 13:24:18 peter
  1601. * saving of asmsymbols in ppu supported
  1602. * asmsymbollist global is removed and moved into a new class
  1603. tasmlibrarydata that will hold the info of a .a file which
  1604. corresponds with a single module. Added librarydata to tmodule
  1605. to keep the library info stored for the module. In the future the
  1606. objectfiles will also be stored to the tasmlibrarydata class
  1607. * all getlabel/newasmsymbol and friends are moved to the new class
  1608. Revision 1.38 2002/08/11 11:39:31 jonas
  1609. + powerpc-specific genlinearlist
  1610. Revision 1.37 2002/08/10 17:15:31 jonas
  1611. * various fixes and optimizations
  1612. Revision 1.36 2002/08/06 20:55:23 florian
  1613. * first part of ppc calling conventions fix
  1614. Revision 1.35 2002/08/06 07:12:05 jonas
  1615. * fixed bug in g_flags2reg()
  1616. * and yet more constant operation fixes :)
  1617. Revision 1.34 2002/08/05 08:58:53 jonas
  1618. * fixed compilation problems
  1619. Revision 1.33 2002/08/04 12:57:55 jonas
  1620. * more misc. fixes, mostly constant-related
  1621. }