aasmcpu.pas 56 KB

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