cgcpu.pas 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the code generator for the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef,
  23. cgbase,cgutils,cgobj,
  24. aasmbase,aasmcpu,aasmtai,aasmdata,
  25. parabase,
  26. cpubase,cpuinfo,node,cg64f32,rgcpu;
  27. type
  28. tcgarm = class(tcg)
  29. { true, if the next arithmetic operation should modify the flags }
  30. cgsetflags : boolean;
  31. procedure init_register_allocators;override;
  32. procedure done_register_allocators;override;
  33. procedure a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : TCGPara);override;
  34. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  35. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  36. procedure a_call_name(list : TAsmList;const s : string);override;
  37. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  38. procedure a_call_ref(list : TAsmList;ref: treference);override;
  39. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  40. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  41. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  42. size: tcgsize; a: aint; src, dst: tregister); override;
  43. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  44. size: tcgsize; src1, src2, dst: tregister); override;
  45. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  46. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  47. { move instructions }
  48. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  49. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  50. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  51. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  52. function a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  53. function a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  54. { fpu move instructions }
  55. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  56. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  57. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  58. procedure a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  59. { comparison operations }
  60. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  61. l : tasmlabel);override;
  62. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  63. procedure a_jmp_name(list : TAsmList;const s : string); override;
  64. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  65. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  66. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  67. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  68. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  69. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  70. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  71. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);override;
  72. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : aint);
  73. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : aint;aligned : boolean);
  74. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  75. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  76. procedure g_save_standard_registers(list : TAsmList);override;
  77. procedure g_restore_standard_registers(list : TAsmList);override;
  78. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  79. procedure fixref(list : TAsmList;var ref : treference);
  80. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  81. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  82. end;
  83. tcg64farm = class(tcg64f32)
  84. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  85. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  86. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  87. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  88. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  89. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  90. end;
  91. const
  92. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  93. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  94. winstackpagesize = 4096;
  95. function get_fpu_postfix(def : tdef) : toppostfix;
  96. implementation
  97. uses
  98. globals,verbose,systems,cutils,
  99. fmodule,
  100. symconst,symsym,
  101. tgobj,
  102. procinfo,cpupi,
  103. paramgr;
  104. function get_fpu_postfix(def : tdef) : toppostfix;
  105. begin
  106. if def.typ=floatdef then
  107. begin
  108. case tfloatdef(def).floattype of
  109. s32real:
  110. result:=PF_S;
  111. s64real:
  112. result:=PF_D;
  113. s80real:
  114. result:=PF_E;
  115. else
  116. internalerror(200401272);
  117. end;
  118. end
  119. else
  120. internalerror(200401271);
  121. end;
  122. procedure tcgarm.init_register_allocators;
  123. begin
  124. inherited init_register_allocators;
  125. { currently, we save R14 always, so we can use it }
  126. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  127. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  128. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  129. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  130. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  131. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  132. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  133. end;
  134. procedure tcgarm.done_register_allocators;
  135. begin
  136. rg[R_INTREGISTER].free;
  137. rg[R_FPUREGISTER].free;
  138. rg[R_MMREGISTER].free;
  139. inherited done_register_allocators;
  140. end;
  141. procedure tcgarm.a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : TCGPara);
  142. var
  143. ref: treference;
  144. begin
  145. paraloc.check_simple_location;
  146. case paraloc.location^.loc of
  147. LOC_REGISTER,LOC_CREGISTER:
  148. a_load_const_reg(list,size,a,paraloc.location^.register);
  149. LOC_REFERENCE:
  150. begin
  151. reference_reset(ref);
  152. ref.base:=paraloc.location^.reference.index;
  153. ref.offset:=paraloc.location^.reference.offset;
  154. a_load_const_ref(list,size,a,ref);
  155. end;
  156. else
  157. internalerror(2002081101);
  158. end;
  159. end;
  160. procedure tcgarm.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  161. var
  162. tmpref, ref: treference;
  163. location: pcgparalocation;
  164. sizeleft: aint;
  165. begin
  166. location := paraloc.location;
  167. tmpref := r;
  168. sizeleft := paraloc.intsize;
  169. while assigned(location) do
  170. begin
  171. case location^.loc of
  172. LOC_REGISTER,LOC_CREGISTER:
  173. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  174. LOC_REFERENCE:
  175. begin
  176. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  177. { doubles in softemu mode have a strange order of registers and references }
  178. if location^.size=OS_32 then
  179. g_concatcopy(list,tmpref,ref,4)
  180. else
  181. begin
  182. g_concatcopy(list,tmpref,ref,sizeleft);
  183. if assigned(location^.next) then
  184. internalerror(2005010710);
  185. end;
  186. end;
  187. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  188. case location^.size of
  189. OS_F32, OS_F64:
  190. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  191. else
  192. internalerror(2002072801);
  193. end;
  194. LOC_VOID:
  195. begin
  196. // nothing to do
  197. end;
  198. else
  199. internalerror(2002081103);
  200. end;
  201. inc(tmpref.offset,tcgsize2size[location^.size]);
  202. dec(sizeleft,tcgsize2size[location^.size]);
  203. location := location^.next;
  204. end;
  205. end;
  206. procedure tcgarm.a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : TCGPara);
  207. var
  208. ref: treference;
  209. tmpreg: tregister;
  210. begin
  211. paraloc.check_simple_location;
  212. case paraloc.location^.loc of
  213. LOC_REGISTER,LOC_CREGISTER:
  214. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  215. LOC_REFERENCE:
  216. begin
  217. reference_reset(ref);
  218. ref.base := paraloc.location^.reference.index;
  219. ref.offset := paraloc.location^.reference.offset;
  220. tmpreg := getintregister(list,OS_ADDR);
  221. a_loadaddr_ref_reg(list,r,tmpreg);
  222. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  223. end;
  224. else
  225. internalerror(2002080701);
  226. end;
  227. end;
  228. procedure tcgarm.a_call_name(list : TAsmList;const s : string);
  229. begin
  230. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  231. {
  232. the compiler does not properly set this flag anymore in pass 1, and
  233. for now we only need it after pass 2 (I hope) (JM)
  234. if not(pi_do_call in current_procinfo.flags) then
  235. internalerror(2003060703);
  236. }
  237. include(current_procinfo.flags,pi_do_call);
  238. end;
  239. procedure tcgarm.a_call_reg(list : TAsmList;reg: tregister);
  240. begin
  241. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  242. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  243. {
  244. the compiler does not properly set this flag anymore in pass 1, and
  245. for now we only need it after pass 2 (I hope) (JM)
  246. if not(pi_do_call in current_procinfo.flags) then
  247. internalerror(2003060703);
  248. }
  249. include(current_procinfo.flags,pi_do_call);
  250. end;
  251. procedure tcgarm.a_call_ref(list : TAsmList;ref: treference);
  252. begin
  253. a_reg_alloc(list,NR_R12);
  254. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,NR_R12);
  255. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  256. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  257. a_reg_dealloc(list,NR_R12);
  258. include(current_procinfo.flags,pi_do_call);
  259. end;
  260. procedure tcgarm.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  261. begin
  262. a_op_const_reg_reg(list,op,size,a,reg,reg);
  263. end;
  264. procedure tcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  265. begin
  266. case op of
  267. OP_NEG:
  268. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0));
  269. OP_NOT:
  270. begin
  271. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  272. case size of
  273. OS_8 :
  274. a_op_const_reg_reg(list,OP_AND,OS_INT,$ff,dst,dst);
  275. OS_16 :
  276. a_op_const_reg_reg(list,OP_AND,OS_INT,$ffff,dst,dst);
  277. end;
  278. end
  279. else
  280. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  281. end;
  282. end;
  283. const
  284. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  285. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  286. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR);
  287. procedure tcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  288. size: tcgsize; a: aint; src, dst: tregister);
  289. var
  290. ovloc : tlocation;
  291. begin
  292. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,ovloc);
  293. end;
  294. procedure tcgarm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  295. size: tcgsize; src1, src2, dst: tregister);
  296. var
  297. ovloc : tlocation;
  298. begin
  299. a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  300. end;
  301. procedure tcgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  302. var
  303. shift : byte;
  304. tmpreg : tregister;
  305. so : tshifterop;
  306. l1 : longint;
  307. begin
  308. ovloc.loc:=LOC_VOID;
  309. if is_shifter_const(-a,shift) then
  310. case op of
  311. OP_ADD:
  312. begin
  313. op:=OP_SUB;
  314. a:=aint(dword(-a));
  315. end;
  316. OP_SUB:
  317. begin
  318. op:=OP_ADD;
  319. a:=aint(dword(-a));
  320. end
  321. end;
  322. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  323. case op of
  324. OP_NEG,OP_NOT,
  325. OP_DIV,OP_IDIV:
  326. internalerror(200308281);
  327. OP_SHL:
  328. begin
  329. if a>32 then
  330. internalerror(200308294);
  331. if a<>0 then
  332. begin
  333. shifterop_reset(so);
  334. so.shiftmode:=SM_LSL;
  335. so.shiftimm:=a;
  336. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  337. end
  338. else
  339. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  340. end;
  341. OP_SHR:
  342. begin
  343. if a>32 then
  344. internalerror(200308292);
  345. shifterop_reset(so);
  346. if a<>0 then
  347. begin
  348. so.shiftmode:=SM_LSR;
  349. so.shiftimm:=a;
  350. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  351. end
  352. else
  353. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  354. end;
  355. OP_SAR:
  356. begin
  357. if a>32 then
  358. internalerror(200308295);
  359. if a<>0 then
  360. begin
  361. shifterop_reset(so);
  362. so.shiftmode:=SM_ASR;
  363. so.shiftimm:=a;
  364. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  365. end
  366. else
  367. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  368. end;
  369. else
  370. list.concat(setoppostfix(
  371. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  372. ));
  373. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  374. begin
  375. ovloc.loc:=LOC_FLAGS;
  376. case op of
  377. OP_ADD:
  378. ovloc.resflags:=F_CS;
  379. OP_SUB:
  380. ovloc.resflags:=F_CC;
  381. end;
  382. end;
  383. end
  384. else
  385. begin
  386. { there could be added some more sophisticated optimizations }
  387. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  388. a_load_reg_reg(list,size,size,src,dst)
  389. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  390. a_load_const_reg(list,size,0,dst)
  391. else if (op in [OP_IMUL]) and (a=-1) then
  392. a_op_reg_reg(list,OP_NEG,size,src,dst)
  393. { we do this here instead in the peephole optimizer because
  394. it saves us a register }
  395. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  396. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  397. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  398. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  399. begin
  400. if l1>32 then{roozbeh does this ever happen?}
  401. internalerror(200308296);
  402. shifterop_reset(so);
  403. so.shiftmode:=SM_LSL;
  404. so.shiftimm:=l1;
  405. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  406. end
  407. else
  408. begin
  409. tmpreg:=getintregister(list,size);
  410. a_load_const_reg(list,size,a,tmpreg);
  411. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  412. end;
  413. end;
  414. end;
  415. procedure tcgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  416. var
  417. so : tshifterop;
  418. tmpreg,overflowreg : tregister;
  419. asmop : tasmop;
  420. begin
  421. ovloc.loc:=LOC_VOID;
  422. case op of
  423. OP_NEG,OP_NOT,
  424. OP_DIV,OP_IDIV:
  425. internalerror(200308281);
  426. OP_SHL:
  427. begin
  428. shifterop_reset(so);
  429. so.rs:=src1;
  430. so.shiftmode:=SM_LSL;
  431. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  432. end;
  433. OP_SHR:
  434. begin
  435. shifterop_reset(so);
  436. so.rs:=src1;
  437. so.shiftmode:=SM_LSR;
  438. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  439. end;
  440. OP_SAR:
  441. begin
  442. shifterop_reset(so);
  443. so.rs:=src1;
  444. so.shiftmode:=SM_ASR;
  445. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  446. end;
  447. OP_IMUL,
  448. OP_MUL:
  449. begin
  450. if cgsetflags or setflags then
  451. begin
  452. overflowreg:=getintregister(list,size);
  453. if op=OP_IMUL then
  454. asmop:=A_SMULL
  455. else
  456. asmop:=A_UMULL;
  457. { the arm doesn't allow that rd and rm are the same }
  458. if dst=src2 then
  459. begin
  460. if dst<>src1 then
  461. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  462. else
  463. begin
  464. tmpreg:=getintregister(list,size);
  465. a_load_reg_reg(list,size,size,src2,dst);
  466. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  467. end;
  468. end
  469. else
  470. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  471. if op=OP_IMUL then
  472. begin
  473. shifterop_reset(so);
  474. so.shiftmode:=SM_ASR;
  475. so.shiftimm:=31;
  476. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  477. end
  478. else
  479. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  480. ovloc.loc:=LOC_FLAGS;
  481. ovloc.resflags:=F_NE;
  482. end
  483. else
  484. begin
  485. { the arm doesn't allow that rd and rm are the same }
  486. if dst=src2 then
  487. begin
  488. if dst<>src1 then
  489. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  490. else
  491. begin
  492. tmpreg:=getintregister(list,size);
  493. a_load_reg_reg(list,size,size,src2,dst);
  494. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  495. end;
  496. end
  497. else
  498. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  499. end;
  500. end;
  501. else
  502. list.concat(setoppostfix(
  503. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  504. ));
  505. end;
  506. end;
  507. procedure tcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);
  508. var
  509. imm_shift : byte;
  510. l : tasmlabel;
  511. hr : treference;
  512. begin
  513. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  514. internalerror(2002090902);
  515. if is_shifter_const(a,imm_shift) then
  516. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  517. else if is_shifter_const(not(a),imm_shift) then
  518. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  519. { loading of constants with mov and orr }
  520. else if (is_shifter_const(a-byte(a),imm_shift)) then
  521. begin
  522. list.concat(taicpu.op_reg_const(A_MOV,reg,a-byte(a)));
  523. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,byte(a)));
  524. end
  525. else if (is_shifter_const(a-word(a),imm_shift)) and (is_shifter_const(word(a),imm_shift)) then
  526. begin
  527. list.concat(taicpu.op_reg_const(A_MOV,reg,a-word(a)));
  528. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,word(a)));
  529. end
  530. else if (is_shifter_const(a-(dword(a) shl 8) shr 8,imm_shift)) and (is_shifter_const((dword(a) shl 8) shr 8,imm_shift)) then
  531. begin
  532. list.concat(taicpu.op_reg_const(A_MOV,reg,a-(dword(a) shl 8) shr 8));
  533. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,(dword(a) shl 8) shr 8));
  534. end
  535. else
  536. begin
  537. reference_reset(hr);
  538. current_asmdata.getjumplabel(l);
  539. cg.a_label(current_procinfo.aktlocaldata,l);
  540. hr.symboldata:=current_procinfo.aktlocaldata.last;
  541. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  542. hr.symbol:=l;
  543. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  544. end;
  545. end;
  546. function tcgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  547. var
  548. tmpreg : tregister;
  549. tmpref : treference;
  550. l : tasmlabel;
  551. begin
  552. tmpreg:=NR_NO;
  553. { Be sure to have a base register }
  554. if (ref.base=NR_NO) then
  555. begin
  556. if ref.shiftmode<>SM_None then
  557. internalerror(200308294);
  558. ref.base:=ref.index;
  559. ref.index:=NR_NO;
  560. end;
  561. { absolute symbols can't be handled directly, we've to store the symbol reference
  562. in the text segment and access it pc relative
  563. For now, we assume that references where base or index equals to PC are already
  564. relative, all other references are assumed to be absolute and thus they need
  565. to be handled extra.
  566. A proper solution would be to change refoptions to a set and store the information
  567. if the symbol is absolute or relative there.
  568. }
  569. if (assigned(ref.symbol) and
  570. not(is_pc(ref.base)) and
  571. not(is_pc(ref.index))
  572. ) or
  573. { [#xxx] isn't a valid address operand }
  574. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  575. (ref.offset<-4095) or
  576. (ref.offset>4095) or
  577. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  578. ((ref.offset<-255) or
  579. (ref.offset>255)
  580. )
  581. ) or
  582. ((op in [A_LDF,A_STF]) and
  583. ((ref.offset<-1020) or
  584. (ref.offset>1020) or
  585. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  586. assigned(ref.symbol)
  587. )
  588. ) then
  589. begin
  590. reference_reset(tmpref);
  591. { load symbol }
  592. tmpreg:=getintregister(list,OS_INT);
  593. if assigned(ref.symbol) then
  594. begin
  595. current_asmdata.getjumplabel(l);
  596. cg.a_label(current_procinfo.aktlocaldata,l);
  597. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  598. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  599. { load consts entry }
  600. tmpref.symbol:=l;
  601. tmpref.base:=NR_R15;
  602. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  603. { in case of LDF/STF, we got rid of the NR_R15 }
  604. if is_pc(ref.base) then
  605. ref.base:=NR_NO;
  606. if is_pc(ref.index) then
  607. ref.index:=NR_NO;
  608. end
  609. else
  610. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  611. if (ref.base<>NR_NO) then
  612. begin
  613. if ref.index<>NR_NO then
  614. begin
  615. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  616. ref.base:=tmpreg;
  617. end
  618. else
  619. begin
  620. ref.index:=tmpreg;
  621. ref.shiftimm:=0;
  622. ref.signindex:=1;
  623. ref.shiftmode:=SM_None;
  624. end;
  625. end
  626. else
  627. ref.base:=tmpreg;
  628. ref.offset:=0;
  629. ref.symbol:=nil;
  630. end;
  631. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  632. begin
  633. if tmpreg<>NR_NO then
  634. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  635. else
  636. begin
  637. tmpreg:=getintregister(list,OS_ADDR);
  638. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  639. ref.base:=tmpreg;
  640. end;
  641. ref.offset:=0;
  642. end;
  643. { floating point operations have only limited references
  644. we expect here, that a base is already set }
  645. if (op in [A_LDF,A_STF]) and (ref.index<>NR_NO) then
  646. begin
  647. if ref.shiftmode<>SM_none then
  648. internalerror(200309121);
  649. if tmpreg<>NR_NO then
  650. begin
  651. if ref.base=tmpreg then
  652. begin
  653. if ref.signindex<0 then
  654. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  655. else
  656. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  657. ref.index:=NR_NO;
  658. end
  659. else
  660. begin
  661. if ref.index<>tmpreg then
  662. internalerror(200403161);
  663. if ref.signindex<0 then
  664. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  665. else
  666. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  667. ref.base:=tmpreg;
  668. ref.index:=NR_NO;
  669. end;
  670. end
  671. else
  672. begin
  673. tmpreg:=getintregister(list,OS_ADDR);
  674. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  675. ref.base:=tmpreg;
  676. ref.index:=NR_NO;
  677. end;
  678. end;
  679. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  680. Result := ref;
  681. end;
  682. procedure tcgarm.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  683. var
  684. oppostfix:toppostfix;
  685. usedtmpref: treference;
  686. tmpreg : tregister;
  687. so : tshifterop;
  688. dir : integer;
  689. begin
  690. case ToSize of
  691. { signed integer registers }
  692. OS_8,
  693. OS_S8:
  694. oppostfix:=PF_B;
  695. OS_16,
  696. OS_S16:
  697. oppostfix:=PF_H;
  698. OS_32,
  699. OS_S32:
  700. oppostfix:=PF_None;
  701. else
  702. InternalError(200308295);
  703. end;
  704. if ref.alignment<>0 then
  705. begin
  706. if target_info.endian=endian_big then
  707. dir:=-1
  708. else
  709. dir:=1;
  710. case FromSize of
  711. OS_16,OS_S16:
  712. begin
  713. shifterop_reset(so);so.shiftmode:=SM_LSR;so.shiftimm:=8;
  714. tmpreg:=getintregister(list,OS_INT);
  715. usedtmpref:=ref;
  716. if target_info.endian=endian_big then
  717. inc(usedtmpref.offset,1);
  718. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  719. inc(usedtmpref.offset,dir);
  720. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  721. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  722. end;
  723. OS_32,OS_S32:
  724. begin
  725. shifterop_reset(so);so.shiftmode:=SM_LSR;so.shiftimm:=8;
  726. tmpreg:=getintregister(list,OS_INT);
  727. usedtmpref:=ref;
  728. if target_info.endian=endian_big then
  729. inc(usedtmpref.offset,3);
  730. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  731. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  732. inc(usedtmpref.offset,dir);
  733. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  734. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  735. inc(usedtmpref.offset,dir);
  736. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  737. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  738. inc(usedtmpref.offset,dir);
  739. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  740. end
  741. else
  742. handle_load_store(list,A_STR,oppostfix,reg,ref);
  743. end;
  744. end
  745. else
  746. handle_load_store(list,A_STR,oppostfix,reg,ref);
  747. end;
  748. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  749. var
  750. oppostfix:toppostfix;
  751. usedtmpref: treference;
  752. tmpreg,tmpreg2,tmpreg3 : tregister;
  753. so : tshifterop;
  754. dir : integer;
  755. begin
  756. case FromSize of
  757. { signed integer registers }
  758. OS_8:
  759. oppostfix:=PF_B;
  760. OS_S8:
  761. oppostfix:=PF_SB;
  762. OS_16:
  763. oppostfix:=PF_H;
  764. OS_S16:
  765. oppostfix:=PF_SH;
  766. OS_32,
  767. OS_S32:
  768. oppostfix:=PF_None;
  769. else
  770. InternalError(200308297);
  771. end;
  772. if Ref.alignment<>0 then
  773. begin
  774. if target_info.endian=endian_big then
  775. dir:=-1
  776. else
  777. dir:=0;
  778. case FromSize of
  779. OS_16,OS_S16:
  780. begin
  781. { only complicated references need an extra loadaddr }
  782. if assigned(ref.symbol) or
  783. (ref.index<>NR_NO) or
  784. (ref.offset<-4095) or
  785. (ref.offset>4094) or
  786. { sometimes the compiler reused registers }
  787. (reg=ref.index) or
  788. (reg=ref.base) then
  789. begin
  790. tmpreg3:=getintregister(list,OS_INT);
  791. a_loadaddr_ref_reg(list,ref,tmpreg3);
  792. reference_reset_base(usedtmpref,tmpreg3,0);
  793. end
  794. else
  795. usedtmpref:=ref;
  796. if target_info.endian=endian_big then
  797. inc(usedtmpref.offset,1);
  798. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  799. tmpreg:=getintregister(list,OS_INT);
  800. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  801. inc(usedtmpref.offset,dir);
  802. tmpreg2:=getintregister(list,OS_INT);
  803. if FromSize=OS_16 then
  804. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2)
  805. else
  806. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg2);
  807. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,tmpreg,tmpreg2,so));
  808. end;
  809. OS_32,OS_S32:
  810. begin
  811. tmpreg:=getintregister(list,OS_INT);
  812. tmpreg2:=getintregister(list,OS_INT);
  813. { only complicated references need an extra loadaddr }
  814. if assigned(ref.symbol) or
  815. (ref.index<>NR_NO) or
  816. (ref.offset<-4095) or
  817. (ref.offset>4092) or
  818. { sometimes the compiler reused registers }
  819. (reg=ref.index) or
  820. (reg=ref.base) then
  821. begin
  822. tmpreg3:=getintregister(list,OS_INT);
  823. a_loadaddr_ref_reg(list,ref,tmpreg3);
  824. reference_reset_base(usedtmpref,tmpreg3,0);
  825. end
  826. else
  827. usedtmpref:=ref;
  828. if target_info.endian=endian_big then
  829. inc(usedtmpref.offset,3);
  830. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  831. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  832. inc(usedtmpref.offset,dir);
  833. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  834. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,tmpreg2,reg,tmpreg,so));
  835. inc(usedtmpref.offset,dir);
  836. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  837. so.shiftimm:=16;
  838. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,tmpreg,tmpreg2,reg,so));
  839. inc(usedtmpref.offset,dir);
  840. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  841. so.shiftimm:=24;
  842. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,tmpreg,tmpreg2,so));
  843. end
  844. else
  845. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  846. end;
  847. end
  848. else
  849. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  850. end;
  851. function tcgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  852. var
  853. oppostfix:toppostfix;
  854. begin
  855. case ToSize of
  856. { signed integer registers }
  857. OS_8,
  858. OS_S8:
  859. oppostfix:=PF_B;
  860. OS_16,
  861. OS_S16:
  862. oppostfix:=PF_H;
  863. OS_32,
  864. OS_S32:
  865. oppostfix:=PF_None;
  866. else
  867. InternalError(2003082910);
  868. end;
  869. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  870. end;
  871. function tcgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  872. var
  873. oppostfix:toppostfix;
  874. begin
  875. case FromSize of
  876. { signed integer registers }
  877. OS_8:
  878. oppostfix:=PF_B;
  879. OS_S8:
  880. oppostfix:=PF_SB;
  881. OS_16:
  882. oppostfix:=PF_H;
  883. OS_S16:
  884. oppostfix:=PF_SH;
  885. OS_32,
  886. OS_S32:
  887. oppostfix:=PF_None;
  888. else
  889. InternalError(200308291);
  890. end;
  891. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  892. end;
  893. procedure tcgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  894. var
  895. so : tshifterop;
  896. conv_done: boolean;
  897. procedure do_shift(shiftmode : tshiftmode; shiftimm : byte; reg : tregister);
  898. begin
  899. so.shiftmode:=shiftmode;
  900. so.shiftimm:=shiftimm;
  901. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg,so));
  902. end;
  903. function do_conv(size : tcgsize) : boolean;
  904. begin
  905. result:=true;
  906. case size of
  907. OS_8:
  908. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  909. OS_S8:
  910. begin
  911. do_shift(SM_LSL,24,reg1);
  912. do_shift(SM_ASR,24,reg2);
  913. end;
  914. OS_16,OS_S16:
  915. begin
  916. do_shift(SM_LSL,16,reg1);
  917. if size=OS_S16 then
  918. do_shift(SM_ASR,16,reg2)
  919. else
  920. do_shift(SM_LSR,16,reg2);
  921. end;
  922. else
  923. result:=false;
  924. end;
  925. conv_done:=result;
  926. end;
  927. var
  928. instr: taicpu;
  929. begin
  930. conv_done:=false;
  931. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  932. begin
  933. shifterop_reset(so);
  934. if not do_conv(tosize) then
  935. if tosize in [OS_32,OS_S32] then
  936. do_conv(fromsize)
  937. else
  938. internalerror(2002090901);
  939. end;
  940. if not conv_done and (reg1<>reg2) then
  941. begin
  942. { same size, only a register mov required }
  943. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  944. list.Concat(instr);
  945. { Notify the register allocator that we have written a move instruction so
  946. it can try to eliminate it. }
  947. add_move_instruction(instr);
  948. end;
  949. end;
  950. procedure tcgarm.a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  951. var
  952. href,href2 : treference;
  953. hloc : pcgparalocation;
  954. begin
  955. href:=ref;
  956. hloc:=paraloc.location;
  957. while assigned(hloc) do
  958. begin
  959. case hloc^.loc of
  960. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  961. a_loadfpu_ref_reg(list,size,size,ref,hloc^.register);
  962. LOC_REGISTER :
  963. case hloc^.size of
  964. OS_F32:
  965. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  966. OS_64,
  967. OS_F64:
  968. cg64.a_param64_ref(list,href,paraloc);
  969. else
  970. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  971. end;
  972. LOC_REFERENCE :
  973. begin
  974. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset);
  975. { concatcopy should choose the best way to copy the data }
  976. g_concatcopy(list,href,href2,tcgsize2size[size]);
  977. end;
  978. else
  979. internalerror(200408241);
  980. end;
  981. inc(href.offset,tcgsize2size[hloc^.size]);
  982. hloc:=hloc^.next;
  983. end;
  984. end;
  985. procedure tcgarm.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  986. begin
  987. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[tosize]));
  988. end;
  989. procedure tcgarm.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  990. var
  991. oppostfix:toppostfix;
  992. begin
  993. case tosize of
  994. OS_32,
  995. OS_F32:
  996. oppostfix:=PF_S;
  997. OS_64,
  998. OS_F64:
  999. oppostfix:=PF_D;
  1000. OS_F80:
  1001. oppostfix:=PF_E;
  1002. else
  1003. InternalError(200309021);
  1004. end;
  1005. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  1006. end;
  1007. procedure tcgarm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1008. var
  1009. oppostfix:toppostfix;
  1010. begin
  1011. case tosize of
  1012. OS_F32:
  1013. oppostfix:=PF_S;
  1014. OS_F64:
  1015. oppostfix:=PF_D;
  1016. OS_F80:
  1017. oppostfix:=PF_E;
  1018. else
  1019. InternalError(200309022);
  1020. end;
  1021. handle_load_store(list,A_STF,oppostfix,reg,ref);
  1022. end;
  1023. { comparison operations }
  1024. procedure tcgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1025. l : tasmlabel);
  1026. var
  1027. tmpreg : tregister;
  1028. b : byte;
  1029. begin
  1030. if is_shifter_const(a,b) then
  1031. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  1032. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  1033. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  1034. else if (a<>$7fffffff) and (a<>-1) and is_shifter_const(-a,b) then
  1035. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  1036. else
  1037. begin
  1038. tmpreg:=getintregister(list,size);
  1039. a_load_const_reg(list,size,a,tmpreg);
  1040. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1041. end;
  1042. a_jmp_cond(list,cmp_op,l);
  1043. end;
  1044. procedure tcgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1045. begin
  1046. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1047. a_jmp_cond(list,cmp_op,l);
  1048. end;
  1049. procedure tcgarm.a_jmp_name(list : TAsmList;const s : string);
  1050. var
  1051. ai : taicpu;
  1052. begin
  1053. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  1054. ai.is_jmp:=true;
  1055. list.concat(ai);
  1056. end;
  1057. procedure tcgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1058. var
  1059. ai : taicpu;
  1060. begin
  1061. ai:=taicpu.op_sym(A_B,l);
  1062. ai.is_jmp:=true;
  1063. list.concat(ai);
  1064. end;
  1065. procedure tcgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1066. var
  1067. ai : taicpu;
  1068. begin
  1069. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1070. ai.is_jmp:=true;
  1071. list.concat(ai);
  1072. end;
  1073. procedure tcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1074. begin
  1075. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1076. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1077. end;
  1078. procedure tcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1079. var
  1080. ref : treference;
  1081. shift : byte;
  1082. firstfloatreg,lastfloatreg,
  1083. r : byte;
  1084. regs : tcpuregisterset;
  1085. begin
  1086. LocalSize:=align(LocalSize,4);
  1087. if not(nostackframe) then
  1088. begin
  1089. firstfloatreg:=RS_NO;
  1090. { save floating point registers? }
  1091. for r:=RS_F0 to RS_F7 do
  1092. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1093. begin
  1094. if firstfloatreg=RS_NO then
  1095. firstfloatreg:=r;
  1096. lastfloatreg:=r;
  1097. end;
  1098. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1099. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1100. begin
  1101. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1102. a_reg_alloc(list,NR_R12);
  1103. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1104. end;
  1105. { save int registers }
  1106. reference_reset(ref);
  1107. ref.index:=NR_STACK_POINTER_REG;
  1108. ref.addressmode:=AM_PREINDEXED;
  1109. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1110. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1111. regs:=regs+[RS_R11,RS_R12,RS_R14,RS_R15]
  1112. else
  1113. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1114. include(regs,RS_R14);
  1115. if regs<>[] then
  1116. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,regs),PF_FD));
  1117. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1118. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1119. { allocate necessary stack size
  1120. not necessary according to Yury Sidorov
  1121. { don't use a_op_const_reg_reg here because we don't allow register allocations
  1122. in the entry/exit code }
  1123. if (target_info.system in [system_arm_wince]) and
  1124. (localsize>=winstackpagesize) then
  1125. begin
  1126. if localsize div winstackpagesize<=5 then
  1127. begin
  1128. if is_shifter_const(localsize,shift) then
  1129. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize))
  1130. else
  1131. begin
  1132. a_load_const_reg(list,OS_ADDR,localsize,NR_R12);
  1133. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1134. end;
  1135. for i:=1 to localsize div winstackpagesize do
  1136. begin
  1137. if localsize-i*winstackpagesize<4096 then
  1138. reference_reset_base(href,NR_STACK_POINTER_REG,-(localsize-i*winstackpagesize))
  1139. else
  1140. begin
  1141. a_load_const_reg(list,OS_ADDR,-(localsize-i*winstackpagesize),NR_R12);
  1142. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1143. href.index:=NR_R12;
  1144. end;
  1145. { the data stored doesn't matter }
  1146. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1147. end;
  1148. a_reg_dealloc(list,NR_R12);
  1149. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1150. { the data stored doesn't matter }
  1151. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1152. end
  1153. else
  1154. begin
  1155. current_asmdata.getjumplabel(again);
  1156. list.concat(Taicpu.op_reg_const(A_MOV,NR_R12,localsize div winstackpagesize));
  1157. a_label(list,again);
  1158. { always shifterop }
  1159. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,winstackpagesize));
  1160. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1161. { the data stored doesn't matter }
  1162. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1163. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_R12,NR_R12,1));
  1164. a_jmp_cond(list,OC_NE,again);
  1165. if is_shifter_const(localsize mod winstackpagesize,shift) then
  1166. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize mod winstackpagesize))
  1167. else
  1168. begin
  1169. a_load_const_reg(list,OS_ADDR,localsize mod winstackpagesize,NR_R12);
  1170. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1171. end;
  1172. a_reg_dealloc(list,NR_R12);
  1173. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1174. { the data stored doesn't matter }
  1175. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1176. end
  1177. end
  1178. else
  1179. }
  1180. if LocalSize<>0 then
  1181. if not(is_shifter_const(localsize,shift)) then
  1182. begin
  1183. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1184. a_reg_alloc(list,NR_R12);
  1185. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1186. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1187. a_reg_dealloc(list,NR_R12);
  1188. end
  1189. else
  1190. begin
  1191. a_reg_dealloc(list,NR_R12);
  1192. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1193. end;
  1194. if firstfloatreg<>RS_NO then
  1195. begin
  1196. reference_reset(ref);
  1197. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  1198. begin
  1199. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1200. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1201. ref.base:=NR_R12;
  1202. end
  1203. else
  1204. begin
  1205. ref.base:=current_procinfo.framepointer;
  1206. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1207. end;
  1208. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1209. lastfloatreg-firstfloatreg+1,ref));
  1210. end;
  1211. end;
  1212. end;
  1213. procedure tcgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1214. var
  1215. ref : treference;
  1216. firstfloatreg,lastfloatreg,
  1217. r : byte;
  1218. shift : byte;
  1219. regs : tcpuregisterset;
  1220. LocalSize : longint;
  1221. begin
  1222. if not(nostackframe) then
  1223. begin
  1224. { restore floating point register }
  1225. firstfloatreg:=RS_NO;
  1226. { save floating point registers? }
  1227. for r:=RS_F0 to RS_F7 do
  1228. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1229. begin
  1230. if firstfloatreg=RS_NO then
  1231. firstfloatreg:=r;
  1232. lastfloatreg:=r;
  1233. end;
  1234. if firstfloatreg<>RS_NO then
  1235. begin
  1236. reference_reset(ref);
  1237. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  1238. begin
  1239. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1240. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1241. ref.base:=NR_R12;
  1242. end
  1243. else
  1244. begin
  1245. ref.base:=current_procinfo.framepointer;
  1246. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1247. end;
  1248. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1249. lastfloatreg-firstfloatreg+1,ref));
  1250. end;
  1251. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1252. begin
  1253. LocalSize:=current_procinfo.calc_stackframe_size;
  1254. if LocalSize<>0 then
  1255. if not(is_shifter_const(LocalSize,shift)) then
  1256. begin
  1257. a_reg_alloc(list,NR_R12);
  1258. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1259. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1260. a_reg_dealloc(list,NR_R12);
  1261. end
  1262. else
  1263. begin
  1264. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1265. end;
  1266. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1267. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  1268. begin
  1269. exclude(regs,RS_R14);
  1270. include(regs,RS_R15);
  1271. end;
  1272. if regs=[] then
  1273. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  1274. else
  1275. begin
  1276. reference_reset(ref);
  1277. ref.index:=NR_STACK_POINTER_REG;
  1278. ref.addressmode:=AM_PREINDEXED;
  1279. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,regs),PF_FD));
  1280. end;
  1281. end
  1282. else
  1283. begin
  1284. { restore int registers and return }
  1285. reference_reset(ref);
  1286. ref.index:=NR_FRAME_POINTER_REG;
  1287. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)+[RS_R11,RS_R13,RS_R15]),PF_EA));
  1288. end;
  1289. end
  1290. else
  1291. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  1292. end;
  1293. procedure tcgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1294. var
  1295. b : byte;
  1296. tmpref : treference;
  1297. instr : taicpu;
  1298. begin
  1299. if ref.addressmode<>AM_OFFSET then
  1300. internalerror(200309071);
  1301. tmpref:=ref;
  1302. { Be sure to have a base register }
  1303. if (tmpref.base=NR_NO) then
  1304. begin
  1305. if tmpref.shiftmode<>SM_None then
  1306. internalerror(200308294);
  1307. if tmpref.signindex<0 then
  1308. internalerror(200312023);
  1309. tmpref.base:=tmpref.index;
  1310. tmpref.index:=NR_NO;
  1311. end;
  1312. if assigned(tmpref.symbol) or
  1313. not((is_shifter_const(tmpref.offset,b)) or
  1314. (is_shifter_const(-tmpref.offset,b))
  1315. ) then
  1316. fixref(list,tmpref);
  1317. { expect a base here if there is an index }
  1318. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  1319. internalerror(200312022);
  1320. if tmpref.index<>NR_NO then
  1321. begin
  1322. if tmpref.shiftmode<>SM_None then
  1323. internalerror(200312021);
  1324. if tmpref.signindex<0 then
  1325. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  1326. else
  1327. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  1328. if tmpref.offset<>0 then
  1329. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  1330. end
  1331. else
  1332. begin
  1333. if tmpref.base=NR_NO then
  1334. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  1335. else
  1336. if tmpref.offset<>0 then
  1337. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  1338. else
  1339. begin
  1340. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  1341. list.concat(instr);
  1342. add_move_instruction(instr);
  1343. end;
  1344. end;
  1345. end;
  1346. procedure tcgarm.fixref(list : TAsmList;var ref : treference);
  1347. var
  1348. tmpreg : tregister;
  1349. tmpref : treference;
  1350. l : tasmlabel;
  1351. begin
  1352. { absolute symbols can't be handled directly, we've to store the symbol reference
  1353. in the text segment and access it pc relative
  1354. For now, we assume that references where base or index equals to PC are already
  1355. relative, all other references are assumed to be absolute and thus they need
  1356. to be handled extra.
  1357. A proper solution would be to change refoptions to a set and store the information
  1358. if the symbol is absolute or relative there.
  1359. }
  1360. { create consts entry }
  1361. reference_reset(tmpref);
  1362. current_asmdata.getjumplabel(l);
  1363. cg.a_label(current_procinfo.aktlocaldata,l);
  1364. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  1365. if assigned(ref.symbol) then
  1366. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  1367. else
  1368. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  1369. { load consts entry }
  1370. tmpreg:=getintregister(list,OS_INT);
  1371. tmpref.symbol:=l;
  1372. tmpref.base:=NR_PC;
  1373. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  1374. if (ref.base<>NR_NO) then
  1375. begin
  1376. if ref.index<>NR_NO then
  1377. begin
  1378. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1379. ref.base:=tmpreg;
  1380. end
  1381. else
  1382. if ref.base<>NR_PC then
  1383. begin
  1384. ref.index:=tmpreg;
  1385. ref.shiftimm:=0;
  1386. ref.signindex:=1;
  1387. ref.shiftmode:=SM_None;
  1388. end
  1389. else
  1390. ref.base:=tmpreg;
  1391. end
  1392. else
  1393. ref.base:=tmpreg;
  1394. ref.offset:=0;
  1395. ref.symbol:=nil;
  1396. end;
  1397. procedure tcgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : aint);
  1398. var
  1399. paraloc1,paraloc2,paraloc3 : TCGPara;
  1400. begin
  1401. paraloc1.init;
  1402. paraloc2.init;
  1403. paraloc3.init;
  1404. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1405. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1406. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1407. paramanager.allocparaloc(list,paraloc3);
  1408. a_param_const(list,OS_INT,len,paraloc3);
  1409. paramanager.allocparaloc(list,paraloc2);
  1410. a_paramaddr_ref(list,dest,paraloc2);
  1411. paramanager.allocparaloc(list,paraloc2);
  1412. a_paramaddr_ref(list,source,paraloc1);
  1413. paramanager.freeparaloc(list,paraloc3);
  1414. paramanager.freeparaloc(list,paraloc2);
  1415. paramanager.freeparaloc(list,paraloc1);
  1416. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1417. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1418. a_call_name(list,'FPC_MOVE');
  1419. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1420. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1421. paraloc3.done;
  1422. paraloc2.done;
  1423. paraloc1.done;
  1424. end;
  1425. procedure tcgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : aint;aligned : boolean);
  1426. const
  1427. maxtmpreg=10;{roozbeh: can be reduced to 8 or lower if might conflick with reserved ones,also +2 is used becouse of regs required for referencing}
  1428. var
  1429. srcref,dstref,usedtmpref,usedtmpref2:treference;
  1430. srcreg,destreg,countreg,r,tmpreg:tregister;
  1431. helpsize:aint;
  1432. copysize:byte;
  1433. cgsize:Tcgsize;
  1434. tmpregisters:array[1..maxtmpreg] of tregister;
  1435. tmpregi,tmpregi2:byte;
  1436. { will never be called with count<=4 }
  1437. procedure genloop(count : aword;size : byte);
  1438. const
  1439. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1440. var
  1441. l : tasmlabel;
  1442. begin
  1443. current_asmdata.getjumplabel(l);
  1444. if count<size then size:=1;
  1445. a_load_const_reg(list,OS_INT,count div size,countreg);
  1446. cg.a_label(list,l);
  1447. srcref.addressmode:=AM_POSTINDEXED;
  1448. dstref.addressmode:=AM_POSTINDEXED;
  1449. srcref.offset:=size;
  1450. dstref.offset:=size;
  1451. r:=getintregister(list,size2opsize[size]);
  1452. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  1453. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  1454. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  1455. a_jmp_flags(list,F_NE,l);
  1456. srcref.offset:=1;
  1457. dstref.offset:=1;
  1458. case count mod size of
  1459. 1:
  1460. begin
  1461. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1462. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1463. end;
  1464. 2:
  1465. if aligned then
  1466. begin
  1467. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1468. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1469. end
  1470. else
  1471. begin
  1472. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1473. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1474. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1475. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1476. end;
  1477. 3:
  1478. if aligned then
  1479. begin
  1480. srcref.offset:=2;
  1481. dstref.offset:=2;
  1482. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1483. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1484. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1485. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1486. end
  1487. else
  1488. begin
  1489. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1490. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1491. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1492. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1493. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1494. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1495. end;
  1496. end;
  1497. { keep the registers alive }
  1498. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1499. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  1500. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  1501. end;
  1502. begin
  1503. if len=0 then
  1504. exit;
  1505. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  1506. dstref:=dest;
  1507. srcref:=source;
  1508. if cs_opt_size in current_settings.optimizerswitches then
  1509. helpsize:=8;
  1510. if (len<=helpsize) and aligned then
  1511. begin
  1512. tmpregi:=0;
  1513. srcreg:=getintregister(list,OS_ADDR);
  1514. { explicit pc relative addressing, could be
  1515. e.g. a floating point constant }
  1516. if source.base=NR_PC then
  1517. begin
  1518. { ... then we don't need a loadaddr }
  1519. srcref:=source;
  1520. end
  1521. else
  1522. begin
  1523. a_loadaddr_ref_reg(list,source,srcreg);
  1524. reference_reset_base(srcref,srcreg,0);
  1525. end;
  1526. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  1527. begin
  1528. inc(tmpregi);
  1529. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  1530. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  1531. inc(srcref.offset,4);
  1532. dec(len,4);
  1533. end;
  1534. destreg:=getintregister(list,OS_ADDR);
  1535. a_loadaddr_ref_reg(list,dest,destreg);
  1536. reference_reset_base(dstref,destreg,0);
  1537. tmpregi2:=1;
  1538. while (tmpregi2<=tmpregi) do
  1539. begin
  1540. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  1541. inc(dstref.offset,4);
  1542. inc(tmpregi2);
  1543. end;
  1544. copysize:=4;
  1545. cgsize:=OS_32;
  1546. while len<>0 do
  1547. begin
  1548. if len<2 then
  1549. begin
  1550. copysize:=1;
  1551. cgsize:=OS_8;
  1552. end
  1553. else if len<4 then
  1554. begin
  1555. copysize:=2;
  1556. cgsize:=OS_16;
  1557. end;
  1558. dec(len,copysize);
  1559. r:=getintregister(list,cgsize);
  1560. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1561. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1562. inc(srcref.offset,copysize);
  1563. inc(dstref.offset,copysize);
  1564. end;{end of while}
  1565. end
  1566. else
  1567. begin
  1568. cgsize:=OS_32;
  1569. if (len<=4) then{len<=4 and not aligned}
  1570. begin
  1571. r:=getintregister(list,cgsize);
  1572. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1573. if Len=1 then
  1574. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  1575. else
  1576. begin
  1577. tmpreg:=getintregister(list,cgsize);
  1578. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1579. inc(usedtmpref.offset,1);
  1580. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1581. inc(usedtmpref2.offset,1);
  1582. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1583. if len>2 then
  1584. begin
  1585. inc(usedtmpref.offset,1);
  1586. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1587. inc(usedtmpref2.offset,1);
  1588. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1589. if len>3 then
  1590. begin
  1591. inc(usedtmpref.offset,1);
  1592. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1593. inc(usedtmpref2.offset,1);
  1594. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1595. end;
  1596. end;
  1597. end;
  1598. end{end of if len<=4}
  1599. else
  1600. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  1601. destreg:=getintregister(list,OS_ADDR);
  1602. a_loadaddr_ref_reg(list,dest,destreg);
  1603. reference_reset_base(dstref,destreg,0);
  1604. srcreg:=getintregister(list,OS_ADDR);
  1605. a_loadaddr_ref_reg(list,source,srcreg);
  1606. reference_reset_base(srcref,srcreg,0);
  1607. countreg:=getintregister(list,OS_32);
  1608. // if cs_opt_size in current_settings.optimizerswitches then
  1609. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  1610. {if aligned then
  1611. genloop(len,4)
  1612. else}
  1613. genloop(len,1);
  1614. end;
  1615. end;
  1616. end;
  1617. procedure tcgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);
  1618. begin
  1619. g_concatcopy_internal(list,source,dest,len,false);
  1620. end;
  1621. procedure tcgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1622. begin
  1623. if (source.alignment in [1..3]) or
  1624. (dest.alignment in [1..3]) then
  1625. g_concatcopy_internal(list,source,dest,len,false)
  1626. else
  1627. g_concatcopy_internal(list,source,dest,len,true);
  1628. end;
  1629. procedure tcgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1630. var
  1631. ovloc : tlocation;
  1632. begin
  1633. ovloc.loc:=LOC_VOID;
  1634. g_overflowCheck_loc(list,l,def,ovloc);
  1635. end;
  1636. procedure tcgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1637. var
  1638. hl : tasmlabel;
  1639. ai:TAiCpu;
  1640. hflags : tresflags;
  1641. begin
  1642. if not(cs_check_overflow in current_settings.localswitches) then
  1643. exit;
  1644. current_asmdata.getjumplabel(hl);
  1645. case ovloc.loc of
  1646. LOC_VOID:
  1647. begin
  1648. ai:=taicpu.op_sym(A_B,hl);
  1649. ai.is_jmp:=true;
  1650. if not((def.typ=pointerdef) or
  1651. ((def.typ=orddef) and
  1652. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,bool8bit,bool16bit,bool32bit]))) then
  1653. ai.SetCondition(C_VC)
  1654. else
  1655. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  1656. ai.SetCondition(C_CS)
  1657. else
  1658. ai.SetCondition(C_CC);
  1659. list.concat(ai);
  1660. end;
  1661. LOC_FLAGS:
  1662. begin
  1663. hflags:=ovloc.resflags;
  1664. inverse_flags(hflags);
  1665. cg.a_jmp_flags(list,hflags,hl);
  1666. end;
  1667. else
  1668. internalerror(200409281);
  1669. end;
  1670. a_call_name(list,'FPC_OVERFLOW');
  1671. a_label(list,hl);
  1672. end;
  1673. procedure tcgarm.g_save_standard_registers(list : TAsmList);
  1674. begin
  1675. { this work is done in g_proc_entry }
  1676. end;
  1677. procedure tcgarm.g_restore_standard_registers(list : TAsmList);
  1678. begin
  1679. { this work is done in g_proc_exit }
  1680. end;
  1681. procedure tcgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1682. var
  1683. ai : taicpu;
  1684. begin
  1685. ai:=Taicpu.Op_sym(A_B,l);
  1686. ai.SetCondition(OpCmp2AsmCond[cond]);
  1687. ai.is_jmp:=true;
  1688. list.concat(ai);
  1689. end;
  1690. procedure tcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1691. procedure loadvmttor12;
  1692. var
  1693. href : treference;
  1694. begin
  1695. reference_reset_base(href,NR_R0,0);
  1696. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1697. end;
  1698. procedure op_onr12methodaddr;
  1699. var
  1700. href : treference;
  1701. begin
  1702. if (procdef.extnumber=$ffff) then
  1703. Internalerror(200006139);
  1704. { call/jmp vmtoffs(%eax) ; method offs }
  1705. reference_reset_base(href,NR_R12,procdef._class.vmtmethodoffset(procdef.extnumber));
  1706. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1707. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  1708. end;
  1709. var
  1710. make_global : boolean;
  1711. begin
  1712. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1713. Internalerror(200006137);
  1714. if not assigned(procdef._class) or
  1715. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1716. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1717. Internalerror(200006138);
  1718. if procdef.owner.symtabletype<>ObjectSymtable then
  1719. Internalerror(200109191);
  1720. make_global:=false;
  1721. if (not current_module.is_unit) or
  1722. create_smartlink or
  1723. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1724. make_global:=true;
  1725. if make_global then
  1726. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1727. else
  1728. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1729. { set param1 interface to self }
  1730. g_adjust_self_value(list,procdef,ioffset);
  1731. { case 4 }
  1732. if po_virtualmethod in procdef.procoptions then
  1733. begin
  1734. loadvmttor12;
  1735. op_onr12methodaddr;
  1736. end
  1737. { case 0 }
  1738. else
  1739. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1740. list.concat(Tai_symbol_end.Createname(labelname));
  1741. end;
  1742. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1743. begin
  1744. case op of
  1745. OP_NEG:
  1746. begin
  1747. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  1748. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  1749. end;
  1750. OP_NOT:
  1751. begin
  1752. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  1753. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  1754. end;
  1755. else
  1756. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1757. end;
  1758. end;
  1759. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1760. begin
  1761. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1762. end;
  1763. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1764. var
  1765. ovloc : tlocation;
  1766. begin
  1767. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  1768. end;
  1769. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1770. var
  1771. ovloc : tlocation;
  1772. begin
  1773. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  1774. end;
  1775. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1776. var
  1777. tmpreg : tregister;
  1778. b : byte;
  1779. begin
  1780. ovloc.loc:=LOC_VOID;
  1781. case op of
  1782. OP_NEG,
  1783. OP_NOT :
  1784. internalerror(200306017);
  1785. end;
  1786. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1787. begin
  1788. case op of
  1789. OP_ADD:
  1790. begin
  1791. if is_shifter_const(lo(value),b) then
  1792. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1793. else
  1794. begin
  1795. tmpreg:=cg.getintregister(list,OS_32);
  1796. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1797. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1798. end;
  1799. if is_shifter_const(hi(value),b) then
  1800. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  1801. else
  1802. begin
  1803. tmpreg:=cg.getintregister(list,OS_32);
  1804. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1805. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1806. end;
  1807. end;
  1808. OP_SUB:
  1809. begin
  1810. if is_shifter_const(lo(value),b) then
  1811. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1812. else
  1813. begin
  1814. tmpreg:=cg.getintregister(list,OS_32);
  1815. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1816. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1817. end;
  1818. if is_shifter_const(hi(value),b) then
  1819. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  1820. else
  1821. begin
  1822. tmpreg:=cg.getintregister(list,OS_32);
  1823. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1824. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1825. end;
  1826. end;
  1827. else
  1828. internalerror(200502131);
  1829. end;
  1830. if size=OS_64 then
  1831. begin
  1832. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1833. ovloc.loc:=LOC_FLAGS;
  1834. case op of
  1835. OP_ADD:
  1836. ovloc.resflags:=F_CS;
  1837. OP_SUB:
  1838. ovloc.resflags:=F_CC;
  1839. end;
  1840. end;
  1841. end
  1842. else
  1843. begin
  1844. case op of
  1845. OP_AND,OP_OR,OP_XOR:
  1846. begin
  1847. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  1848. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  1849. end;
  1850. OP_ADD:
  1851. begin
  1852. if is_shifter_const(aint(lo(value)),b) then
  1853. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  1854. else
  1855. begin
  1856. tmpreg:=cg.getintregister(list,OS_32);
  1857. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  1858. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1859. end;
  1860. if is_shifter_const(aint(hi(value)),b) then
  1861. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  1862. else
  1863. begin
  1864. tmpreg:=cg.getintregister(list,OS_32);
  1865. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  1866. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  1867. end;
  1868. end;
  1869. OP_SUB:
  1870. begin
  1871. if is_shifter_const(aint(lo(value)),b) then
  1872. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  1873. else
  1874. begin
  1875. tmpreg:=cg.getintregister(list,OS_32);
  1876. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  1877. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1878. end;
  1879. if is_shifter_const(aint(hi(value)),b) then
  1880. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  1881. else
  1882. begin
  1883. tmpreg:=cg.getintregister(list,OS_32);
  1884. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1885. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  1886. end;
  1887. end;
  1888. else
  1889. internalerror(2003083101);
  1890. end;
  1891. end;
  1892. end;
  1893. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1894. begin
  1895. ovloc.loc:=LOC_VOID;
  1896. case op of
  1897. OP_NEG,
  1898. OP_NOT :
  1899. internalerror(200306017);
  1900. end;
  1901. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1902. begin
  1903. case op of
  1904. OP_ADD:
  1905. begin
  1906. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1907. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  1908. end;
  1909. OP_SUB:
  1910. begin
  1911. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1912. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  1913. end;
  1914. else
  1915. internalerror(2003083101);
  1916. end;
  1917. if size=OS_64 then
  1918. begin
  1919. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1920. ovloc.loc:=LOC_FLAGS;
  1921. case op of
  1922. OP_ADD:
  1923. ovloc.resflags:=F_CS;
  1924. OP_SUB:
  1925. ovloc.resflags:=F_CC;
  1926. end;
  1927. end;
  1928. end
  1929. else
  1930. begin
  1931. case op of
  1932. OP_AND,OP_OR,OP_XOR:
  1933. begin
  1934. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1935. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1936. end;
  1937. OP_ADD:
  1938. begin
  1939. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1940. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1941. end;
  1942. OP_SUB:
  1943. begin
  1944. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1945. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  1946. end;
  1947. else
  1948. internalerror(2003083101);
  1949. end;
  1950. end;
  1951. end;
  1952. begin
  1953. cg:=tcgarm.create;
  1954. cg64:=tcg64farm.create;
  1955. end.