cgcpu.pas 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741
  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. symtype,
  23. cginfo,cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,node,cg64f32;
  26. type
  27. tcgppc = class(tcg)
  28. { passing parameters, per default the parameter is pushed }
  29. { nr gives the number of the parameter (enumerated from }
  30. { left to right), this allows to move the parameter to }
  31. { register, if the cpu supports register calling }
  32. { conventions }
  33. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  34. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  35. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  36. procedure a_call_name(list : taasmoutput;const s : string);override;
  37. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  38. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  39. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  40. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  41. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  42. size: tcgsize; a: aword; src, dst: tregister); override;
  43. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  44. size: tcgsize; src1, src2, dst: tregister); override;
  45. { move instructions }
  46. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  47. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  48. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  49. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  50. { fpu move instructions }
  51. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  52. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  53. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  54. { comparison operations }
  55. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  56. l : tasmlabel);override;
  57. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  58. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  59. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  60. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  61. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  62. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  63. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  64. procedure g_restore_frame_pointer(list : taasmoutput);override;
  65. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  66. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  67. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  68. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  69. { that's the case, we can use rlwinm to do an AND operation }
  70. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  71. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);override;
  72. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);override;
  73. procedure g_save_all_registers(list : taasmoutput);override;
  74. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  75. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  76. private
  77. (* NOT IN USE: *)
  78. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  79. (* NOT IN USE: *)
  80. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  81. { Make sure ref is a valid reference for the PowerPC and sets the }
  82. { base to the value of the index if (base = R_NO). }
  83. { Returns true if the reference contained a base, index and an }
  84. { offset or symbol, in which case the base will have been changed }
  85. { to a tempreg (which has to be freed by the caller) containing }
  86. { the sum of part of the original reference }
  87. function fixref(list: taasmoutput; var ref: treference): boolean;
  88. { returns whether a reference can be used immediately in a powerpc }
  89. { instruction }
  90. function issimpleref(const ref: treference): boolean;
  91. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  92. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  93. ref: treference);
  94. { creates the correct branch instruction for a given combination }
  95. { of asmcondflags and destination addressing mode }
  96. procedure a_jmp(list: taasmoutput; op: tasmop;
  97. c: tasmcondflag; crval: longint; l: tasmlabel);
  98. end;
  99. tcg64fppc = class(tcg64f32)
  100. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  101. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  102. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  103. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  104. end;
  105. const
  106. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  107. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  108. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  109. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  110. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  111. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  112. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  113. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  114. implementation
  115. uses
  116. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  117. { parameter passing... Still needs extra support from the processor }
  118. { independent code generator }
  119. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  120. var
  121. ref: treference;
  122. begin
  123. case locpara.loc of
  124. LOC_REGISTER,LOC_CREGISTER:
  125. a_load_const_reg(list,size,a,locpara.register);
  126. LOC_REFERENCE:
  127. begin
  128. reference_reset(ref);
  129. ref.base:=locpara.reference.index;
  130. ref.offset:=locpara.reference.offset;
  131. a_load_const_ref(list,size,a,ref);
  132. end;
  133. else
  134. internalerror(2002081101);
  135. end;
  136. if locpara.sp_fixup<>0 then
  137. internalerror(2002081102);
  138. end;
  139. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  140. var
  141. ref: treference;
  142. tmpreg: tregister;
  143. begin
  144. case locpara.loc of
  145. LOC_REGISTER,LOC_CREGISTER:
  146. a_load_ref_reg(list,size,size,r,locpara.register);
  147. LOC_REFERENCE:
  148. begin
  149. reference_reset(ref);
  150. ref.base:=locpara.reference.index;
  151. ref.offset:=locpara.reference.offset;
  152. tmpreg := rg.getregisterint(list,size);
  153. a_load_ref_reg(list,size,size,r,tmpreg);
  154. a_load_reg_ref(list,size,size,tmpreg,ref);
  155. rg.ungetregisterint(list,tmpreg);
  156. end;
  157. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  158. case size of
  159. OS_F32, OS_F64:
  160. a_loadfpu_ref_reg(list,size,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 := rg.getregisterint(list,OS_ADDR);
  184. a_loadaddr_ref_reg(list,r,tmpreg);
  185. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  186. rg.ungetregisterint(list,tmpreg);
  187. end;
  188. else
  189. internalerror(2002080701);
  190. end;
  191. end;
  192. { calling a procedure by name }
  193. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  194. var
  195. href : treference;
  196. begin
  197. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  198. if it is a cross-TOC call. If so, it also replaces the NOP
  199. with some restore code.}
  200. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  201. if target_info.system=system_powerpc_macos then
  202. list.concat(taicpu.op_none(A_NOP));
  203. if not(pi_do_call in current_procinfo.flags) then
  204. internalerror(2003060703);
  205. end;
  206. { calling a procedure by address }
  207. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  208. var
  209. tmpreg : tregister;
  210. tmpref : treference;
  211. begin
  212. if target_info.system=system_powerpc_macos then
  213. begin
  214. {Generate instruction to load the procedure address from
  215. the transition vector.}
  216. //TODO: Support cross-TOC calls.
  217. tmpreg := rg.getregisterint(list,OS_INT);
  218. reference_reset(tmpref);
  219. tmpref.offset := 0;
  220. //tmpref.symaddr := refs_full;
  221. tmpref.base:= reg;
  222. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  223. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  224. rg.ungetregisterint(list,tmpreg);
  225. end
  226. else
  227. list.concat(taicpu.op_reg(A_MTCTR,reg));
  228. list.concat(taicpu.op_none(A_BCTRL));
  229. //if target_info.system=system_powerpc_macos then
  230. // //NOP is not needed here.
  231. // list.concat(taicpu.op_none(A_NOP));
  232. if not(pi_do_call in current_procinfo.flags) then
  233. internalerror(2003060704);
  234. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  235. end;
  236. { calling a procedure by address }
  237. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  238. var
  239. tmpreg : tregister;
  240. tmpref : treference;
  241. begin
  242. tmpreg := rg.getregisterint(list,OS_ADDR);
  243. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,tmpreg);
  244. if target_info.system=system_powerpc_macos then
  245. begin
  246. {Generate instruction to load the procedure address from
  247. the transition vector.}
  248. //TODO: Support cross-TOC calls.
  249. reference_reset(tmpref);
  250. tmpref.offset := 0;
  251. //tmpref.symaddr := refs_full;
  252. tmpref.base:= tmpreg;
  253. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  254. end;
  255. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  256. rg.ungetregisterint(list,tmpreg);
  257. list.concat(taicpu.op_none(A_BCTRL));
  258. //if target_info.system=system_powerpc_macos then
  259. // //NOP is not needed here.
  260. // list.concat(taicpu.op_none(A_NOP));
  261. if not(pi_do_call in current_procinfo.flags) then
  262. internalerror(2003060705);
  263. //list.concat(tai_comment.create(strpnew('***** a_call_ref')));
  264. end;
  265. {********************** load instructions ********************}
  266. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  267. begin
  268. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  269. internalerror(2002090902);
  270. if (longint(a) >= low(smallint)) and
  271. (longint(a) <= high(smallint)) then
  272. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  273. else if ((a and $ffff) <> 0) then
  274. begin
  275. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  276. if ((a shr 16) <> 0) or
  277. (smallint(a and $ffff) < 0) then
  278. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  279. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  280. end
  281. else
  282. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  283. end;
  284. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  285. const
  286. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  287. { indexed? updating?}
  288. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  289. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  290. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  291. var
  292. op: TAsmOp;
  293. ref2: TReference;
  294. freereg: boolean;
  295. begin
  296. ref2 := ref;
  297. freereg := fixref(list,ref2);
  298. if tosize in [OS_S8..OS_S16] then
  299. { storing is the same for signed and unsigned values }
  300. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  301. { 64 bit stuff should be handled separately }
  302. if tosize in [OS_64,OS_S64] then
  303. internalerror(200109236);
  304. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  305. a_load_store(list,op,reg,ref2);
  306. if freereg then
  307. rg.ungetregisterint(list,ref2.base);
  308. End;
  309. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  310. const
  311. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  312. { indexed? updating?}
  313. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  314. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  315. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  316. { 64bit stuff should be handled separately }
  317. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  318. { there's no load-byte-with-sign-extend :( }
  319. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  320. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  321. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  322. var
  323. op: tasmop;
  324. tmpreg: tregister;
  325. ref2, tmpref: treference;
  326. freereg: boolean;
  327. begin
  328. { TODO: optimize/take into consideration fromsize/tosize. Will }
  329. { probably only matter for OS_S8 loads though }
  330. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  331. internalerror(2002090902);
  332. ref2 := ref;
  333. freereg := fixref(list,ref2);
  334. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  335. a_load_store(list,op,reg,ref2);
  336. if freereg then
  337. rg.ungetregisterint(list,ref2.base);
  338. { sign extend shortint if necessary, since there is no }
  339. { load instruction that does that automatically (JM) }
  340. if fromsize = OS_S8 then
  341. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  342. end;
  343. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  344. var
  345. instr: taicpu;
  346. begin
  347. if (reg1<>reg2) or
  348. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  349. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  350. (tosize <> fromsize) and
  351. not(fromsize in [OS_32,OS_S32])) then
  352. begin
  353. case tosize of
  354. OS_8:
  355. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  356. reg2,reg1,0,31-8+1,31);
  357. OS_S8:
  358. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  359. OS_16:
  360. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  361. reg2,reg1,0,31-16+1,31);
  362. OS_S16:
  363. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  364. OS_32,OS_S32:
  365. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  366. else internalerror(2002090901);
  367. end;
  368. list.concat(instr);
  369. rg.add_move_instruction(instr);
  370. end;
  371. end;
  372. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  373. begin
  374. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  375. end;
  376. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  377. const
  378. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  379. { indexed? updating?}
  380. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  381. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  382. var
  383. op: tasmop;
  384. ref2: treference;
  385. freereg: boolean;
  386. begin
  387. { several functions call this procedure with OS_32 or OS_64 }
  388. { so this makes life easier (FK) }
  389. case size of
  390. OS_32,OS_F32:
  391. size:=OS_F32;
  392. OS_64,OS_F64,OS_C64:
  393. size:=OS_F64;
  394. else
  395. internalerror(200201121);
  396. end;
  397. ref2 := ref;
  398. freereg := fixref(list,ref2);
  399. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  400. a_load_store(list,op,reg,ref2);
  401. if freereg then
  402. rg.ungetregisterint(list,ref2.base);
  403. end;
  404. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  405. const
  406. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  407. { indexed? updating?}
  408. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  409. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  410. var
  411. op: tasmop;
  412. ref2: treference;
  413. freereg: boolean;
  414. begin
  415. if not(size in [OS_F32,OS_F64]) then
  416. internalerror(200201122);
  417. ref2 := ref;
  418. freereg := fixref(list,ref2);
  419. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  420. a_load_store(list,op,reg,ref2);
  421. if freereg then
  422. rg.ungetregisterint(list,ref2.base);
  423. end;
  424. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  425. begin
  426. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  427. end;
  428. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  429. begin
  430. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  431. end;
  432. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  433. size: tcgsize; a: aword; src, dst: tregister);
  434. var
  435. l1,l2: longint;
  436. oplo, ophi: tasmop;
  437. scratchreg: tregister;
  438. useReg, gotrlwi: boolean;
  439. procedure do_lo_hi;
  440. begin
  441. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  442. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  443. end;
  444. begin
  445. if op = OP_SUB then
  446. begin
  447. {$ifopt q+}
  448. {$q-}
  449. {$define overflowon}
  450. {$endif}
  451. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  452. {$ifdef overflowon}
  453. {$q+}
  454. {$undef overflowon}
  455. {$endif}
  456. exit;
  457. end;
  458. ophi := TOpCG2AsmOpConstHi[op];
  459. oplo := TOpCG2AsmOpConstLo[op];
  460. gotrlwi := get_rlwi_const(a,l1,l2);
  461. if (op in [OP_AND,OP_OR,OP_XOR]) then
  462. begin
  463. if (a = 0) then
  464. begin
  465. if op = OP_AND then
  466. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  467. else
  468. a_load_reg_reg(list,size,size,src,dst);
  469. exit;
  470. end
  471. else if (a = high(aword)) then
  472. begin
  473. case op of
  474. OP_OR:
  475. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  476. OP_XOR:
  477. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  478. OP_AND:
  479. a_load_reg_reg(list,size,size,src,dst);
  480. end;
  481. exit;
  482. end
  483. else if (a <= high(word)) and
  484. ((op <> OP_AND) or
  485. not gotrlwi) then
  486. begin
  487. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  488. exit;
  489. end;
  490. { all basic constant instructions also have a shifted form that }
  491. { works only on the highest 16bits, so if lo(a) is 0, we can }
  492. { use that one }
  493. if (word(a) = 0) and
  494. (not(op = OP_AND) or
  495. not gotrlwi) then
  496. begin
  497. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  498. exit;
  499. end;
  500. end
  501. else if (op = OP_ADD) then
  502. if a = 0 then
  503. exit
  504. else if (longint(a) >= low(smallint)) and
  505. (longint(a) <= high(smallint)) then
  506. begin
  507. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  508. exit;
  509. end;
  510. { otherwise, the instructions we can generate depend on the }
  511. { operation }
  512. useReg := false;
  513. case op of
  514. OP_DIV,OP_IDIV:
  515. if (a = 0) then
  516. internalerror(200208103)
  517. else if (a = 1) then
  518. begin
  519. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  520. exit
  521. end
  522. else if ispowerof2(a,l1) then
  523. begin
  524. case op of
  525. OP_DIV:
  526. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  527. OP_IDIV:
  528. begin
  529. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  530. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  531. end;
  532. end;
  533. exit;
  534. end
  535. else
  536. usereg := true;
  537. OP_IMUL, OP_MUL:
  538. if (a = 0) then
  539. begin
  540. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  541. exit
  542. end
  543. else if (a = 1) then
  544. begin
  545. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  546. exit
  547. end
  548. else if ispowerof2(a,l1) then
  549. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  550. else if (longint(a) >= low(smallint)) and
  551. (longint(a) <= high(smallint)) then
  552. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  553. else
  554. usereg := true;
  555. OP_ADD:
  556. begin
  557. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  558. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  559. smallint((a shr 16) + ord(smallint(a) < 0))));
  560. end;
  561. OP_OR:
  562. { try to use rlwimi }
  563. if gotrlwi and
  564. (src = dst) then
  565. begin
  566. scratchreg := rg.getregisterint(list,OS_INT);
  567. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  568. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  569. scratchreg,0,l1,l2));
  570. rg.ungetregisterint(list,scratchreg);
  571. end
  572. else
  573. do_lo_hi;
  574. OP_AND:
  575. { try to use rlwinm }
  576. if gotrlwi then
  577. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  578. src,0,l1,l2))
  579. else
  580. useReg := true;
  581. OP_XOR:
  582. do_lo_hi;
  583. OP_SHL,OP_SHR,OP_SAR:
  584. begin
  585. if (a and 31) <> 0 Then
  586. list.concat(taicpu.op_reg_reg_const(
  587. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  588. else
  589. a_load_reg_reg(list,size,size,src,dst);
  590. if (a shr 5) <> 0 then
  591. internalError(68991);
  592. end
  593. else
  594. internalerror(200109091);
  595. end;
  596. { if all else failed, load the constant in a register and then }
  597. { perform the operation }
  598. if useReg then
  599. begin
  600. scratchreg := rg.getregisterint(list,OS_INT);
  601. a_load_const_reg(list,OS_32,a,scratchreg);
  602. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  603. rg.ungetregisterint(list,scratchreg);
  604. end;
  605. end;
  606. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  607. size: tcgsize; src1, src2, dst: tregister);
  608. const
  609. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  610. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  611. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  612. begin
  613. case op of
  614. OP_NEG,OP_NOT:
  615. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  616. else
  617. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  618. end;
  619. end;
  620. {*************** compare instructructions ****************}
  621. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  622. l : tasmlabel);
  623. var
  624. p: taicpu;
  625. scratch_register: TRegister;
  626. signed: boolean;
  627. begin
  628. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  629. { in the following case, we generate more efficient code when }
  630. { signed is true }
  631. if (cmp_op in [OC_EQ,OC_NE]) and
  632. (a > $ffff) then
  633. signed := true;
  634. if signed then
  635. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  636. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,longint(a)))
  637. else
  638. begin
  639. scratch_register := rg.getregisterint(list,OS_INT);
  640. a_load_const_reg(list,OS_32,a,scratch_register);
  641. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  642. rg.ungetregisterint(list,scratch_register);
  643. end
  644. else
  645. if (a <= $ffff) then
  646. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,a))
  647. else
  648. begin
  649. scratch_register := rg.getregisterint(list,OS_INT);
  650. a_load_const_reg(list,OS_32,a,scratch_register);
  651. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  652. rg.ungetregisterint(list,scratch_register);
  653. end;
  654. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  655. end;
  656. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  657. reg1,reg2 : tregister;l : tasmlabel);
  658. var
  659. p: taicpu;
  660. op: tasmop;
  661. begin
  662. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  663. op := A_CMPW
  664. else
  665. op := A_CMPLW;
  666. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  667. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  668. end;
  669. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);
  670. begin
  671. {$warning FIX ME}
  672. end;
  673. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);
  674. begin
  675. {$warning FIX ME}
  676. end;
  677. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  678. begin
  679. {$warning FIX ME}
  680. end;
  681. procedure tcgppc.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  682. begin
  683. {$warning FIX ME}
  684. end;
  685. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  686. begin
  687. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  688. end;
  689. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  690. begin
  691. a_jmp(list,A_B,C_None,0,l);
  692. end;
  693. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  694. var
  695. c: tasmcond;
  696. begin
  697. c := flags_to_cond(f);
  698. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  699. end;
  700. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  701. var
  702. testbit: byte;
  703. bitvalue: boolean;
  704. begin
  705. { get the bit to extract from the conditional register + its }
  706. { requested value (0 or 1) }
  707. testbit := ((f.cr-RS_CR0) * 4);
  708. case f.flag of
  709. F_EQ,F_NE:
  710. begin
  711. inc(testbit,2);
  712. bitvalue := f.flag = F_EQ;
  713. end;
  714. F_LT,F_GE:
  715. begin
  716. bitvalue := f.flag = F_LT;
  717. end;
  718. F_GT,F_LE:
  719. begin
  720. inc(testbit);
  721. bitvalue := f.flag = F_GT;
  722. end;
  723. else
  724. internalerror(200112261);
  725. end;
  726. { load the conditional register in the destination reg }
  727. list.concat(taicpu.op_reg(A_MFCR,reg));
  728. { we will move the bit that has to be tested to bit 0 by rotating }
  729. { left }
  730. testbit := (testbit + 1) and 31;
  731. { extract bit }
  732. list.concat(taicpu.op_reg_reg_const_const_const(
  733. A_RLWINM,reg,reg,testbit,31,31));
  734. { if we need the inverse, xor with 1 }
  735. if not bitvalue then
  736. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  737. end;
  738. (*
  739. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  740. var
  741. testbit: byte;
  742. bitvalue: boolean;
  743. begin
  744. { get the bit to extract from the conditional register + its }
  745. { requested value (0 or 1) }
  746. case f.simple of
  747. false:
  748. begin
  749. { we don't generate this in the compiler }
  750. internalerror(200109062);
  751. end;
  752. true:
  753. case f.cond of
  754. C_None:
  755. internalerror(200109063);
  756. C_LT..C_NU:
  757. begin
  758. testbit := (ord(f.cr) - ord(R_CR0))*4;
  759. inc(testbit,AsmCondFlag2BI[f.cond]);
  760. bitvalue := AsmCondFlagTF[f.cond];
  761. end;
  762. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  763. begin
  764. testbit := f.crbit
  765. bitvalue := AsmCondFlagTF[f.cond];
  766. end;
  767. else
  768. internalerror(200109064);
  769. end;
  770. end;
  771. { load the conditional register in the destination reg }
  772. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  773. { we will move the bit that has to be tested to bit 31 -> rotate }
  774. { left by bitpos+1 (remember, this is big-endian!) }
  775. if bitpos <> 31 then
  776. inc(bitpos)
  777. else
  778. bitpos := 0;
  779. { extract bit }
  780. list.concat(taicpu.op_reg_reg_const_const_const(
  781. A_RLWINM,reg,reg,bitpos,31,31));
  782. { if we need the inverse, xor with 1 }
  783. if not bitvalue then
  784. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  785. end;
  786. *)
  787. { *********** entry/exit code and address loading ************ }
  788. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  789. { generated the entry code of a procedure/function. Note: localsize is the }
  790. { sum of the size necessary for local variables and the maximum possible }
  791. { combined size of ALL the parameters of a procedure called by the current }
  792. { one. }
  793. { This procedure may be called before, as well as after
  794. g_return_from_proc is called.}
  795. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  796. href,href2 : treference;
  797. usesfpr,usesgpr,gotgot : boolean;
  798. parastart : aword;
  799. offset : aword;
  800. // r,r2,rsp:Tregister;
  801. regcounter2: Tsuperregister;
  802. regidx : tregisterindex;
  803. hp: tparaitem;
  804. begin
  805. { CR and LR only have to be saved in case they are modified by the current }
  806. { procedure, but currently this isn't checked, so save them always }
  807. { following is the entry code as described in "Altivec Programming }
  808. { Interface Manual", bar the saving of AltiVec registers }
  809. a_reg_alloc(list,NR_STACK_POINTER_REG);
  810. a_reg_alloc(list,NR_R0);
  811. if current_procinfo.procdef.parast.symtablelevel>1 then
  812. a_reg_alloc(list,NR_R11);
  813. usesfpr:=false;
  814. if not (po_assembler in current_procinfo.procdef.procoptions) then
  815. {$warning FIXME!!}
  816. { FIXME: has to be R_F8 instad of R_F14 for SYSV abi }
  817. for regcounter:=RS_F14 to RS_F31 do
  818. begin
  819. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  820. if regidx in rg.used_in_proc_other then
  821. begin
  822. usesfpr:= true;
  823. firstregfpu:=regcounter;
  824. break;
  825. end;
  826. end;
  827. usesgpr:=false;
  828. if not (po_assembler in current_procinfo.procdef.procoptions) then
  829. for regcounter2:=firstsaveintreg to RS_R31 do
  830. begin
  831. if regcounter2 in rg.used_in_proc_int then
  832. begin
  833. usesgpr:=true;
  834. firstreggpr:=regcounter2;
  835. break;
  836. end;
  837. end;
  838. { save link register? }
  839. if not (po_assembler in current_procinfo.procdef.procoptions) then
  840. if (pi_do_call in current_procinfo.flags) then
  841. begin
  842. { save return address... }
  843. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  844. { ... in caller's frame }
  845. case target_info.abi of
  846. abi_powerpc_aix:
  847. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  848. abi_powerpc_sysv:
  849. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  850. end;
  851. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  852. a_reg_dealloc(list,NR_R0);
  853. end;
  854. { save the CR if necessary in callers frame. }
  855. if not (po_assembler in current_procinfo.procdef.procoptions) then
  856. if target_info.abi = abi_powerpc_aix then
  857. if false then { Not needed at the moment. }
  858. begin
  859. a_reg_alloc(list,NR_R0);
  860. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  861. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  862. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  863. a_reg_dealloc(list,NR_R0);
  864. end;
  865. { !!! always allocate space for all registers for now !!! }
  866. if not (po_assembler in current_procinfo.procdef.procoptions) then
  867. { if usesfpr or usesgpr then }
  868. begin
  869. a_reg_alloc(list,NR_R12);
  870. { save end of fpr save area }
  871. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  872. end;
  873. if (localsize <> 0) then
  874. begin
  875. if (localsize <= high(smallint)) then
  876. begin
  877. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  878. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  879. end
  880. else
  881. begin
  882. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  883. { can't use getregisterint here, the register colouring }
  884. { is already done when we get here }
  885. href.index := NR_R11;
  886. a_reg_alloc(list,href.index);
  887. a_load_const_reg(list,OS_S32,-localsize,href.index);
  888. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  889. a_reg_dealloc(list,href.index);
  890. end;
  891. end;
  892. { no GOT pointer loaded yet }
  893. gotgot:=false;
  894. if usesfpr then
  895. begin
  896. { save floating-point registers
  897. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  898. begin
  899. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  900. gotgot:=true;
  901. end
  902. else
  903. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  904. }
  905. reference_reset_base(href,NR_R12,-8);
  906. for regcounter:=firstregfpu to RS_F31 do
  907. begin
  908. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  909. if regidx in rg.used_in_proc_other then
  910. begin
  911. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  912. dec(href.offset,8);
  913. end;
  914. end;
  915. { compute end of gpr save area }
  916. a_op_const_reg(list,OP_ADD,OS_ADDR,aword(href.offset+8),NR_R12);
  917. end;
  918. { save gprs and fetch GOT pointer }
  919. if usesgpr then
  920. begin
  921. {
  922. if cs_create_pic in aktmoduleswitches then
  923. begin
  924. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  925. gotgot:=true;
  926. end
  927. else
  928. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  929. }
  930. reference_reset_base(href,NR_R12,-4);
  931. for regcounter2:=firstsaveintreg to RS_R31 do
  932. begin
  933. if regcounter2 in rg.used_in_proc_int then
  934. begin
  935. usesgpr:=true;
  936. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  937. dec(href.offset,4);
  938. end;
  939. end;
  940. {
  941. r.enum:=R_INTREGISTER;
  942. r.:=;
  943. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  944. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  945. }
  946. end;
  947. if assigned(current_procinfo.procdef.parast) then
  948. begin
  949. if not (po_assembler in current_procinfo.procdef.procoptions) then
  950. begin
  951. { copy memory parameters to local parast }
  952. hp:=tparaitem(current_procinfo.procdef.para.first);
  953. while assigned(hp) do
  954. begin
  955. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  956. begin
  957. reference_reset_base(href,current_procinfo.framepointer,tvarsym(hp.parasym).adjusted_address);
  958. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  959. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  960. end
  961. {$ifdef dummy}
  962. else if (hp.calleeparaloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  963. begin
  964. rg.getexplicitregisterint(list,hp.calleeparaloc.register);
  965. end
  966. {$endif dummy}
  967. ;
  968. hp := tparaitem(hp.next);
  969. end;
  970. end;
  971. end;
  972. if usesfpr or usesgpr then
  973. a_reg_dealloc(list,NR_R12);
  974. { PIC code support, }
  975. if cs_create_pic in aktmoduleswitches then
  976. begin
  977. { if we didn't get the GOT pointer till now, we've to calculate it now }
  978. if not(gotgot) then
  979. begin
  980. {!!!!!!!!!!!!!}
  981. end;
  982. a_reg_alloc(list,NR_R31);
  983. { place GOT ptr in r31 }
  984. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  985. end;
  986. { save the CR if necessary ( !!! always done currently ) }
  987. { still need to find out where this has to be done for SystemV
  988. a_reg_alloc(list,R_0);
  989. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  990. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  991. new_reference(STACK_POINTER_REG,LA_CR)));
  992. a_reg_dealloc(list,R_0); }
  993. { now comes the AltiVec context save, not yet implemented !!! }
  994. { if we're in a nested procedure, we've to save R11 }
  995. if current_procinfo.procdef.parast.symtablelevel>2 then
  996. begin
  997. reference_reset_base(href,NR_STACK_POINTER_REG,PARENT_FRAMEPOINTER_OFFSET);
  998. list.concat(taicpu.op_reg_ref(A_STW,NR_R11,href));
  999. end;
  1000. end;
  1001. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  1002. { This procedure may be called before, as well as after
  1003. g_stackframe_entry is called.}
  1004. var
  1005. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1006. href : treference;
  1007. usesfpr,usesgpr,genret : boolean;
  1008. regcounter2:Tsuperregister;
  1009. localsize: aword;
  1010. regidx : tregisterindex;
  1011. begin
  1012. { AltiVec context restore, not yet implemented !!! }
  1013. usesfpr:=false;
  1014. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1015. for regcounter:=RS_F14 to RS_F31 do
  1016. begin
  1017. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1018. if regidx in rg.used_in_proc_other then
  1019. begin
  1020. usesfpr:=true;
  1021. firstregfpu:=regcounter;
  1022. break;
  1023. end;
  1024. end;
  1025. usesgpr:=false;
  1026. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1027. for regcounter2:=firstsaveintreg to RS_R31 do
  1028. begin
  1029. if regcounter2 in rg.used_in_proc_int then
  1030. begin
  1031. usesgpr:=true;
  1032. firstreggpr:=regcounter2;
  1033. break;
  1034. end;
  1035. end;
  1036. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1037. { no return (blr) generated yet }
  1038. genret:=true;
  1039. if usesgpr or usesfpr then
  1040. begin
  1041. { address of gpr save area to r11 }
  1042. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12);
  1043. if usesfpr then
  1044. begin
  1045. reference_reset_base(href,NR_R12,-8);
  1046. for regcounter := firstregfpu to RS_F31 do
  1047. begin
  1048. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1049. if regidx in rg.used_in_proc_other then
  1050. begin
  1051. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1052. dec(href.offset,8);
  1053. end;
  1054. end;
  1055. inc(href.offset,4);
  1056. end
  1057. else
  1058. reference_reset_base(href,NR_R12,-4);
  1059. for regcounter2:=firstsaveintreg to RS_R31 do
  1060. begin
  1061. if regcounter2 in rg.used_in_proc_int then
  1062. begin
  1063. usesgpr:=true;
  1064. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1065. dec(href.offset,4);
  1066. end;
  1067. end;
  1068. (*
  1069. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1070. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1071. *)
  1072. end;
  1073. (*
  1074. { restore fprs and return }
  1075. if usesfpr then
  1076. begin
  1077. { address of fpr save area to r11 }
  1078. r:=NR_R12;
  1079. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1080. {
  1081. if (pi_do_call in current_procinfo.flags) then
  1082. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1083. '_x')
  1084. else
  1085. { leaf node => lr haven't to be restored }
  1086. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1087. '_l');
  1088. genret:=false;
  1089. }
  1090. end;
  1091. *)
  1092. { if we didn't generate the return code, we've to do it now }
  1093. if genret then
  1094. begin
  1095. { adjust r1 }
  1096. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1097. { load link register? }
  1098. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1099. begin
  1100. if (pi_do_call in current_procinfo.flags) then
  1101. begin
  1102. case target_info.abi of
  1103. abi_powerpc_aix:
  1104. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1105. abi_powerpc_sysv:
  1106. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1107. end;
  1108. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1109. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1110. end;
  1111. { restore the CR if necessary from callers frame}
  1112. if target_info.abi = abi_powerpc_aix then
  1113. if false then { Not needed at the moment. }
  1114. begin
  1115. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1116. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1117. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1118. a_reg_dealloc(list,NR_R0);
  1119. end;
  1120. end;
  1121. list.concat(taicpu.op_none(A_BLR));
  1122. end;
  1123. end;
  1124. function save_regs(list : taasmoutput):longint;
  1125. {Generates code which saves used non-volatile registers in
  1126. the save area right below the address the stackpointer point to.
  1127. Returns the actual used save area size.}
  1128. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1129. usesfpr,usesgpr: boolean;
  1130. href : treference;
  1131. offset: integer;
  1132. regcounter2: Tsuperregister;
  1133. regidx : tregisterindex;
  1134. begin
  1135. usesfpr:=false;
  1136. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1137. for regcounter:=RS_F14 to RS_F31 do
  1138. begin
  1139. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1140. if regidx in rg.used_in_proc_other then
  1141. begin
  1142. usesfpr:=true;
  1143. firstregfpu:=regcounter;
  1144. break;
  1145. end;
  1146. end;
  1147. usesgpr:=false;
  1148. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1149. for regcounter2:=firstsaveintreg to RS_R31 do
  1150. begin
  1151. if regcounter2 in rg.used_in_proc_int then
  1152. begin
  1153. usesgpr:=true;
  1154. firstreggpr:=regcounter2;
  1155. break;
  1156. end;
  1157. end;
  1158. offset:= 0;
  1159. { save floating-point registers }
  1160. if usesfpr then
  1161. for regcounter := firstregfpu to RS_F31 do
  1162. begin
  1163. offset:= offset - 8;
  1164. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1165. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1166. end;
  1167. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1168. { save gprs in gpr save area }
  1169. if usesgpr then
  1170. if firstreggpr < RS_R30 then
  1171. begin
  1172. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1173. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1174. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1175. {STMW stores multiple registers}
  1176. end
  1177. else
  1178. begin
  1179. for regcounter := firstreggpr to RS_R31 do
  1180. begin
  1181. offset:= offset - 4;
  1182. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1183. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1184. end;
  1185. end;
  1186. { now comes the AltiVec context save, not yet implemented !!! }
  1187. save_regs:= -offset;
  1188. end;
  1189. procedure restore_regs(list : taasmoutput);
  1190. {Generates code which restores used non-volatile registers from
  1191. the save area right below the address the stackpointer point to.}
  1192. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1193. usesfpr,usesgpr: boolean;
  1194. href : treference;
  1195. offset: integer;
  1196. regcounter2: Tsuperregister;
  1197. regidx : tregisterindex;
  1198. begin
  1199. usesfpr:=false;
  1200. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1201. for regcounter:=RS_F14 to RS_F31 do
  1202. begin
  1203. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1204. if regidx in rg.used_in_proc_other then
  1205. begin
  1206. usesfpr:=true;
  1207. firstregfpu:=regcounter;
  1208. break;
  1209. end;
  1210. end;
  1211. usesgpr:=false;
  1212. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1213. for regcounter2:=RS_R13 to RS_R31 do
  1214. begin
  1215. if regcounter2 in rg.used_in_proc_int then
  1216. begin
  1217. usesgpr:=true;
  1218. firstreggpr:=regcounter2;
  1219. break;
  1220. end;
  1221. end;
  1222. offset:= 0;
  1223. { restore fp registers }
  1224. if usesfpr then
  1225. for regcounter := firstregfpu to RS_F31 do
  1226. begin
  1227. offset:= offset - 8;
  1228. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1229. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1230. end;
  1231. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1232. { restore gprs }
  1233. if usesgpr then
  1234. if firstreggpr < RS_R30 then
  1235. begin
  1236. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1237. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1238. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1239. {LMW loads multiple registers}
  1240. end
  1241. else
  1242. begin
  1243. for regcounter := firstreggpr to RS_R31 do
  1244. begin
  1245. offset:= offset - 4;
  1246. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1247. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1248. end;
  1249. end;
  1250. { now comes the AltiVec context restore, not yet implemented !!! }
  1251. end;
  1252. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1253. (* NOT IN USE *)
  1254. { generated the entry code of a procedure/function. Note: localsize is the }
  1255. { sum of the size necessary for local variables and the maximum possible }
  1256. { combined size of ALL the parameters of a procedure called by the current }
  1257. { one }
  1258. const
  1259. macosLinkageAreaSize = 24;
  1260. var regcounter: TRegister;
  1261. href : treference;
  1262. registerSaveAreaSize : longint;
  1263. begin
  1264. if (localsize mod 8) <> 0 then
  1265. internalerror(58991);
  1266. { CR and LR only have to be saved in case they are modified by the current }
  1267. { procedure, but currently this isn't checked, so save them always }
  1268. { following is the entry code as described in "Altivec Programming }
  1269. { Interface Manual", bar the saving of AltiVec registers }
  1270. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1271. a_reg_alloc(list,NR_R0);
  1272. { save return address in callers frame}
  1273. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1274. { ... in caller's frame }
  1275. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1276. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1277. a_reg_dealloc(list,NR_R0);
  1278. { save non-volatile registers in callers frame}
  1279. registerSaveAreaSize:= save_regs(list);
  1280. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1281. a_reg_alloc(list,NR_R0);
  1282. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1283. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1284. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1285. a_reg_dealloc(list,NR_R0);
  1286. (*
  1287. { save pointer to incoming arguments }
  1288. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1289. *)
  1290. (*
  1291. a_reg_alloc(list,R_12);
  1292. { 0 or 8 based on SP alignment }
  1293. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1294. R_12,STACK_POINTER_REG,0,28,28));
  1295. { add in stack length }
  1296. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1297. -localsize));
  1298. { establish new alignment }
  1299. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1300. a_reg_dealloc(list,R_12);
  1301. *)
  1302. { allocate stack frame }
  1303. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1304. inc(localsize,tg.lasttemp);
  1305. localsize:=align(localsize,16);
  1306. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1307. if (localsize <> 0) then
  1308. begin
  1309. if (localsize <= high(smallint)) then
  1310. begin
  1311. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1312. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1313. end
  1314. else
  1315. begin
  1316. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1317. href.index := NR_R11;
  1318. a_reg_alloc(list,href.index);
  1319. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1320. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1321. a_reg_dealloc(list,href.index);
  1322. end;
  1323. end;
  1324. end;
  1325. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1326. (* NOT IN USE *)
  1327. var
  1328. href : treference;
  1329. begin
  1330. a_reg_alloc(list,NR_R0);
  1331. { restore stack pointer }
  1332. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1333. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1334. (*
  1335. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1336. *)
  1337. { restore the CR if necessary from callers frame
  1338. ( !!! always done currently ) }
  1339. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1340. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1341. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1342. a_reg_dealloc(list,NR_R0);
  1343. (*
  1344. { restore return address from callers frame }
  1345. reference_reset_base(href,STACK_POINTER_REG,8);
  1346. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1347. *)
  1348. { restore non-volatile registers from callers frame }
  1349. restore_regs(list);
  1350. (*
  1351. { return to caller }
  1352. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1353. list.concat(taicpu.op_none(A_BLR));
  1354. *)
  1355. { restore return address from callers frame }
  1356. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1357. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1358. { return to caller }
  1359. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1360. list.concat(taicpu.op_none(A_BLR));
  1361. end;
  1362. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1363. begin
  1364. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1365. end;
  1366. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1367. var
  1368. ref2, tmpref: treference;
  1369. freereg: boolean;
  1370. tmpreg:Tregister;
  1371. begin
  1372. ref2 := ref;
  1373. freereg := fixref(list,ref2);
  1374. if assigned(ref2.symbol) then
  1375. begin
  1376. if target_info.system = system_powerpc_macos then
  1377. begin
  1378. if macos_direct_globals then
  1379. begin
  1380. reference_reset(tmpref);
  1381. tmpref.offset := ref2.offset;
  1382. tmpref.symbol := ref2.symbol;
  1383. tmpref.base := NR_NO;
  1384. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1385. end
  1386. else
  1387. begin
  1388. reference_reset(tmpref);
  1389. tmpref.symbol := ref2.symbol;
  1390. tmpref.offset := 0;
  1391. tmpref.base := NR_RTOC;
  1392. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1393. if ref2.offset <> 0 then
  1394. begin
  1395. reference_reset(tmpref);
  1396. tmpref.offset := ref2.offset;
  1397. tmpref.base:= r;
  1398. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1399. end;
  1400. end;
  1401. if ref2.base <> NR_NO then
  1402. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1403. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1404. end
  1405. else
  1406. begin
  1407. { add the symbol's value to the base of the reference, and if the }
  1408. { reference doesn't have a base, create one }
  1409. reference_reset(tmpref);
  1410. tmpref.offset := ref2.offset;
  1411. tmpref.symbol := ref2.symbol;
  1412. tmpref.symaddr := refs_ha;
  1413. if ref2.base<> NR_NO then
  1414. begin
  1415. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1416. ref2.base,tmpref));
  1417. if freereg then
  1418. begin
  1419. rg.ungetregisterint(list,ref2.base);
  1420. freereg := false;
  1421. end;
  1422. end
  1423. else
  1424. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1425. tmpref.base := NR_NO;
  1426. tmpref.symaddr := refs_l;
  1427. { can be folded with one of the next instructions by the }
  1428. { optimizer probably }
  1429. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1430. end
  1431. end
  1432. else if ref2.offset <> 0 Then
  1433. if ref2.base <> NR_NO then
  1434. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1435. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1436. { occurs, so now only ref.offset has to be loaded }
  1437. else
  1438. a_load_const_reg(list,OS_32,ref2.offset,r)
  1439. else if ref.index <> NR_NO Then
  1440. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1441. else if (ref2.base <> NR_NO) and
  1442. (r <> ref2.base) then
  1443. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1444. if freereg then
  1445. rg.ungetregisterint(list,ref2.base);
  1446. end;
  1447. { ************* concatcopy ************ }
  1448. {$ifndef ppc603}
  1449. const
  1450. maxmoveunit = 8;
  1451. {$else ppc603}
  1452. const
  1453. maxmoveunit = 4;
  1454. {$endif ppc603}
  1455. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1456. var
  1457. countreg: TRegister;
  1458. src, dst: TReference;
  1459. lab: tasmlabel;
  1460. count, count2: aword;
  1461. orgsrc, orgdst: boolean;
  1462. size: tcgsize;
  1463. begin
  1464. {$ifdef extdebug}
  1465. if len > high(longint) then
  1466. internalerror(2002072704);
  1467. {$endif extdebug}
  1468. { make sure short loads are handled as optimally as possible }
  1469. if not loadref then
  1470. if (len <= maxmoveunit) and
  1471. (byte(len) in [1,2,4,8]) then
  1472. begin
  1473. if len < 8 then
  1474. begin
  1475. size := int_cgsize(len);
  1476. a_load_ref_ref(list,size,size,source,dest);
  1477. if delsource then
  1478. begin
  1479. reference_release(list,source);
  1480. tg.ungetiftemp(list,source);
  1481. end;
  1482. end
  1483. else
  1484. begin
  1485. a_reg_alloc(list,NR_F0);
  1486. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1487. if delsource then
  1488. begin
  1489. reference_release(list,source);
  1490. tg.ungetiftemp(list,source);
  1491. end;
  1492. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1493. a_reg_dealloc(list,NR_F0);
  1494. end;
  1495. exit;
  1496. end;
  1497. count := len div maxmoveunit;
  1498. reference_reset(src);
  1499. reference_reset(dst);
  1500. { load the address of source into src.base }
  1501. if loadref then
  1502. begin
  1503. src.base := rg.getregisterint(list,OS_ADDR);
  1504. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1505. orgsrc := false;
  1506. end
  1507. else if (count > 4) or
  1508. not issimpleref(source) or
  1509. ((source.index <> NR_NO) and
  1510. ((source.offset + longint(len)) > high(smallint))) then
  1511. begin
  1512. src.base := rg.getregisterint(list,OS_ADDR);
  1513. a_loadaddr_ref_reg(list,source,src.base);
  1514. orgsrc := false;
  1515. end
  1516. else
  1517. begin
  1518. src := source;
  1519. orgsrc := true;
  1520. end;
  1521. if not orgsrc and delsource then
  1522. reference_release(list,source);
  1523. { load the address of dest into dst.base }
  1524. if (count > 4) or
  1525. not issimpleref(dest) or
  1526. ((dest.index <> NR_NO) and
  1527. ((dest.offset + longint(len)) > high(smallint))) then
  1528. begin
  1529. dst.base := rg.getregisterint(list,OS_ADDR);
  1530. a_loadaddr_ref_reg(list,dest,dst.base);
  1531. orgdst := false;
  1532. end
  1533. else
  1534. begin
  1535. dst := dest;
  1536. orgdst := true;
  1537. end;
  1538. {$ifndef ppc603}
  1539. if count > 4 then
  1540. { generate a loop }
  1541. begin
  1542. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1543. { have to be set to 8. I put an Inc there so debugging may be }
  1544. { easier (should offset be different from zero here, it will be }
  1545. { easy to notice in the generated assembler }
  1546. inc(dst.offset,8);
  1547. inc(src.offset,8);
  1548. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1549. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1550. countreg := rg.getregisterint(list,OS_INT);
  1551. a_load_const_reg(list,OS_32,count,countreg);
  1552. { explicitely allocate R_0 since it can be used safely here }
  1553. { (for holding date that's being copied) }
  1554. a_reg_alloc(list,NR_F0);
  1555. objectlibrary.getlabel(lab);
  1556. a_label(list, lab);
  1557. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1558. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1559. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1560. a_jmp(list,A_BC,C_NE,0,lab);
  1561. rg.ungetregisterint(list,countreg);
  1562. a_reg_dealloc(list,NR_F0);
  1563. len := len mod 8;
  1564. end;
  1565. count := len div 8;
  1566. if count > 0 then
  1567. { unrolled loop }
  1568. begin
  1569. a_reg_alloc(list,NR_F0);
  1570. for count2 := 1 to count do
  1571. begin
  1572. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1573. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1574. inc(src.offset,8);
  1575. inc(dst.offset,8);
  1576. end;
  1577. a_reg_dealloc(list,NR_F0);
  1578. len := len mod 8;
  1579. end;
  1580. if (len and 4) <> 0 then
  1581. begin
  1582. a_reg_alloc(list,NR_R0);
  1583. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1584. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1585. inc(src.offset,4);
  1586. inc(dst.offset,4);
  1587. a_reg_dealloc(list,NR_R0);
  1588. end;
  1589. {$else not ppc603}
  1590. if count > 4 then
  1591. { generate a loop }
  1592. begin
  1593. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1594. { have to be set to 4. I put an Inc there so debugging may be }
  1595. { easier (should offset be different from zero here, it will be }
  1596. { easy to notice in the generated assembler }
  1597. inc(dst.offset,4);
  1598. inc(src.offset,4);
  1599. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1600. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1601. countreg := rg.getregisterint(list,OS_INT);
  1602. a_load_const_reg(list,OS_32,count,countreg);
  1603. { explicitely allocate R_0 since it can be used safely here }
  1604. { (for holding date that's being copied) }
  1605. a_reg_alloc(list,NR_R0);
  1606. objectlibrary.getlabel(lab);
  1607. a_label(list, lab);
  1608. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1609. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1610. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1611. a_jmp(list,A_BC,C_NE,0,lab);
  1612. rg.ungetregisterint(list,countreg);
  1613. a_reg_dealloc(list,NR_R0);
  1614. len := len mod 4;
  1615. end;
  1616. count := len div 4;
  1617. if count > 0 then
  1618. { unrolled loop }
  1619. begin
  1620. a_reg_alloc(list,NR_R0);
  1621. for count2 := 1 to count do
  1622. begin
  1623. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1624. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1625. inc(src.offset,4);
  1626. inc(dst.offset,4);
  1627. end;
  1628. a_reg_dealloc(list,r);
  1629. len := len mod 4;
  1630. end;
  1631. {$endif not ppc603}
  1632. { copy the leftovers }
  1633. if (len and 2) <> 0 then
  1634. begin
  1635. a_reg_alloc(list,NR_R0);
  1636. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1637. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1638. inc(src.offset,2);
  1639. inc(dst.offset,2);
  1640. a_reg_dealloc(list,NR_R0);
  1641. end;
  1642. if (len and 1) <> 0 then
  1643. begin
  1644. a_reg_alloc(list,NR_R0);
  1645. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1646. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1647. a_reg_dealloc(list,NR_R0);
  1648. end;
  1649. if orgsrc then
  1650. begin
  1651. if delsource then
  1652. reference_release(list,source);
  1653. end
  1654. else
  1655. rg.ungetregisterint(list,src.base);
  1656. if not orgdst then
  1657. rg.ungetregisterint(list,dst.base);
  1658. if delsource then
  1659. tg.ungetiftemp(list,source);
  1660. end;
  1661. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  1662. var
  1663. power,len : longint;
  1664. {$ifndef __NOWINPECOFF__}
  1665. again,ok : tasmlabel;
  1666. {$endif}
  1667. // r,r2,rsp:Tregister;
  1668. begin
  1669. {$warning !!!! FIX ME !!!!}
  1670. internalerror(200305231);
  1671. (* !!!!
  1672. lenref:=ref;
  1673. inc(lenref.offset,4);
  1674. { get stack space }
  1675. r.enum:=R_INTREGISTER;
  1676. r.number:=NR_EDI;
  1677. rsp.enum:=R_INTREGISTER;
  1678. rsp.number:=NR_ESP;
  1679. r2.enum:=R_INTREGISTER;
  1680. rg.getexplicitregisterint(list,NR_EDI);
  1681. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1682. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1683. if (elesize<>1) then
  1684. begin
  1685. if ispowerof2(elesize, power) then
  1686. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1687. else
  1688. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1689. end;
  1690. {$ifndef __NOWINPECOFF__}
  1691. { windows guards only a few pages for stack growing, }
  1692. { so we have to access every page first }
  1693. if target_info.system=system_i386_win32 then
  1694. begin
  1695. objectlibrary.getlabel(again);
  1696. objectlibrary.getlabel(ok);
  1697. a_label(list,again);
  1698. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1699. a_jmp_cond(list,OC_B,ok);
  1700. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1701. r2.number:=NR_EAX;
  1702. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1703. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1704. a_jmp_always(list,again);
  1705. a_label(list,ok);
  1706. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1707. rg.ungetregisterint(list,r);
  1708. { now reload EDI }
  1709. rg.getexplicitregisterint(list,NR_EDI);
  1710. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1711. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1712. if (elesize<>1) then
  1713. begin
  1714. if ispowerof2(elesize, power) then
  1715. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1716. else
  1717. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1718. end;
  1719. end
  1720. else
  1721. {$endif __NOWINPECOFF__}
  1722. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1723. { align stack on 4 bytes }
  1724. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1725. { load destination }
  1726. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1727. { don't destroy the registers! }
  1728. r2.number:=NR_ECX;
  1729. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1730. r2.number:=NR_ESI;
  1731. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1732. { load count }
  1733. r2.number:=NR_ECX;
  1734. a_load_ref_reg(list,OS_INT,lenref,r2);
  1735. { load source }
  1736. r2.number:=NR_ESI;
  1737. a_load_ref_reg(list,OS_INT,ref,r2);
  1738. { scheduled .... }
  1739. r2.number:=NR_ECX;
  1740. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1741. { calculate size }
  1742. len:=elesize;
  1743. opsize:=S_B;
  1744. if (len and 3)=0 then
  1745. begin
  1746. opsize:=S_L;
  1747. len:=len shr 2;
  1748. end
  1749. else
  1750. if (len and 1)=0 then
  1751. begin
  1752. opsize:=S_W;
  1753. len:=len shr 1;
  1754. end;
  1755. if ispowerof2(len, power) then
  1756. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1757. else
  1758. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1759. list.concat(Taicpu.op_none(A_REP,S_NO));
  1760. case opsize of
  1761. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1762. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1763. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1764. end;
  1765. rg.ungetregisterint(list,r);
  1766. r2.number:=NR_ESI;
  1767. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1768. r2.number:=NR_ECX;
  1769. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1770. { patch the new address }
  1771. a_load_reg_ref(list,OS_INT,rsp,ref);
  1772. !!!! *)
  1773. end;
  1774. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1775. var
  1776. hl : tasmlabel;
  1777. begin
  1778. if not(cs_check_overflow in aktlocalswitches) then
  1779. exit;
  1780. objectlibrary.getlabel(hl);
  1781. if not ((def.deftype=pointerdef) or
  1782. ((def.deftype=orddef) and
  1783. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1784. bool8bit,bool16bit,bool32bit]))) then
  1785. begin
  1786. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1787. a_jmp(list,A_BC,C_OV,7,hl)
  1788. end
  1789. else
  1790. a_jmp_cond(list,OC_AE,hl);
  1791. a_call_name(list,'FPC_OVERFLOW');
  1792. a_label(list,hl);
  1793. end;
  1794. {***************** This is private property, keep out! :) *****************}
  1795. function tcgppc.issimpleref(const ref: treference): boolean;
  1796. begin
  1797. if (ref.base = NR_NO) and
  1798. (ref.index <> NR_NO) then
  1799. internalerror(200208101);
  1800. result :=
  1801. not(assigned(ref.symbol)) and
  1802. (((ref.index = NR_NO) and
  1803. (ref.offset >= low(smallint)) and
  1804. (ref.offset <= high(smallint))) or
  1805. ((ref.index <> NR_NO) and
  1806. (ref.offset = 0)));
  1807. end;
  1808. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1809. var
  1810. tmpreg: tregister;
  1811. orgindex: tregister;
  1812. freeindex: boolean;
  1813. begin
  1814. result := false;
  1815. if (ref.base = NR_NO) then
  1816. begin
  1817. ref.base := ref.index;
  1818. ref.base := NR_NO;
  1819. end;
  1820. if (ref.base <> NR_NO) then
  1821. begin
  1822. if (ref.index <> NR_NO) and
  1823. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1824. begin
  1825. result := true;
  1826. { references are often freed before they are used. Since we allocate }
  1827. { a register here, we must first reallocate the index register, since }
  1828. { otherwise it may be overwritten (and it's still used afterwards) }
  1829. freeindex := false;
  1830. if (getsupreg(ref.index) >= first_int_supreg) and
  1831. (getsupreg(ref.index) in rg.unusedregsint) then
  1832. begin
  1833. rg.getexplicitregisterint(list,ref.index);
  1834. orgindex := ref.index;
  1835. freeindex := true;
  1836. end;
  1837. tmpreg := rg.getregisterint(list,OS_ADDR);
  1838. if not assigned(ref.symbol) and
  1839. (cardinal(ref.offset-low(smallint)) <=
  1840. high(smallint)-low(smallint)) then
  1841. begin
  1842. list.concat(taicpu.op_reg_reg_const(
  1843. A_ADDI,tmpreg,ref.base,ref.offset));
  1844. ref.offset := 0;
  1845. end
  1846. else
  1847. begin
  1848. list.concat(taicpu.op_reg_reg_reg(
  1849. A_ADD,tmpreg,ref.base,ref.index));
  1850. ref.index := NR_NO;
  1851. end;
  1852. ref.base := tmpreg;
  1853. if freeindex then
  1854. rg.ungetregisterint(list,orgindex);
  1855. end
  1856. end
  1857. else
  1858. if ref.index <> NR_NO then
  1859. internalerror(200208102);
  1860. end;
  1861. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1862. { that's the case, we can use rlwinm to do an AND operation }
  1863. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1864. var
  1865. temp : longint;
  1866. testbit : aword;
  1867. compare: boolean;
  1868. begin
  1869. get_rlwi_const := false;
  1870. if (a = 0) or (a = $ffffffff) then
  1871. exit;
  1872. { start with the lowest bit }
  1873. testbit := 1;
  1874. { check its value }
  1875. compare := boolean(a and testbit);
  1876. { find out how long the run of bits with this value is }
  1877. { (it's impossible that all bits are 1 or 0, because in that case }
  1878. { this function wouldn't have been called) }
  1879. l1 := 31;
  1880. while (((a and testbit) <> 0) = compare) do
  1881. begin
  1882. testbit := testbit shl 1;
  1883. dec(l1);
  1884. end;
  1885. { check the length of the run of bits that comes next }
  1886. compare := not compare;
  1887. l2 := l1;
  1888. while (((a and testbit) <> 0) = compare) and
  1889. (l2 >= 0) do
  1890. begin
  1891. testbit := testbit shl 1;
  1892. dec(l2);
  1893. end;
  1894. { and finally the check whether the rest of the bits all have the }
  1895. { same value }
  1896. compare := not compare;
  1897. temp := l2;
  1898. if temp >= 0 then
  1899. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1900. exit;
  1901. { we have done "not(not(compare))", so compare is back to its }
  1902. { initial value. If the lowest bit was 0, a is of the form }
  1903. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1904. { because l2 now contains the position of the last zero of the }
  1905. { first run instead of that of the first 1) so switch l1 and l2 }
  1906. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1907. if not compare then
  1908. begin
  1909. temp := l1;
  1910. l1 := l2+1;
  1911. l2 := temp;
  1912. end
  1913. else
  1914. { otherwise, l1 currently contains the position of the last }
  1915. { zero instead of that of the first 1 of the second run -> +1 }
  1916. inc(l1);
  1917. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1918. l1 := l1 and 31;
  1919. l2 := l2 and 31;
  1920. get_rlwi_const := true;
  1921. end;
  1922. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1923. ref: treference);
  1924. var
  1925. tmpreg: tregister;
  1926. tmpregUsed: Boolean;
  1927. tmpref: treference;
  1928. largeOffset: Boolean;
  1929. begin
  1930. tmpreg := NR_NO;
  1931. if target_info.system = system_powerpc_macos then
  1932. begin
  1933. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1934. high(smallint)-low(smallint));
  1935. tmpreg := rg.getregisterint(list,OS_ADDR);
  1936. tmpregUsed:= false;
  1937. if assigned(ref.symbol) then
  1938. begin //Load symbol's value
  1939. reference_reset(tmpref);
  1940. tmpref.symbol := ref.symbol;
  1941. tmpref.base := NR_RTOC;
  1942. if macos_direct_globals then
  1943. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1944. else
  1945. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1946. tmpregUsed:= true;
  1947. end;
  1948. if largeOffset then
  1949. begin //Add hi part of offset
  1950. reference_reset(tmpref);
  1951. tmpref.offset := Hi(ref.offset);
  1952. if tmpregUsed then
  1953. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1954. tmpreg,tmpref))
  1955. else
  1956. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1957. tmpregUsed:= true;
  1958. end;
  1959. if tmpregUsed then
  1960. begin
  1961. //Add content of base register
  1962. if ref.base <> NR_NO then
  1963. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1964. ref.base,tmpreg));
  1965. //Make ref ready to be used by op
  1966. ref.symbol:= nil;
  1967. ref.base:= tmpreg;
  1968. if largeOffset then
  1969. ref.offset := Lo(ref.offset);
  1970. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1971. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1972. end
  1973. else
  1974. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1975. end
  1976. else {if target_info.system <> system_powerpc_macos}
  1977. begin
  1978. if assigned(ref.symbol) or
  1979. (cardinal(ref.offset-low(smallint)) >
  1980. high(smallint)-low(smallint)) then
  1981. begin
  1982. tmpreg := rg.getregisterint(list,OS_ADDR);
  1983. reference_reset(tmpref);
  1984. tmpref.symbol := ref.symbol;
  1985. tmpref.offset := ref.offset;
  1986. tmpref.symaddr := refs_ha;
  1987. if ref.base <> NR_NO then
  1988. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1989. ref.base,tmpref))
  1990. else
  1991. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1992. ref.base := tmpreg;
  1993. ref.symaddr := refs_l;
  1994. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1995. end
  1996. else
  1997. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1998. end;
  1999. if (tmpreg <> NR_NO) then
  2000. rg.ungetregisterint(list,tmpreg);
  2001. end;
  2002. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  2003. crval: longint; l: tasmlabel);
  2004. var
  2005. p: taicpu;
  2006. begin
  2007. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  2008. if op <> A_B then
  2009. create_cond_norm(c,crval,p.condition);
  2010. p.is_jmp := true;
  2011. list.concat(p)
  2012. end;
  2013. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2014. begin
  2015. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2016. end;
  2017. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  2018. begin
  2019. a_op64_const_reg_reg(list,op,value,reg,reg);
  2020. end;
  2021. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2022. begin
  2023. case op of
  2024. OP_AND,OP_OR,OP_XOR:
  2025. begin
  2026. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2027. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2028. end;
  2029. OP_ADD:
  2030. begin
  2031. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2032. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2033. end;
  2034. OP_SUB:
  2035. begin
  2036. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2037. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2038. end;
  2039. else
  2040. internalerror(2002072801);
  2041. end;
  2042. end;
  2043. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2044. const
  2045. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2046. (A_SUBIC,A_SUBC,A_ADDME));
  2047. var
  2048. tmpreg: tregister;
  2049. tmpreg64: tregister64;
  2050. issub: boolean;
  2051. begin
  2052. case op of
  2053. OP_AND,OP_OR,OP_XOR:
  2054. begin
  2055. cg.a_op_const_reg_reg(list,op,OS_32,aword(value),regsrc.reglo,regdst.reglo);
  2056. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2057. regdst.reghi);
  2058. end;
  2059. OP_ADD, OP_SUB:
  2060. begin
  2061. if (int64(value) < 0) then
  2062. begin
  2063. if op = OP_ADD then
  2064. op := OP_SUB
  2065. else
  2066. op := OP_ADD;
  2067. int64(value) := -int64(value);
  2068. end;
  2069. if (longint(value) <> 0) then
  2070. begin
  2071. issub := op = OP_SUB;
  2072. if (int64(value) > 0) and
  2073. (int64(value)-ord(issub) <= 32767) then
  2074. begin
  2075. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2076. regdst.reglo,regsrc.reglo,longint(value)));
  2077. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2078. regdst.reghi,regsrc.reghi));
  2079. end
  2080. else if ((value shr 32) = 0) then
  2081. begin
  2082. tmpreg := rg.getregisterint(list,OS_32);
  2083. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2084. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2085. regdst.reglo,regsrc.reglo,tmpreg));
  2086. rg.ungetregisterint(list,tmpreg);
  2087. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2088. regdst.reghi,regsrc.reghi));
  2089. end
  2090. else
  2091. begin
  2092. tmpreg64.reglo := rg.getregisterint(list,OS_32);
  2093. tmpreg64.reghi := rg.getregisterint(list,OS_32);
  2094. a_load64_const_reg(list,value,tmpreg64);
  2095. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2096. rg.ungetregisterint(list,tmpreg64.reglo);
  2097. rg.ungetregisterint(list,tmpreg64.reghi);
  2098. end
  2099. end
  2100. else
  2101. begin
  2102. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2103. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2104. regdst.reghi);
  2105. end;
  2106. end;
  2107. else
  2108. internalerror(2002072802);
  2109. end;
  2110. end;
  2111. begin
  2112. cg := tcgppc.create;
  2113. cg64 :=tcg64fppc.create;
  2114. end.
  2115. {
  2116. $Log$
  2117. Revision 1.125 2003-09-03 21:04:14 peter
  2118. * some fixes for ppc
  2119. Revision 1.124 2003/09/03 19:35:24 peter
  2120. * powerpc compiles again
  2121. Revision 1.123 2003/09/03 15:55:01 peter
  2122. * NEWRA branch merged
  2123. Revision 1.122.2.1 2003/08/31 21:08:16 peter
  2124. * first batch of sparc fixes
  2125. Revision 1.122 2003/08/18 21:27:00 jonas
  2126. * some newra optimizations (eliminate lots of moves between registers)
  2127. Revision 1.121 2003/08/18 11:50:55 olle
  2128. + cleaning up in proc entry and exit, now calc_stack_frame always is used.
  2129. Revision 1.120 2003/08/17 16:59:20 jonas
  2130. * fixed regvars so they work with newra (at least for ppc)
  2131. * fixed some volatile register bugs
  2132. + -dnotranslation option for -dnewra, which causes the registers not to
  2133. be translated from virtual to normal registers. Requires support in
  2134. the assembler writer as well, which is only implemented in aggas/
  2135. agppcgas currently
  2136. Revision 1.119 2003/08/11 21:18:20 peter
  2137. * start of sparc support for newra
  2138. Revision 1.118 2003/08/08 15:50:45 olle
  2139. * merged macos entry/exit code generation into the general one.
  2140. Revision 1.117 2002/10/01 05:24:28 olle
  2141. * made a_load_store more robust and to accept large offsets and cleaned up code
  2142. Revision 1.116 2003/07/23 11:02:23 jonas
  2143. * don't use rg.getregisterint() anymore in g_stackframe_entry_*, because
  2144. the register colouring has already occurred then, use a hard-coded
  2145. register instead
  2146. Revision 1.115 2003/07/20 20:39:20 jonas
  2147. * fixed newra bug due to the fact that we sometimes need a temp reg
  2148. when loading/storing to memory (base+index+offset is not possible)
  2149. and because a reference is often freed before it is last used, this
  2150. temp register was soemtimes the same as one of the reference regs
  2151. Revision 1.114 2003/07/20 16:15:58 jonas
  2152. * fixed bug in g_concatcopy with -dnewra
  2153. Revision 1.113 2003/07/06 20:25:03 jonas
  2154. * fixed ppc compiler
  2155. Revision 1.112 2003/07/05 20:11:42 jonas
  2156. * create_paraloc_info() is now called separately for the caller and
  2157. callee info
  2158. * fixed ppc cycle
  2159. Revision 1.111 2003/07/02 22:18:04 peter
  2160. * paraloc splitted in callerparaloc,calleeparaloc
  2161. * sparc calling convention updates
  2162. Revision 1.110 2003/06/18 10:12:36 olle
  2163. * macos: fixes of loading-code
  2164. Revision 1.109 2003/06/14 22:32:43 jonas
  2165. * ppc compiles with -dnewra, haven't tried to compile anything with it
  2166. yet though
  2167. Revision 1.108 2003/06/13 21:19:31 peter
  2168. * current_procdef removed, use current_procinfo.procdef instead
  2169. Revision 1.107 2003/06/09 14:54:26 jonas
  2170. * (de)allocation of registers for parameters is now performed properly
  2171. (and checked on the ppc)
  2172. - removed obsolete allocation of all parameter registers at the start
  2173. of a procedure (and deallocation at the end)
  2174. Revision 1.106 2003/06/08 18:19:27 jonas
  2175. - removed duplicate identifier
  2176. Revision 1.105 2003/06/07 18:57:04 jonas
  2177. + added freeintparaloc
  2178. * ppc get/freeintparaloc now check whether the parameter regs are
  2179. properly allocated/deallocated (and get an extra list para)
  2180. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  2181. * fixed lot of missing pi_do_call's
  2182. Revision 1.104 2003/06/04 11:58:58 jonas
  2183. * calculate localsize also in g_return_from_proc since it's now called
  2184. before g_stackframe_entry (still have to fix macos)
  2185. * compilation fixes (cycle doesn't work yet though)
  2186. Revision 1.103 2003/06/01 21:38:06 peter
  2187. * getregisterfpu size parameter added
  2188. * op_const_reg size parameter added
  2189. * sparc updates
  2190. Revision 1.102 2003/06/01 13:42:18 jonas
  2191. * fix for bug in fixref that Peter found during the Sparc conversion
  2192. Revision 1.101 2003/05/30 18:52:10 jonas
  2193. * fixed bug with intregvars
  2194. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2195. rcgppc.a_param_ref, which previously got bogus size values
  2196. Revision 1.100 2003/05/29 21:17:27 jonas
  2197. * compile with -dppc603 to not use unaligned float loads in move() and
  2198. g_concatcopy, because the 603 and 604 take an exception for those
  2199. (and netbsd doesn't even handle those in the kernel). There are
  2200. still some of those left that could cause problems though (e.g.
  2201. in the set helpers)
  2202. Revision 1.99 2003/05/29 10:06:09 jonas
  2203. * also free temps in g_concatcopy if delsource is true
  2204. Revision 1.98 2003/05/28 23:58:18 jonas
  2205. * added missing initialization of rg.usedintin,byproc
  2206. * ppc now also saves/restores used fpu registers
  2207. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  2208. i386
  2209. Revision 1.97 2003/05/28 23:18:31 florian
  2210. * started to fix and clean up the sparc port
  2211. Revision 1.96 2003/05/24 11:59:42 jonas
  2212. * fixed integer typeconversion problems
  2213. Revision 1.95 2003/05/23 18:51:26 jonas
  2214. * fixed support for nested procedures and more parameters than those
  2215. which fit in registers (untested/probably not working: calling a
  2216. nested procedure from a deeper nested procedure)
  2217. Revision 1.94 2003/05/20 23:54:00 florian
  2218. + basic darwin support added
  2219. Revision 1.93 2003/05/15 22:14:42 florian
  2220. * fixed last commit, changing lastsaveintreg to r31 caused some strange problems
  2221. Revision 1.92 2003/05/15 21:37:00 florian
  2222. * sysv entry code saves r13 now as well
  2223. Revision 1.91 2003/05/15 19:39:09 florian
  2224. * fixed ppc compiler which was broken by Peter's changes
  2225. Revision 1.90 2003/05/12 18:43:50 jonas
  2226. * fixed g_concatcopy
  2227. Revision 1.89 2003/05/11 20:59:23 jonas
  2228. * fixed bug with large offsets in entrycode
  2229. Revision 1.88 2003/05/11 11:45:08 jonas
  2230. * fixed shifts
  2231. Revision 1.87 2003/05/11 11:07:33 jonas
  2232. * fixed optimizations in a_op_const_reg_reg()
  2233. Revision 1.86 2003/04/27 11:21:36 peter
  2234. * aktprocdef renamed to current_procinfo.procdef
  2235. * procinfo renamed to current_procinfo
  2236. * procinfo will now be stored in current_module so it can be
  2237. cleaned up properly
  2238. * gen_main_procsym changed to create_main_proc and release_main_proc
  2239. to also generate a tprocinfo structure
  2240. * fixed unit implicit initfinal
  2241. Revision 1.85 2003/04/26 22:56:11 jonas
  2242. * fix to a_op64_const_reg_reg
  2243. Revision 1.84 2003/04/26 16:08:41 jonas
  2244. * fixed g_flags2reg
  2245. Revision 1.83 2003/04/26 15:25:29 florian
  2246. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2247. Revision 1.82 2003/04/25 20:55:34 florian
  2248. * stack frame calculations are now completly done using the code generator
  2249. routines instead of generating directly assembler so also large stack frames
  2250. are handle properly
  2251. Revision 1.81 2003/04/24 11:24:00 florian
  2252. * fixed several issues with nested procedures
  2253. Revision 1.80 2003/04/23 22:18:01 peter
  2254. * fixes to get rtl compiled
  2255. Revision 1.79 2003/04/23 12:35:35 florian
  2256. * fixed several issues with powerpc
  2257. + applied a patch from Jonas for nested function calls (PowerPC only)
  2258. * ...
  2259. Revision 1.78 2003/04/16 09:26:55 jonas
  2260. * assembler procedures now again get a stackframe if they have local
  2261. variables. No space is reserved for a function result however.
  2262. Also, the register parameters aren't automatically saved on the stack
  2263. anymore in assembler procedures.
  2264. Revision 1.77 2003/04/06 16:39:11 jonas
  2265. * don't generate entry/exit code for assembler procedures
  2266. Revision 1.76 2003/03/22 18:01:13 jonas
  2267. * fixed linux entry/exit code generation
  2268. Revision 1.75 2003/03/19 14:26:26 jonas
  2269. * fixed R_TOC bugs introduced by new register allocator conversion
  2270. Revision 1.74 2003/03/13 22:57:45 olle
  2271. * change in a_loadaddr_ref_reg
  2272. Revision 1.73 2003/03/12 22:43:38 jonas
  2273. * more powerpc and generic fixes related to the new register allocator
  2274. Revision 1.72 2003/03/11 21:46:24 jonas
  2275. * lots of new regallocator fixes, both in generic and ppc-specific code
  2276. (ppc compiler still can't compile the linux system unit though)
  2277. Revision 1.71 2003/02/19 22:00:16 daniel
  2278. * Code generator converted to new register notation
  2279. - Horribily outdated todo.txt removed
  2280. Revision 1.70 2003/01/13 17:17:50 olle
  2281. * changed global var access, TOC now contain pointers to globals
  2282. * fixed handling of function pointers
  2283. Revision 1.69 2003/01/09 22:00:53 florian
  2284. * fixed some PowerPC issues
  2285. Revision 1.68 2003/01/08 18:43:58 daniel
  2286. * Tregister changed into a record
  2287. Revision 1.67 2002/12/15 19:22:01 florian
  2288. * fixed some crashes and a rte 201
  2289. Revision 1.66 2002/11/28 10:55:16 olle
  2290. * macos: changing code gen for references to globals
  2291. Revision 1.65 2002/11/07 15:50:23 jonas
  2292. * fixed bctr(l) problems
  2293. Revision 1.64 2002/11/04 18:24:19 olle
  2294. * macos: globals are located in TOC and relative r2, instead of absolute
  2295. Revision 1.63 2002/10/28 22:24:28 olle
  2296. * macos entry/exit: only used registers are saved
  2297. - macos entry/exit: stackptr not saved in r31 anymore
  2298. * macos entry/exit: misc fixes
  2299. Revision 1.62 2002/10/19 23:51:48 olle
  2300. * macos stack frame size computing updated
  2301. + macos epilogue: control register now restored
  2302. * macos prologue and epilogue: fp reg now saved and restored
  2303. Revision 1.61 2002/10/19 12:50:36 olle
  2304. * reorganized prologue and epilogue routines
  2305. Revision 1.60 2002/10/02 21:49:51 florian
  2306. * all A_BL instructions replaced by calls to a_call_name
  2307. Revision 1.59 2002/10/02 13:24:58 jonas
  2308. * changed a_call_* so that no superfluous code is generated anymore
  2309. Revision 1.58 2002/09/17 18:54:06 jonas
  2310. * a_load_reg_reg() now has two size parameters: source and dest. This
  2311. allows some optimizations on architectures that don't encode the
  2312. register size in the register name.
  2313. Revision 1.57 2002/09/10 21:22:25 jonas
  2314. + added some internal errors
  2315. * fixed bug in sysv exit code
  2316. Revision 1.56 2002/09/08 20:11:56 jonas
  2317. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2318. Revision 1.55 2002/09/08 13:03:26 jonas
  2319. * several large offset-related fixes
  2320. Revision 1.54 2002/09/07 17:54:58 florian
  2321. * first part of PowerPC fixes
  2322. Revision 1.53 2002/09/07 15:25:14 peter
  2323. * old logs removed and tabs fixed
  2324. Revision 1.52 2002/09/02 10:14:51 jonas
  2325. + a_call_reg()
  2326. * small fix in a_call_ref()
  2327. Revision 1.51 2002/09/02 06:09:02 jonas
  2328. * fixed range error
  2329. Revision 1.50 2002/09/01 21:04:49 florian
  2330. * several powerpc related stuff fixed
  2331. Revision 1.49 2002/09/01 12:09:27 peter
  2332. + a_call_reg, a_call_loc added
  2333. * removed exprasmlist references
  2334. Revision 1.48 2002/08/31 21:38:02 jonas
  2335. * fixed a_call_ref (it should load ctr, not lr)
  2336. Revision 1.47 2002/08/31 21:30:45 florian
  2337. * fixed several problems caused by Jonas' commit :)
  2338. Revision 1.46 2002/08/31 19:25:50 jonas
  2339. + implemented a_call_ref()
  2340. Revision 1.45 2002/08/18 22:16:14 florian
  2341. + the ppc gas assembler writer adds now registers aliases
  2342. to the assembler file
  2343. Revision 1.44 2002/08/17 18:23:53 florian
  2344. * some assembler writer bugs fixed
  2345. Revision 1.43 2002/08/17 09:23:49 florian
  2346. * first part of procinfo rewrite
  2347. Revision 1.42 2002/08/16 14:24:59 carl
  2348. * issameref() to test if two references are the same (then emit no opcodes)
  2349. + ret_in_reg to replace ret_in_acc
  2350. (fix some register allocation bugs at the same time)
  2351. + save_std_register now has an extra parameter which is the
  2352. usedinproc registers
  2353. Revision 1.41 2002/08/15 08:13:54 carl
  2354. - a_load_sym_ofs_reg removed
  2355. * loadvmt now calls loadaddr_ref_reg instead
  2356. Revision 1.40 2002/08/11 14:32:32 peter
  2357. * renamed current_library to objectlibrary
  2358. Revision 1.39 2002/08/11 13:24:18 peter
  2359. * saving of asmsymbols in ppu supported
  2360. * asmsymbollist global is removed and moved into a new class
  2361. tasmlibrarydata that will hold the info of a .a file which
  2362. corresponds with a single module. Added librarydata to tmodule
  2363. to keep the library info stored for the module. In the future the
  2364. objectfiles will also be stored to the tasmlibrarydata class
  2365. * all getlabel/newasmsymbol and friends are moved to the new class
  2366. Revision 1.38 2002/08/11 11:39:31 jonas
  2367. + powerpc-specific genlinearlist
  2368. Revision 1.37 2002/08/10 17:15:31 jonas
  2369. * various fixes and optimizations
  2370. Revision 1.36 2002/08/06 20:55:23 florian
  2371. * first part of ppc calling conventions fix
  2372. Revision 1.35 2002/08/06 07:12:05 jonas
  2373. * fixed bug in g_flags2reg()
  2374. * and yet more constant operation fixes :)
  2375. Revision 1.34 2002/08/05 08:58:53 jonas
  2376. * fixed compilation problems
  2377. Revision 1.33 2002/08/04 12:57:55 jonas
  2378. * more misc. fixes, mostly constant-related
  2379. }