aasmcpu.pas 57 KB

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