cgcpu.pas 92 KB

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