aasmcpu.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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_RET:
  877. result:=operand_read;
  878. A_STR,A_STUR:
  879. if opnr=0 then
  880. result:=operand_read
  881. else
  882. { check for pre/post indexed in spilling_get_operation_type_ref }
  883. result:=operand_read;
  884. A_STLXP,
  885. A_STLXR,
  886. A_STXP,
  887. A_STXR:
  888. if opnr=0 then
  889. result:=operand_write
  890. else
  891. result:=operand_read;
  892. A_STP:
  893. begin
  894. if opnr in [0,1] then
  895. result:=operand_read
  896. else
  897. { check for pre/post indexed in spilling_get_operation_type_ref }
  898. result:=operand_read;
  899. end;
  900. A_LDP,
  901. A_LDXP:
  902. begin
  903. if opnr in [0,1] then
  904. result:=operand_write
  905. else
  906. { check for pre/post indexed in spilling_get_operation_type_ref }
  907. result:=operand_read;
  908. end;
  909. {$ifdef EXTDEBUG}
  910. { play save to avoid hard to find bugs, better fail at compile time }
  911. A_ADD,
  912. A_ADRP,
  913. A_AND,
  914. A_ASR,
  915. A_BFI,
  916. A_BFXIL,
  917. A_CLZ,
  918. A_CSEL,
  919. A_CSET,
  920. A_CSETM,
  921. A_FABS,
  922. A_EON,
  923. A_EOR,
  924. A_FADD,
  925. A_FCVT,
  926. A_FDIV,
  927. A_FMADD,
  928. A_FMOV,
  929. A_FMSUB,
  930. A_FMUL,
  931. A_FNEG,
  932. A_FNMADD,
  933. A_FNMSUB,
  934. A_FRINTX,
  935. A_FSQRT,
  936. A_FSUB,
  937. A_ORR,
  938. A_LSL,
  939. A_LSLV,
  940. A_LSR,
  941. A_LSRV,
  942. A_MOV,
  943. A_MOVK,
  944. A_MOVN,
  945. A_MOVZ,
  946. A_MSUB,
  947. A_MUL,
  948. A_MVN,
  949. A_NEG,
  950. A_LDR,
  951. A_LDUR,
  952. A_RBIT,
  953. A_ROR,
  954. A_RORV,
  955. A_SBFX,
  956. A_SCVTF,
  957. A_FCVTZS,
  958. A_SDIV,
  959. A_SMULL,
  960. A_SUB,
  961. A_UBFIZ,
  962. A_UBFX,
  963. A_UCVTF,
  964. A_UDIV,
  965. A_UMULL:
  966. if opnr=0 then
  967. result:=operand_write
  968. else
  969. result:=operand_read;
  970. else
  971. Internalerror(2019090802);
  972. {$else EXTDEBUG}
  973. else
  974. if opnr=0 then
  975. result:=operand_write
  976. else
  977. result:=operand_read;
  978. {$endif EXTDEBUG}
  979. end;
  980. end;
  981. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  982. begin
  983. result:=operand_read;
  984. if (oper[opnr]^.ref^.base = reg) and
  985. (oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  986. result:=operand_readwrite;
  987. end;
  988. procedure BuildInsTabCache;
  989. // var
  990. // i : longint;
  991. begin
  992. (* new(instabcache);
  993. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  994. i:=0;
  995. while (i<InsTabEntries) do
  996. begin
  997. if InsTabCache^[InsTab[i].Opcode]=-1 then
  998. InsTabCache^[InsTab[i].Opcode]:=i;
  999. inc(i);
  1000. end; *)
  1001. end;
  1002. procedure InitAsm;
  1003. begin
  1004. if not assigned(instabcache) then
  1005. BuildInsTabCache;
  1006. end;
  1007. procedure DoneAsm;
  1008. begin
  1009. if assigned(instabcache) then
  1010. begin
  1011. dispose(instabcache);
  1012. instabcache:=nil;
  1013. end;
  1014. end;
  1015. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  1016. begin
  1017. i.oppostfix:=pf;
  1018. result:=i;
  1019. end;
  1020. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  1021. begin
  1022. i.condition:=c;
  1023. result:=i;
  1024. end;
  1025. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  1026. Begin
  1027. Current:=tai(Current.Next);
  1028. While Assigned(Current) And (Current.typ In SkipInstr) Do
  1029. Current:=tai(Current.Next);
  1030. Next:=Current;
  1031. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  1032. Result:=True
  1033. Else
  1034. Begin
  1035. Next:=Nil;
  1036. Result:=False;
  1037. End;
  1038. End;
  1039. (*
  1040. function armconstequal(hp1,hp2: tai): boolean;
  1041. begin
  1042. result:=false;
  1043. if hp1.typ<>hp2.typ then
  1044. exit;
  1045. case hp1.typ of
  1046. tai_const:
  1047. result:=
  1048. (tai_const(hp2).sym=tai_const(hp).sym) and
  1049. (tai_const(hp2).value=tai_const(hp).value) and
  1050. (tai(hp2.previous).typ=ait_label);
  1051. tai_const:
  1052. result:=
  1053. (tai_const(hp2).sym=tai_const(hp).sym) and
  1054. (tai_const(hp2).value=tai_const(hp).value) and
  1055. (tai(hp2.previous).typ=ait_label);
  1056. end;
  1057. end;
  1058. *)
  1059. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  1060. (*
  1061. var
  1062. curinspos,
  1063. penalty,
  1064. lastinspos,
  1065. { increased for every data element > 4 bytes inserted }
  1066. currentsize,
  1067. extradataoffset,
  1068. limit: longint;
  1069. curop : longint;
  1070. curtai : tai;
  1071. curdatatai,hp,hp2 : tai;
  1072. curdata : TAsmList;
  1073. l : tasmlabel;
  1074. doinsert,
  1075. removeref : boolean;
  1076. *)
  1077. begin
  1078. (*
  1079. curdata:=TAsmList.create;
  1080. lastinspos:=-1;
  1081. curinspos:=0;
  1082. extradataoffset:=0;
  1083. limit:=1016;
  1084. curtai:=tai(list.first);
  1085. doinsert:=false;
  1086. while assigned(curtai) do
  1087. begin
  1088. { instruction? }
  1089. case curtai.typ of
  1090. ait_instruction:
  1091. begin
  1092. { walk through all operand of the instruction }
  1093. for curop:=0 to taicpu(curtai).ops-1 do
  1094. begin
  1095. { reference? }
  1096. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  1097. begin
  1098. { pc relative symbol? }
  1099. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  1100. if assigned(curdatatai) and
  1101. { move only if we're at the first reference of a label }
  1102. not(tai_label(curdatatai).moved) then
  1103. begin
  1104. tai_label(curdatatai).moved:=true;
  1105. { check if symbol already used. }
  1106. { if yes, reuse the symbol }
  1107. hp:=tai(curdatatai.next);
  1108. removeref:=false;
  1109. if assigned(hp) then
  1110. begin
  1111. case hp.typ of
  1112. ait_const:
  1113. begin
  1114. if (tai_const(hp).consttype=aitconst_64bit) then
  1115. inc(extradataoffset);
  1116. end;
  1117. ait_realconst:
  1118. begin
  1119. inc(extradataoffset,((tai_realconst(hp).savesize-4+3) div 4));
  1120. end;
  1121. end;
  1122. if (hp.typ=ait_const) then
  1123. begin
  1124. hp2:=tai(curdata.first);
  1125. while assigned(hp2) do
  1126. begin
  1127. { if armconstequal(hp2,hp) then }
  1128. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  1129. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  1130. then
  1131. begin
  1132. with taicpu(curtai).oper[curop]^.ref^ do
  1133. begin
  1134. symboldata:=hp2.previous;
  1135. symbol:=tai_label(hp2.previous).labsym;
  1136. end;
  1137. removeref:=true;
  1138. break;
  1139. end;
  1140. hp2:=tai(hp2.next);
  1141. end;
  1142. end;
  1143. end;
  1144. { move or remove symbol reference }
  1145. repeat
  1146. hp:=tai(curdatatai.next);
  1147. listtoinsert.remove(curdatatai);
  1148. if removeref then
  1149. curdatatai.free
  1150. else
  1151. curdata.concat(curdatatai);
  1152. curdatatai:=hp;
  1153. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  1154. if lastinspos=-1 then
  1155. lastinspos:=curinspos;
  1156. end;
  1157. end;
  1158. end;
  1159. inc(curinspos);
  1160. end;
  1161. ait_align:
  1162. begin
  1163. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  1164. requires also incrementing curinspos by 1 }
  1165. inc(curinspos,(tai_align(curtai).aligntype div 4));
  1166. end;
  1167. ait_const:
  1168. begin
  1169. inc(curinspos);
  1170. if (tai_const(curtai).consttype=aitconst_64bit) then
  1171. inc(curinspos);
  1172. end;
  1173. ait_realconst:
  1174. begin
  1175. inc(curinspos,(tai_realconst(hp).savesize+3) div 4);
  1176. end;
  1177. end;
  1178. { special case for case jump tables }
  1179. if SimpleGetNextInstruction(curtai,hp) and
  1180. (tai(hp).typ=ait_instruction) and
  1181. (taicpu(hp).opcode=A_LDR) and
  1182. (taicpu(hp).oper[0]^.typ=top_reg) and
  1183. (taicpu(hp).oper[0]^.reg=NR_PC) then
  1184. begin
  1185. penalty:=1;
  1186. hp:=tai(hp.next);
  1187. { skip register allocations and comments inserted by the optimizer }
  1188. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc]) do
  1189. hp:=tai(hp.next);
  1190. while assigned(hp) and (hp.typ=ait_const) do
  1191. begin
  1192. inc(penalty);
  1193. hp:=tai(hp.next);
  1194. end;
  1195. end
  1196. else
  1197. penalty:=0;
  1198. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096 }
  1199. if SimpleGetNextInstruction(curtai,hp) and
  1200. (tai(hp).typ=ait_instruction) and
  1201. ((taicpu(hp).opcode=A_FLDS) or
  1202. (taicpu(hp).opcode=A_FLDD)) then
  1203. limit:=254;
  1204. { don't miss an insert }
  1205. doinsert:=doinsert or
  1206. (not(curdata.empty) and
  1207. (curinspos-lastinspos+penalty+extradataoffset>limit));
  1208. { split only at real instructions else the test below fails }
  1209. if doinsert and (curtai.typ=ait_instruction) and
  1210. (
  1211. { don't split loads of pc to lr and the following move }
  1212. not(
  1213. (taicpu(curtai).opcode=A_MOV) and
  1214. (taicpu(curtai).oper[0]^.typ=top_reg) and
  1215. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  1216. (taicpu(curtai).oper[1]^.typ=top_reg) and
  1217. (taicpu(curtai).oper[1]^.reg=NR_PC)
  1218. )
  1219. ) then
  1220. begin
  1221. lastinspos:=-1;
  1222. extradataoffset:=0;
  1223. limit:=1016;
  1224. doinsert:=false;
  1225. hp:=tai(curtai.next);
  1226. current_asmdata.getjumplabel(l);
  1227. curdata.insert(taicpu.op_sym(A_B,l));
  1228. curdata.concat(tai_label.create(l));
  1229. list.insertlistafter(curtai,curdata);
  1230. curtai:=hp;
  1231. end
  1232. else
  1233. curtai:=tai(curtai.next);
  1234. end;
  1235. list.concatlist(curdata);
  1236. curdata.free;
  1237. *)
  1238. end;
  1239. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1240. begin
  1241. insertpcrelativedata(list, listtoinsert);
  1242. end;
  1243. (*
  1244. Floating point instruction format information, taken from the linux kernel
  1245. ARM Floating Point Instruction Classes
  1246. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1247. |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
  1248. |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)
  1249. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1250. |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
  1251. |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
  1252. |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
  1253. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1254. CPDT data transfer instructions
  1255. LDF, STF, LFM (copro 2), SFM (copro 2)
  1256. CPDO dyadic arithmetic instructions
  1257. ADF, MUF, SUF, RSF, DVF, RDF,
  1258. POW, RPW, RMF, FML, FDV, FRD, POL
  1259. CPDO monadic arithmetic instructions
  1260. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1261. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1262. CPRT joint arithmetic/data transfer instructions
  1263. FIX (arithmetic followed by load/store)
  1264. FLT (load/store followed by arithmetic)
  1265. CMF, CNF CMFE, CNFE (comparisons)
  1266. WFS, RFS (write/read floating point status register)
  1267. WFC, RFC (write/read floating point control register)
  1268. cond condition codes
  1269. P pre/post index bit: 0 = postindex, 1 = preindex
  1270. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1271. W write back bit: 1 = update base register (Rn)
  1272. L load/store bit: 0 = store, 1 = load
  1273. Rn base register
  1274. Rd destination/source register
  1275. Fd floating point destination register
  1276. Fn floating point source register
  1277. Fm floating point source register or floating point constant
  1278. uv transfer length (TABLE 1)
  1279. wx register count (TABLE 2)
  1280. abcd arithmetic opcode (TABLES 3 & 4)
  1281. ef destination size (rounding precision) (TABLE 5)
  1282. gh rounding mode (TABLE 6)
  1283. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1284. i constant bit: 1 = constant (TABLE 6)
  1285. */
  1286. /*
  1287. TABLE 1
  1288. +-------------------------+---+---+---------+---------+
  1289. | Precision | u | v | FPSR.EP | length |
  1290. +-------------------------+---+---+---------+---------+
  1291. | Single | 0 | 0 | x | 1 words |
  1292. | Double | 1 | 1 | x | 2 words |
  1293. | Extended | 1 | 1 | x | 3 words |
  1294. | Packed decimal | 1 | 1 | 0 | 3 words |
  1295. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1296. +-------------------------+---+---+---------+---------+
  1297. Note: x = don't care
  1298. */
  1299. /*
  1300. TABLE 2
  1301. +---+---+---------------------------------+
  1302. | w | x | Number of registers to transfer |
  1303. +---+---+---------------------------------+
  1304. | 0 | 1 | 1 |
  1305. | 1 | 0 | 2 |
  1306. | 1 | 1 | 3 |
  1307. | 0 | 0 | 4 |
  1308. +---+---+---------------------------------+
  1309. */
  1310. /*
  1311. TABLE 3: Dyadic Floating Point Opcodes
  1312. +---+---+---+---+----------+-----------------------+-----------------------+
  1313. | a | b | c | d | Mnemonic | Description | Operation |
  1314. +---+---+---+---+----------+-----------------------+-----------------------+
  1315. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1316. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1317. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1318. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1319. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1320. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1321. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1322. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1323. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1324. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1325. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1326. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1327. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1328. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1329. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1330. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1331. +---+---+---+---+----------+-----------------------+-----------------------+
  1332. Note: POW, RPW, POL are deprecated, and are available for backwards
  1333. compatibility only.
  1334. */
  1335. /*
  1336. TABLE 4: Monadic Floating Point Opcodes
  1337. +---+---+---+---+----------+-----------------------+-----------------------+
  1338. | a | b | c | d | Mnemonic | Description | Operation |
  1339. +---+---+---+---+----------+-----------------------+-----------------------+
  1340. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1341. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1342. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1343. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1344. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1345. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1346. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1347. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1348. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1349. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1350. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1351. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1352. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1353. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1354. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1355. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1356. +---+---+---+---+----------+-----------------------+-----------------------+
  1357. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1358. available for backwards compatibility only.
  1359. */
  1360. /*
  1361. TABLE 5
  1362. +-------------------------+---+---+
  1363. | Rounding Precision | e | f |
  1364. +-------------------------+---+---+
  1365. | IEEE Single precision | 0 | 0 |
  1366. | IEEE Double precision | 0 | 1 |
  1367. | IEEE Extended precision | 1 | 0 |
  1368. | undefined (trap) | 1 | 1 |
  1369. +-------------------------+---+---+
  1370. */
  1371. /*
  1372. TABLE 5
  1373. +---------------------------------+---+---+
  1374. | Rounding Mode | g | h |
  1375. +---------------------------------+---+---+
  1376. | Round to nearest (default) | 0 | 0 |
  1377. | Round toward plus infinity | 0 | 1 |
  1378. | Round toward negative infinity | 1 | 0 |
  1379. | Round toward zero | 1 | 1 |
  1380. +---------------------------------+---+---+
  1381. *)
  1382. function taicpu.GetString:string;
  1383. var
  1384. i : longint;
  1385. s : string;
  1386. addsize : boolean;
  1387. begin
  1388. s:='['+gas_op2str[opcode];
  1389. for i:=0 to ops-1 do
  1390. begin
  1391. with oper[i]^ do
  1392. begin
  1393. if i=0 then
  1394. s:=s+' '
  1395. else
  1396. s:=s+',';
  1397. { type }
  1398. addsize:=false;
  1399. if (ot and OT_VREG)=OT_VREG then
  1400. s:=s+'vreg'
  1401. else
  1402. if (ot and OT_FPUREG)=OT_FPUREG then
  1403. s:=s+'fpureg'
  1404. else
  1405. if (ot and OT_REGISTER)=OT_REGISTER then
  1406. begin
  1407. s:=s+'reg';
  1408. addsize:=true;
  1409. end
  1410. else
  1411. if (ot and OT_REGLIST)=OT_REGLIST then
  1412. begin
  1413. s:=s+'reglist';
  1414. addsize:=false;
  1415. end
  1416. else
  1417. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1418. begin
  1419. s:=s+'imm';
  1420. addsize:=true;
  1421. end
  1422. else
  1423. if (ot and OT_MEMORY)=OT_MEMORY then
  1424. begin
  1425. s:=s+'mem';
  1426. addsize:=true;
  1427. if (ot and OT_AM2)<>0 then
  1428. s:=s+' am2 ';
  1429. end
  1430. else
  1431. s:=s+'???';
  1432. { size }
  1433. if addsize then
  1434. begin
  1435. if (ot and OT_BITS8)<>0 then
  1436. s:=s+'8'
  1437. else
  1438. if (ot and OT_BITS16)<>0 then
  1439. s:=s+'24'
  1440. else
  1441. if (ot and OT_BITS32)<>0 then
  1442. s:=s+'32'
  1443. else
  1444. if (ot and OT_BITSSHIFTER)<>0 then
  1445. s:=s+'shifter'
  1446. else
  1447. s:=s+'??';
  1448. { signed }
  1449. if (ot and OT_SIGNED)<>0 then
  1450. s:=s+'s';
  1451. end;
  1452. end;
  1453. end;
  1454. GetString:=s+']';
  1455. end;
  1456. procedure taicpu.ResetPass1;
  1457. begin
  1458. { we need to reset everything here, because the choosen insentry
  1459. can be invalid for a new situation where the previously optimized
  1460. insentry is not correct }
  1461. end;
  1462. procedure taicpu.ResetPass2;
  1463. begin
  1464. { we are here in a second pass, check if the instruction can be optimized }
  1465. end;
  1466. function taicpu.CheckIfValid:boolean;
  1467. begin
  1468. Result:=False; { unimplemented }
  1469. end;
  1470. function taicpu.Pass1(objdata:TObjData):longint;
  1471. begin
  1472. Pass1:=0;
  1473. end;
  1474. procedure taicpu.Pass2(objdata:TObjData);
  1475. begin
  1476. { error in pass1 ? }
  1477. current_filepos:=fileinfo;
  1478. { Generate the instruction }
  1479. { GenCode(objdata); }
  1480. end;
  1481. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1482. begin
  1483. end;
  1484. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1485. begin
  1486. end;
  1487. procedure taicpu.ppubuildderefimploper(var o:toper);
  1488. begin
  1489. end;
  1490. procedure taicpu.ppuderefoper(var o:toper);
  1491. begin
  1492. end;
  1493. begin
  1494. cai_align:=tai_align;
  1495. end.