aasmcpu.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. {
  2. Copyright (c) 2003-2012 by Florian Klaempfl and others
  3. Contains the assembler object for Aarch64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. { Operand types }
  32. OT_NONE = $00000000;
  33. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  34. OT_BITS16 = $00000002;
  35. OT_BITS32 = $00000004;
  36. OT_BITS64 = $00000008; { FPU only }
  37. OT_BITS80 = $00000010;
  38. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  39. OT_NEAR = $00000040;
  40. OT_SHORT = $00000080;
  41. OT_BITSTINY = $00000100; { fpu constant }
  42. OT_BITSSHIFTER =
  43. $00000200;
  44. OT_SIZE_MASK = $000003FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_SHIFTEROP = $00000800;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_REGLIST = $00008000;
  54. OT_IMM8 = $00002001;
  55. OT_IMM24 = $00002002;
  56. OT_IMM32 = $00002004;
  57. OT_IMM64 = $00002008;
  58. OT_IMM80 = $00002010;
  59. OT_IMMTINY = $00002100;
  60. OT_IMMSHIFTER= $00002200;
  61. OT_IMMEDIATE24 = OT_IMM24;
  62. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  63. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  64. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  65. OT_IMMEDIATEFPU = OT_IMMTINY;
  66. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  67. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  68. OT_REG8 = $00201001;
  69. OT_REG16 = $00201002;
  70. OT_REG32 = $00201004;
  71. OT_REG64 = $00201008;
  72. OT_VREG = $00201010; { vector register }
  73. OT_REGF = $00201020; { coproc register }
  74. OT_MEMORY = $00204000; { register number in 'basereg' }
  75. OT_MEM8 = $00204001;
  76. OT_MEM16 = $00204002;
  77. OT_MEM32 = $00204004;
  78. OT_MEM64 = $00204008;
  79. OT_MEM80 = $00204010;
  80. { word/byte load/store }
  81. OT_AM2 = $00010000;
  82. { misc ld/st operations }
  83. OT_AM3 = $00020000;
  84. { multiple ld/st operations }
  85. OT_AM4 = $00040000;
  86. { co proc. ld/st operations }
  87. OT_AM5 = $00080000;
  88. OT_AMMASK = $000f0000;
  89. { IT instruction }
  90. OT_CONDITION = $00100000;
  91. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  92. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  93. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  94. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  95. OT_FPUREG = $01000000; { floating point stack registers }
  96. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  97. { a mask for the following }
  98. OT_MEM_OFFS = $00604000; { special type of EA }
  99. { simple [address] offset }
  100. OT_ONENESS = $00800000; { special type of immediate operand }
  101. { so UNITY == IMMEDIATE | ONENESS }
  102. OT_UNITY = $00802000; { for shift/rotate instructions }
  103. instabentries = {$i a64nop.inc}
  104. maxinfolen = 5;
  105. IF_NONE = $00000000;
  106. IF_ARMMASK = $000F0000;
  107. IF_ARM7 = $00070000;
  108. IF_FPMASK = $00F00000;
  109. IF_FPA = $00100000;
  110. { if the instruction can change in a second pass }
  111. IF_PASS2 = longint($80000000);
  112. type
  113. TInsTabCache=array[TasmOp] of longint;
  114. PInsTabCache=^TInsTabCache;
  115. tinsentry = record
  116. opcode : tasmop;
  117. ops : byte;
  118. optypes : array[0..3] of longint;
  119. code : array[0..maxinfolen] of char;
  120. flags : longint;
  121. end;
  122. pinsentry=^tinsentry;
  123. var
  124. InsTabCache : PInsTabCache;
  125. type
  126. taicpu = class(tai_cpu_abstract_sym)
  127. oppostfix : TOpPostfix;
  128. procedure loadshifterop(opidx:longint;const so:tshifterop);
  129. procedure loadconditioncode(opidx: longint; const c: tasmcond);
  130. procedure loadrealconst(opidx: longint; const _value: bestreal);
  131. procedure loadregset(opidx: longint; _basereg: tregister; _nregs: byte; _regsetindex: byte = 255);
  132. procedure loadindexedreg(opidx: longint; _indexedreg: tregister; _regindex: byte);
  133. constructor op_none(op : tasmop);
  134. constructor op_reg(op : tasmop;_op1 : tregister);
  135. constructor op_ref(op : tasmop;const _op1 : treference);
  136. constructor op_const(op : tasmop;_op1 : longint);
  137. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  138. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  139. constructor op_reg_cond(op: tasmop; _op1: tregister; _op2: tasmcond);
  140. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  141. constructor op_reg_const_shifterop(op : tasmop;_op1: tregister; _op2: aint;_op3 : tshifterop);
  142. constructor op_reg_realconst(op: tasmop; _op1: tregister; _op2: bestreal);
  143. constructor op_indexedreg_reg(op : tasmop;_op1: tregister; _op1index: byte; _op2 : tregister);
  144. constructor op_reg_indexedreg(op : tasmop;_op1: tregister; _op2 : tregister; _op2index: byte);
  145. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  146. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  147. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  148. constructor op_reg_reg_const_const(op : tasmop;_op1,_op2 : tregister; _op3, _op4: aint);
  149. constructor op_reg_reg_const_shifterop(op : tasmop;_op1,_op2 : tregister; _op3: aint; const _op4 : tshifterop);
  150. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  151. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  152. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  153. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister; const _op4 : tshifterop);
  154. constructor op_reg_reg_reg_cond(op : tasmop;_op1,_op2,_op3 : tregister; const _op4: tasmcond);
  155. constructor op_const_ref(op:tasmop; _op1: aint; _op2: treference);
  156. { this is for Jmp instructions }
  157. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  158. { ldN(r)/stN }
  159. constructor op_regset_reg_ref(op: tasmop; basereg: tregister; nregs: byte; const ref: treference);
  160. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  161. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  162. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  163. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  164. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  165. function spilling_get_operation_type(opnr: longint): topertype;override;
  166. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  167. { assembler }
  168. public
  169. { the next will reset all instructions that can change in pass 2 }
  170. procedure ResetPass1;override;
  171. procedure ResetPass2;override;
  172. function CheckIfValid:boolean;
  173. function GetString:string;
  174. function Pass1(objdata:TObjData):longint;override;
  175. procedure Pass2(objdata:TObjData);override;
  176. protected
  177. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  178. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  179. procedure ppubuildderefimploper(var o:toper);override;
  180. procedure ppuderefoper(var o:toper);override;
  181. end;
  182. tai_align = class(tai_align_abstract)
  183. { nothing to add }
  184. end;
  185. type
  186. tsimplereftype =
  187. { valid reference }
  188. (sr_simple,
  189. { invalid reference, should not be generated by the code generator (but
  190. can be encountered via inline assembly, where it must be rejected) }
  191. sr_internal_illegal,
  192. { invalid reference, may be generated by the code generator and then
  193. must be simplified (also rejected in inline assembly) }
  194. sr_complex);
  195. function simple_ref_type(op: tasmop; size:tcgsize; oppostfix: toppostfix; const ref: treference): tsimplereftype;
  196. function can_be_shifter_operand(opc: tasmop; opnr: longint): boolean;
  197. function valid_shifter_operand(opc: tasmop; useszr, usessp, is64bit: boolean; sm: tshiftmode; shiftimm: longint): boolean;
  198. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  199. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  200. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  201. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  202. { inserts pc relative symbols at places where they are reachable
  203. and transforms special instructions to valid instruction encodings }
  204. procedure finalizearmcode(list,listtoinsert : TAsmList);
  205. procedure InitAsm;
  206. procedure DoneAsm;
  207. implementation
  208. uses
  209. cutils,rgobj,itcpugas,aoptcpu;
  210. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  211. begin
  212. allocate_oper(opidx+1);
  213. with oper[opidx]^ do
  214. begin
  215. if typ<>top_shifterop then
  216. begin
  217. clearop(opidx);
  218. new(shifterop);
  219. end;
  220. shifterop^:=so;
  221. typ:=top_shifterop;
  222. end;
  223. end;
  224. procedure taicpu.loadconditioncode(opidx: longint; const c: tasmcond);
  225. begin
  226. allocate_oper(opidx+1);
  227. with oper[opidx]^ do
  228. begin
  229. if typ<>top_conditioncode then
  230. begin
  231. clearop(opidx);
  232. end;
  233. cc:=c;
  234. typ:=top_conditioncode;
  235. end;
  236. end;
  237. procedure taicpu.loadrealconst(opidx:longint;const _value:bestreal);
  238. begin
  239. allocate_oper(opidx+1);
  240. with oper[opidx]^ do
  241. begin
  242. if typ<>top_realconst then
  243. clearop(opidx);
  244. val_real:=_value;
  245. typ:=top_realconst;
  246. end;
  247. end;
  248. procedure taicpu.loadregset(opidx: longint; _basereg: tregister; _nregs: byte; _regsetindex: byte = 255);
  249. begin
  250. allocate_oper(opidx+1);
  251. with oper[opidx]^ do
  252. begin
  253. if typ<>top_regset then
  254. clearop(opidx);
  255. basereg:=_basereg;
  256. nregs:=_nregs;
  257. regsetindex:=_regsetindex;
  258. typ:=top_regset;
  259. end;
  260. end;
  261. procedure taicpu.loadindexedreg(opidx: longint; _indexedreg: tregister; _regindex: byte);
  262. begin
  263. allocate_oper(opidx+1);
  264. with oper[opidx]^ do
  265. begin
  266. if typ<>top_indexedreg then
  267. clearop(opidx);
  268. indexedreg:=_indexedreg;
  269. regindex:=_regindex;
  270. typ:=top_indexedreg;
  271. end;
  272. end;
  273. {*****************************************************************************
  274. taicpu Constructors
  275. *****************************************************************************}
  276. constructor taicpu.op_none(op : tasmop);
  277. begin
  278. inherited create(op);
  279. end;
  280. { for pld }
  281. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  282. begin
  283. inherited create(op);
  284. ops:=1;
  285. loadref(0,_op1);
  286. end;
  287. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  288. begin
  289. inherited create(op);
  290. ops:=1;
  291. loadreg(0,_op1);
  292. end;
  293. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  294. begin
  295. inherited create(op);
  296. ops:=1;
  297. loadconst(0,aint(_op1));
  298. end;
  299. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  300. begin
  301. inherited create(op);
  302. ops:=2;
  303. loadreg(0,_op1);
  304. loadreg(1,_op2);
  305. end;
  306. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  307. begin
  308. inherited create(op);
  309. ops:=2;
  310. loadreg(0,_op1);
  311. loadconst(1,aint(_op2));
  312. end;
  313. constructor taicpu.op_reg_const_shifterop(op: tasmop; _op1: tregister; _op2: aint; _op3: tshifterop);
  314. begin
  315. inherited create(op);
  316. ops:=3;
  317. loadreg(0,_op1);
  318. loadconst(1,_op2);
  319. loadshifterop(2,_op3);
  320. end;
  321. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  322. begin
  323. inherited create(op);
  324. ops:=2;
  325. loadreg(0,_op1);
  326. loadref(1,_op2);
  327. end;
  328. constructor taicpu.op_reg_cond(op: tasmop; _op1: tregister; _op2: tasmcond);
  329. begin
  330. inherited create(op);
  331. ops:=2;
  332. loadreg(0,_op1);
  333. loadconditioncode(1,_op2);
  334. end;
  335. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  336. begin
  337. inherited create(op);
  338. ops:=3;
  339. loadreg(0,_op1);
  340. loadreg(1,_op2);
  341. loadreg(2,_op3);
  342. end;
  343. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  344. begin
  345. inherited create(op);
  346. ops:=4;
  347. loadreg(0,_op1);
  348. loadreg(1,_op2);
  349. loadreg(2,_op3);
  350. loadreg(3,_op4);
  351. end;
  352. constructor taicpu.op_reg_realconst(op : tasmop; _op1 : tregister; _op2 : bestreal);
  353. begin
  354. inherited create(op);
  355. ops:=2;
  356. loadreg(0,_op1);
  357. loadrealconst(1,_op2);
  358. end;
  359. constructor taicpu.op_indexedreg_reg(op: tasmop; _op1: tregister; _op1index: byte; _op2: tregister);
  360. begin
  361. inherited create(op);
  362. ops:=2;
  363. loadindexedreg(0,_op1,_op1index);
  364. loadreg(1,_op2);
  365. end;
  366. constructor taicpu.op_reg_indexedreg(op: tasmop; _op1: tregister; _op2: tregister; _op2index: byte);
  367. begin
  368. inherited create(op);
  369. ops:=2;
  370. loadreg(0,_op1);
  371. loadindexedreg(1,_op2,_op2index);
  372. end;
  373. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  374. begin
  375. inherited create(op);
  376. ops:=3;
  377. loadreg(0,_op1);
  378. loadreg(1,_op2);
  379. loadconst(2,aint(_op3));
  380. end;
  381. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  382. begin
  383. inherited create(op);
  384. ops:=4;
  385. loadreg(0,_op1);
  386. loadreg(1,_op2);
  387. loadconst(2,aint(_op3));
  388. loadconst(3,aint(_op4));
  389. end;
  390. constructor taicpu.op_reg_reg_const_shifterop(op: tasmop; _op1, _op2: tregister; _op3: aint; const _op4: tshifterop);
  391. begin
  392. inherited create(op);
  393. ops:=4;
  394. loadreg(0,_op1);
  395. loadreg(1,_op2);
  396. loadconst(2,aint(_op3));
  397. loadshifterop(3,_op4);
  398. end;
  399. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  400. begin
  401. inherited create(op);
  402. ops:=3;
  403. loadreg(0,_op1);
  404. loadreg(1,_op2);
  405. loadsymbol(0,_op3,_op3ofs);
  406. end;
  407. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  408. begin
  409. inherited create(op);
  410. ops:=3;
  411. loadreg(0,_op1);
  412. loadreg(1,_op2);
  413. loadref(2,_op3);
  414. end;
  415. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  416. begin
  417. inherited create(op);
  418. ops:=3;
  419. loadreg(0,_op1);
  420. loadreg(1,_op2);
  421. loadshifterop(2,_op3);
  422. end;
  423. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister; const _op4 : tshifterop);
  424. begin
  425. inherited create(op);
  426. ops:=4;
  427. loadreg(0,_op1);
  428. loadreg(1,_op2);
  429. loadreg(2,_op3);
  430. loadshifterop(3,_op4);
  431. end;
  432. constructor taicpu.op_reg_reg_reg_cond(op: tasmop; _op1, _op2, _op3: tregister; const _op4: tasmcond);
  433. begin
  434. inherited create(op);
  435. ops:=4;
  436. loadreg(0,_op1);
  437. loadreg(1,_op2);
  438. loadreg(2,_op3);
  439. loadconditioncode(3,_op4);
  440. end;
  441. constructor taicpu.op_const_ref(op : tasmop; _op1 : aint; _op2 : treference);
  442. begin
  443. inherited create(op);
  444. ops:=2;
  445. loadconst(0,_op1);
  446. loadref(1,_op2);
  447. end;
  448. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  449. begin
  450. inherited create(op);
  451. condition:=cond;
  452. ops:=1;
  453. loadsymbol(0,_op1,0);
  454. end;
  455. constructor taicpu.op_regset_reg_ref(op: tasmop; basereg: tregister; nregs: byte; const ref: treference);
  456. begin
  457. inherited create(op);
  458. ops:=2;
  459. loadregset(0,basereg,nregs);
  460. loadref(1, ref);
  461. end;
  462. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  463. begin
  464. inherited create(op);
  465. ops:=1;
  466. loadsymbol(0,_op1,0);
  467. end;
  468. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  469. begin
  470. inherited create(op);
  471. ops:=1;
  472. loadsymbol(0,_op1,_op1ofs);
  473. end;
  474. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  475. begin
  476. inherited create(op);
  477. ops:=2;
  478. loadreg(0,_op1);
  479. loadsymbol(1,_op2,_op2ofs);
  480. end;
  481. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  482. begin
  483. inherited create(op);
  484. ops:=2;
  485. loadsymbol(0,_op1,_op1ofs);
  486. loadref(1,_op2);
  487. end;
  488. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  489. begin
  490. { allow the register allocator to remove unnecessary moves }
  491. result:=(
  492. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  493. ((opcode=A_FMOV) and (regtype = R_MMREGISTER))
  494. ) and
  495. (oppostfix in [PF_None]) and
  496. (condition=C_None) and
  497. (ops=2) and
  498. (oper[0]^.typ=top_reg) and
  499. (oper[1]^.typ=top_reg) and
  500. (oper[0]^.reg=oper[1]^.reg);
  501. end;
  502. function spilling_create_op(op: tasmop; const ref: treference; r: tregister): taicpu;
  503. const
  504. { invalid sizes for aarch64 are 0 }
  505. subreg2bytesize: array[TSubRegister] of byte =
  506. (0,0,0,0,4,8,0,0,0,4,8,0,0,0,0,0,0,0,0,0,0,0,0,8,16,0,16,16,16,16,16,16,16,16,16,16);
  507. var
  508. scalefactor: byte;
  509. begin
  510. scalefactor:=subreg2bytesize[getsubreg(r)];
  511. if scalefactor=0 then
  512. internalerror(2014120301);
  513. if (ref.offset>4095*scalefactor) or
  514. ((ref.offset>255) and
  515. ((ref.offset mod scalefactor)<>0)) or
  516. (ref.offset<-256) then
  517. internalerror(2014120302);
  518. case getregtype(r) of
  519. R_INTREGISTER,
  520. R_MMREGISTER:
  521. result:=taicpu.op_reg_ref(op,r,ref);
  522. else
  523. internalerror(2004010407);
  524. end;
  525. end;
  526. function is_valid_load_symbol(op: tasmop; oppostfix: toppostfix; const ref: treference): tsimplereftype;
  527. begin
  528. result:=sr_complex;
  529. if not assigned(ref.symboldata) and
  530. not(ref.refaddr in [addr_pic,addr_gotpageoffset,addr_gotpage,addr_pageoffset,addr_page]) then
  531. exit;
  532. { can't use pre-/post-indexed mode here (makes no sense either) }
  533. if ref.addressmode<>AM_OFFSET then
  534. exit;
  535. { "ldr literal" must be a 32/64 bit LDR and have a symbol }
  536. if (ref.refaddr=addr_pic) and
  537. (not (op in [A_LDR,A_B,A_BL]) or
  538. not(oppostfix in [PF_NONE,PF_W,PF_SW]) or
  539. (not assigned(ref.symbol) and
  540. not assigned(ref.symboldata))) then
  541. exit;
  542. { if this is a (got) page offset load, we must have a base register and a
  543. symbol (except if we have an ADD with a non-got page offset load) }
  544. if (ref.refaddr in [addr_gotpageoffset,addr_pageoffset]) and
  545. (
  546. (
  547. (
  548. (op<>A_ADD) or
  549. (ref.refaddr=addr_gotpageoffset)
  550. ) and
  551. (
  552. not assigned(ref.symbol) or
  553. (ref.base=NR_NO)
  554. )
  555. ) or
  556. (
  557. (
  558. (op=A_ADD) and
  559. (ref.refaddr=addr_pageoffset)
  560. ) and
  561. not assigned(ref.symbol) and
  562. (ref.base=NR_NO)
  563. ) or
  564. (ref.index<>NR_NO) or
  565. (ref.offset<>0)) then
  566. begin
  567. result:=sr_internal_illegal;
  568. exit;
  569. end;
  570. { cannot have base or index register (we generate these kind of
  571. references internally, they should never end up here with an
  572. extra base or offset) }
  573. if (ref.refaddr in [addr_gotpage,addr_page]) and
  574. (ref.base<>NR_NO) or
  575. (ref.index<>NR_NO) then
  576. begin
  577. result:=sr_internal_illegal;
  578. exit;
  579. end;
  580. result:=sr_simple;
  581. end;
  582. function simple_ref_type(op: tasmop; size:tcgsize; oppostfix: toppostfix; const ref: treference): tsimplereftype;
  583. var
  584. accesssize: longint;
  585. begin
  586. result:=sr_internal_illegal;
  587. { post-indexed is only allowed for vector and immediate loads/stores }
  588. if (ref.addressmode=AM_POSTINDEXED) and
  589. not((op = A_LD1) or (op = A_LD2) or (op = A_LD3) or (op = A_LD4) or
  590. (op = A_LD1R) or (op = A_LD2R) or (op = A_LD3R) or (op = A_LD4R) or
  591. (op = A_ST1) or (op = A_ST2) or (op = A_ST3) or (op = A_ST4)) and
  592. (not(op in [A_LDR,A_STR,A_LDP,A_STP]) or
  593. (ref.base=NR_NO) or
  594. (ref.index<>NR_NO)) then
  595. exit;
  596. { can only have a shift mode if we have an index }
  597. if (ref.index=NR_NO) and
  598. (ref.shiftmode<>SM_None) then
  599. exit;
  600. { the index can never be the stack pointer }
  601. if ref.index=NR_SP then
  602. exit;
  603. { no instruction supports an index without a base }
  604. if (ref.base=NR_NO) and
  605. (ref.index<>NR_NO) then
  606. begin
  607. result:=sr_complex;
  608. exit;
  609. end;
  610. { LDR literal or GOT entry: 32 or 64 bit, label }
  611. if assigned(ref.symboldata) or
  612. assigned(ref.symbol) then
  613. begin
  614. { we generate these kind of references internally; at least for now,
  615. they should never end up here with an extra base or offset or so }
  616. result:=is_valid_load_symbol(op,oppostfix,ref);
  617. exit;
  618. end;
  619. { any other reference cannot be gotpage/gotpageoffset/pic }
  620. if ref.refaddr in [addr_gotpage,addr_gotpageoffset,addr_page,addr_pageoffset,addr_pic] then
  621. exit;
  622. { base & index:
  623. * index cannot be the stack pointer
  624. * offset must be 0
  625. * can scale with the size of the access
  626. * can zero/sign extend 32 bit index register, and/or multiple by
  627. access size
  628. * no pre/post-indexing except for ldN(r)/stN
  629. }
  630. if (ref.base<>NR_NO) and
  631. (ref.index<>NR_NO) then
  632. begin
  633. case op of
  634. { this holds for both integer and fpu/vector loads }
  635. A_LDR,A_STR:
  636. begin
  637. if ref.addressmode in [AM_PREINDEXED,AM_POSTINDEXED] then
  638. exit;
  639. if (ref.offset=0) and
  640. (((ref.shiftmode=SM_None) and
  641. (ref.shiftimm=0)) or
  642. ((ref.shiftmode in [SM_LSL,SM_UXTW,SM_SXTW]) and
  643. (ref.shiftimm=tcgsizep2size[size]))) then
  644. result:=sr_simple
  645. else
  646. result:=sr_complex;
  647. end;
  648. A_LD1,A_LD2,A_LD3,A_LD4,
  649. A_LD1R,A_LD2R,A_LD3R,A_LD4R,
  650. A_ST1,A_ST2,A_ST3,A_ST4:
  651. begin
  652. if ref.addressmode in [AM_PREINDEXED] then
  653. exit;
  654. if (ref.offset=0) and
  655. (ref.addressmode=AM_POSTINDEXED) then
  656. result:=sr_simple
  657. else
  658. result:=sr_complex;
  659. end;
  660. { these don't support base+index }
  661. A_LDUR,A_STUR,
  662. A_LDP,A_STP:
  663. begin
  664. if ref.addressmode in [AM_PREINDEXED,AM_POSTINDEXED] then
  665. exit;
  666. result:=sr_complex;
  667. end
  668. else
  669. { nothing: result is already sr_internal_illegal };
  670. end;
  671. exit;
  672. end;
  673. { base + immediate offset. Variants:
  674. * LDR*/STR*:
  675. - pre- or post-indexed with signed 9 bit immediate
  676. - regular with unsiged scaled immediate (multiple of access
  677. size), in the range 0 to (12 bit * access_size)-1
  678. * LDP/STP
  679. - pre- or post-indexed with signed 9 bit immediate
  680. - regular with signed 9 bit immediate
  681. * LDUR*/STUR*:
  682. - regular with signed 9 bit immediate
  683. * ldN(r)/stN
  684. - 0 or with postindex
  685. }
  686. if ref.base<>NR_NO then
  687. begin
  688. accesssize:=1 shl tcgsizep2size[size];
  689. case op of
  690. A_LDR,A_STR:
  691. begin
  692. if (ref.addressmode=AM_OFFSET) and
  693. (ref.offset>=0) and
  694. (ref.offset<(((1 shl 12)-1)*accesssize)) and
  695. ((ref.offset mod accesssize)=0) then
  696. result:=sr_simple
  697. else if (ref.offset>=-256) and
  698. (ref.offset<=255) then
  699. begin
  700. { non pre-/post-indexed regular loads/stores can only be
  701. performed using LDUR/STUR }
  702. if ref.addressmode in [AM_PREINDEXED,AM_POSTINDEXED] then
  703. result:=sr_simple
  704. else
  705. result:=sr_complex
  706. end
  707. else
  708. result:=sr_complex;
  709. end;
  710. A_LDP,A_LDNP,
  711. A_STP,A_STNP:
  712. begin
  713. { only supported for 32/64 bit }
  714. if not(oppostfix in [PF_W,PF_SW,PF_None]) then
  715. exit;
  716. { offset must be a multple of the access size }
  717. if (ref.offset mod accesssize)<>0 then
  718. exit;
  719. { offset must fit in a signed 7 bit offset }
  720. if (ref.offset>=-(1 shl (6+tcgsizep2size[size]))) and
  721. (ref.offset<=(1 shl (6+tcgsizep2size[size]))-1) then
  722. result:=sr_simple
  723. else
  724. result:=sr_complex;
  725. end;
  726. A_LDUR,A_STUR:
  727. begin
  728. if ref.addressmode in [AM_PREINDEXED,AM_POSTINDEXED] then
  729. exit;
  730. if (ref.offset>=-256) and
  731. (ref.offset<=255) then
  732. result:=sr_simple
  733. else
  734. result:=sr_complex;
  735. end;
  736. A_LD1,A_LD2,A_LD3,A_LD4,
  737. A_LD1R,A_LD2R,A_LD3R,A_LD4R,
  738. A_ST1,A_ST2,A_ST3,A_ST4:
  739. begin
  740. if ref.addressmode in [AM_PREINDEXED] then
  741. exit;
  742. if (ref.offset=0) or
  743. ((ref.addressmode=AM_POSTINDEXED) and
  744. { to check the validity of the offset, we'd have to analyse the regset argument }
  745. (ref.offset>0)) then
  746. result:=sr_simple
  747. else
  748. result:=sr_complex;
  749. end;
  750. A_LDAR,
  751. A_LDAXR,
  752. A_LDXR,
  753. A_LDXP,
  754. A_STLR,
  755. A_STLXR,
  756. A_STLXP,
  757. A_STXP,
  758. A_STXR:
  759. begin
  760. if (ref.addressmode=AM_OFFSET) and
  761. (ref.offset=0) then
  762. result:=sr_simple;
  763. end
  764. else
  765. { nothing: result is already sr_internal_illegal };
  766. end;
  767. exit;
  768. end;
  769. { absolute addresses are not supported, have to load them first into
  770. a register }
  771. result:=sr_complex;
  772. end;
  773. function can_be_shifter_operand(opc: tasmop; opnr: longint): boolean;
  774. begin
  775. case opc of
  776. A_ADD,
  777. A_AND,
  778. A_EON,
  779. A_EOR,
  780. A_ORN,
  781. A_ORR,
  782. A_SUB:
  783. result:=opnr=3;
  784. A_BIC,
  785. A_CMN,
  786. A_CMP,
  787. A_MOVK,
  788. A_MOVZ,
  789. A_MOVN,
  790. A_MVN,
  791. A_NEG,
  792. A_TST:
  793. result:=opnr=2;
  794. else
  795. result:=false;
  796. end;
  797. end;
  798. function valid_shifter_operand(opc: tasmop; useszr, usessp, is64bit: boolean; sm: tshiftmode; shiftimm: longint): boolean;
  799. begin
  800. case opc of
  801. A_ADD,
  802. A_SUB,
  803. A_NEG,
  804. A_AND,
  805. A_TST,
  806. A_CMN,
  807. A_CMP:
  808. begin
  809. result:=false;
  810. if not useszr then
  811. result:=
  812. (sm in shiftedregmodes) and
  813. ((shiftimm in [0..31]) or
  814. (is64bit and
  815. (shiftimm in [32..63])));
  816. if not usessp then
  817. result:=
  818. result or
  819. ((sm in extendedregmodes) and
  820. (shiftimm in [0..4]));
  821. end;
  822. A_BIC,
  823. A_EON,
  824. A_EOR,
  825. A_MVN,
  826. A_ORN,
  827. A_ORR:
  828. result:=
  829. (sm in shiftedregmodes) and
  830. (shiftimm in [0..31*(ord(is64bit)+1)+ord(is64bit)]);
  831. A_MOVK,
  832. A_MOVZ,
  833. A_MOVN:
  834. result:=
  835. (sm=SM_LSL) and
  836. ((shiftimm in [0,16]) or
  837. (is64bit and
  838. (shiftimm in [32,48])));
  839. else
  840. result:=false;
  841. end;
  842. end;
  843. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  844. var
  845. op: tasmop;
  846. begin
  847. if (ref.index<>NR_NO) or
  848. (ref.offset<-256) or
  849. (ref.offset>255) then
  850. op:=A_LDR
  851. else
  852. op:=A_LDUR;
  853. result:=spilling_create_op(op,ref,r);
  854. end;
  855. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  856. var
  857. op: tasmop;
  858. begin
  859. if (ref.index<>NR_NO) or
  860. (ref.offset<-256) or
  861. (ref.offset>255) then
  862. op:=A_STR
  863. else
  864. op:=A_STUR;
  865. result:=spilling_create_op(op,ref,r);
  866. end;
  867. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  868. begin
  869. case opcode of
  870. A_B,A_BL,A_BR,A_BLR,
  871. A_CMN,A_CMP,
  872. A_CCMN,A_CCMP,
  873. A_TST,
  874. A_FCMP,A_FCMPE,
  875. A_CBZ,A_CBNZ,
  876. A_PRFM,A_PRFUM,
  877. A_RET:
  878. result:=operand_read;
  879. A_STR,A_STUR:
  880. if opnr=0 then
  881. result:=operand_read
  882. else
  883. { check for pre/post indexed in spilling_get_operation_type_ref }
  884. result:=operand_read;
  885. A_STP:
  886. begin
  887. if opnr in [0,1] then
  888. result:=operand_read
  889. else
  890. { check for pre/post indexed in spilling_get_operation_type_ref }
  891. result:=operand_read;
  892. end;
  893. A_LDP,
  894. A_LDXP:
  895. begin
  896. if opnr in [0,1] then
  897. result:=operand_write
  898. else
  899. { check for pre/post indexed in spilling_get_operation_type_ref }
  900. result:=operand_read;
  901. end;
  902. {$ifdef EXTDEBUG}
  903. { play save to avoid hard to find bugs, better fail at compile time }
  904. A_ADD,
  905. A_ADRP,
  906. A_AND,
  907. A_ASR,
  908. A_BFI,
  909. A_BFXIL,
  910. A_CLZ,
  911. A_CSEL,
  912. A_CSET,
  913. A_CSETM,
  914. A_FABS,
  915. A_EON,
  916. A_EOR,
  917. A_FADD,
  918. A_FCVT,
  919. A_FDIV,
  920. A_FMADD,
  921. A_FMOV,
  922. A_FMSUB,
  923. A_FMUL,
  924. A_FNEG,
  925. A_FNMADD,
  926. A_FNMSUB,
  927. A_FRINTX,
  928. A_FSQRT,
  929. A_FSUB,
  930. A_ORR,
  931. A_LSL,
  932. A_LSLV,
  933. A_LSR,
  934. A_LSRV,
  935. A_MOV,
  936. A_MOVK,
  937. A_MOVN,
  938. A_MOVZ,
  939. A_MSUB,
  940. A_MUL,
  941. A_MVN,
  942. A_NEG,
  943. A_LDR,
  944. A_LDUR,
  945. A_RBIT,
  946. A_ROR,
  947. A_RORV,
  948. A_SBFX,
  949. A_SCVTF,
  950. A_FCVTZS,
  951. A_SDIV,
  952. A_SMULL,
  953. A_STLXP,
  954. A_STLXR,
  955. A_STXP,
  956. A_STXR,
  957. A_SUB,
  958. A_SXTB,
  959. A_SXTH,
  960. A_SXTW,
  961. A_UBFIZ,
  962. A_UBFX,
  963. A_UCVTF,
  964. A_UDIV,
  965. A_UMULL,
  966. A_UXTB,
  967. A_UXTH:
  968. if opnr=0 then
  969. result:=operand_write
  970. else
  971. result:=operand_read;
  972. else
  973. Internalerror(2019090802);
  974. {$else EXTDEBUG}
  975. else
  976. if opnr=0 then
  977. result:=operand_write
  978. else
  979. result:=operand_read;
  980. {$endif EXTDEBUG}
  981. end;
  982. end;
  983. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  984. begin
  985. result:=operand_read;
  986. if (oper[opnr]^.ref^.base = reg) and
  987. (oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  988. result:=operand_readwrite;
  989. end;
  990. procedure BuildInsTabCache;
  991. // var
  992. // i : longint;
  993. begin
  994. (* new(instabcache);
  995. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  996. i:=0;
  997. while (i<InsTabEntries) do
  998. begin
  999. if InsTabCache^[InsTab[i].Opcode]=-1 then
  1000. InsTabCache^[InsTab[i].Opcode]:=i;
  1001. inc(i);
  1002. end; *)
  1003. end;
  1004. procedure InitAsm;
  1005. begin
  1006. if not assigned(instabcache) then
  1007. BuildInsTabCache;
  1008. end;
  1009. procedure DoneAsm;
  1010. begin
  1011. if assigned(instabcache) then
  1012. begin
  1013. dispose(instabcache);
  1014. instabcache:=nil;
  1015. end;
  1016. end;
  1017. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  1018. begin
  1019. i.oppostfix:=pf;
  1020. result:=i;
  1021. end;
  1022. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  1023. begin
  1024. i.condition:=c;
  1025. result:=i;
  1026. end;
  1027. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  1028. Begin
  1029. Current:=tai(Current.Next);
  1030. While Assigned(Current) And (Current.typ In SkipInstr) Do
  1031. Current:=tai(Current.Next);
  1032. Next:=Current;
  1033. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  1034. Result:=True
  1035. Else
  1036. Begin
  1037. Next:=Nil;
  1038. Result:=False;
  1039. End;
  1040. End;
  1041. (*
  1042. function armconstequal(hp1,hp2: tai): boolean;
  1043. begin
  1044. result:=false;
  1045. if hp1.typ<>hp2.typ then
  1046. exit;
  1047. case hp1.typ of
  1048. tai_const:
  1049. result:=
  1050. (tai_const(hp2).sym=tai_const(hp).sym) and
  1051. (tai_const(hp2).value=tai_const(hp).value) and
  1052. (tai(hp2.previous).typ=ait_label);
  1053. tai_const:
  1054. result:=
  1055. (tai_const(hp2).sym=tai_const(hp).sym) and
  1056. (tai_const(hp2).value=tai_const(hp).value) and
  1057. (tai(hp2.previous).typ=ait_label);
  1058. end;
  1059. end;
  1060. *)
  1061. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  1062. (*
  1063. var
  1064. curinspos,
  1065. penalty,
  1066. lastinspos,
  1067. { increased for every data element > 4 bytes inserted }
  1068. currentsize,
  1069. extradataoffset,
  1070. limit: longint;
  1071. curop : longint;
  1072. curtai : tai;
  1073. curdatatai,hp,hp2 : tai;
  1074. curdata : TAsmList;
  1075. l : tasmlabel;
  1076. doinsert,
  1077. removeref : boolean;
  1078. *)
  1079. begin
  1080. (*
  1081. curdata:=TAsmList.create;
  1082. lastinspos:=-1;
  1083. curinspos:=0;
  1084. extradataoffset:=0;
  1085. limit:=1016;
  1086. curtai:=tai(list.first);
  1087. doinsert:=false;
  1088. while assigned(curtai) do
  1089. begin
  1090. { instruction? }
  1091. case curtai.typ of
  1092. ait_instruction:
  1093. begin
  1094. { walk through all operand of the instruction }
  1095. for curop:=0 to taicpu(curtai).ops-1 do
  1096. begin
  1097. { reference? }
  1098. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  1099. begin
  1100. { pc relative symbol? }
  1101. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  1102. if assigned(curdatatai) and
  1103. { move only if we're at the first reference of a label }
  1104. not(tai_label(curdatatai).moved) then
  1105. begin
  1106. tai_label(curdatatai).moved:=true;
  1107. { check if symbol already used. }
  1108. { if yes, reuse the symbol }
  1109. hp:=tai(curdatatai.next);
  1110. removeref:=false;
  1111. if assigned(hp) then
  1112. begin
  1113. case hp.typ of
  1114. ait_const:
  1115. begin
  1116. if (tai_const(hp).consttype=aitconst_64bit) then
  1117. inc(extradataoffset);
  1118. end;
  1119. ait_realconst:
  1120. begin
  1121. inc(extradataoffset,((tai_realconst(hp).savesize-4+3) div 4));
  1122. end;
  1123. end;
  1124. if (hp.typ=ait_const) then
  1125. begin
  1126. hp2:=tai(curdata.first);
  1127. while assigned(hp2) do
  1128. begin
  1129. { if armconstequal(hp2,hp) then }
  1130. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  1131. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  1132. then
  1133. begin
  1134. with taicpu(curtai).oper[curop]^.ref^ do
  1135. begin
  1136. symboldata:=hp2.previous;
  1137. symbol:=tai_label(hp2.previous).labsym;
  1138. end;
  1139. removeref:=true;
  1140. break;
  1141. end;
  1142. hp2:=tai(hp2.next);
  1143. end;
  1144. end;
  1145. end;
  1146. { move or remove symbol reference }
  1147. repeat
  1148. hp:=tai(curdatatai.next);
  1149. listtoinsert.remove(curdatatai);
  1150. if removeref then
  1151. curdatatai.free
  1152. else
  1153. curdata.concat(curdatatai);
  1154. curdatatai:=hp;
  1155. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  1156. if lastinspos=-1 then
  1157. lastinspos:=curinspos;
  1158. end;
  1159. end;
  1160. end;
  1161. inc(curinspos);
  1162. end;
  1163. ait_align:
  1164. begin
  1165. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  1166. requires also incrementing curinspos by 1 }
  1167. inc(curinspos,(tai_align(curtai).aligntype div 4));
  1168. end;
  1169. ait_const:
  1170. begin
  1171. inc(curinspos);
  1172. if (tai_const(curtai).consttype=aitconst_64bit) then
  1173. inc(curinspos);
  1174. end;
  1175. ait_realconst:
  1176. begin
  1177. inc(curinspos,(tai_realconst(hp).savesize+3) div 4);
  1178. end;
  1179. end;
  1180. { special case for case jump tables }
  1181. if SimpleGetNextInstruction(curtai,hp) and
  1182. (tai(hp).typ=ait_instruction) and
  1183. (taicpu(hp).opcode=A_LDR) and
  1184. (taicpu(hp).oper[0]^.typ=top_reg) and
  1185. (taicpu(hp).oper[0]^.reg=NR_PC) then
  1186. begin
  1187. penalty:=1;
  1188. hp:=tai(hp.next);
  1189. { skip register allocations and comments inserted by the optimizer }
  1190. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc]) do
  1191. hp:=tai(hp.next);
  1192. while assigned(hp) and (hp.typ=ait_const) do
  1193. begin
  1194. inc(penalty);
  1195. hp:=tai(hp.next);
  1196. end;
  1197. end
  1198. else
  1199. penalty:=0;
  1200. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096 }
  1201. if SimpleGetNextInstruction(curtai,hp) and
  1202. (tai(hp).typ=ait_instruction) and
  1203. ((taicpu(hp).opcode=A_FLDS) or
  1204. (taicpu(hp).opcode=A_FLDD)) then
  1205. limit:=254;
  1206. { don't miss an insert }
  1207. doinsert:=doinsert or
  1208. (not(curdata.empty) and
  1209. (curinspos-lastinspos+penalty+extradataoffset>limit));
  1210. { split only at real instructions else the test below fails }
  1211. if doinsert and (curtai.typ=ait_instruction) and
  1212. (
  1213. { don't split loads of pc to lr and the following move }
  1214. not(
  1215. (taicpu(curtai).opcode=A_MOV) and
  1216. (taicpu(curtai).oper[0]^.typ=top_reg) and
  1217. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  1218. (taicpu(curtai).oper[1]^.typ=top_reg) and
  1219. (taicpu(curtai).oper[1]^.reg=NR_PC)
  1220. )
  1221. ) then
  1222. begin
  1223. lastinspos:=-1;
  1224. extradataoffset:=0;
  1225. limit:=1016;
  1226. doinsert:=false;
  1227. hp:=tai(curtai.next);
  1228. current_asmdata.getjumplabel(l);
  1229. curdata.insert(taicpu.op_sym(A_B,l));
  1230. curdata.concat(tai_label.create(l));
  1231. list.insertlistafter(curtai,curdata);
  1232. curtai:=hp;
  1233. end
  1234. else
  1235. curtai:=tai(curtai.next);
  1236. end;
  1237. list.concatlist(curdata);
  1238. curdata.free;
  1239. *)
  1240. end;
  1241. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1242. begin
  1243. insertpcrelativedata(list, listtoinsert);
  1244. end;
  1245. (*
  1246. Floating point instruction format information, taken from the linux kernel
  1247. ARM Floating Point Instruction Classes
  1248. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1249. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  1250. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  1251. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1252. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  1253. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  1254. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  1255. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1256. CPDT data transfer instructions
  1257. LDF, STF, LFM (copro 2), SFM (copro 2)
  1258. CPDO dyadic arithmetic instructions
  1259. ADF, MUF, SUF, RSF, DVF, RDF,
  1260. POW, RPW, RMF, FML, FDV, FRD, POL
  1261. CPDO monadic arithmetic instructions
  1262. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1263. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1264. CPRT joint arithmetic/data transfer instructions
  1265. FIX (arithmetic followed by load/store)
  1266. FLT (load/store followed by arithmetic)
  1267. CMF, CNF CMFE, CNFE (comparisons)
  1268. WFS, RFS (write/read floating point status register)
  1269. WFC, RFC (write/read floating point control register)
  1270. cond condition codes
  1271. P pre/post index bit: 0 = postindex, 1 = preindex
  1272. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1273. W write back bit: 1 = update base register (Rn)
  1274. L load/store bit: 0 = store, 1 = load
  1275. Rn base register
  1276. Rd destination/source register
  1277. Fd floating point destination register
  1278. Fn floating point source register
  1279. Fm floating point source register or floating point constant
  1280. uv transfer length (TABLE 1)
  1281. wx register count (TABLE 2)
  1282. abcd arithmetic opcode (TABLES 3 & 4)
  1283. ef destination size (rounding precision) (TABLE 5)
  1284. gh rounding mode (TABLE 6)
  1285. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1286. i constant bit: 1 = constant (TABLE 6)
  1287. */
  1288. /*
  1289. TABLE 1
  1290. +-------------------------+---+---+---------+---------+
  1291. | Precision | u | v | FPSR.EP | length |
  1292. +-------------------------+---+---+---------+---------+
  1293. | Single | 0 | 0 | x | 1 words |
  1294. | Double | 1 | 1 | x | 2 words |
  1295. | Extended | 1 | 1 | x | 3 words |
  1296. | Packed decimal | 1 | 1 | 0 | 3 words |
  1297. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1298. +-------------------------+---+---+---------+---------+
  1299. Note: x = don't care
  1300. */
  1301. /*
  1302. TABLE 2
  1303. +---+---+---------------------------------+
  1304. | w | x | Number of registers to transfer |
  1305. +---+---+---------------------------------+
  1306. | 0 | 1 | 1 |
  1307. | 1 | 0 | 2 |
  1308. | 1 | 1 | 3 |
  1309. | 0 | 0 | 4 |
  1310. +---+---+---------------------------------+
  1311. */
  1312. /*
  1313. TABLE 3: Dyadic Floating Point Opcodes
  1314. +---+---+---+---+----------+-----------------------+-----------------------+
  1315. | a | b | c | d | Mnemonic | Description | Operation |
  1316. +---+---+---+---+----------+-----------------------+-----------------------+
  1317. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1318. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1319. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1320. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1321. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1322. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1323. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1324. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1325. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1326. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1327. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1328. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1329. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1330. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1331. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1332. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1333. +---+---+---+---+----------+-----------------------+-----------------------+
  1334. Note: POW, RPW, POL are deprecated, and are available for backwards
  1335. compatibility only.
  1336. */
  1337. /*
  1338. TABLE 4: Monadic Floating Point Opcodes
  1339. +---+---+---+---+----------+-----------------------+-----------------------+
  1340. | a | b | c | d | Mnemonic | Description | Operation |
  1341. +---+---+---+---+----------+-----------------------+-----------------------+
  1342. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1343. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1344. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1345. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1346. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1347. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1348. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1349. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1350. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1351. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1352. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1353. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1354. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1355. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1356. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1357. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1358. +---+---+---+---+----------+-----------------------+-----------------------+
  1359. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1360. available for backwards compatibility only.
  1361. */
  1362. /*
  1363. TABLE 5
  1364. +-------------------------+---+---+
  1365. | Rounding Precision | e | f |
  1366. +-------------------------+---+---+
  1367. | IEEE Single precision | 0 | 0 |
  1368. | IEEE Double precision | 0 | 1 |
  1369. | IEEE Extended precision | 1 | 0 |
  1370. | undefined (trap) | 1 | 1 |
  1371. +-------------------------+---+---+
  1372. */
  1373. /*
  1374. TABLE 5
  1375. +---------------------------------+---+---+
  1376. | Rounding Mode | g | h |
  1377. +---------------------------------+---+---+
  1378. | Round to nearest (default) | 0 | 0 |
  1379. | Round toward plus infinity | 0 | 1 |
  1380. | Round toward negative infinity | 1 | 0 |
  1381. | Round toward zero | 1 | 1 |
  1382. +---------------------------------+---+---+
  1383. *)
  1384. function taicpu.GetString:string;
  1385. var
  1386. i : longint;
  1387. s : string;
  1388. addsize : boolean;
  1389. begin
  1390. s:='['+gas_op2str[opcode];
  1391. for i:=0 to ops-1 do
  1392. begin
  1393. with oper[i]^ do
  1394. begin
  1395. if i=0 then
  1396. s:=s+' '
  1397. else
  1398. s:=s+',';
  1399. { type }
  1400. addsize:=false;
  1401. if (ot and OT_VREG)=OT_VREG then
  1402. s:=s+'vreg'
  1403. else
  1404. if (ot and OT_FPUREG)=OT_FPUREG then
  1405. s:=s+'fpureg'
  1406. else
  1407. if (ot and OT_REGISTER)=OT_REGISTER then
  1408. begin
  1409. s:=s+'reg';
  1410. addsize:=true;
  1411. end
  1412. else
  1413. if (ot and OT_REGLIST)=OT_REGLIST then
  1414. begin
  1415. s:=s+'reglist';
  1416. addsize:=false;
  1417. end
  1418. else
  1419. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1420. begin
  1421. s:=s+'imm';
  1422. addsize:=true;
  1423. end
  1424. else
  1425. if (ot and OT_MEMORY)=OT_MEMORY then
  1426. begin
  1427. s:=s+'mem';
  1428. addsize:=true;
  1429. if (ot and OT_AM2)<>0 then
  1430. s:=s+' am2 ';
  1431. end
  1432. else
  1433. s:=s+'???';
  1434. { size }
  1435. if addsize then
  1436. begin
  1437. if (ot and OT_BITS8)<>0 then
  1438. s:=s+'8'
  1439. else
  1440. if (ot and OT_BITS16)<>0 then
  1441. s:=s+'24'
  1442. else
  1443. if (ot and OT_BITS32)<>0 then
  1444. s:=s+'32'
  1445. else
  1446. if (ot and OT_BITSSHIFTER)<>0 then
  1447. s:=s+'shifter'
  1448. else
  1449. s:=s+'??';
  1450. { signed }
  1451. if (ot and OT_SIGNED)<>0 then
  1452. s:=s+'s';
  1453. end;
  1454. end;
  1455. end;
  1456. GetString:=s+']';
  1457. end;
  1458. procedure taicpu.ResetPass1;
  1459. begin
  1460. { we need to reset everything here, because the choosen insentry
  1461. can be invalid for a new situation where the previously optimized
  1462. insentry is not correct }
  1463. end;
  1464. procedure taicpu.ResetPass2;
  1465. begin
  1466. { we are here in a second pass, check if the instruction can be optimized }
  1467. end;
  1468. function taicpu.CheckIfValid:boolean;
  1469. begin
  1470. Result:=False; { unimplemented }
  1471. end;
  1472. function taicpu.Pass1(objdata:TObjData):longint;
  1473. begin
  1474. Pass1:=0;
  1475. end;
  1476. procedure taicpu.Pass2(objdata:TObjData);
  1477. begin
  1478. { error in pass1 ? }
  1479. current_filepos:=fileinfo;
  1480. { Generate the instruction }
  1481. { GenCode(objdata); }
  1482. end;
  1483. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1484. begin
  1485. end;
  1486. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1487. begin
  1488. end;
  1489. procedure taicpu.ppubuildderefimploper(var o:toper);
  1490. begin
  1491. end;
  1492. procedure taicpu.ppuderefoper(var o:toper);
  1493. begin
  1494. end;
  1495. begin
  1496. cai_align:=tai_align;
  1497. end.