aasmcpu.pas 57 KB

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