cgcpu.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  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. globtype,symtype,
  23. cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  26. parabase;
  27. type
  28. tcgppc = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. { passing parameters, per default the parameter is pushed }
  32. { nr gives the number of the parameter (enumerated from }
  33. { left to right), this allows to move the parameter to }
  34. { register, if the cpu supports register calling }
  35. { conventions }
  36. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : tcgpara);override;
  37. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  38. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);override;
  39. procedure a_call_name(list : taasmoutput;const s : string);override;
  40. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  41. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  42. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  43. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  44. size: tcgsize; a: aint; src, dst: tregister); override;
  45. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  46. size: tcgsize; src1, src2, dst: tregister); override;
  47. { move instructions }
  48. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aint;reg : tregister);override;
  49. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  50. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  51. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  52. { fpu move instructions }
  53. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  54. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  55. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  56. { comparison operations }
  57. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  58. l : tasmlabel);override;
  59. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  60. procedure a_jmp_name(list : taasmoutput;const s : string); override;
  61. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  62. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  63. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  64. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  65. procedure g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean); override;
  66. procedure g_save_standard_registers(list:Taasmoutput); override;
  67. procedure g_restore_standard_registers(list:Taasmoutput); override;
  68. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  69. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);override;
  70. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  71. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  72. { that's the case, we can use rlwinm to do an AND operation }
  73. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  74. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  75. private
  76. (* NOT IN USE: *)
  77. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  78. (* NOT IN USE: *)
  79. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  80. { Make sure ref is a valid reference for the PowerPC and sets the }
  81. { base to the value of the index if (base = R_NO). }
  82. { Returns true if the reference contained a base, index and an }
  83. { offset or symbol, in which case the base will have been changed }
  84. { to a tempreg (which has to be freed by the caller) containing }
  85. { the sum of part of the original reference }
  86. function fixref(list: taasmoutput; var ref: treference): boolean;
  87. { returns whether a reference can be used immediately in a powerpc }
  88. { instruction }
  89. function issimpleref(const ref: treference): boolean;
  90. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  91. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  92. ref: treference);
  93. { creates the correct branch instruction for a given combination }
  94. { of asmcondflags and destination addressing mode }
  95. procedure a_jmp(list: taasmoutput; op: tasmop;
  96. c: tasmcondflag; crval: longint; l: tasmlabel);
  97. function save_regs(list : taasmoutput):longint;
  98. procedure restore_regs(list : taasmoutput);
  99. function get_darwin_call_stub(const s: string): tasmsymbol;
  100. end;
  101. tcg64fppc = class(tcg64f32)
  102. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  103. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);override;
  104. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);override;
  105. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  106. end;
  107. const
  108. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  109. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  110. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  111. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  112. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  113. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  114. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  115. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  116. implementation
  117. uses
  118. globals,verbose,systems,cutils,
  119. symconst,symdef,symsym,
  120. rgobj,tgobj,cpupi,procinfo,paramgr;
  121. procedure tcgppc.init_register_allocators;
  122. begin
  123. inherited init_register_allocators;
  124. if target_info.system=system_powerpc_darwin then
  125. begin
  126. if pi_needs_got in current_procinfo.flags then
  127. begin
  128. current_procinfo.got:=NR_R31;
  129. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  130. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  131. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  132. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  133. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  134. RS_R14,RS_R13],first_int_imreg,[]);
  135. end
  136. else
  137. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  138. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  139. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  140. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  141. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  142. RS_R14,RS_R13],first_int_imreg,[]);
  143. end
  144. else
  145. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  146. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  147. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  148. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  149. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  150. RS_R14,RS_R13],first_int_imreg,[]);
  151. case target_info.abi of
  152. abi_powerpc_aix:
  153. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  154. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  155. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  156. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  157. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  158. abi_powerpc_sysv:
  159. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  160. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  161. RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,RS_F26,RS_F25,RS_F24,RS_F23,
  162. RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,RS_F17,RS_F16,RS_F15,RS_F14,
  163. RS_F13,RS_F12,RS_F11,RS_F10],first_fpu_imreg,[]);
  164. else
  165. internalerror(2003122903);
  166. end;
  167. {$warning FIX ME}
  168. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  169. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  170. end;
  171. procedure tcgppc.done_register_allocators;
  172. begin
  173. rg[R_INTREGISTER].free;
  174. rg[R_FPUREGISTER].free;
  175. rg[R_MMREGISTER].free;
  176. inherited done_register_allocators;
  177. end;
  178. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : tcgpara);
  179. var
  180. ref: treference;
  181. begin
  182. paraloc.check_simple_location;
  183. case paraloc.location^.loc of
  184. LOC_REGISTER,LOC_CREGISTER:
  185. a_load_const_reg(list,size,a,paraloc.location^.register);
  186. LOC_REFERENCE:
  187. begin
  188. reference_reset(ref);
  189. ref.base:=paraloc.location^.reference.index;
  190. ref.offset:=paraloc.location^.reference.offset;
  191. a_load_const_ref(list,size,a,ref);
  192. end;
  193. else
  194. internalerror(2002081101);
  195. end;
  196. end;
  197. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : tcgpara);
  198. var
  199. ref: treference;
  200. tmpreg: tregister;
  201. begin
  202. paraloc.check_simple_location;
  203. case paraloc.location^.loc of
  204. LOC_REGISTER,LOC_CREGISTER:
  205. a_load_ref_reg(list,size,size,r,paraloc.location^.register);
  206. LOC_REFERENCE:
  207. begin
  208. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  209. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  210. a_load_ref_reg(list,size,size,r,tmpreg);
  211. a_load_reg_ref(list,size,size,tmpreg,ref);
  212. end;
  213. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  214. case size of
  215. OS_F32, OS_F64:
  216. a_loadfpu_ref_reg(list,size,r,paraloc.location^.register);
  217. else
  218. internalerror(2002072801);
  219. end;
  220. else
  221. internalerror(2002081103);
  222. end;
  223. end;
  224. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);
  225. var
  226. ref: treference;
  227. tmpreg: tregister;
  228. begin
  229. paraloc.check_simple_location;
  230. case paraloc.location^.loc of
  231. LOC_REGISTER,LOC_CREGISTER:
  232. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  233. LOC_REFERENCE:
  234. begin
  235. reference_reset(ref);
  236. ref.base := paraloc.location^.reference.index;
  237. ref.offset := paraloc.location^.reference.offset;
  238. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  239. a_loadaddr_ref_reg(list,r,tmpreg);
  240. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  241. end;
  242. else
  243. internalerror(2002080701);
  244. end;
  245. end;
  246. function tcgppc.get_darwin_call_stub(const s: string): tasmsymbol;
  247. var
  248. stubname: string;
  249. href: treference;
  250. l1: tasmsymbol;
  251. begin
  252. { function declared in the current unit? }
  253. result := objectlibrary.getasmsymbol(s);
  254. if not(assigned(result)) then
  255. begin
  256. stubname := 'L'+s+'$stub';
  257. result := objectlibrary.getasmsymbol(stubname);
  258. end;
  259. if assigned(result) then
  260. exit;
  261. if not(assigned(importssection)) then
  262. importssection:=TAAsmoutput.create;
  263. importsSection.concat(Tai_section.Create(sec_data,'',0));
  264. importsSection.concat(Tai_direct.create(strpnew('.section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16')));
  265. importsSection.concat(Tai_align.Create(4));
  266. result := objectlibrary.newasmsymbol(stubname,AB_EXTERNAL,AT_FUNCTION);
  267. importsSection.concat(Tai_symbol.Create(result,0));
  268. importsSection.concat(Tai_direct.create(strpnew((#9+'.indirect_symbol ')+s)));
  269. l1 := objectlibrary.newasmsymbol('L'+s+'$lazy_ptr',AB_EXTERNAL,AT_FUNCTION);
  270. reference_reset_symbol(href,l1,0);
  271. {$ifdef powerpc}
  272. href.refaddr := addr_hi;
  273. importsSection.concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  274. href.refaddr := addr_lo;
  275. href.base := NR_R11;
  276. importsSection.concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  277. importsSection.concat(taicpu.op_reg(A_MTCTR,NR_R12));
  278. importsSection.concat(taicpu.op_none(A_BCTR));
  279. {$else powerpc}
  280. internalerror(2004010502);
  281. {$endif powerpc}
  282. importsSection.concat(Tai_section.Create(sec_data,'',0));
  283. importsSection.concat(Tai_direct.create(strpnew('.lazy_symbol_pointer')));
  284. importsSection.concat(Tai_symbol.Create(l1,0));
  285. importsSection.concat(Tai_direct.create(strpnew((#9+'.indirect_symbol ')+s)));
  286. importsSection.concat(tai_const.createname(strpnew('dyld_stub_binding_helper'),AT_FUNCTION,0));
  287. end;
  288. { calling a procedure by name }
  289. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  290. var
  291. href : treference;
  292. begin
  293. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  294. if it is a cross-TOC call. If so, it also replaces the NOP
  295. with some restore code.}
  296. if (target_info.system <> system_powerpc_darwin) then
  297. begin
  298. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  299. if target_info.system=system_powerpc_macos then
  300. list.concat(taicpu.op_none(A_NOP));
  301. end
  302. else
  303. begin
  304. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  305. end;
  306. {
  307. the compiler does not properly set this flag anymore in pass 1, and
  308. for now we only need it after pass 2 (I hope) (JM)
  309. if not(pi_do_call in current_procinfo.flags) then
  310. internalerror(2003060703);
  311. }
  312. include(current_procinfo.flags,pi_do_call);
  313. end;
  314. { calling a procedure by address }
  315. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  316. var
  317. tmpreg : tregister;
  318. tmpref : treference;
  319. begin
  320. if target_info.system=system_powerpc_macos then
  321. begin
  322. {Generate instruction to load the procedure address from
  323. the transition vector.}
  324. //TODO: Support cross-TOC calls.
  325. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  326. reference_reset(tmpref);
  327. tmpref.offset := 0;
  328. //tmpref.symaddr := refs_full;
  329. tmpref.base:= reg;
  330. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  331. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  332. end
  333. else
  334. list.concat(taicpu.op_reg(A_MTCTR,reg));
  335. list.concat(taicpu.op_none(A_BCTRL));
  336. //if target_info.system=system_powerpc_macos then
  337. // //NOP is not needed here.
  338. // list.concat(taicpu.op_none(A_NOP));
  339. include(current_procinfo.flags,pi_do_call);
  340. {
  341. if not(pi_do_call in current_procinfo.flags) then
  342. internalerror(2003060704);
  343. }
  344. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  345. end;
  346. {********************** load instructions ********************}
  347. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aint; reg : TRegister);
  348. begin
  349. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  350. internalerror(2002090902);
  351. if (a >= low(smallint)) and
  352. (a <= high(smallint)) then
  353. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  354. else if ((a and $ffff) <> 0) then
  355. begin
  356. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  357. if ((a shr 16) <> 0) or
  358. (smallint(a and $ffff) < 0) then
  359. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  360. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  361. end
  362. else
  363. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  364. end;
  365. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  366. const
  367. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  368. { indexed? updating?}
  369. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  370. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  371. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  372. var
  373. op: TAsmOp;
  374. ref2: TReference;
  375. begin
  376. ref2 := ref;
  377. fixref(list,ref2);
  378. if tosize in [OS_S8..OS_S16] then
  379. { storing is the same for signed and unsigned values }
  380. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  381. { 64 bit stuff should be handled separately }
  382. if tosize in [OS_64,OS_S64] then
  383. internalerror(200109236);
  384. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  385. a_load_store(list,op,reg,ref2);
  386. End;
  387. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  388. const
  389. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  390. { indexed? updating?}
  391. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  392. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  393. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  394. { 64bit stuff should be handled separately }
  395. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  396. { 128bit stuff too }
  397. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  398. { there's no load-byte-with-sign-extend :( }
  399. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  400. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  401. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  402. var
  403. op: tasmop;
  404. tmpreg: tregister;
  405. ref2, tmpref: treference;
  406. begin
  407. { TODO: optimize/take into consideration fromsize/tosize. Will }
  408. { probably only matter for OS_S8 loads though }
  409. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  410. internalerror(2002090902);
  411. ref2 := ref;
  412. fixref(list,ref2);
  413. { the caller is expected to have adjusted the reference already }
  414. { in this case }
  415. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  416. fromsize := tosize;
  417. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  418. a_load_store(list,op,reg,ref2);
  419. { sign extend shortint if necessary, since there is no }
  420. { load instruction that does that automatically (JM) }
  421. if fromsize = OS_S8 then
  422. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  423. end;
  424. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  425. var
  426. instr: taicpu;
  427. begin
  428. case tosize of
  429. OS_8:
  430. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  431. reg2,reg1,0,31-8+1,31);
  432. OS_S8:
  433. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  434. OS_16:
  435. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  436. reg2,reg1,0,31-16+1,31);
  437. OS_S16:
  438. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  439. OS_32,OS_S32:
  440. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  441. else internalerror(2002090901);
  442. end;
  443. list.concat(instr);
  444. rg[R_INTREGISTER].add_move_instruction(instr);
  445. end;
  446. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  447. var
  448. instr: taicpu;
  449. begin
  450. instr := taicpu.op_reg_reg(A_FMR,reg2,reg1);
  451. list.concat(instr);
  452. rg[R_FPUREGISTER].add_move_instruction(instr);
  453. end;
  454. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  455. const
  456. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  457. { indexed? updating?}
  458. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  459. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  460. var
  461. op: tasmop;
  462. ref2: treference;
  463. begin
  464. { several functions call this procedure with OS_32 or OS_64 }
  465. { so this makes life easier (FK) }
  466. case size of
  467. OS_32,OS_F32:
  468. size:=OS_F32;
  469. OS_64,OS_F64,OS_C64:
  470. size:=OS_F64;
  471. else
  472. internalerror(200201121);
  473. end;
  474. ref2 := ref;
  475. fixref(list,ref2);
  476. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  477. a_load_store(list,op,reg,ref2);
  478. end;
  479. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  480. const
  481. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  482. { indexed? updating?}
  483. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  484. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  485. var
  486. op: tasmop;
  487. ref2: treference;
  488. begin
  489. if not(size in [OS_F32,OS_F64]) then
  490. internalerror(200201122);
  491. ref2 := ref;
  492. fixref(list,ref2);
  493. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  494. a_load_store(list,op,reg,ref2);
  495. end;
  496. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  497. begin
  498. a_op_const_reg_reg(list,op,size,a,reg,reg);
  499. end;
  500. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  501. begin
  502. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  503. end;
  504. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  505. size: tcgsize; a: aint; src, dst: tregister);
  506. var
  507. l1,l2: longint;
  508. oplo, ophi: tasmop;
  509. scratchreg: tregister;
  510. useReg, gotrlwi: boolean;
  511. procedure do_lo_hi;
  512. begin
  513. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  514. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  515. end;
  516. begin
  517. if op = OP_SUB then
  518. begin
  519. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  520. exit;
  521. end;
  522. ophi := TOpCG2AsmOpConstHi[op];
  523. oplo := TOpCG2AsmOpConstLo[op];
  524. gotrlwi := get_rlwi_const(a,l1,l2);
  525. if (op in [OP_AND,OP_OR,OP_XOR]) then
  526. begin
  527. if (a = 0) then
  528. begin
  529. if op = OP_AND then
  530. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  531. else
  532. a_load_reg_reg(list,size,size,src,dst);
  533. exit;
  534. end
  535. else if (a = -1) then
  536. begin
  537. case op of
  538. OP_OR:
  539. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  540. OP_XOR:
  541. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  542. OP_AND:
  543. a_load_reg_reg(list,size,size,src,dst);
  544. end;
  545. exit;
  546. end
  547. else if (aword(a) <= high(word)) and
  548. ((op <> OP_AND) or
  549. not gotrlwi) then
  550. begin
  551. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  552. exit;
  553. end;
  554. { all basic constant instructions also have a shifted form that }
  555. { works only on the highest 16bits, so if lo(a) is 0, we can }
  556. { use that one }
  557. if (word(a) = 0) and
  558. (not(op = OP_AND) or
  559. not gotrlwi) then
  560. begin
  561. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  562. exit;
  563. end;
  564. end
  565. else if (op = OP_ADD) then
  566. if a = 0 then
  567. begin
  568. a_load_reg_reg(list,size,size,src,dst);
  569. exit
  570. end
  571. else if (a >= low(smallint)) and
  572. (a <= high(smallint)) then
  573. begin
  574. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  575. exit;
  576. end;
  577. { otherwise, the instructions we can generate depend on the }
  578. { operation }
  579. useReg := false;
  580. case op of
  581. OP_DIV,OP_IDIV:
  582. if (a = 0) then
  583. internalerror(200208103)
  584. else if (a = 1) then
  585. begin
  586. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  587. exit
  588. end
  589. else if ispowerof2(a,l1) then
  590. begin
  591. case op of
  592. OP_DIV:
  593. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  594. OP_IDIV:
  595. begin
  596. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  597. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  598. end;
  599. end;
  600. exit;
  601. end
  602. else
  603. usereg := true;
  604. OP_IMUL, OP_MUL:
  605. if (a = 0) then
  606. begin
  607. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  608. exit
  609. end
  610. else if (a = 1) then
  611. begin
  612. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  613. exit
  614. end
  615. else if ispowerof2(a,l1) then
  616. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  617. else if (longint(a) >= low(smallint)) and
  618. (longint(a) <= high(smallint)) then
  619. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  620. else
  621. usereg := true;
  622. OP_ADD:
  623. begin
  624. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  625. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  626. smallint((a shr 16) + ord(smallint(a) < 0))));
  627. end;
  628. OP_OR:
  629. { try to use rlwimi }
  630. if gotrlwi and
  631. (src = dst) then
  632. begin
  633. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  634. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  635. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  636. scratchreg,0,l1,l2));
  637. end
  638. else
  639. do_lo_hi;
  640. OP_AND:
  641. { try to use rlwinm }
  642. if gotrlwi then
  643. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  644. src,0,l1,l2))
  645. else
  646. useReg := true;
  647. OP_XOR:
  648. do_lo_hi;
  649. OP_SHL,OP_SHR,OP_SAR:
  650. begin
  651. if (a and 31) <> 0 Then
  652. list.concat(taicpu.op_reg_reg_const(
  653. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  654. else
  655. a_load_reg_reg(list,size,size,src,dst);
  656. if (a shr 5) <> 0 then
  657. internalError(68991);
  658. end
  659. else
  660. internalerror(200109091);
  661. end;
  662. { if all else failed, load the constant in a register and then }
  663. { perform the operation }
  664. if useReg then
  665. begin
  666. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  667. a_load_const_reg(list,OS_32,a,scratchreg);
  668. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  669. end;
  670. end;
  671. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  672. size: tcgsize; src1, src2, dst: tregister);
  673. const
  674. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  675. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  676. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  677. begin
  678. case op of
  679. OP_NEG,OP_NOT:
  680. begin
  681. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  682. if (op = OP_NOT) and
  683. not(size in [OS_32,OS_S32]) then
  684. { zero/sign extend result again }
  685. a_load_reg_reg(list,OS_32,size,dst,dst);
  686. end;
  687. else
  688. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  689. end;
  690. end;
  691. {*************** compare instructructions ****************}
  692. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  693. l : tasmlabel);
  694. var
  695. p: taicpu;
  696. scratch_register: TRegister;
  697. signed: boolean;
  698. begin
  699. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  700. { in the following case, we generate more efficient code when }
  701. { signed is true }
  702. if (cmp_op in [OC_EQ,OC_NE]) and
  703. (aword(a) > $ffff) then
  704. signed := true;
  705. if signed then
  706. if (a >= low(smallint)) and (a <= high(smallint)) Then
  707. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  708. else
  709. begin
  710. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  711. a_load_const_reg(list,OS_32,a,scratch_register);
  712. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  713. end
  714. else
  715. if (aword(a) <= $ffff) then
  716. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  717. else
  718. begin
  719. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  720. a_load_const_reg(list,OS_32,a,scratch_register);
  721. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  722. end;
  723. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  724. end;
  725. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  726. reg1,reg2 : tregister;l : tasmlabel);
  727. var
  728. p: taicpu;
  729. op: tasmop;
  730. begin
  731. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  732. op := A_CMPW
  733. else
  734. op := A_CMPLW;
  735. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  736. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  737. end;
  738. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  739. begin
  740. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  741. end;
  742. procedure tcgppc.a_jmp_name(list : taasmoutput;const s : string);
  743. var
  744. p : taicpu;
  745. begin
  746. p := taicpu.op_sym(A_B,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION));
  747. p.is_jmp := true;
  748. list.concat(p)
  749. end;
  750. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  751. begin
  752. a_jmp(list,A_B,C_None,0,l);
  753. end;
  754. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  755. var
  756. c: tasmcond;
  757. begin
  758. c := flags_to_cond(f);
  759. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  760. end;
  761. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  762. var
  763. testbit: byte;
  764. bitvalue: boolean;
  765. begin
  766. { get the bit to extract from the conditional register + its }
  767. { requested value (0 or 1) }
  768. testbit := ((f.cr-RS_CR0) * 4);
  769. case f.flag of
  770. F_EQ,F_NE:
  771. begin
  772. inc(testbit,2);
  773. bitvalue := f.flag = F_EQ;
  774. end;
  775. F_LT,F_GE:
  776. begin
  777. bitvalue := f.flag = F_LT;
  778. end;
  779. F_GT,F_LE:
  780. begin
  781. inc(testbit);
  782. bitvalue := f.flag = F_GT;
  783. end;
  784. else
  785. internalerror(200112261);
  786. end;
  787. { load the conditional register in the destination reg }
  788. list.concat(taicpu.op_reg(A_MFCR,reg));
  789. { we will move the bit that has to be tested to bit 0 by rotating }
  790. { left }
  791. testbit := (testbit + 1) and 31;
  792. { extract bit }
  793. list.concat(taicpu.op_reg_reg_const_const_const(
  794. A_RLWINM,reg,reg,testbit,31,31));
  795. { if we need the inverse, xor with 1 }
  796. if not bitvalue then
  797. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  798. end;
  799. (*
  800. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  801. var
  802. testbit: byte;
  803. bitvalue: boolean;
  804. begin
  805. { get the bit to extract from the conditional register + its }
  806. { requested value (0 or 1) }
  807. case f.simple of
  808. false:
  809. begin
  810. { we don't generate this in the compiler }
  811. internalerror(200109062);
  812. end;
  813. true:
  814. case f.cond of
  815. C_None:
  816. internalerror(200109063);
  817. C_LT..C_NU:
  818. begin
  819. testbit := (ord(f.cr) - ord(R_CR0))*4;
  820. inc(testbit,AsmCondFlag2BI[f.cond]);
  821. bitvalue := AsmCondFlagTF[f.cond];
  822. end;
  823. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  824. begin
  825. testbit := f.crbit
  826. bitvalue := AsmCondFlagTF[f.cond];
  827. end;
  828. else
  829. internalerror(200109064);
  830. end;
  831. end;
  832. { load the conditional register in the destination reg }
  833. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  834. { we will move the bit that has to be tested to bit 31 -> rotate }
  835. { left by bitpos+1 (remember, this is big-endian!) }
  836. if bitpos <> 31 then
  837. inc(bitpos)
  838. else
  839. bitpos := 0;
  840. { extract bit }
  841. list.concat(taicpu.op_reg_reg_const_const_const(
  842. A_RLWINM,reg,reg,bitpos,31,31));
  843. { if we need the inverse, xor with 1 }
  844. if not bitvalue then
  845. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  846. end;
  847. *)
  848. { *********** entry/exit code and address loading ************ }
  849. procedure tcgppc.g_save_standard_registers(list:Taasmoutput);
  850. begin
  851. { this work is done in g_proc_entry }
  852. end;
  853. procedure tcgppc.g_restore_standard_registers(list:Taasmoutput);
  854. begin
  855. { this work is done in g_proc_exit }
  856. end;
  857. procedure tcgppc.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  858. { generated the entry code of a procedure/function. Note: localsize is the }
  859. { sum of the size necessary for local variables and the maximum possible }
  860. { combined size of ALL the parameters of a procedure called by the current }
  861. { one. }
  862. { This procedure may be called before, as well as after g_return_from_proc }
  863. { is called. NOTE registers are not to be allocated through the register }
  864. { allocator here, because the register colouring has already occured !! }
  865. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  866. href,href2 : treference;
  867. usesfpr,usesgpr,gotgot : boolean;
  868. parastart : aint;
  869. l : tasmlabel;
  870. regcounter2, firstfpureg: Tsuperregister;
  871. i : integer;
  872. hp: tparavarsym;
  873. cond : tasmcond;
  874. instr : taicpu;
  875. size: tcgsize;
  876. begin
  877. { CR and LR only have to be saved in case they are modified by the current }
  878. { procedure, but currently this isn't checked, so save them always }
  879. { following is the entry code as described in "Altivec Programming }
  880. { Interface Manual", bar the saving of AltiVec registers }
  881. a_reg_alloc(list,NR_STACK_POINTER_REG);
  882. a_reg_alloc(list,NR_R0);
  883. usesfpr:=false;
  884. if not (po_assembler in current_procinfo.procdef.procoptions) then
  885. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  886. case target_info.abi of
  887. abi_powerpc_aix:
  888. firstfpureg := RS_F14;
  889. abi_powerpc_sysv:
  890. firstfpureg := RS_F9;
  891. else
  892. internalerror(2003122903);
  893. end;
  894. for regcounter:=firstfpureg to RS_F31 do
  895. begin
  896. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  897. begin
  898. usesfpr:= true;
  899. firstregfpu:=regcounter;
  900. break;
  901. end;
  902. end;
  903. usesgpr:=false;
  904. if not (po_assembler in current_procinfo.procdef.procoptions) then
  905. for regcounter2:=RS_R13 to RS_R31 do
  906. begin
  907. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  908. begin
  909. usesgpr:=true;
  910. firstreggpr:=regcounter2;
  911. break;
  912. end;
  913. end;
  914. { save link register? }
  915. if not (po_assembler in current_procinfo.procdef.procoptions) then
  916. if (pi_do_call in current_procinfo.flags) then
  917. begin
  918. { save return address... }
  919. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  920. { ... in caller's frame }
  921. case target_info.abi of
  922. abi_powerpc_aix:
  923. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  924. abi_powerpc_sysv:
  925. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  926. end;
  927. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  928. a_reg_dealloc(list,NR_R0);
  929. end;
  930. { save the CR if necessary in callers frame. }
  931. if not (po_assembler in current_procinfo.procdef.procoptions) then
  932. if target_info.abi = abi_powerpc_aix then
  933. if false then { Not needed at the moment. }
  934. begin
  935. a_reg_alloc(list,NR_R0);
  936. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  937. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  938. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  939. a_reg_dealloc(list,NR_R0);
  940. end;
  941. { !!! always allocate space for all registers for now !!! }
  942. if not (po_assembler in current_procinfo.procdef.procoptions) then
  943. { if usesfpr or usesgpr then }
  944. begin
  945. a_reg_alloc(list,NR_R12);
  946. { save end of fpr save area }
  947. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  948. end;
  949. if (not nostackframe) and
  950. (localsize <> 0) then
  951. begin
  952. if (localsize <= high(smallint)) then
  953. begin
  954. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  955. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  956. end
  957. else
  958. begin
  959. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  960. { can't use getregisterint here, the register colouring }
  961. { is already done when we get here }
  962. href.index := NR_R11;
  963. a_reg_alloc(list,href.index);
  964. a_load_const_reg(list,OS_S32,-localsize,href.index);
  965. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  966. a_reg_dealloc(list,href.index);
  967. end;
  968. end;
  969. { no GOT pointer loaded yet }
  970. gotgot:=false;
  971. if usesfpr then
  972. begin
  973. { save floating-point registers
  974. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  975. begin
  976. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  977. gotgot:=true;
  978. end
  979. else
  980. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14),AB_EXTERNAL,AT_FUNCTION));
  981. }
  982. reference_reset_base(href,NR_R12,-8);
  983. for regcounter:=firstregfpu to RS_F31 do
  984. begin
  985. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  986. begin
  987. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  988. dec(href.offset,8);
  989. end;
  990. end;
  991. { compute end of gpr save area }
  992. a_op_const_reg(list,OP_ADD,OS_ADDR,href.offset+8,NR_R12);
  993. end;
  994. { save gprs and fetch GOT pointer }
  995. if usesgpr then
  996. begin
  997. {
  998. if cs_create_pic in aktmoduleswitches then
  999. begin
  1000. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1001. gotgot:=true;
  1002. end
  1003. else
  1004. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14),AB_EXTERNAL,AT_FUNCTION))
  1005. }
  1006. reference_reset_base(href,NR_R12,-4);
  1007. for regcounter2:=RS_R13 to RS_R31 do
  1008. begin
  1009. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1010. begin
  1011. usesgpr:=true;
  1012. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  1013. dec(href.offset,4);
  1014. end;
  1015. end;
  1016. {
  1017. r.enum:=R_INTREGISTER;
  1018. r.:=;
  1019. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  1020. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1021. }
  1022. end;
  1023. { see "!!! always allocate space for all registers for now !!!" above }
  1024. { if usesfpr or usesgpr then }
  1025. a_reg_dealloc(list,NR_R12);
  1026. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1027. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1028. case target_info.system of
  1029. system_powerpc_darwin:
  1030. begin
  1031. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1032. fillchar(cond,sizeof(cond),0);
  1033. cond.simple:=false;
  1034. cond.bo:=20;
  1035. cond.bi:=31;
  1036. instr:=taicpu.op_sym(A_BCL,current_procinfo.gotlabel);
  1037. instr.setcondition(cond);
  1038. list.concat(instr);
  1039. a_label(list,current_procinfo.gotlabel);
  1040. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1041. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1042. end;
  1043. else
  1044. begin
  1045. a_reg_alloc(list,NR_R31);
  1046. { place GOT ptr in r31 }
  1047. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1048. end;
  1049. end;
  1050. { save the CR if necessary ( !!! always done currently ) }
  1051. { still need to find out where this has to be done for SystemV
  1052. a_reg_alloc(list,R_0);
  1053. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1054. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1055. new_reference(STACK_POINTER_REG,LA_CR)));
  1056. a_reg_dealloc(list,R_0); }
  1057. { now comes the AltiVec context save, not yet implemented !!! }
  1058. end;
  1059. procedure tcgppc.g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean);
  1060. { This procedure may be called before, as well as after g_stackframe_entry }
  1061. { is called. NOTE registers are not to be allocated through the register }
  1062. { allocator here, because the register colouring has already occured !! }
  1063. var
  1064. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1065. href : treference;
  1066. usesfpr,usesgpr,genret : boolean;
  1067. regcounter2, firstfpureg:Tsuperregister;
  1068. localsize: aint;
  1069. begin
  1070. { AltiVec context restore, not yet implemented !!! }
  1071. usesfpr:=false;
  1072. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1073. begin
  1074. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1075. case target_info.abi of
  1076. abi_powerpc_aix:
  1077. firstfpureg := RS_F14;
  1078. abi_powerpc_sysv:
  1079. firstfpureg := RS_F9;
  1080. else
  1081. internalerror(2003122903);
  1082. end;
  1083. for regcounter:=firstfpureg to RS_F31 do
  1084. begin
  1085. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1086. begin
  1087. usesfpr:=true;
  1088. firstregfpu:=regcounter;
  1089. break;
  1090. end;
  1091. end;
  1092. end;
  1093. usesgpr:=false;
  1094. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1095. for regcounter2:=RS_R13 to RS_R31 do
  1096. begin
  1097. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1098. begin
  1099. usesgpr:=true;
  1100. firstreggpr:=regcounter2;
  1101. break;
  1102. end;
  1103. end;
  1104. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1105. { no return (blr) generated yet }
  1106. genret:=true;
  1107. if usesgpr or usesfpr then
  1108. begin
  1109. { address of gpr save area to r11 }
  1110. { (register allocator is no longer valid at this time and an add of 0 }
  1111. { is translated into a move, which is then registered with the register }
  1112. { allocator, causing a crash }
  1113. if (localsize <> 0) then
  1114. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12)
  1115. else
  1116. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  1117. if usesfpr then
  1118. begin
  1119. reference_reset_base(href,NR_R12,-8);
  1120. for regcounter := firstregfpu to RS_F31 do
  1121. begin
  1122. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1123. begin
  1124. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1125. dec(href.offset,8);
  1126. end;
  1127. end;
  1128. inc(href.offset,4);
  1129. end
  1130. else
  1131. reference_reset_base(href,NR_R12,-4);
  1132. for regcounter2:=RS_R13 to RS_R31 do
  1133. begin
  1134. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1135. begin
  1136. usesgpr:=true;
  1137. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1138. dec(href.offset,4);
  1139. end;
  1140. end;
  1141. (*
  1142. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1143. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1144. *)
  1145. end;
  1146. (*
  1147. { restore fprs and return }
  1148. if usesfpr then
  1149. begin
  1150. { address of fpr save area to r11 }
  1151. r:=NR_R12;
  1152. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1153. {
  1154. if (pi_do_call in current_procinfo.flags) then
  1155. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1156. '_x',AB_EXTERNAL,AT_FUNCTION))
  1157. else
  1158. { leaf node => lr haven't to be restored }
  1159. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1160. '_l');
  1161. genret:=false;
  1162. }
  1163. end;
  1164. *)
  1165. { if we didn't generate the return code, we've to do it now }
  1166. if genret then
  1167. begin
  1168. { adjust r1 }
  1169. { (register allocator is no longer valid at this time and an add of 0 }
  1170. { is translated into a move, which is then registered with the register }
  1171. { allocator, causing a crash }
  1172. if (not nostackframe) and
  1173. (localsize <> 0) then
  1174. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1175. { load link register? }
  1176. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1177. begin
  1178. if (pi_do_call in current_procinfo.flags) then
  1179. begin
  1180. case target_info.abi of
  1181. abi_powerpc_aix:
  1182. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1183. abi_powerpc_sysv:
  1184. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1185. end;
  1186. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1187. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1188. end;
  1189. { restore the CR if necessary from callers frame}
  1190. if target_info.abi = abi_powerpc_aix then
  1191. if false then { Not needed at the moment. }
  1192. begin
  1193. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1194. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1195. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1196. a_reg_dealloc(list,NR_R0);
  1197. end;
  1198. end;
  1199. list.concat(taicpu.op_none(A_BLR));
  1200. end;
  1201. end;
  1202. function tcgppc.save_regs(list : taasmoutput):longint;
  1203. {Generates code which saves used non-volatile registers in
  1204. the save area right below the address the stackpointer point to.
  1205. Returns the actual used save area size.}
  1206. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1207. usesfpr,usesgpr: boolean;
  1208. href : treference;
  1209. offset: aint;
  1210. regcounter2, firstfpureg: Tsuperregister;
  1211. begin
  1212. usesfpr:=false;
  1213. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1214. begin
  1215. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1216. case target_info.abi of
  1217. abi_powerpc_aix:
  1218. firstfpureg := RS_F14;
  1219. abi_powerpc_sysv:
  1220. firstfpureg := RS_F9;
  1221. else
  1222. internalerror(2003122903);
  1223. end;
  1224. for regcounter:=firstfpureg to RS_F31 do
  1225. begin
  1226. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1227. begin
  1228. usesfpr:=true;
  1229. firstregfpu:=regcounter;
  1230. break;
  1231. end;
  1232. end;
  1233. end;
  1234. usesgpr:=false;
  1235. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1236. for regcounter2:=RS_R13 to RS_R31 do
  1237. begin
  1238. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1239. begin
  1240. usesgpr:=true;
  1241. firstreggpr:=regcounter2;
  1242. break;
  1243. end;
  1244. end;
  1245. offset:= 0;
  1246. { save floating-point registers }
  1247. if usesfpr then
  1248. for regcounter := firstregfpu to RS_F31 do
  1249. begin
  1250. offset:= offset - 8;
  1251. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1252. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1253. end;
  1254. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1255. { save gprs in gpr save area }
  1256. if usesgpr then
  1257. if firstreggpr < RS_R30 then
  1258. begin
  1259. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1260. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1261. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1262. {STMW stores multiple registers}
  1263. end
  1264. else
  1265. begin
  1266. for regcounter := firstreggpr to RS_R31 do
  1267. begin
  1268. offset:= offset - 4;
  1269. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1270. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1271. end;
  1272. end;
  1273. { now comes the AltiVec context save, not yet implemented !!! }
  1274. save_regs:= -offset;
  1275. end;
  1276. procedure tcgppc.restore_regs(list : taasmoutput);
  1277. {Generates code which restores used non-volatile registers from
  1278. the save area right below the address the stackpointer point to.}
  1279. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1280. usesfpr,usesgpr: boolean;
  1281. href : treference;
  1282. offset: integer;
  1283. regcounter2, firstfpureg: Tsuperregister;
  1284. begin
  1285. usesfpr:=false;
  1286. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1287. begin
  1288. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1289. case target_info.abi of
  1290. abi_powerpc_aix:
  1291. firstfpureg := RS_F14;
  1292. abi_powerpc_sysv:
  1293. firstfpureg := RS_F9;
  1294. else
  1295. internalerror(2003122903);
  1296. end;
  1297. for regcounter:=firstfpureg to RS_F31 do
  1298. begin
  1299. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1300. begin
  1301. usesfpr:=true;
  1302. firstregfpu:=regcounter;
  1303. break;
  1304. end;
  1305. end;
  1306. end;
  1307. usesgpr:=false;
  1308. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1309. for regcounter2:=RS_R13 to RS_R31 do
  1310. begin
  1311. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1312. begin
  1313. usesgpr:=true;
  1314. firstreggpr:=regcounter2;
  1315. break;
  1316. end;
  1317. end;
  1318. offset:= 0;
  1319. { restore fp registers }
  1320. if usesfpr then
  1321. for regcounter := firstregfpu to RS_F31 do
  1322. begin
  1323. offset:= offset - 8;
  1324. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1325. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1326. end;
  1327. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1328. { restore gprs }
  1329. if usesgpr then
  1330. if firstreggpr < RS_R30 then
  1331. begin
  1332. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1333. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1334. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1335. {LMW loads multiple registers}
  1336. end
  1337. else
  1338. begin
  1339. for regcounter := firstreggpr to RS_R31 do
  1340. begin
  1341. offset:= offset - 4;
  1342. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1343. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1344. end;
  1345. end;
  1346. { now comes the AltiVec context restore, not yet implemented !!! }
  1347. end;
  1348. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1349. (* NOT IN USE *)
  1350. { generated the entry code of a procedure/function. Note: localsize is the }
  1351. { sum of the size necessary for local variables and the maximum possible }
  1352. { combined size of ALL the parameters of a procedure called by the current }
  1353. { one }
  1354. const
  1355. macosLinkageAreaSize = 24;
  1356. var regcounter: TRegister;
  1357. href : treference;
  1358. registerSaveAreaSize : longint;
  1359. begin
  1360. if (localsize mod 8) <> 0 then
  1361. internalerror(58991);
  1362. { CR and LR only have to be saved in case they are modified by the current }
  1363. { procedure, but currently this isn't checked, so save them always }
  1364. { following is the entry code as described in "Altivec Programming }
  1365. { Interface Manual", bar the saving of AltiVec registers }
  1366. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1367. a_reg_alloc(list,NR_R0);
  1368. { save return address in callers frame}
  1369. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1370. { ... in caller's frame }
  1371. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1372. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1373. a_reg_dealloc(list,NR_R0);
  1374. { save non-volatile registers in callers frame}
  1375. registerSaveAreaSize:= save_regs(list);
  1376. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1377. a_reg_alloc(list,NR_R0);
  1378. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1379. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1380. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1381. a_reg_dealloc(list,NR_R0);
  1382. (*
  1383. { save pointer to incoming arguments }
  1384. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1385. *)
  1386. (*
  1387. a_reg_alloc(list,R_12);
  1388. { 0 or 8 based on SP alignment }
  1389. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1390. R_12,STACK_POINTER_REG,0,28,28));
  1391. { add in stack length }
  1392. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1393. -localsize));
  1394. { establish new alignment }
  1395. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1396. a_reg_dealloc(list,R_12);
  1397. *)
  1398. { allocate stack frame }
  1399. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1400. inc(localsize,tg.lasttemp);
  1401. localsize:=align(localsize,16);
  1402. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1403. if (localsize <> 0) then
  1404. begin
  1405. if (localsize <= high(smallint)) then
  1406. begin
  1407. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1408. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1409. end
  1410. else
  1411. begin
  1412. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1413. href.index := NR_R11;
  1414. a_reg_alloc(list,href.index);
  1415. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1416. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1417. a_reg_dealloc(list,href.index);
  1418. end;
  1419. end;
  1420. end;
  1421. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  1422. (* NOT IN USE *)
  1423. var
  1424. href : treference;
  1425. begin
  1426. a_reg_alloc(list,NR_R0);
  1427. { restore stack pointer }
  1428. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1429. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1430. (*
  1431. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1432. *)
  1433. { restore the CR if necessary from callers frame
  1434. ( !!! always done currently ) }
  1435. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1436. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1437. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1438. a_reg_dealloc(list,NR_R0);
  1439. (*
  1440. { restore return address from callers frame }
  1441. reference_reset_base(href,STACK_POINTER_REG,8);
  1442. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1443. *)
  1444. { restore non-volatile registers from callers frame }
  1445. restore_regs(list);
  1446. (*
  1447. { return to caller }
  1448. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1449. list.concat(taicpu.op_none(A_BLR));
  1450. *)
  1451. { restore return address from callers frame }
  1452. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1453. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1454. { return to caller }
  1455. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1456. list.concat(taicpu.op_none(A_BLR));
  1457. end;
  1458. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1459. var
  1460. ref2, tmpref: treference;
  1461. tmpreg:Tregister;
  1462. begin
  1463. ref2 := ref;
  1464. fixref(list,ref2);
  1465. if assigned(ref2.symbol) then
  1466. begin
  1467. if target_info.system = system_powerpc_macos then
  1468. begin
  1469. if macos_direct_globals then
  1470. begin
  1471. reference_reset(tmpref);
  1472. tmpref.offset := ref2.offset;
  1473. tmpref.symbol := ref2.symbol;
  1474. tmpref.base := NR_NO;
  1475. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1476. end
  1477. else
  1478. begin
  1479. reference_reset(tmpref);
  1480. tmpref.symbol := ref2.symbol;
  1481. tmpref.offset := 0;
  1482. tmpref.base := NR_RTOC;
  1483. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1484. if ref2.offset <> 0 then
  1485. begin
  1486. reference_reset(tmpref);
  1487. tmpref.offset := ref2.offset;
  1488. tmpref.base:= r;
  1489. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1490. end;
  1491. end;
  1492. if ref2.base <> NR_NO then
  1493. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1494. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1495. end
  1496. else
  1497. begin
  1498. { add the symbol's value to the base of the reference, and if the }
  1499. { reference doesn't have a base, create one }
  1500. reference_reset(tmpref);
  1501. tmpref.offset := ref2.offset;
  1502. tmpref.symbol := ref2.symbol;
  1503. tmpref.relsymbol := ref2.relsymbol;
  1504. tmpref.refaddr := addr_hi;
  1505. if ref2.base<> NR_NO then
  1506. begin
  1507. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1508. ref2.base,tmpref));
  1509. end
  1510. else
  1511. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1512. tmpref.base := NR_NO;
  1513. tmpref.refaddr := addr_lo;
  1514. { can be folded with one of the next instructions by the }
  1515. { optimizer probably }
  1516. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1517. end
  1518. end
  1519. else if ref2.offset <> 0 Then
  1520. if ref2.base <> NR_NO then
  1521. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1522. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1523. { occurs, so now only ref.offset has to be loaded }
  1524. else
  1525. a_load_const_reg(list,OS_32,ref2.offset,r)
  1526. else if ref.index <> NR_NO Then
  1527. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1528. else if (ref2.base <> NR_NO) and
  1529. (r <> ref2.base) then
  1530. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1531. else
  1532. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1533. end;
  1534. { ************* concatcopy ************ }
  1535. {$ifndef ppc603}
  1536. const
  1537. maxmoveunit = 8;
  1538. {$else ppc603}
  1539. const
  1540. maxmoveunit = 4;
  1541. {$endif ppc603}
  1542. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);
  1543. var
  1544. countreg: TRegister;
  1545. src, dst: TReference;
  1546. lab: tasmlabel;
  1547. count, count2: aint;
  1548. orgsrc, orgdst: boolean;
  1549. size: tcgsize;
  1550. begin
  1551. {$ifdef extdebug}
  1552. if len > high(longint) then
  1553. internalerror(2002072704);
  1554. {$endif extdebug}
  1555. { make sure short loads are handled as optimally as possible }
  1556. if (len <= maxmoveunit) and
  1557. (byte(len) in [1,2,4,8]) then
  1558. begin
  1559. if len < 8 then
  1560. begin
  1561. size := int_cgsize(len);
  1562. a_load_ref_ref(list,size,size,source,dest);
  1563. end
  1564. else
  1565. begin
  1566. a_reg_alloc(list,NR_F0);
  1567. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1568. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1569. a_reg_dealloc(list,NR_F0);
  1570. end;
  1571. exit;
  1572. end;
  1573. count := len div maxmoveunit;
  1574. reference_reset(src);
  1575. reference_reset(dst);
  1576. { load the address of source into src.base }
  1577. if (count > 4) or
  1578. not issimpleref(source) or
  1579. ((source.index <> NR_NO) and
  1580. ((source.offset + longint(len)) > high(smallint))) then
  1581. begin
  1582. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1583. a_loadaddr_ref_reg(list,source,src.base);
  1584. orgsrc := false;
  1585. end
  1586. else
  1587. begin
  1588. src := source;
  1589. orgsrc := true;
  1590. end;
  1591. { load the address of dest into dst.base }
  1592. if (count > 4) or
  1593. not issimpleref(dest) or
  1594. ((dest.index <> NR_NO) and
  1595. ((dest.offset + longint(len)) > high(smallint))) then
  1596. begin
  1597. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1598. a_loadaddr_ref_reg(list,dest,dst.base);
  1599. orgdst := false;
  1600. end
  1601. else
  1602. begin
  1603. dst := dest;
  1604. orgdst := true;
  1605. end;
  1606. {$ifndef ppc603}
  1607. if count > 4 then
  1608. { generate a loop }
  1609. begin
  1610. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1611. { have to be set to 8. I put an Inc there so debugging may be }
  1612. { easier (should offset be different from zero here, it will be }
  1613. { easy to notice in the generated assembler }
  1614. inc(dst.offset,8);
  1615. inc(src.offset,8);
  1616. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1617. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1618. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1619. a_load_const_reg(list,OS_32,count,countreg);
  1620. { explicitely allocate R_0 since it can be used safely here }
  1621. { (for holding date that's being copied) }
  1622. a_reg_alloc(list,NR_F0);
  1623. objectlibrary.getlabel(lab);
  1624. a_label(list, lab);
  1625. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1626. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1627. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1628. a_jmp(list,A_BC,C_NE,0,lab);
  1629. a_reg_dealloc(list,NR_F0);
  1630. len := len mod 8;
  1631. end;
  1632. count := len div 8;
  1633. if count > 0 then
  1634. { unrolled loop }
  1635. begin
  1636. a_reg_alloc(list,NR_F0);
  1637. for count2 := 1 to count do
  1638. begin
  1639. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1640. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1641. inc(src.offset,8);
  1642. inc(dst.offset,8);
  1643. end;
  1644. a_reg_dealloc(list,NR_F0);
  1645. len := len mod 8;
  1646. end;
  1647. if (len and 4) <> 0 then
  1648. begin
  1649. a_reg_alloc(list,NR_R0);
  1650. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1651. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1652. inc(src.offset,4);
  1653. inc(dst.offset,4);
  1654. a_reg_dealloc(list,NR_R0);
  1655. end;
  1656. {$else not ppc603}
  1657. if count > 4 then
  1658. { generate a loop }
  1659. begin
  1660. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1661. { have to be set to 4. I put an Inc there so debugging may be }
  1662. { easier (should offset be different from zero here, it will be }
  1663. { easy to notice in the generated assembler }
  1664. inc(dst.offset,4);
  1665. inc(src.offset,4);
  1666. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1667. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1668. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1669. a_load_const_reg(list,OS_32,count,countreg);
  1670. { explicitely allocate R_0 since it can be used safely here }
  1671. { (for holding date that's being copied) }
  1672. a_reg_alloc(list,NR_R0);
  1673. objectlibrary.getlabel(lab);
  1674. a_label(list, lab);
  1675. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1676. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1677. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1678. a_jmp(list,A_BC,C_NE,0,lab);
  1679. a_reg_dealloc(list,NR_R0);
  1680. len := len mod 4;
  1681. end;
  1682. count := len div 4;
  1683. if count > 0 then
  1684. { unrolled loop }
  1685. begin
  1686. a_reg_alloc(list,NR_R0);
  1687. for count2 := 1 to count do
  1688. begin
  1689. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1690. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1691. inc(src.offset,4);
  1692. inc(dst.offset,4);
  1693. end;
  1694. a_reg_dealloc(list,NR_R0);
  1695. len := len mod 4;
  1696. end;
  1697. {$endif not ppc603}
  1698. { copy the leftovers }
  1699. if (len and 2) <> 0 then
  1700. begin
  1701. a_reg_alloc(list,NR_R0);
  1702. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1703. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1704. inc(src.offset,2);
  1705. inc(dst.offset,2);
  1706. a_reg_dealloc(list,NR_R0);
  1707. end;
  1708. if (len and 1) <> 0 then
  1709. begin
  1710. a_reg_alloc(list,NR_R0);
  1711. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1712. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1713. a_reg_dealloc(list,NR_R0);
  1714. end;
  1715. end;
  1716. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1717. var
  1718. hl : tasmlabel;
  1719. begin
  1720. if not(cs_check_overflow in aktlocalswitches) then
  1721. exit;
  1722. objectlibrary.getlabel(hl);
  1723. if not ((def.deftype=pointerdef) or
  1724. ((def.deftype=orddef) and
  1725. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1726. bool8bit,bool16bit,bool32bit]))) then
  1727. begin
  1728. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1729. a_jmp(list,A_BC,C_NO,7,hl)
  1730. end
  1731. else
  1732. a_jmp_cond(list,OC_AE,hl);
  1733. a_call_name(list,'FPC_OVERFLOW');
  1734. a_label(list,hl);
  1735. end;
  1736. {***************** This is private property, keep out! :) *****************}
  1737. function tcgppc.issimpleref(const ref: treference): boolean;
  1738. begin
  1739. if (ref.base = NR_NO) and
  1740. (ref.index <> NR_NO) then
  1741. internalerror(200208101);
  1742. result :=
  1743. not(assigned(ref.symbol)) and
  1744. (((ref.index = NR_NO) and
  1745. (ref.offset >= low(smallint)) and
  1746. (ref.offset <= high(smallint))) or
  1747. ((ref.index <> NR_NO) and
  1748. (ref.offset = 0)));
  1749. end;
  1750. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1751. var
  1752. tmpreg: tregister;
  1753. orgindex: tregister;
  1754. begin
  1755. result := false;
  1756. if (ref.base = NR_NO) then
  1757. begin
  1758. ref.base := ref.index;
  1759. ref.base := NR_NO;
  1760. end;
  1761. if (ref.base <> NR_NO) then
  1762. begin
  1763. if (ref.index <> NR_NO) and
  1764. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1765. begin
  1766. result := true;
  1767. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1768. list.concat(taicpu.op_reg_reg_reg(
  1769. A_ADD,tmpreg,ref.base,ref.index));
  1770. ref.index := NR_NO;
  1771. ref.base := tmpreg;
  1772. end
  1773. end
  1774. else
  1775. if ref.index <> NR_NO then
  1776. internalerror(200208102);
  1777. end;
  1778. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1779. { that's the case, we can use rlwinm to do an AND operation }
  1780. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1781. var
  1782. temp : longint;
  1783. testbit : aint;
  1784. compare: boolean;
  1785. begin
  1786. get_rlwi_const := false;
  1787. if (a = 0) or (a = -1) then
  1788. exit;
  1789. { start with the lowest bit }
  1790. testbit := 1;
  1791. { check its value }
  1792. compare := boolean(a and testbit);
  1793. { find out how long the run of bits with this value is }
  1794. { (it's impossible that all bits are 1 or 0, because in that case }
  1795. { this function wouldn't have been called) }
  1796. l1 := 31;
  1797. while (((a and testbit) <> 0) = compare) do
  1798. begin
  1799. testbit := testbit shl 1;
  1800. dec(l1);
  1801. end;
  1802. { check the length of the run of bits that comes next }
  1803. compare := not compare;
  1804. l2 := l1;
  1805. while (((a and testbit) <> 0) = compare) and
  1806. (l2 >= 0) do
  1807. begin
  1808. testbit := testbit shl 1;
  1809. dec(l2);
  1810. end;
  1811. { and finally the check whether the rest of the bits all have the }
  1812. { same value }
  1813. compare := not compare;
  1814. temp := l2;
  1815. if temp >= 0 then
  1816. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1817. exit;
  1818. { we have done "not(not(compare))", so compare is back to its }
  1819. { initial value. If the lowest bit was 0, a is of the form }
  1820. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1821. { because l2 now contains the position of the last zero of the }
  1822. { first run instead of that of the first 1) so switch l1 and l2 }
  1823. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1824. if not compare then
  1825. begin
  1826. temp := l1;
  1827. l1 := l2+1;
  1828. l2 := temp;
  1829. end
  1830. else
  1831. { otherwise, l1 currently contains the position of the last }
  1832. { zero instead of that of the first 1 of the second run -> +1 }
  1833. inc(l1);
  1834. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1835. l1 := l1 and 31;
  1836. l2 := l2 and 31;
  1837. get_rlwi_const := true;
  1838. end;
  1839. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1840. ref: treference);
  1841. var
  1842. tmpreg: tregister;
  1843. tmpref: treference;
  1844. largeOffset: Boolean;
  1845. begin
  1846. tmpreg := NR_NO;
  1847. if target_info.system = system_powerpc_macos then
  1848. begin
  1849. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1850. high(smallint)-low(smallint));
  1851. if assigned(ref.symbol) then
  1852. begin {Load symbol's value}
  1853. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1854. reference_reset(tmpref);
  1855. tmpref.symbol := ref.symbol;
  1856. tmpref.base := NR_RTOC;
  1857. if macos_direct_globals then
  1858. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1859. else
  1860. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1861. end;
  1862. if largeOffset then
  1863. begin {Add hi part of offset}
  1864. reference_reset(tmpref);
  1865. if Smallint(Lo(ref.offset)) < 0 then
  1866. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1867. else
  1868. tmpref.offset := Hi(ref.offset);
  1869. if (tmpreg <> NR_NO) then
  1870. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1871. else
  1872. begin
  1873. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1874. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1875. end;
  1876. end;
  1877. if (tmpreg <> NR_NO) then
  1878. begin
  1879. {Add content of base register}
  1880. if ref.base <> NR_NO then
  1881. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1882. ref.base,tmpreg));
  1883. {Make ref ready to be used by op}
  1884. ref.symbol:= nil;
  1885. ref.base:= tmpreg;
  1886. if largeOffset then
  1887. ref.offset := Smallint(Lo(ref.offset));
  1888. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1889. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1890. end
  1891. else
  1892. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1893. end
  1894. else {if target_info.system <> system_powerpc_macos}
  1895. begin
  1896. if assigned(ref.symbol) or
  1897. (cardinal(ref.offset-low(smallint)) >
  1898. high(smallint)-low(smallint)) then
  1899. begin
  1900. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1901. reference_reset(tmpref);
  1902. tmpref.symbol := ref.symbol;
  1903. tmpref.relsymbol := ref.relsymbol;
  1904. tmpref.offset := ref.offset;
  1905. tmpref.refaddr := addr_hi;
  1906. if ref.base <> NR_NO then
  1907. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1908. ref.base,tmpref))
  1909. else
  1910. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1911. ref.base := tmpreg;
  1912. ref.refaddr := addr_lo;
  1913. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1914. end
  1915. else
  1916. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1917. end;
  1918. end;
  1919. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1920. crval: longint; l: tasmlabel);
  1921. var
  1922. p: taicpu;
  1923. begin
  1924. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name,AB_EXTERNAL,AT_FUNCTION));
  1925. if op <> A_B then
  1926. create_cond_norm(c,crval,p.condition);
  1927. p.is_jmp := true;
  1928. list.concat(p)
  1929. end;
  1930. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1931. begin
  1932. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1933. end;
  1934. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);
  1935. begin
  1936. a_op64_const_reg_reg(list,op,value,reg,reg);
  1937. end;
  1938. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1939. begin
  1940. case op of
  1941. OP_AND,OP_OR,OP_XOR:
  1942. begin
  1943. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1944. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1945. end;
  1946. OP_ADD:
  1947. begin
  1948. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1949. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1950. end;
  1951. OP_SUB:
  1952. begin
  1953. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1954. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1955. end;
  1956. else
  1957. internalerror(2002072801);
  1958. end;
  1959. end;
  1960. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);
  1961. const
  1962. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1963. (A_SUBIC,A_SUBC,A_ADDME));
  1964. var
  1965. tmpreg: tregister;
  1966. tmpreg64: tregister64;
  1967. issub: boolean;
  1968. begin
  1969. case op of
  1970. OP_AND,OP_OR,OP_XOR:
  1971. begin
  1972. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1973. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1974. regdst.reghi);
  1975. end;
  1976. OP_ADD, OP_SUB:
  1977. begin
  1978. if (value < 0) then
  1979. begin
  1980. if op = OP_ADD then
  1981. op := OP_SUB
  1982. else
  1983. op := OP_ADD;
  1984. value := -value;
  1985. end;
  1986. if (longint(value) <> 0) then
  1987. begin
  1988. issub := op = OP_SUB;
  1989. if (value > 0) and
  1990. (value-ord(issub) <= 32767) then
  1991. begin
  1992. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1993. regdst.reglo,regsrc.reglo,longint(value)));
  1994. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1995. regdst.reghi,regsrc.reghi));
  1996. end
  1997. else if ((value shr 32) = 0) then
  1998. begin
  1999. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2000. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2001. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2002. regdst.reglo,regsrc.reglo,tmpreg));
  2003. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2004. regdst.reghi,regsrc.reghi));
  2005. end
  2006. else
  2007. begin
  2008. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2009. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2010. a_load64_const_reg(list,value,tmpreg64);
  2011. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2012. end
  2013. end
  2014. else
  2015. begin
  2016. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2017. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2018. regdst.reghi);
  2019. end;
  2020. end;
  2021. else
  2022. internalerror(2002072802);
  2023. end;
  2024. end;
  2025. begin
  2026. cg := tcgppc.create;
  2027. cg64 :=tcg64fppc.create;
  2028. end.
  2029. {
  2030. $Log$
  2031. Revision 1.187 2004-12-04 21:47:46 jonas
  2032. * modifications to work with the generic code to copy LOC_REFERENCE
  2033. parameters to local temps (fixes tests/test/cg/tmanypara)
  2034. Revision 1.186 2004/11/15 23:35:31 peter
  2035. * tparaitem removed, use tparavarsym instead
  2036. * parameter order is now calculated from paranr value in tparavarsym
  2037. Revision 1.185 2004/11/11 19:31:33 peter
  2038. * fixed compile of powerpc,sparc,arm
  2039. Revision 1.184 2004/10/31 21:45:03 peter
  2040. * generic tlocation
  2041. * move tlocation to cgutils
  2042. Revision 1.183 2004/10/26 18:21:29 jonas
  2043. + empty g_save_standard_registers/g_restore_standard_registers overrides
  2044. (their work was/is done by g_proc_entry/g_proc_exit, and the generic
  2045. version saves the registers in the wrong place)
  2046. Revision 1.182 2004/10/24 20:01:08 peter
  2047. * remove saveregister calling convention
  2048. Revision 1.181 2004/10/24 11:53:45 peter
  2049. * fixed compilation with removed loadref
  2050. Revision 1.180 2004/10/20 07:32:42 jonas
  2051. + support for nostackframe directive
  2052. Revision 1.179 2004/10/11 07:13:14 jonas
  2053. * include pi_do_call if we generate a call instead of internalerroring
  2054. (workaround)
  2055. Revision 1.178 2004/09/25 14:23:54 peter
  2056. * ungetregister is now only used for cpuregisters, renamed to
  2057. ungetcpuregister
  2058. * renamed (get|unget)explicitregister(s) to ..cpuregister
  2059. * removed location-release/reference_release
  2060. Revision 1.177 2004/09/21 17:25:12 peter
  2061. * paraloc branch merged
  2062. Revision 1.176.4.2 2004/09/18 20:21:08 jonas
  2063. * fixed ppc, but still needs fix in tgobj
  2064. Revision 1.176.4.1 2004/09/10 11:10:08 florian
  2065. * first part of ppc fixes
  2066. Revision 1.176 2004/07/17 14:48:20 jonas
  2067. * fixed op_const_reg_reg for (OP_ADD,0,reg1,reg2)
  2068. Revision 1.175 2004/07/09 21:45:24 jonas
  2069. * fixed passing of fpu paras on the stack
  2070. * fixed number of fpu parameters passed in registers
  2071. * skip corresponding integer registers when using an fpu register for a
  2072. parameter under the AIX abi
  2073. Revision 1.174 2004/07/01 18:00:00 jonas
  2074. * fixed several errors due to aword -> aint change
  2075. Revision 1.173 2004/06/20 08:55:32 florian
  2076. * logs truncated
  2077. Revision 1.172 2004/06/17 16:55:46 peter
  2078. * powerpc compiles again
  2079. Revision 1.171 2004/06/02 17:18:10 jonas
  2080. * parameters passed on the stack now also work as register variables
  2081. Revision 1.170 2004/05/31 18:08:41 jonas
  2082. * changed calling of external procedures to be the same as under gcc
  2083. (don't worry about all the generated stubs, they're optimized away
  2084. by the linker)
  2085. -> side effect: no need anymore to use special declarations for
  2086. external C functions under Darwin compared to other platforms
  2087. (it's still necessary for variables though)
  2088. Revision 1.169 2004/04/04 17:50:36 olle
  2089. * macos: fixed large offsets in references
  2090. Revision 1.168 2004/03/06 21:37:45 florian
  2091. * fixed ppc compilation
  2092. }