aasmcpu.pas 57 KB

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