aasmcpu.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. {
  2. Copyright (c) 2003-2012 by Florian Klaempfl and others
  3. Contains the assembler object for ARM64
  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. constructor op_none(op : tasmop);
  130. constructor op_reg(op : tasmop;_op1 : tregister);
  131. constructor op_ref(op : tasmop;const _op1 : treference);
  132. constructor op_const(op : tasmop;_op1 : longint);
  133. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  134. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  135. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  136. constructor op_reg_const_shifterop(op : tasmop;_op1: tregister; _op2: aint;_op3 : tshifterop);
  137. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  138. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  139. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  140. constructor op_reg_reg_const_shifterop(op : tasmop;_op1,_op2 : tregister; _op3: aint; const _op4 : tshifterop);
  141. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  142. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  143. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  144. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister; const _op4 : tshifterop);
  145. { this is for Jmp instructions }
  146. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  147. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  148. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  149. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  150. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  151. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  152. function spilling_get_operation_type(opnr: longint): topertype;override;
  153. { assembler }
  154. public
  155. { the next will reset all instructions that can change in pass 2 }
  156. procedure ResetPass1;override;
  157. procedure ResetPass2;override;
  158. function CheckIfValid:boolean;
  159. function GetString:string;
  160. function Pass1(objdata:TObjData):longint;override;
  161. procedure Pass2(objdata:TObjData);override;
  162. protected
  163. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  164. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  165. procedure ppubuildderefimploper(var o:toper);override;
  166. procedure ppuderefoper(var o:toper);override;
  167. private
  168. { next fields are filled in pass1, so pass2 is faster }
  169. inssize : shortint;
  170. insoffset : longint;
  171. LastInsOffset : longint; { need to be public to be reset }
  172. insentry : PInsEntry;
  173. function InsEnd:longint;
  174. procedure create_ot(objdata:TObjData);
  175. function Matches(p:PInsEntry):longint;
  176. function calcsize(p:PInsEntry):shortint;
  177. procedure gencode(objdata:TObjData);
  178. function NeedAddrPrefix(opidx:byte):boolean;
  179. procedure Swapoperands;
  180. function FindInsentry(objdata:TObjData):boolean;
  181. end;
  182. tai_align = class(tai_align_abstract)
  183. { nothing to add }
  184. end;
  185. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  186. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  187. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  188. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  189. { inserts pc relative symbols at places where they are reachable
  190. and transforms special instructions to valid instruction encodings }
  191. procedure finalizearmcode(list,listtoinsert : TAsmList);
  192. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  193. procedure InsertPData;
  194. procedure InitAsm;
  195. procedure DoneAsm;
  196. implementation
  197. uses
  198. cutils,rgobj,itcpugas,aoptcpu;
  199. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  200. begin
  201. allocate_oper(opidx+1);
  202. with oper[opidx]^ do
  203. begin
  204. if typ<>top_shifterop then
  205. begin
  206. clearop(opidx);
  207. new(shifterop);
  208. end;
  209. shifterop^:=so;
  210. typ:=top_shifterop;
  211. end;
  212. end;
  213. {*****************************************************************************
  214. taicpu Constructors
  215. *****************************************************************************}
  216. constructor taicpu.op_none(op : tasmop);
  217. begin
  218. inherited create(op);
  219. end;
  220. { for pld }
  221. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  222. begin
  223. inherited create(op);
  224. ops:=1;
  225. loadref(0,_op1);
  226. end;
  227. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  228. begin
  229. inherited create(op);
  230. ops:=1;
  231. loadreg(0,_op1);
  232. end;
  233. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  234. begin
  235. inherited create(op);
  236. ops:=1;
  237. loadconst(0,aint(_op1));
  238. end;
  239. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  240. begin
  241. inherited create(op);
  242. ops:=2;
  243. loadreg(0,_op1);
  244. loadreg(1,_op2);
  245. end;
  246. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  247. begin
  248. inherited create(op);
  249. ops:=2;
  250. loadreg(0,_op1);
  251. loadconst(1,aint(_op2));
  252. end;
  253. constructor taicpu.op_reg_const_shifterop(op: tasmop; _op1: tregister; _op2: aint; _op3: tshifterop);
  254. begin
  255. inherited create(op);
  256. ops:=3;
  257. loadreg(0,_op1);
  258. loadconst(1,_op2);
  259. loadshifterop(2,_op3);
  260. end;
  261. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  262. begin
  263. inherited create(op);
  264. ops:=2;
  265. loadreg(0,_op1);
  266. loadref(1,_op2);
  267. end;
  268. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  269. begin
  270. inherited create(op);
  271. ops:=3;
  272. loadreg(0,_op1);
  273. loadreg(1,_op2);
  274. loadreg(2,_op3);
  275. end;
  276. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  277. begin
  278. inherited create(op);
  279. ops:=4;
  280. loadreg(0,_op1);
  281. loadreg(1,_op2);
  282. loadreg(2,_op3);
  283. loadreg(3,_op4);
  284. end;
  285. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  286. begin
  287. inherited create(op);
  288. ops:=3;
  289. loadreg(0,_op1);
  290. loadreg(1,_op2);
  291. loadconst(2,aint(_op3));
  292. end;
  293. constructor taicpu.op_reg_reg_const_shifterop(op: tasmop; _op1, _op2: tregister; _op3: aint; const _op4: tshifterop);
  294. begin
  295. inherited create(op);
  296. ops:=4;
  297. loadreg(0,_op1);
  298. loadreg(1,_op2);
  299. loadconst(2,aint(_op3));
  300. loadshifterop(3,_op4);
  301. end;
  302. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  303. begin
  304. inherited create(op);
  305. ops:=3;
  306. loadreg(0,_op1);
  307. loadreg(1,_op2);
  308. loadsymbol(0,_op3,_op3ofs);
  309. end;
  310. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  311. begin
  312. inherited create(op);
  313. ops:=3;
  314. loadreg(0,_op1);
  315. loadreg(1,_op2);
  316. loadref(2,_op3);
  317. end;
  318. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  319. begin
  320. inherited create(op);
  321. ops:=3;
  322. loadreg(0,_op1);
  323. loadreg(1,_op2);
  324. loadshifterop(2,_op3);
  325. end;
  326. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister; const _op4 : tshifterop);
  327. begin
  328. inherited create(op);
  329. ops:=4;
  330. loadreg(0,_op1);
  331. loadreg(1,_op2);
  332. loadreg(2,_op3);
  333. loadshifterop(3,_op4);
  334. end;
  335. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  336. begin
  337. inherited create(op);
  338. condition:=cond;
  339. ops:=1;
  340. loadsymbol(0,_op1,0);
  341. end;
  342. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  343. begin
  344. inherited create(op);
  345. ops:=1;
  346. loadsymbol(0,_op1,0);
  347. end;
  348. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  349. begin
  350. inherited create(op);
  351. ops:=1;
  352. loadsymbol(0,_op1,_op1ofs);
  353. end;
  354. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  355. begin
  356. inherited create(op);
  357. ops:=2;
  358. loadreg(0,_op1);
  359. loadsymbol(1,_op2,_op2ofs);
  360. end;
  361. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  362. begin
  363. inherited create(op);
  364. ops:=2;
  365. loadsymbol(0,_op1,_op1ofs);
  366. loadref(1,_op2);
  367. end;
  368. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  369. begin
  370. { allow the register allocator to remove unnecessary moves }
  371. result:=(
  372. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  373. ((opcode=A_FMOV) and (regtype = R_MMREGISTER))
  374. ) and
  375. (oppostfix in [PF_None]) and
  376. (condition=C_None) and
  377. (ops=2) and
  378. (oper[0]^.typ=top_reg) and
  379. (oper[1]^.typ=top_reg) and
  380. (oper[0]^.reg=oper[1]^.reg);
  381. end;
  382. function spilling_create_op(op: tasmop; const ref: treference; r: tregister): taicpu;
  383. const
  384. { invalid sizes for aarch64 are 0 }
  385. subreg2bytesize: array[TSubRegister] of byte =
  386. (0,0,0,0,4,8,0,0,0,4,8,0,0,0);
  387. var
  388. scalefactor: byte;
  389. begin
  390. scalefactor:=subreg2bytesize[getsubreg(r)];
  391. if scalefactor=0 then
  392. internalerror(2014120301);
  393. if (ref.offset>4095*scalefactor) or
  394. ((ref.offset>255) and
  395. ((ref.offset mod scalefactor)<>0)) or
  396. (ref.offset<-256) then
  397. internalerror(2014120302);
  398. case getregtype(r) of
  399. R_INTREGISTER,
  400. R_MMREGISTER:
  401. result:=taicpu.op_reg_ref(op,r,ref);
  402. else
  403. internalerror(200401041);
  404. end;
  405. end;
  406. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  407. var
  408. op: tasmop;
  409. begin
  410. if (ref.index<>NR_NO) or
  411. (ref.offset<-256) or
  412. (ref.offset>255) then
  413. op:=A_LDR
  414. else
  415. op:=A_LDUR;
  416. result:=spilling_create_op(op,ref,r);
  417. end;
  418. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  419. var
  420. op: tasmop;
  421. begin
  422. if (ref.index<>NR_NO) or
  423. (ref.offset<-256) or
  424. (ref.offset>255) then
  425. op:=A_STR
  426. else
  427. op:=A_STUR;
  428. result:=spilling_create_op(op,ref,r);
  429. end;
  430. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  431. begin
  432. case opcode of
  433. A_ADC,A_ADD,A_AND,A_BIC,
  434. A_EOR,A_CLZ,A_RBIT,
  435. A_LDR,
  436. A_MOV,A_MVN,A_MUL,
  437. A_ORR,A_SBC,A_SUB,
  438. A_UXT,A_SXT:
  439. if opnr=0 then
  440. result:=operand_write
  441. else
  442. result:=operand_read;
  443. A_B,A_BL,
  444. A_CMN,A_CMP,A_TST:
  445. result:=operand_read;
  446. A_STR:
  447. { important is what happens with the involved registers }
  448. if opnr=0 then
  449. result := operand_read
  450. else
  451. { check for pre/post indexed }
  452. result := operand_read;
  453. else
  454. internalerror(200403151);
  455. end;
  456. end;
  457. procedure BuildInsTabCache;
  458. var
  459. i : longint;
  460. begin
  461. (* new(instabcache);
  462. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  463. i:=0;
  464. while (i<InsTabEntries) do
  465. begin
  466. if InsTabCache^[InsTab[i].Opcode]=-1 then
  467. InsTabCache^[InsTab[i].Opcode]:=i;
  468. inc(i);
  469. end; *)
  470. end;
  471. procedure InitAsm;
  472. begin
  473. if not assigned(instabcache) then
  474. BuildInsTabCache;
  475. end;
  476. procedure DoneAsm;
  477. begin
  478. if assigned(instabcache) then
  479. begin
  480. dispose(instabcache);
  481. instabcache:=nil;
  482. end;
  483. end;
  484. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  485. begin
  486. i.oppostfix:=pf;
  487. result:=i;
  488. end;
  489. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  490. begin
  491. i.condition:=c;
  492. result:=i;
  493. end;
  494. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  495. Begin
  496. Current:=tai(Current.Next);
  497. While Assigned(Current) And (Current.typ In SkipInstr) Do
  498. Current:=tai(Current.Next);
  499. Next:=Current;
  500. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  501. Result:=True
  502. Else
  503. Begin
  504. Next:=Nil;
  505. Result:=False;
  506. End;
  507. End;
  508. (*
  509. function armconstequal(hp1,hp2: tai): boolean;
  510. begin
  511. result:=false;
  512. if hp1.typ<>hp2.typ then
  513. exit;
  514. case hp1.typ of
  515. tai_const:
  516. result:=
  517. (tai_const(hp2).sym=tai_const(hp).sym) and
  518. (tai_const(hp2).value=tai_const(hp).value) and
  519. (tai(hp2.previous).typ=ait_label);
  520. tai_const:
  521. result:=
  522. (tai_const(hp2).sym=tai_const(hp).sym) and
  523. (tai_const(hp2).value=tai_const(hp).value) and
  524. (tai(hp2.previous).typ=ait_label);
  525. end;
  526. end;
  527. *)
  528. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  529. var
  530. curinspos,
  531. penalty,
  532. lastinspos,
  533. { increased for every data element > 4 bytes inserted }
  534. currentsize,
  535. extradataoffset,
  536. limit: longint;
  537. curop : longint;
  538. curtai : tai;
  539. curdatatai,hp,hp2 : tai;
  540. curdata : TAsmList;
  541. l : tasmlabel;
  542. doinsert,
  543. removeref : boolean;
  544. begin
  545. (*
  546. curdata:=TAsmList.create;
  547. lastinspos:=-1;
  548. curinspos:=0;
  549. extradataoffset:=0;
  550. limit:=1016;
  551. curtai:=tai(list.first);
  552. doinsert:=false;
  553. while assigned(curtai) do
  554. begin
  555. { instruction? }
  556. case curtai.typ of
  557. ait_instruction:
  558. begin
  559. { walk through all operand of the instruction }
  560. for curop:=0 to taicpu(curtai).ops-1 do
  561. begin
  562. { reference? }
  563. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  564. begin
  565. { pc relative symbol? }
  566. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  567. if assigned(curdatatai) and
  568. { move only if we're at the first reference of a label }
  569. not(tai_label(curdatatai).moved) then
  570. begin
  571. tai_label(curdatatai).moved:=true;
  572. { check if symbol already used. }
  573. { if yes, reuse the symbol }
  574. hp:=tai(curdatatai.next);
  575. removeref:=false;
  576. if assigned(hp) then
  577. begin
  578. case hp.typ of
  579. ait_const:
  580. begin
  581. if (tai_const(hp).consttype=aitconst_64bit) then
  582. inc(extradataoffset);
  583. end;
  584. ait_comp_64bit,
  585. ait_real_64bit:
  586. begin
  587. inc(extradataoffset);
  588. end;
  589. ait_real_80bit:
  590. begin
  591. inc(extradataoffset,2);
  592. end;
  593. end;
  594. if (hp.typ=ait_const) then
  595. begin
  596. hp2:=tai(curdata.first);
  597. while assigned(hp2) do
  598. begin
  599. { if armconstequal(hp2,hp) then }
  600. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  601. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  602. then
  603. begin
  604. with taicpu(curtai).oper[curop]^.ref^ do
  605. begin
  606. symboldata:=hp2.previous;
  607. symbol:=tai_label(hp2.previous).labsym;
  608. end;
  609. removeref:=true;
  610. break;
  611. end;
  612. hp2:=tai(hp2.next);
  613. end;
  614. end;
  615. end;
  616. { move or remove symbol reference }
  617. repeat
  618. hp:=tai(curdatatai.next);
  619. listtoinsert.remove(curdatatai);
  620. if removeref then
  621. curdatatai.free
  622. else
  623. curdata.concat(curdatatai);
  624. curdatatai:=hp;
  625. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  626. if lastinspos=-1 then
  627. lastinspos:=curinspos;
  628. end;
  629. end;
  630. end;
  631. inc(curinspos);
  632. end;
  633. ait_align:
  634. begin
  635. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  636. requires also incrementing curinspos by 1 }
  637. inc(curinspos,(tai_align(curtai).aligntype div 4));
  638. end;
  639. ait_const:
  640. begin
  641. inc(curinspos);
  642. if (tai_const(curtai).consttype=aitconst_64bit) then
  643. inc(curinspos);
  644. end;
  645. ait_real_32bit:
  646. begin
  647. inc(curinspos);
  648. end;
  649. ait_comp_64bit,
  650. ait_real_64bit:
  651. begin
  652. inc(curinspos,2);
  653. end;
  654. ait_real_80bit:
  655. begin
  656. inc(curinspos,3);
  657. end;
  658. end;
  659. { special case for case jump tables }
  660. if SimpleGetNextInstruction(curtai,hp) and
  661. (tai(hp).typ=ait_instruction) and
  662. (taicpu(hp).opcode=A_LDR) and
  663. (taicpu(hp).oper[0]^.typ=top_reg) and
  664. (taicpu(hp).oper[0]^.reg=NR_PC) then
  665. begin
  666. penalty:=1;
  667. hp:=tai(hp.next);
  668. { skip register allocations and comments inserted by the optimizer }
  669. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc]) do
  670. hp:=tai(hp.next);
  671. while assigned(hp) and (hp.typ=ait_const) do
  672. begin
  673. inc(penalty);
  674. hp:=tai(hp.next);
  675. end;
  676. end
  677. else
  678. penalty:=0;
  679. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096 }
  680. if SimpleGetNextInstruction(curtai,hp) and
  681. (tai(hp).typ=ait_instruction) and
  682. ((taicpu(hp).opcode=A_FLDS) or
  683. (taicpu(hp).opcode=A_FLDD)) then
  684. limit:=254;
  685. { don't miss an insert }
  686. doinsert:=doinsert or
  687. (not(curdata.empty) and
  688. (curinspos-lastinspos+penalty+extradataoffset>limit));
  689. { split only at real instructions else the test below fails }
  690. if doinsert and (curtai.typ=ait_instruction) and
  691. (
  692. { don't split loads of pc to lr and the following move }
  693. not(
  694. (taicpu(curtai).opcode=A_MOV) and
  695. (taicpu(curtai).oper[0]^.typ=top_reg) and
  696. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  697. (taicpu(curtai).oper[1]^.typ=top_reg) and
  698. (taicpu(curtai).oper[1]^.reg=NR_PC)
  699. )
  700. ) then
  701. begin
  702. lastinspos:=-1;
  703. extradataoffset:=0;
  704. limit:=1016;
  705. doinsert:=false;
  706. hp:=tai(curtai.next);
  707. current_asmdata.getjumplabel(l);
  708. curdata.insert(taicpu.op_sym(A_B,l));
  709. curdata.concat(tai_label.create(l));
  710. list.insertlistafter(curtai,curdata);
  711. curtai:=hp;
  712. end
  713. else
  714. curtai:=tai(curtai.next);
  715. end;
  716. list.concatlist(curdata);
  717. curdata.free;
  718. *)
  719. end;
  720. procedure finalizearmcode(list, listtoinsert: TAsmList);
  721. begin
  722. insertpcrelativedata(list, listtoinsert);
  723. end;
  724. procedure InsertPData;
  725. var
  726. prolog: TAsmList;
  727. begin
  728. prolog:=TAsmList.create;
  729. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  730. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  731. prolog.concat(Tai_const.Create_32bit(0));
  732. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  733. { dummy function }
  734. prolog.concat(taicpu.op_reg(A_BR,NR_X29));
  735. current_asmdata.asmlists[al_start].insertList(prolog);
  736. prolog.Free;
  737. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  738. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  739. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  740. end;
  741. (*
  742. Floating point instruction format information, taken from the linux kernel
  743. ARM Floating Point Instruction Classes
  744. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  745. |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
  746. |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)
  747. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  748. |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
  749. |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
  750. |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
  751. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  752. CPDT data transfer instructions
  753. LDF, STF, LFM (copro 2), SFM (copro 2)
  754. CPDO dyadic arithmetic instructions
  755. ADF, MUF, SUF, RSF, DVF, RDF,
  756. POW, RPW, RMF, FML, FDV, FRD, POL
  757. CPDO monadic arithmetic instructions
  758. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  759. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  760. CPRT joint arithmetic/data transfer instructions
  761. FIX (arithmetic followed by load/store)
  762. FLT (load/store followed by arithmetic)
  763. CMF, CNF CMFE, CNFE (comparisons)
  764. WFS, RFS (write/read floating point status register)
  765. WFC, RFC (write/read floating point control register)
  766. cond condition codes
  767. P pre/post index bit: 0 = postindex, 1 = preindex
  768. U up/down bit: 0 = stack grows down, 1 = stack grows up
  769. W write back bit: 1 = update base register (Rn)
  770. L load/store bit: 0 = store, 1 = load
  771. Rn base register
  772. Rd destination/source register
  773. Fd floating point destination register
  774. Fn floating point source register
  775. Fm floating point source register or floating point constant
  776. uv transfer length (TABLE 1)
  777. wx register count (TABLE 2)
  778. abcd arithmetic opcode (TABLES 3 & 4)
  779. ef destination size (rounding precision) (TABLE 5)
  780. gh rounding mode (TABLE 6)
  781. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  782. i constant bit: 1 = constant (TABLE 6)
  783. */
  784. /*
  785. TABLE 1
  786. +-------------------------+---+---+---------+---------+
  787. | Precision | u | v | FPSR.EP | length |
  788. +-------------------------+---+---+---------+---------+
  789. | Single | 0 | 0 | x | 1 words |
  790. | Double | 1 | 1 | x | 2 words |
  791. | Extended | 1 | 1 | x | 3 words |
  792. | Packed decimal | 1 | 1 | 0 | 3 words |
  793. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  794. +-------------------------+---+---+---------+---------+
  795. Note: x = don't care
  796. */
  797. /*
  798. TABLE 2
  799. +---+---+---------------------------------+
  800. | w | x | Number of registers to transfer |
  801. +---+---+---------------------------------+
  802. | 0 | 1 | 1 |
  803. | 1 | 0 | 2 |
  804. | 1 | 1 | 3 |
  805. | 0 | 0 | 4 |
  806. +---+---+---------------------------------+
  807. */
  808. /*
  809. TABLE 3: Dyadic Floating Point Opcodes
  810. +---+---+---+---+----------+-----------------------+-----------------------+
  811. | a | b | c | d | Mnemonic | Description | Operation |
  812. +---+---+---+---+----------+-----------------------+-----------------------+
  813. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  814. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  815. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  816. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  817. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  818. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  819. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  820. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  821. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  822. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  823. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  824. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  825. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  826. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  827. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  828. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  829. +---+---+---+---+----------+-----------------------+-----------------------+
  830. Note: POW, RPW, POL are deprecated, and are available for backwards
  831. compatibility only.
  832. */
  833. /*
  834. TABLE 4: Monadic Floating Point Opcodes
  835. +---+---+---+---+----------+-----------------------+-----------------------+
  836. | a | b | c | d | Mnemonic | Description | Operation |
  837. +---+---+---+---+----------+-----------------------+-----------------------+
  838. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  839. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  840. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  841. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  842. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  843. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  844. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  845. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  846. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  847. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  848. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  849. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  850. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  851. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  852. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  853. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  854. +---+---+---+---+----------+-----------------------+-----------------------+
  855. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  856. available for backwards compatibility only.
  857. */
  858. /*
  859. TABLE 5
  860. +-------------------------+---+---+
  861. | Rounding Precision | e | f |
  862. +-------------------------+---+---+
  863. | IEEE Single precision | 0 | 0 |
  864. | IEEE Double precision | 0 | 1 |
  865. | IEEE Extended precision | 1 | 0 |
  866. | undefined (trap) | 1 | 1 |
  867. +-------------------------+---+---+
  868. */
  869. /*
  870. TABLE 5
  871. +---------------------------------+---+---+
  872. | Rounding Mode | g | h |
  873. +---------------------------------+---+---+
  874. | Round to nearest (default) | 0 | 0 |
  875. | Round toward plus infinity | 0 | 1 |
  876. | Round toward negative infinity | 1 | 0 |
  877. | Round toward zero | 1 | 1 |
  878. +---------------------------------+---+---+
  879. *)
  880. function taicpu.GetString:string;
  881. var
  882. i : longint;
  883. s : string;
  884. addsize : boolean;
  885. begin
  886. s:='['+gas_op2str[opcode];
  887. for i:=0 to ops-1 do
  888. begin
  889. with oper[i]^ do
  890. begin
  891. if i=0 then
  892. s:=s+' '
  893. else
  894. s:=s+',';
  895. { type }
  896. addsize:=false;
  897. if (ot and OT_VREG)=OT_VREG then
  898. s:=s+'vreg'
  899. else
  900. if (ot and OT_FPUREG)=OT_FPUREG then
  901. s:=s+'fpureg'
  902. else
  903. if (ot and OT_REGISTER)=OT_REGISTER then
  904. begin
  905. s:=s+'reg';
  906. addsize:=true;
  907. end
  908. else
  909. if (ot and OT_REGLIST)=OT_REGLIST then
  910. begin
  911. s:=s+'reglist';
  912. addsize:=false;
  913. end
  914. else
  915. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  916. begin
  917. s:=s+'imm';
  918. addsize:=true;
  919. end
  920. else
  921. if (ot and OT_MEMORY)=OT_MEMORY then
  922. begin
  923. s:=s+'mem';
  924. addsize:=true;
  925. if (ot and OT_AM2)<>0 then
  926. s:=s+' am2 ';
  927. end
  928. else
  929. s:=s+'???';
  930. { size }
  931. if addsize then
  932. begin
  933. if (ot and OT_BITS8)<>0 then
  934. s:=s+'8'
  935. else
  936. if (ot and OT_BITS16)<>0 then
  937. s:=s+'24'
  938. else
  939. if (ot and OT_BITS32)<>0 then
  940. s:=s+'32'
  941. else
  942. if (ot and OT_BITSSHIFTER)<>0 then
  943. s:=s+'shifter'
  944. else
  945. s:=s+'??';
  946. { signed }
  947. if (ot and OT_SIGNED)<>0 then
  948. s:=s+'s';
  949. end;
  950. end;
  951. end;
  952. GetString:=s+']';
  953. end;
  954. procedure taicpu.ResetPass1;
  955. begin
  956. { we need to reset everything here, because the choosen insentry
  957. can be invalid for a new situation where the previously optimized
  958. insentry is not correct }
  959. InsEntry:=nil;
  960. InsSize:=0;
  961. LastInsOffset:=-1;
  962. end;
  963. procedure taicpu.ResetPass2;
  964. begin
  965. { we are here in a second pass, check if the instruction can be optimized }
  966. if assigned(InsEntry) and
  967. ((InsEntry^.flags and IF_PASS2)<>0) then
  968. begin
  969. InsEntry:=nil;
  970. InsSize:=0;
  971. end;
  972. LastInsOffset:=-1;
  973. end;
  974. function taicpu.CheckIfValid:boolean;
  975. begin
  976. Result:=False; { unimplemented }
  977. end;
  978. function taicpu.Pass1(objdata:TObjData):longint;
  979. begin
  980. Pass1:=0;
  981. LastInsOffset:=-1;
  982. end;
  983. procedure taicpu.Pass2(objdata:TObjData);
  984. begin
  985. { error in pass1 ? }
  986. if insentry=nil then
  987. exit;
  988. current_filepos:=fileinfo;
  989. { Generate the instruction }
  990. GenCode(objdata);
  991. end;
  992. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  993. begin
  994. end;
  995. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  996. begin
  997. end;
  998. procedure taicpu.ppubuildderefimploper(var o:toper);
  999. begin
  1000. end;
  1001. procedure taicpu.ppuderefoper(var o:toper);
  1002. begin
  1003. end;
  1004. function taicpu.InsEnd:longint;
  1005. begin
  1006. Result:=0; { unimplemented }
  1007. end;
  1008. procedure taicpu.create_ot(objdata:TObjData);
  1009. begin
  1010. end;
  1011. function taicpu.Matches(p:PInsEntry):longint;
  1012. begin
  1013. result:=0; { unimplemented }
  1014. end;
  1015. function taicpu.calcsize(p:PInsEntry):shortint;
  1016. begin
  1017. result:=4;
  1018. end;
  1019. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1020. begin
  1021. Result:=False; { unimplemented }
  1022. end;
  1023. procedure taicpu.Swapoperands;
  1024. begin
  1025. end;
  1026. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1027. begin
  1028. result:=false;
  1029. end;
  1030. procedure taicpu.gencode(objdata:TObjData);
  1031. var
  1032. bytes : dword;
  1033. i_field : byte;
  1034. procedure setshifterop(op : byte);
  1035. begin
  1036. case oper[op]^.typ of
  1037. top_const:
  1038. begin
  1039. i_field:=1;
  1040. bytes:=bytes or dword(oper[op]^.val and $fff);
  1041. end;
  1042. top_reg:
  1043. begin
  1044. i_field:=0;
  1045. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1046. { does a real shifter op follow? }
  1047. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1048. begin
  1049. end;
  1050. end;
  1051. else
  1052. internalerror(2005091103);
  1053. end;
  1054. end;
  1055. begin
  1056. bytes:=$0;
  1057. { evaluate and set condition code }
  1058. { condition code allowed? }
  1059. { setup rest of the instruction }
  1060. case insentry^.code[0] of
  1061. #$08:
  1062. begin
  1063. { set instruction code }
  1064. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1065. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1066. { set destination }
  1067. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1068. { create shifter op }
  1069. setshifterop(1);
  1070. { set i field }
  1071. bytes:=bytes or (i_field shl 25);
  1072. { set s if necessary }
  1073. if oppostfix=PF_S then
  1074. bytes:=bytes or (1 shl 20);
  1075. end;
  1076. #$ff:
  1077. internalerror(2005091101);
  1078. else
  1079. internalerror(2005091102);
  1080. end;
  1081. { we're finished, write code }
  1082. objdata.writebytes(bytes,sizeof(bytes));
  1083. end;
  1084. {$ifdef dummy}
  1085. (*
  1086. static void gencode (long segment, long offset, int bits,
  1087. insn *ins, char *codes, long insn_end)
  1088. {
  1089. int has_S_code; /* S - setflag */
  1090. int has_B_code; /* B - setflag */
  1091. int has_T_code; /* T - setflag */
  1092. int has_W_code; /* ! => W flag */
  1093. int has_F_code; /* ^ => S flag */
  1094. int keep;
  1095. unsigned char c;
  1096. unsigned char bytes[4];
  1097. long data, size;
  1098. static int cc_code[] = /* bit pattern of cc */
  1099. { /* order as enum in */
  1100. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1101. 0x0A, 0x0C, 0x08, 0x0D,
  1102. 0x09, 0x0B, 0x04, 0x01,
  1103. 0x05, 0x07, 0x06,
  1104. };
  1105. #ifdef DEBUG
  1106. static char *CC[] =
  1107. { /* condition code names */
  1108. "AL", "CC", "CS", "EQ",
  1109. "GE", "GT", "HI", "LE",
  1110. "LS", "LT", "MI", "NE",
  1111. "PL", "VC", "VS", "",
  1112. "S"
  1113. };
  1114. has_S_code = (ins->condition & C_SSETFLAG);
  1115. has_B_code = (ins->condition & C_BSETFLAG);
  1116. has_T_code = (ins->condition & C_TSETFLAG);
  1117. has_W_code = (ins->condition & C_EXSETFLAG);
  1118. has_F_code = (ins->condition & C_FSETFLAG);
  1119. ins->condition = (ins->condition & 0x0F);
  1120. if (rt_debug)
  1121. {
  1122. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1123. CC[ins->condition & 0x0F]);
  1124. if (has_S_code)
  1125. printf ("S");
  1126. if (has_B_code)
  1127. printf ("B");
  1128. if (has_T_code)
  1129. printf ("T");
  1130. if (has_W_code)
  1131. printf ("!");
  1132. if (has_F_code)
  1133. printf ("^");
  1134. printf ("\n");
  1135. c = *codes;
  1136. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1137. bytes[0] = 0xB;
  1138. bytes[1] = 0xE;
  1139. bytes[2] = 0xE;
  1140. bytes[3] = 0xF;
  1141. }
  1142. // First condition code in upper nibble
  1143. if (ins->condition < C_NONE)
  1144. {
  1145. c = cc_code[ins->condition] << 4;
  1146. }
  1147. else
  1148. {
  1149. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1150. }
  1151. switch (keep = *codes)
  1152. {
  1153. case 1:
  1154. // B, BL
  1155. ++codes;
  1156. c |= *codes++;
  1157. bytes[0] = c;
  1158. if (ins->oprs[0].segment != segment)
  1159. {
  1160. // fais une relocation
  1161. c = 1;
  1162. data = 0; // Let the linker locate ??
  1163. }
  1164. else
  1165. {
  1166. c = 0;
  1167. data = ins->oprs[0].offset - (offset + 8);
  1168. if (data % 4)
  1169. {
  1170. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1171. }
  1172. }
  1173. if (data >= 0x1000)
  1174. {
  1175. errfunc (ERR_NONFATAL, "too long offset");
  1176. }
  1177. data = data >> 2;
  1178. bytes[1] = (data >> 16) & 0xFF;
  1179. bytes[2] = (data >> 8) & 0xFF;
  1180. bytes[3] = (data ) & 0xFF;
  1181. if (c == 1)
  1182. {
  1183. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1184. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1185. }
  1186. else
  1187. {
  1188. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1189. }
  1190. return;
  1191. case 2:
  1192. // SWI
  1193. ++codes;
  1194. c |= *codes++;
  1195. bytes[0] = c;
  1196. data = ins->oprs[0].offset;
  1197. bytes[1] = (data >> 16) & 0xFF;
  1198. bytes[2] = (data >> 8) & 0xFF;
  1199. bytes[3] = (data) & 0xFF;
  1200. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1201. return;
  1202. case 3:
  1203. // BX
  1204. ++codes;
  1205. c |= *codes++;
  1206. bytes[0] = c;
  1207. bytes[1] = *codes++;
  1208. bytes[2] = *codes++;
  1209. bytes[3] = *codes++;
  1210. c = regval (&ins->oprs[0],1);
  1211. if (c == 15) // PC
  1212. {
  1213. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1214. }
  1215. else if (c > 15)
  1216. {
  1217. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1218. }
  1219. bytes[3] |= (c & 0x0F);
  1220. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1221. return;
  1222. case 4: // AND Rd,Rn,Rm
  1223. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1224. case 6: // AND Rd,Rn,Rm,<shift>imm
  1225. case 7: // AND Rd,Rn,<shift>imm
  1226. ++codes;
  1227. #ifdef DEBUG
  1228. if (rt_debug)
  1229. {
  1230. printf (" decode - '0x%02X'\n", keep);
  1231. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1232. }
  1233. #endif
  1234. bytes[0] = c | *codes;
  1235. ++codes;
  1236. bytes[1] = *codes;
  1237. if (has_S_code)
  1238. bytes[1] |= 0x10;
  1239. c = regval (&ins->oprs[1],1);
  1240. // Rn in low nibble
  1241. bytes[1] |= c;
  1242. // Rd in high nibble
  1243. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1244. if (keep != 7)
  1245. {
  1246. // Rm in low nibble
  1247. bytes[3] = regval (&ins->oprs[2],1);
  1248. }
  1249. // Shifts if any
  1250. if (keep == 5 || keep == 6)
  1251. {
  1252. // Shift in bytes 2 and 3
  1253. if (keep == 5)
  1254. {
  1255. // Rs
  1256. c = regval (&ins->oprs[3],1);
  1257. bytes[2] |= c;
  1258. c = 0x10; // Set bit 4 in byte[3]
  1259. }
  1260. if (keep == 6)
  1261. {
  1262. c = (ins->oprs[3].offset) & 0x1F;
  1263. // #imm
  1264. bytes[2] |= c >> 1;
  1265. if (c & 0x01)
  1266. {
  1267. bytes[3] |= 0x80;
  1268. }
  1269. c = 0; // Clr bit 4 in byte[3]
  1270. }
  1271. // <shift>
  1272. c |= shiftval (&ins->oprs[3]) << 5;
  1273. bytes[3] |= c;
  1274. }
  1275. // reg,reg,imm
  1276. if (keep == 7)
  1277. {
  1278. int shimm;
  1279. shimm = imm_shift (ins->oprs[2].offset);
  1280. if (shimm == -1)
  1281. {
  1282. errfunc (ERR_NONFATAL, "cannot create that constant");
  1283. }
  1284. bytes[3] = shimm & 0xFF;
  1285. bytes[2] |= (shimm & 0xF00) >> 8;
  1286. }
  1287. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1288. return;
  1289. case 8: // MOV Rd,Rm
  1290. case 9: // MOV Rd,Rm,<shift>Rs
  1291. case 0xA: // MOV Rd,Rm,<shift>imm
  1292. case 0xB: // MOV Rd,<shift>imm
  1293. ++codes;
  1294. #ifdef DEBUG
  1295. if (rt_debug)
  1296. {
  1297. printf (" decode - '0x%02X'\n", keep);
  1298. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1299. }
  1300. #endif
  1301. bytes[0] = c | *codes;
  1302. ++codes;
  1303. bytes[1] = *codes;
  1304. if (has_S_code)
  1305. bytes[1] |= 0x10;
  1306. // Rd in high nibble
  1307. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1308. if (keep != 0x0B)
  1309. {
  1310. // Rm in low nibble
  1311. bytes[3] = regval (&ins->oprs[1],1);
  1312. }
  1313. // Shifts if any
  1314. if (keep == 0x09 || keep == 0x0A)
  1315. {
  1316. // Shift in bytes 2 and 3
  1317. if (keep == 0x09)
  1318. {
  1319. // Rs
  1320. c = regval (&ins->oprs[2],1);
  1321. bytes[2] |= c;
  1322. c = 0x10; // Set bit 4 in byte[3]
  1323. }
  1324. if (keep == 0x0A)
  1325. {
  1326. c = (ins->oprs[2].offset) & 0x1F;
  1327. // #imm
  1328. bytes[2] |= c >> 1;
  1329. if (c & 0x01)
  1330. {
  1331. bytes[3] |= 0x80;
  1332. }
  1333. c = 0; // Clr bit 4 in byte[3]
  1334. }
  1335. // <shift>
  1336. c |= shiftval (&ins->oprs[2]) << 5;
  1337. bytes[3] |= c;
  1338. }
  1339. // reg,imm
  1340. if (keep == 0x0B)
  1341. {
  1342. int shimm;
  1343. shimm = imm_shift (ins->oprs[1].offset);
  1344. if (shimm == -1)
  1345. {
  1346. errfunc (ERR_NONFATAL, "cannot create that constant");
  1347. }
  1348. bytes[3] = shimm & 0xFF;
  1349. bytes[2] |= (shimm & 0xF00) >> 8;
  1350. }
  1351. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1352. return;
  1353. case 0xC: // CMP Rn,Rm
  1354. case 0xD: // CMP Rn,Rm,<shift>Rs
  1355. case 0xE: // CMP Rn,Rm,<shift>imm
  1356. case 0xF: // CMP Rn,<shift>imm
  1357. ++codes;
  1358. bytes[0] = c | *codes++;
  1359. bytes[1] = *codes;
  1360. // Implicit S code
  1361. bytes[1] |= 0x10;
  1362. c = regval (&ins->oprs[0],1);
  1363. // Rn in low nibble
  1364. bytes[1] |= c;
  1365. // No destination
  1366. bytes[2] = 0;
  1367. if (keep != 0x0B)
  1368. {
  1369. // Rm in low nibble
  1370. bytes[3] = regval (&ins->oprs[1],1);
  1371. }
  1372. // Shifts if any
  1373. if (keep == 0x0D || keep == 0x0E)
  1374. {
  1375. // Shift in bytes 2 and 3
  1376. if (keep == 0x0D)
  1377. {
  1378. // Rs
  1379. c = regval (&ins->oprs[2],1);
  1380. bytes[2] |= c;
  1381. c = 0x10; // Set bit 4 in byte[3]
  1382. }
  1383. if (keep == 0x0E)
  1384. {
  1385. c = (ins->oprs[2].offset) & 0x1F;
  1386. // #imm
  1387. bytes[2] |= c >> 1;
  1388. if (c & 0x01)
  1389. {
  1390. bytes[3] |= 0x80;
  1391. }
  1392. c = 0; // Clr bit 4 in byte[3]
  1393. }
  1394. // <shift>
  1395. c |= shiftval (&ins->oprs[2]) << 5;
  1396. bytes[3] |= c;
  1397. }
  1398. // reg,imm
  1399. if (keep == 0x0F)
  1400. {
  1401. int shimm;
  1402. shimm = imm_shift (ins->oprs[1].offset);
  1403. if (shimm == -1)
  1404. {
  1405. errfunc (ERR_NONFATAL, "cannot create that constant");
  1406. }
  1407. bytes[3] = shimm & 0xFF;
  1408. bytes[2] |= (shimm & 0xF00) >> 8;
  1409. }
  1410. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1411. return;
  1412. case 0x10: // MRS Rd,<psr>
  1413. ++codes;
  1414. bytes[0] = c | *codes++;
  1415. bytes[1] = *codes++;
  1416. // Rd
  1417. c = regval (&ins->oprs[0],1);
  1418. bytes[2] = c << 4;
  1419. bytes[3] = 0;
  1420. c = ins->oprs[1].basereg;
  1421. if (c == R_CPSR || c == R_SPSR)
  1422. {
  1423. if (c == R_SPSR)
  1424. {
  1425. bytes[1] |= 0x40;
  1426. }
  1427. }
  1428. else
  1429. {
  1430. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1431. }
  1432. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1433. return;
  1434. case 0x11: // MSR <psr>,Rm
  1435. case 0x12: // MSR <psrf>,Rm
  1436. case 0x13: // MSR <psrf>,#expression
  1437. ++codes;
  1438. bytes[0] = c | *codes++;
  1439. bytes[1] = *codes++;
  1440. bytes[2] = *codes;
  1441. if (keep == 0x11 || keep == 0x12)
  1442. {
  1443. // Rm
  1444. c = regval (&ins->oprs[1],1);
  1445. bytes[3] = c;
  1446. }
  1447. else
  1448. {
  1449. int shimm;
  1450. shimm = imm_shift (ins->oprs[1].offset);
  1451. if (shimm == -1)
  1452. {
  1453. errfunc (ERR_NONFATAL, "cannot create that constant");
  1454. }
  1455. bytes[3] = shimm & 0xFF;
  1456. bytes[2] |= (shimm & 0xF00) >> 8;
  1457. }
  1458. c = ins->oprs[0].basereg;
  1459. if ( keep == 0x11)
  1460. {
  1461. if ( c == R_CPSR || c == R_SPSR)
  1462. {
  1463. if ( c== R_SPSR)
  1464. {
  1465. bytes[1] |= 0x40;
  1466. }
  1467. }
  1468. else
  1469. {
  1470. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1471. }
  1472. }
  1473. else
  1474. {
  1475. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1476. {
  1477. if ( c== R_SPSR_FLG)
  1478. {
  1479. bytes[1] |= 0x40;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1485. }
  1486. }
  1487. break;
  1488. case 0x14: // MUL Rd,Rm,Rs
  1489. case 0x15: // MULA Rd,Rm,Rs,Rn
  1490. ++codes;
  1491. bytes[0] = c | *codes++;
  1492. bytes[1] = *codes++;
  1493. bytes[3] = *codes;
  1494. // Rd
  1495. bytes[1] |= regval (&ins->oprs[0],1);
  1496. if (has_S_code)
  1497. bytes[1] |= 0x10;
  1498. // Rm
  1499. bytes[3] |= regval (&ins->oprs[1],1);
  1500. // Rs
  1501. bytes[2] = regval (&ins->oprs[2],1);
  1502. if (keep == 0x15)
  1503. {
  1504. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1505. }
  1506. break;
  1507. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1508. ++codes;
  1509. bytes[0] = c | *codes++;
  1510. bytes[1] = *codes++;
  1511. bytes[3] = *codes;
  1512. // RdHi
  1513. bytes[1] |= regval (&ins->oprs[1],1);
  1514. if (has_S_code)
  1515. bytes[1] |= 0x10;
  1516. // RdLo
  1517. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1518. // Rm
  1519. bytes[3] |= regval (&ins->oprs[2],1);
  1520. // Rs
  1521. bytes[2] |= regval (&ins->oprs[3],1);
  1522. break;
  1523. case 0x17: // LDR Rd, expression
  1524. ++codes;
  1525. bytes[0] = c | *codes++;
  1526. bytes[1] = *codes++;
  1527. // Rd
  1528. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1529. if (has_B_code)
  1530. bytes[1] |= 0x40;
  1531. if (has_T_code)
  1532. {
  1533. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1534. }
  1535. if (has_W_code)
  1536. {
  1537. errfunc (ERR_NONFATAL, "'!' not allowed");
  1538. }
  1539. // Rn - implicit R15
  1540. bytes[1] |= 0xF;
  1541. if (ins->oprs[1].segment != segment)
  1542. {
  1543. errfunc (ERR_NONFATAL, "label not in same segment");
  1544. }
  1545. data = ins->oprs[1].offset - (offset + 8);
  1546. if (data < 0)
  1547. {
  1548. data = -data;
  1549. }
  1550. else
  1551. {
  1552. bytes[1] |= 0x80;
  1553. }
  1554. if (data >= 0x1000)
  1555. {
  1556. errfunc (ERR_NONFATAL, "too long offset");
  1557. }
  1558. bytes[2] |= ((data & 0xF00) >> 8);
  1559. bytes[3] = data & 0xFF;
  1560. break;
  1561. case 0x18: // LDR Rd, [Rn]
  1562. ++codes;
  1563. bytes[0] = c | *codes++;
  1564. bytes[1] = *codes++;
  1565. // Rd
  1566. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1567. if (has_B_code)
  1568. bytes[1] |= 0x40;
  1569. if (has_T_code)
  1570. {
  1571. bytes[1] |= 0x20; // write-back
  1572. }
  1573. else
  1574. {
  1575. bytes[0] |= 0x01; // implicit pre-index mode
  1576. }
  1577. if (has_W_code)
  1578. {
  1579. bytes[1] |= 0x20; // write-back
  1580. }
  1581. // Rn
  1582. c = regval (&ins->oprs[1],1);
  1583. bytes[1] |= c;
  1584. if (c == 0x15) // R15
  1585. data = -8;
  1586. else
  1587. data = 0;
  1588. if (data < 0)
  1589. {
  1590. data = -data;
  1591. }
  1592. else
  1593. {
  1594. bytes[1] |= 0x80;
  1595. }
  1596. bytes[2] |= ((data & 0xF00) >> 8);
  1597. bytes[3] = data & 0xFF;
  1598. break;
  1599. case 0x19: // LDR Rd, [Rn,#expression]
  1600. case 0x20: // LDR Rd, [Rn,Rm]
  1601. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1602. ++codes;
  1603. bytes[0] = c | *codes++;
  1604. bytes[1] = *codes++;
  1605. // Rd
  1606. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1607. if (has_B_code)
  1608. bytes[1] |= 0x40;
  1609. // Rn
  1610. c = regval (&ins->oprs[1],1);
  1611. bytes[1] |= c;
  1612. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1613. {
  1614. bytes[0] |= 0x01; // pre-index mode
  1615. if (has_W_code)
  1616. {
  1617. bytes[1] |= 0x20;
  1618. }
  1619. if (has_T_code)
  1620. {
  1621. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1622. }
  1623. }
  1624. else
  1625. {
  1626. if (has_T_code) // Forced write-back in post-index mode
  1627. {
  1628. bytes[1] |= 0x20;
  1629. }
  1630. if (has_W_code)
  1631. {
  1632. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1633. }
  1634. }
  1635. if (keep == 0x19)
  1636. {
  1637. data = ins->oprs[2].offset;
  1638. if (data < 0)
  1639. {
  1640. data = -data;
  1641. }
  1642. else
  1643. {
  1644. bytes[1] |= 0x80;
  1645. }
  1646. if (data >= 0x1000)
  1647. {
  1648. errfunc (ERR_NONFATAL, "too long offset");
  1649. }
  1650. bytes[2] |= ((data & 0xF00) >> 8);
  1651. bytes[3] = data & 0xFF;
  1652. }
  1653. else
  1654. {
  1655. if (ins->oprs[2].minus == 0)
  1656. {
  1657. bytes[1] |= 0x80;
  1658. }
  1659. c = regval (&ins->oprs[2],1);
  1660. bytes[3] = c;
  1661. if (keep == 0x21)
  1662. {
  1663. c = ins->oprs[3].offset;
  1664. if (c > 0x1F)
  1665. {
  1666. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1667. c = c & 0x1F;
  1668. }
  1669. bytes[2] |= c >> 1;
  1670. if (c & 0x01)
  1671. {
  1672. bytes[3] |= 0x80;
  1673. }
  1674. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1675. }
  1676. }
  1677. break;
  1678. case 0x22: // LDRH Rd, expression
  1679. ++codes;
  1680. bytes[0] = c | 0x01; // Implicit pre-index
  1681. bytes[1] = *codes++;
  1682. // Rd
  1683. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1684. // Rn - implicit R15
  1685. bytes[1] |= 0xF;
  1686. if (ins->oprs[1].segment != segment)
  1687. {
  1688. errfunc (ERR_NONFATAL, "label not in same segment");
  1689. }
  1690. data = ins->oprs[1].offset - (offset + 8);
  1691. if (data < 0)
  1692. {
  1693. data = -data;
  1694. }
  1695. else
  1696. {
  1697. bytes[1] |= 0x80;
  1698. }
  1699. if (data >= 0x100)
  1700. {
  1701. errfunc (ERR_NONFATAL, "too long offset");
  1702. }
  1703. bytes[3] = *codes++;
  1704. bytes[2] |= ((data & 0xF0) >> 4);
  1705. bytes[3] |= data & 0xF;
  1706. break;
  1707. case 0x23: // LDRH Rd, Rn
  1708. ++codes;
  1709. bytes[0] = c | 0x01; // Implicit pre-index
  1710. bytes[1] = *codes++;
  1711. // Rd
  1712. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1713. // Rn
  1714. c = regval (&ins->oprs[1],1);
  1715. bytes[1] |= c;
  1716. if (c == 0x15) // R15
  1717. data = -8;
  1718. else
  1719. data = 0;
  1720. if (data < 0)
  1721. {
  1722. data = -data;
  1723. }
  1724. else
  1725. {
  1726. bytes[1] |= 0x80;
  1727. }
  1728. if (data >= 0x100)
  1729. {
  1730. errfunc (ERR_NONFATAL, "too long offset");
  1731. }
  1732. bytes[3] = *codes++;
  1733. bytes[2] |= ((data & 0xF0) >> 4);
  1734. bytes[3] |= data & 0xF;
  1735. break;
  1736. case 0x24: // LDRH Rd, Rn, expression
  1737. case 0x25: // LDRH Rd, Rn, Rm
  1738. ++codes;
  1739. bytes[0] = c;
  1740. bytes[1] = *codes++;
  1741. // Rd
  1742. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1743. // Rn
  1744. c = regval (&ins->oprs[1],1);
  1745. bytes[1] |= c;
  1746. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1747. {
  1748. bytes[0] |= 0x01; // pre-index mode
  1749. if (has_W_code)
  1750. {
  1751. bytes[1] |= 0x20;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. if (has_W_code)
  1757. {
  1758. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1759. }
  1760. }
  1761. bytes[3] = *codes++;
  1762. if (keep == 0x24)
  1763. {
  1764. data = ins->oprs[2].offset;
  1765. if (data < 0)
  1766. {
  1767. data = -data;
  1768. }
  1769. else
  1770. {
  1771. bytes[1] |= 0x80;
  1772. }
  1773. if (data >= 0x100)
  1774. {
  1775. errfunc (ERR_NONFATAL, "too long offset");
  1776. }
  1777. bytes[2] |= ((data & 0xF0) >> 4);
  1778. bytes[3] |= data & 0xF;
  1779. }
  1780. else
  1781. {
  1782. if (ins->oprs[2].minus == 0)
  1783. {
  1784. bytes[1] |= 0x80;
  1785. }
  1786. c = regval (&ins->oprs[2],1);
  1787. bytes[3] |= c;
  1788. }
  1789. break;
  1790. case 0x26: // LDM/STM Rn, {reg-list}
  1791. ++codes;
  1792. bytes[0] = c;
  1793. bytes[0] |= ( *codes >> 4) & 0xF;
  1794. bytes[1] = ( *codes << 4) & 0xF0;
  1795. ++codes;
  1796. if (has_W_code)
  1797. {
  1798. bytes[1] |= 0x20;
  1799. }
  1800. if (has_F_code)
  1801. {
  1802. bytes[1] |= 0x40;
  1803. }
  1804. // Rn
  1805. bytes[1] |= regval (&ins->oprs[0],1);
  1806. data = ins->oprs[1].basereg;
  1807. bytes[2] = ((data >> 8) & 0xFF);
  1808. bytes[3] = (data & 0xFF);
  1809. break;
  1810. case 0x27: // SWP Rd, Rm, [Rn]
  1811. ++codes;
  1812. bytes[0] = c;
  1813. bytes[0] |= *codes++;
  1814. bytes[1] = regval (&ins->oprs[2],1);
  1815. if (has_B_code)
  1816. {
  1817. bytes[1] |= 0x40;
  1818. }
  1819. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1820. bytes[3] = *codes++;
  1821. bytes[3] |= regval (&ins->oprs[1],1);
  1822. break;
  1823. default:
  1824. errfunc (ERR_FATAL, "unknown decoding of instruction");
  1825. bytes[0] = c;
  1826. // And a fix nibble
  1827. ++codes;
  1828. bytes[0] |= *codes++;
  1829. if ( *codes == 0x01) // An I bit
  1830. {
  1831. }
  1832. if ( *codes == 0x02) // An I bit
  1833. {
  1834. }
  1835. ++codes;
  1836. }
  1837. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1838. }
  1839. *)
  1840. {$endif dummy}
  1841. begin
  1842. cai_align:=tai_align;
  1843. end.