aasmcpu.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  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,
  23. symtype,
  24. cpubase,cpuinfo,cgbase,cgutils;
  25. const
  26. { "mov reg,reg" source operand number }
  27. O_MOV_SOURCE = 1;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 0;
  30. { Operand types }
  31. OT_NONE = $00000000;
  32. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  33. OT_BITS16 = $00000002;
  34. OT_BITS32 = $00000004;
  35. OT_BITS64 = $00000008; { FPU only }
  36. OT_BITS80 = $00000010;
  37. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  38. OT_NEAR = $00000040;
  39. OT_SHORT = $00000080;
  40. OT_BITSTINY = $00000100; { fpu constant }
  41. OT_BITSSHIFTER =
  42. $00000200;
  43. OT_SIZE_MASK = $000003FF; { all the size attributes }
  44. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  45. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  46. OT_TO = $00000200; { operand is followed by a colon }
  47. { reverse effect in FADD, FSUB &c }
  48. OT_COLON = $00000400;
  49. OT_SHIFTEROP = $00000800;
  50. OT_REGISTER = $00001000;
  51. OT_IMMEDIATE = $00002000;
  52. OT_REGLIST = $00008000;
  53. OT_IMM8 = $00002001;
  54. OT_IMM24 = $00002002;
  55. OT_IMM32 = $00002004;
  56. OT_IMM64 = $00002008;
  57. OT_IMM80 = $00002010;
  58. OT_IMMTINY = $00002100;
  59. OT_IMMSHIFTER= $00002200;
  60. OT_IMMEDIATE24 = OT_IMM24;
  61. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  62. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  63. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  64. OT_IMMEDIATEFPU = OT_IMMTINY;
  65. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  66. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  67. OT_REG8 = $00201001;
  68. OT_REG16 = $00201002;
  69. OT_REG32 = $00201004;
  70. OT_REG64 = $00201008;
  71. OT_VREG = $00201010; { vector register }
  72. OT_MEMORY = $00204000; { register number in 'basereg' }
  73. OT_MEM8 = $00204001;
  74. OT_MEM16 = $00204002;
  75. OT_MEM32 = $00204004;
  76. OT_MEM64 = $00204008;
  77. OT_MEM80 = $00204010;
  78. { word/byte load/store }
  79. OT_AM2 = $00010000;
  80. { misc ld/st operations }
  81. OT_AM3 = $00020000;
  82. { multiple ld/st operations }
  83. OT_AM4 = $00040000;
  84. { co proc. ld/st operations }
  85. OT_AM5 = $00080000;
  86. OT_AMMASK = $000f0000;
  87. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  88. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  89. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  90. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  91. OT_FPUREG = $01000000; { floating point stack registers }
  92. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  93. { a mask for the following }
  94. OT_MEM_OFFS = $00604000; { special type of EA }
  95. { simple [address] offset }
  96. OT_ONENESS = $00800000; { special type of immediate operand }
  97. { so UNITY == IMMEDIATE | ONENESS }
  98. OT_UNITY = $00802000; { for shift/rotate instructions }
  99. instabentries = {$i armnop.inc}
  100. maxinfolen = 5;
  101. IF_NONE = $00000000;
  102. IF_ARMMASK = $000F0000;
  103. IF_ARM7 = $00070000;
  104. IF_FPMASK = $00F00000;
  105. IF_FPA = $00100000;
  106. { if the instruction can change in a second pass }
  107. IF_PASS2 = longint($80000000);
  108. type
  109. TInsTabCache=array[TasmOp] of longint;
  110. PInsTabCache=^TInsTabCache;
  111. tinsentry = record
  112. opcode : tasmop;
  113. ops : byte;
  114. optypes : array[0..3] of longint;
  115. code : array[0..maxinfolen] of char;
  116. flags : longint;
  117. end;
  118. pinsentry=^tinsentry;
  119. const
  120. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  121. var
  122. InsTabCache : PInsTabCache;
  123. type
  124. taicpu = class(tai_cpu_abstract)
  125. oppostfix : TOpPostfix;
  126. roundingmode : troundingmode;
  127. procedure loadshifterop(opidx:longint;const so:tshifterop);
  128. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  129. constructor op_none(op : tasmop);
  130. constructor op_reg(op : tasmop;_op1 : tregister);
  131. constructor op_const(op : tasmop;_op1 : longint);
  132. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  133. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  134. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  135. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  136. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  137. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  138. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  139. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  140. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  141. { SFM/LFM }
  142. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  143. { *M*LL }
  144. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  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;
  157. procedure ResetPass2;
  158. function CheckIfValid:boolean;
  159. function GetString:string;
  160. function Pass1(offset:longint):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;
  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: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): tai;
  186. function spilling_create_store(r:tregister; const ref:treference): tai;
  187. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  188. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  189. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  190. { inserts pc relative symbols at places where they are reachable }
  191. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  192. procedure InitAsm;
  193. procedure DoneAsm;
  194. implementation
  195. uses
  196. cutils,rgobj,itcpugas;
  197. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  198. begin
  199. allocate_oper(opidx+1);
  200. with oper[opidx]^ do
  201. begin
  202. if typ<>top_shifterop then
  203. begin
  204. clearop(opidx);
  205. new(shifterop);
  206. end;
  207. shifterop^:=so;
  208. typ:=top_shifterop;
  209. if assigned(add_reg_instruction_hook) then
  210. add_reg_instruction_hook(self,shifterop^.rs);
  211. end;
  212. end;
  213. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  214. var
  215. i : byte;
  216. begin
  217. allocate_oper(opidx+1);
  218. with oper[opidx]^ do
  219. begin
  220. if typ<>top_regset then
  221. clearop(opidx);
  222. new(regset);
  223. regset^:=s;
  224. typ:=top_regset;
  225. for i:=RS_R0 to RS_R15 do
  226. begin
  227. if assigned(add_reg_instruction_hook) and (i in regset^) then
  228. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  229. end;
  230. end;
  231. end;
  232. {*****************************************************************************
  233. taicpu Constructors
  234. *****************************************************************************}
  235. constructor taicpu.op_none(op : tasmop);
  236. begin
  237. inherited create(op);
  238. end;
  239. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  240. begin
  241. inherited create(op);
  242. ops:=1;
  243. loadreg(0,_op1);
  244. end;
  245. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  246. begin
  247. inherited create(op);
  248. ops:=1;
  249. loadconst(0,aint(_op1));
  250. end;
  251. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  252. begin
  253. inherited create(op);
  254. ops:=2;
  255. loadreg(0,_op1);
  256. loadreg(1,_op2);
  257. end;
  258. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  259. begin
  260. inherited create(op);
  261. ops:=2;
  262. loadreg(0,_op1);
  263. loadconst(1,aint(_op2));
  264. end;
  265. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  266. begin
  267. inherited create(op);
  268. ops:=2;
  269. loadref(0,_op1);
  270. loadregset(1,_op2);
  271. end;
  272. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  273. begin
  274. inherited create(op);
  275. ops:=2;
  276. loadreg(0,_op1);
  277. loadref(1,_op2);
  278. end;
  279. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  280. begin
  281. inherited create(op);
  282. ops:=3;
  283. loadreg(0,_op1);
  284. loadreg(1,_op2);
  285. loadreg(2,_op3);
  286. end;
  287. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  288. begin
  289. inherited create(op);
  290. ops:=4;
  291. loadreg(0,_op1);
  292. loadreg(1,_op2);
  293. loadreg(2,_op3);
  294. loadreg(3,_op4);
  295. end;
  296. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  297. begin
  298. inherited create(op);
  299. ops:=3;
  300. loadreg(0,_op1);
  301. loadreg(1,_op2);
  302. loadconst(2,aint(_op3));
  303. end;
  304. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  305. begin
  306. inherited create(op);
  307. ops:=3;
  308. loadreg(0,_op1);
  309. loadconst(1,_op2);
  310. loadref(2,_op3);
  311. end;
  312. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  313. begin
  314. inherited create(op);
  315. ops:=3;
  316. loadreg(0,_op1);
  317. loadreg(1,_op2);
  318. loadsymbol(0,_op3,_op3ofs);
  319. end;
  320. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  321. begin
  322. inherited create(op);
  323. ops:=3;
  324. loadreg(0,_op1);
  325. loadreg(1,_op2);
  326. loadref(2,_op3);
  327. end;
  328. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  329. begin
  330. inherited create(op);
  331. ops:=3;
  332. loadreg(0,_op1);
  333. loadreg(1,_op2);
  334. loadshifterop(2,_op3);
  335. end;
  336. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  337. begin
  338. inherited create(op);
  339. condition:=cond;
  340. ops:=1;
  341. loadsymbol(0,_op1,0);
  342. end;
  343. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  344. begin
  345. inherited create(op);
  346. ops:=1;
  347. loadsymbol(0,_op1,0);
  348. end;
  349. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  350. begin
  351. inherited create(op);
  352. ops:=1;
  353. loadsymbol(0,_op1,_op1ofs);
  354. end;
  355. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  356. begin
  357. inherited create(op);
  358. ops:=2;
  359. loadreg(0,_op1);
  360. loadsymbol(1,_op2,_op2ofs);
  361. end;
  362. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  363. begin
  364. inherited create(op);
  365. ops:=2;
  366. loadsymbol(0,_op1,_op1ofs);
  367. loadref(1,_op2);
  368. end;
  369. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  370. begin
  371. { allow the register allocator to remove unnecessary moves }
  372. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  373. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  374. ) and
  375. (condition=C_None) and
  376. (ops=2) and
  377. (oper[0]^.typ=top_reg) and
  378. (oper[1]^.typ=top_reg) and
  379. (oper[0]^.reg=oper[1]^.reg);
  380. end;
  381. function spilling_create_load(const ref:treference;r:tregister): tai;
  382. begin
  383. case getregtype(r) of
  384. R_INTREGISTER :
  385. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  386. R_FPUREGISTER :
  387. { use lfm because we don't know the current internal format
  388. and avoid exceptions
  389. }
  390. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  391. else
  392. internalerror(200401041);
  393. end;
  394. end;
  395. function spilling_create_store(r:tregister; const ref:treference): tai;
  396. begin
  397. case getregtype(r) of
  398. R_INTREGISTER :
  399. result:=taicpu.op_reg_ref(A_STR,r,ref);
  400. R_FPUREGISTER :
  401. { use sfm because we don't know the current internal format
  402. and avoid exceptions
  403. }
  404. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  405. else
  406. internalerror(200401041);
  407. end;
  408. end;
  409. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  410. begin
  411. case opcode of
  412. A_ADC,A_ADD,A_AND,
  413. A_EOR,A_CLZ,
  414. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  415. A_LDRSH,A_LDRT,
  416. A_MOV,A_MVN,A_MLA,A_MUL,
  417. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  418. A_SWP,A_SWPB,
  419. A_LDF,A_FLT,A_FIX,
  420. A_ADF,A_DVF,A_FDV,A_FML,
  421. A_RFS,A_RFC,A_RDF,
  422. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  423. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  424. A_LFM:
  425. if opnr=0 then
  426. result:=operand_write
  427. else
  428. result:=operand_read;
  429. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  430. A_CMN,A_CMP,A_TEQ,A_TST,
  431. A_CMF,A_CMFE,A_WFS,A_CNF:
  432. result:=operand_read;
  433. A_SMLAL,A_UMLAL:
  434. if opnr in [0,1] then
  435. result:=operand_readwrite
  436. else
  437. result:=operand_read;
  438. A_SMULL,A_UMULL:
  439. if opnr in [0,1] then
  440. result:=operand_write
  441. else
  442. result:=operand_read;
  443. A_STR,A_STRB,A_STRBT,
  444. A_STRH,A_STRT,A_STF,A_SFM:
  445. { important is what happens with the involved registers }
  446. if opnr=0 then
  447. result := operand_read
  448. else
  449. { check for pre/post indexed }
  450. result := operand_read;
  451. else
  452. internalerror(200403151);
  453. end;
  454. end;
  455. procedure BuildInsTabCache;
  456. var
  457. i : longint;
  458. begin
  459. new(instabcache);
  460. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  461. i:=0;
  462. while (i<InsTabEntries) do
  463. begin
  464. if InsTabCache^[InsTab[i].Opcode]=-1 then
  465. InsTabCache^[InsTab[i].Opcode]:=i;
  466. inc(i);
  467. end;
  468. end;
  469. procedure InitAsm;
  470. begin
  471. if not assigned(instabcache) then
  472. BuildInsTabCache;
  473. end;
  474. procedure DoneAsm;
  475. begin
  476. if assigned(instabcache) then
  477. begin
  478. dispose(instabcache);
  479. instabcache:=nil;
  480. end;
  481. end;
  482. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  483. begin
  484. i.oppostfix:=pf;
  485. result:=i;
  486. end;
  487. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  488. begin
  489. i.roundingmode:=rm;
  490. result:=i;
  491. end;
  492. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  493. begin
  494. i.condition:=c;
  495. result:=i;
  496. end;
  497. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  498. var
  499. curpos : longint;
  500. lastpos : longint;
  501. curop : longint;
  502. curtai : tai;
  503. curdatatai,hp : tai;
  504. curdata : taasmoutput;
  505. l : tasmlabel;
  506. begin
  507. curdata:=taasmoutput.create;
  508. lastpos:=-1;
  509. curpos:=0;
  510. curtai:=tai(list.first);
  511. while assigned(curtai) do
  512. begin
  513. { instruction? }
  514. if curtai.typ=ait_instruction then
  515. begin
  516. { walk through all operand of the instruction }
  517. for curop:=0 to taicpu(curtai).ops-1 do
  518. begin
  519. { reference? }
  520. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  521. begin
  522. { pc relative symbol? }
  523. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  524. if assigned(curdatatai) then
  525. begin
  526. { if yes, insert till next symbol }
  527. repeat
  528. hp:=tai(curdatatai.next);
  529. listtoinsert.remove(curdatatai);
  530. curdata.concat(curdatatai);
  531. curdatatai:=hp;
  532. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  533. if lastpos=-1 then
  534. lastpos:=curpos;
  535. end;
  536. end;
  537. end;
  538. inc(curpos);
  539. end;
  540. { split only at real instructions else the test below fails }
  541. if ((curpos-lastpos)>1016) and (curtai.typ=ait_instruction) and
  542. (
  543. { don't split loads of pc to lr and the following move }
  544. not(
  545. (taicpu(curtai).opcode=A_MOV) and
  546. (taicpu(curtai).oper[0]^.typ=top_reg) and
  547. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  548. (taicpu(curtai).oper[1]^.typ=top_reg) and
  549. (taicpu(curtai).oper[1]^.reg=NR_PC)
  550. )
  551. ) then
  552. begin
  553. lastpos:=curpos;
  554. hp:=tai(curtai.next);
  555. objectlibrary.getjumplabel(l);
  556. curdata.insert(taicpu.op_sym(A_B,l));
  557. curdata.concat(tai_label.create(l));
  558. list.insertlistafter(curtai,curdata);
  559. curtai:=hp;
  560. end
  561. else
  562. curtai:=tai(curtai.next);
  563. end;
  564. list.concatlist(curdata);
  565. curdata.free;
  566. end;
  567. (*
  568. Floating point instruction format information, taken from the linux kernel
  569. ARM Floating Point Instruction Classes
  570. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  571. |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
  572. |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)
  573. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  574. |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
  575. |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
  576. |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
  577. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  578. CPDT data transfer instructions
  579. LDF, STF, LFM (copro 2), SFM (copro 2)
  580. CPDO dyadic arithmetic instructions
  581. ADF, MUF, SUF, RSF, DVF, RDF,
  582. POW, RPW, RMF, FML, FDV, FRD, POL
  583. CPDO monadic arithmetic instructions
  584. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  585. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  586. CPRT joint arithmetic/data transfer instructions
  587. FIX (arithmetic followed by load/store)
  588. FLT (load/store followed by arithmetic)
  589. CMF, CNF CMFE, CNFE (comparisons)
  590. WFS, RFS (write/read floating point status register)
  591. WFC, RFC (write/read floating point control register)
  592. cond condition codes
  593. P pre/post index bit: 0 = postindex, 1 = preindex
  594. U up/down bit: 0 = stack grows down, 1 = stack grows up
  595. W write back bit: 1 = update base register (Rn)
  596. L load/store bit: 0 = store, 1 = load
  597. Rn base register
  598. Rd destination/source register
  599. Fd floating point destination register
  600. Fn floating point source register
  601. Fm floating point source register or floating point constant
  602. uv transfer length (TABLE 1)
  603. wx register count (TABLE 2)
  604. abcd arithmetic opcode (TABLES 3 & 4)
  605. ef destination size (rounding precision) (TABLE 5)
  606. gh rounding mode (TABLE 6)
  607. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  608. i constant bit: 1 = constant (TABLE 6)
  609. */
  610. /*
  611. TABLE 1
  612. +-------------------------+---+---+---------+---------+
  613. | Precision | u | v | FPSR.EP | length |
  614. +-------------------------+---+---+---------+---------+
  615. | Single | 0 | 0 | x | 1 words |
  616. | Double | 1 | 1 | x | 2 words |
  617. | Extended | 1 | 1 | x | 3 words |
  618. | Packed decimal | 1 | 1 | 0 | 3 words |
  619. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  620. +-------------------------+---+---+---------+---------+
  621. Note: x = don't care
  622. */
  623. /*
  624. TABLE 2
  625. +---+---+---------------------------------+
  626. | w | x | Number of registers to transfer |
  627. +---+---+---------------------------------+
  628. | 0 | 1 | 1 |
  629. | 1 | 0 | 2 |
  630. | 1 | 1 | 3 |
  631. | 0 | 0 | 4 |
  632. +---+---+---------------------------------+
  633. */
  634. /*
  635. TABLE 3: Dyadic Floating Point Opcodes
  636. +---+---+---+---+----------+-----------------------+-----------------------+
  637. | a | b | c | d | Mnemonic | Description | Operation |
  638. +---+---+---+---+----------+-----------------------+-----------------------+
  639. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  640. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  641. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  642. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  643. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  644. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  645. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  646. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  647. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  648. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  649. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  650. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  651. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  652. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  653. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  654. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  655. +---+---+---+---+----------+-----------------------+-----------------------+
  656. Note: POW, RPW, POL are deprecated, and are available for backwards
  657. compatibility only.
  658. */
  659. /*
  660. TABLE 4: Monadic Floating Point Opcodes
  661. +---+---+---+---+----------+-----------------------+-----------------------+
  662. | a | b | c | d | Mnemonic | Description | Operation |
  663. +---+---+---+---+----------+-----------------------+-----------------------+
  664. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  665. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  666. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  667. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  668. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  669. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  670. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  671. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  672. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  673. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  674. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  675. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  676. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  677. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  678. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  679. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  680. +---+---+---+---+----------+-----------------------+-----------------------+
  681. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  682. available for backwards compatibility only.
  683. */
  684. /*
  685. TABLE 5
  686. +-------------------------+---+---+
  687. | Rounding Precision | e | f |
  688. +-------------------------+---+---+
  689. | IEEE Single precision | 0 | 0 |
  690. | IEEE Double precision | 0 | 1 |
  691. | IEEE Extended precision | 1 | 0 |
  692. | undefined (trap) | 1 | 1 |
  693. +-------------------------+---+---+
  694. */
  695. /*
  696. TABLE 5
  697. +---------------------------------+---+---+
  698. | Rounding Mode | g | h |
  699. +---------------------------------+---+---+
  700. | Round to nearest (default) | 0 | 0 |
  701. | Round toward plus infinity | 0 | 1 |
  702. | Round toward negative infinity | 1 | 0 |
  703. | Round toward zero | 1 | 1 |
  704. +---------------------------------+---+---+
  705. *)
  706. function taicpu.GetString:string;
  707. var
  708. i : longint;
  709. s : string;
  710. addsize : boolean;
  711. begin
  712. s:='['+gas_op2str[opcode];
  713. for i:=0 to ops-1 do
  714. begin
  715. with oper[i]^ do
  716. begin
  717. if i=0 then
  718. s:=s+' '
  719. else
  720. s:=s+',';
  721. { type }
  722. addsize:=false;
  723. if (ot and OT_VREG)=OT_VREG then
  724. s:=s+'vreg'
  725. else
  726. if (ot and OT_FPUREG)=OT_FPUREG then
  727. s:=s+'fpureg'
  728. else
  729. if (ot and OT_REGISTER)=OT_REGISTER then
  730. begin
  731. s:=s+'reg';
  732. addsize:=true;
  733. end
  734. else
  735. if (ot and OT_REGLIST)=OT_REGLIST then
  736. begin
  737. s:=s+'reglist';
  738. addsize:=false;
  739. end
  740. else
  741. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  742. begin
  743. s:=s+'imm';
  744. addsize:=true;
  745. end
  746. else
  747. if (ot and OT_MEMORY)=OT_MEMORY then
  748. begin
  749. s:=s+'mem';
  750. addsize:=true;
  751. if (ot and OT_AM2)<>0 then
  752. s:=s+' am2 ';
  753. end
  754. else
  755. s:=s+'???';
  756. { size }
  757. if addsize then
  758. begin
  759. if (ot and OT_BITS8)<>0 then
  760. s:=s+'8'
  761. else
  762. if (ot and OT_BITS16)<>0 then
  763. s:=s+'24'
  764. else
  765. if (ot and OT_BITS32)<>0 then
  766. s:=s+'32'
  767. else
  768. if (ot and OT_BITSSHIFTER)<>0 then
  769. s:=s+'shifter'
  770. else
  771. s:=s+'??';
  772. { signed }
  773. if (ot and OT_SIGNED)<>0 then
  774. s:=s+'s';
  775. end;
  776. end;
  777. end;
  778. GetString:=s+']';
  779. end;
  780. procedure taicpu.ResetPass1;
  781. begin
  782. { we need to reset everything here, because the choosen insentry
  783. can be invalid for a new situation where the previously optimized
  784. insentry is not correct }
  785. InsEntry:=nil;
  786. InsSize:=0;
  787. LastInsOffset:=-1;
  788. end;
  789. procedure taicpu.ResetPass2;
  790. begin
  791. { we are here in a second pass, check if the instruction can be optimized }
  792. if assigned(InsEntry) and
  793. ((InsEntry^.flags and IF_PASS2)<>0) then
  794. begin
  795. InsEntry:=nil;
  796. InsSize:=0;
  797. end;
  798. LastInsOffset:=-1;
  799. end;
  800. function taicpu.CheckIfValid:boolean;
  801. begin
  802. end;
  803. function taicpu.Pass1(offset:longint):longint;
  804. var
  805. ldr2op : array[PF_B..PF_T] of tasmop = (
  806. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  807. str2op : array[PF_B..PF_T] of tasmop = (
  808. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  809. begin
  810. Pass1:=0;
  811. { Save the old offset and set the new offset }
  812. InsOffset:=Offset;
  813. { Error? }
  814. if (Insentry=nil) and (InsSize=-1) then
  815. exit;
  816. { set the file postion }
  817. aktfilepos:=fileinfo;
  818. { tranlate LDR+postfix to complete opcode }
  819. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  820. begin
  821. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  822. opcode:=ldr2op[oppostfix]
  823. else
  824. internalerror(2005091001);
  825. if opcode=A_None then
  826. internalerror(2005091004);
  827. { postfix has been added to opcode }
  828. oppostfix:=PF_None;
  829. end
  830. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  831. begin
  832. if (oppostfix in [low(str2op)..high(str2op)]) then
  833. opcode:=str2op[oppostfix]
  834. else
  835. internalerror(2005091002);
  836. if opcode=A_None then
  837. internalerror(2005091003);
  838. { postfix has been added to opcode }
  839. oppostfix:=PF_None;
  840. end;
  841. { Get InsEntry }
  842. if FindInsEntry then
  843. begin
  844. InsSize:=4;
  845. LastInsOffset:=InsOffset;
  846. Pass1:=InsSize;
  847. exit;
  848. end;
  849. LastInsOffset:=-1;
  850. end;
  851. procedure taicpu.Pass2(objdata:TObjData);
  852. begin
  853. { error in pass1 ? }
  854. if insentry=nil then
  855. exit;
  856. aktfilepos:=fileinfo;
  857. { Generate the instruction }
  858. GenCode(objdata);
  859. end;
  860. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  861. begin
  862. end;
  863. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  864. begin
  865. end;
  866. procedure taicpu.ppubuildderefimploper(var o:toper);
  867. begin
  868. end;
  869. procedure taicpu.ppuderefoper(var o:toper);
  870. begin
  871. end;
  872. function taicpu.InsEnd:longint;
  873. begin
  874. end;
  875. procedure taicpu.create_ot;
  876. var
  877. i,l,relsize : longint;
  878. dummy : byte;
  879. begin
  880. if ops=0 then
  881. exit;
  882. { update oper[].ot field }
  883. for i:=0 to ops-1 do
  884. with oper[i]^ do
  885. begin
  886. case typ of
  887. top_regset:
  888. begin
  889. ot:=OT_REGLIST;
  890. end;
  891. top_reg :
  892. begin
  893. case getregtype(reg) of
  894. R_INTREGISTER:
  895. ot:=OT_REG32 or OT_SHIFTEROP;
  896. R_FPUREGISTER:
  897. ot:=OT_FPUREG;
  898. else
  899. internalerror(2005090901);
  900. end;
  901. end;
  902. top_ref :
  903. begin
  904. if ref^.refaddr=addr_no then
  905. begin
  906. { create ot field }
  907. { we should get the size here dependend on the
  908. instruction }
  909. if (ot and OT_SIZE_MASK)=0 then
  910. ot:=OT_MEMORY or OT_BITS32
  911. else
  912. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  913. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  914. ot:=ot or OT_MEM_OFFS;
  915. { if we need to fix a reference, we do it here }
  916. { pc relative addressing }
  917. if (ref^.base=NR_NO) and
  918. (ref^.index=NR_NO) and
  919. (ref^.shiftmode=SM_None)
  920. { at least we should check if the destination symbol
  921. is in a text section }
  922. { and
  923. (ref^.symbol^.owner="text") } then
  924. ref^.base:=NR_PC;
  925. { determine possible address modes }
  926. if (ref^.base<>NR_NO) and
  927. (
  928. (
  929. (ref^.index=NR_NO) and
  930. (ref^.shiftmode=SM_None) and
  931. (ref^.offset>=-4097) and
  932. (ref^.offset<=4097)
  933. ) or
  934. (
  935. (ref^.shiftmode=SM_None) and
  936. (ref^.offset=0)
  937. ) or
  938. (
  939. (ref^.index<>NR_NO) and
  940. (ref^.shiftmode<>SM_None) and
  941. (ref^.shiftimm<=31) and
  942. (ref^.offset=0)
  943. )
  944. ) then
  945. ot:=ot or OT_AM2;
  946. if (ref^.index<>NR_NO) and
  947. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  948. (
  949. (ref^.base=NR_NO) and
  950. (ref^.shiftmode=SM_None) and
  951. (ref^.offset=0)
  952. ) then
  953. ot:=ot or OT_AM4;
  954. end
  955. else
  956. begin
  957. l:=ref^.offset;
  958. if assigned(ref^.symbol) then
  959. inc(l,ref^.symbol.address);
  960. relsize:=(InsOffset+2)-l;
  961. if (relsize<-33554428) or (relsize>33554428) then
  962. ot:=OT_IMM32
  963. else
  964. ot:=OT_IMM24;
  965. end;
  966. end;
  967. top_local :
  968. begin
  969. { we should get the size here dependend on the
  970. instruction }
  971. if (ot and OT_SIZE_MASK)=0 then
  972. ot:=OT_MEMORY or OT_BITS32
  973. else
  974. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  975. end;
  976. top_const :
  977. begin
  978. ot:=OT_IMMEDIATE;
  979. if is_shifter_const(val,dummy) then
  980. ot:=OT_IMMSHIFTER
  981. else
  982. ot:=OT_IMM32
  983. end;
  984. top_none :
  985. begin
  986. { generated when there was an error in the
  987. assembler reader. It never happends when generating
  988. assembler }
  989. end;
  990. top_shifterop:
  991. begin
  992. ot:=OT_SHIFTEROP;
  993. end;
  994. else
  995. internalerror(200402261);
  996. end;
  997. end;
  998. end;
  999. function taicpu.Matches(p:PInsEntry):longint;
  1000. { * IF_SM stands for Size Match: any operand whose size is not
  1001. * explicitly specified by the template is `really' intended to be
  1002. * the same size as the first size-specified operand.
  1003. * Non-specification is tolerated in the input instruction, but
  1004. * _wrong_ specification is not.
  1005. *
  1006. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1007. * three-operand instructions such as SHLD: it implies that the
  1008. * first two operands must match in size, but that the third is
  1009. * required to be _unspecified_.
  1010. *
  1011. * IF_SB invokes Size Byte: operands with unspecified size in the
  1012. * template are really bytes, and so no non-byte specification in
  1013. * the input instruction will be tolerated. IF_SW similarly invokes
  1014. * Size Word, and IF_SD invokes Size Doubleword.
  1015. *
  1016. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1017. * that any operand with unspecified size in the template is
  1018. * required to have unspecified size in the instruction too...)
  1019. }
  1020. var
  1021. i,j,asize,oprs : longint;
  1022. siz : array[0..3] of longint;
  1023. begin
  1024. Matches:=100;
  1025. writeln(getstring,'---');
  1026. { Check the opcode and operands }
  1027. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1028. begin
  1029. Matches:=0;
  1030. exit;
  1031. end;
  1032. { Check that no spurious colons or TOs are present }
  1033. for i:=0 to p^.ops-1 do
  1034. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1035. begin
  1036. Matches:=0;
  1037. exit;
  1038. end;
  1039. { Check that the operand flags all match up }
  1040. for i:=0 to p^.ops-1 do
  1041. begin
  1042. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1043. ((p^.optypes[i] and OT_SIZE_MASK) and
  1044. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1045. begin
  1046. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1047. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1048. begin
  1049. Matches:=0;
  1050. exit;
  1051. end
  1052. else
  1053. Matches:=1;
  1054. end;
  1055. end;
  1056. { check postfixes:
  1057. the existance of a certain postfix requires a
  1058. particular code }
  1059. { update condition flags
  1060. or floating point single }
  1061. if (oppostfix=PF_S) and
  1062. not(p^.code[0] in [#$04]) then
  1063. begin
  1064. Matches:=0;
  1065. exit;
  1066. end;
  1067. { floating point size }
  1068. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1069. not(p^.code[0] in []) then
  1070. begin
  1071. Matches:=0;
  1072. exit;
  1073. end;
  1074. { multiple load/store address modes }
  1075. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1076. not(p^.code[0] in [
  1077. // ldr,str,ldrb,strb
  1078. #$17,
  1079. // stm,ldm
  1080. #$26
  1081. ]) then
  1082. begin
  1083. Matches:=0;
  1084. exit;
  1085. end;
  1086. { we shouldn't see any opsize prefixes here }
  1087. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1088. begin
  1089. Matches:=0;
  1090. exit;
  1091. end;
  1092. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1093. begin
  1094. Matches:=0;
  1095. exit;
  1096. end;
  1097. { Check operand sizes }
  1098. { as default an untyped size can get all the sizes, this is different
  1099. from nasm, but else we need to do a lot checking which opcodes want
  1100. size or not with the automatic size generation }
  1101. asize:=longint($ffffffff);
  1102. (*
  1103. if (p^.flags and IF_SB)<>0 then
  1104. asize:=OT_BITS8
  1105. else if (p^.flags and IF_SW)<>0 then
  1106. asize:=OT_BITS16
  1107. else if (p^.flags and IF_SD)<>0 then
  1108. asize:=OT_BITS32;
  1109. if (p^.flags and IF_ARMASK)<>0 then
  1110. begin
  1111. siz[0]:=0;
  1112. siz[1]:=0;
  1113. siz[2]:=0;
  1114. if (p^.flags and IF_AR0)<>0 then
  1115. siz[0]:=asize
  1116. else if (p^.flags and IF_AR1)<>0 then
  1117. siz[1]:=asize
  1118. else if (p^.flags and IF_AR2)<>0 then
  1119. siz[2]:=asize;
  1120. end
  1121. else
  1122. begin
  1123. { we can leave because the size for all operands is forced to be
  1124. the same
  1125. but not if IF_SB IF_SW or IF_SD is set PM }
  1126. if asize=-1 then
  1127. exit;
  1128. siz[0]:=asize;
  1129. siz[1]:=asize;
  1130. siz[2]:=asize;
  1131. end;
  1132. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1133. begin
  1134. if (p^.flags and IF_SM2)<>0 then
  1135. oprs:=2
  1136. else
  1137. oprs:=p^.ops;
  1138. for i:=0 to oprs-1 do
  1139. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1140. begin
  1141. for j:=0 to oprs-1 do
  1142. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1143. break;
  1144. end;
  1145. end
  1146. else
  1147. oprs:=2;
  1148. { Check operand sizes }
  1149. for i:=0 to p^.ops-1 do
  1150. begin
  1151. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1152. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1153. { Immediates can always include smaller size }
  1154. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1155. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1156. Matches:=2;
  1157. end;
  1158. *)
  1159. end;
  1160. function taicpu.calcsize(p:PInsEntry):shortint;
  1161. begin
  1162. result:=4;
  1163. end;
  1164. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1165. begin
  1166. end;
  1167. procedure taicpu.Swapoperands;
  1168. begin
  1169. end;
  1170. function taicpu.FindInsentry:boolean;
  1171. var
  1172. i : longint;
  1173. begin
  1174. result:=false;
  1175. { Things which may only be done once, not when a second pass is done to
  1176. optimize }
  1177. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1178. begin
  1179. { create the .ot fields }
  1180. create_ot;
  1181. { set the file postion }
  1182. aktfilepos:=fileinfo;
  1183. end
  1184. else
  1185. begin
  1186. { we've already an insentry so it's valid }
  1187. result:=true;
  1188. exit;
  1189. end;
  1190. { Lookup opcode in the table }
  1191. InsSize:=-1;
  1192. i:=instabcache^[opcode];
  1193. if i=-1 then
  1194. begin
  1195. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1196. exit;
  1197. end;
  1198. insentry:=@instab[i];
  1199. while (insentry^.opcode=opcode) do
  1200. begin
  1201. if matches(insentry)=100 then
  1202. begin
  1203. result:=true;
  1204. exit;
  1205. end;
  1206. inc(i);
  1207. insentry:=@instab[i];
  1208. end;
  1209. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1210. { No instruction found, set insentry to nil and inssize to -1 }
  1211. insentry:=nil;
  1212. inssize:=-1;
  1213. end;
  1214. procedure taicpu.gencode(objdata:TObjData);
  1215. var
  1216. bytes : dword;
  1217. i_field : byte;
  1218. procedure setshifterop(op : byte);
  1219. begin
  1220. case oper[op]^.typ of
  1221. top_const:
  1222. begin
  1223. i_field:=1;
  1224. bytes:=bytes or (oper[op]^.val and $fff);
  1225. end;
  1226. top_reg:
  1227. begin
  1228. i_field:=0;
  1229. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1230. { does a real shifter op follow? }
  1231. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1232. begin
  1233. end;
  1234. end;
  1235. else
  1236. internalerror(2005091103);
  1237. end;
  1238. end;
  1239. begin
  1240. bytes:=$0;
  1241. { evaluate and set condition code }
  1242. { condition code allowed? }
  1243. { setup rest of the instruction }
  1244. case insentry^.code[0] of
  1245. #$08:
  1246. begin
  1247. { set instruction code }
  1248. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1249. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1250. { set destination }
  1251. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1252. { create shifter op }
  1253. setshifterop(1);
  1254. { set i field }
  1255. bytes:=bytes or (i_field shl 25);
  1256. { set s if necessary }
  1257. if oppostfix=PF_S then
  1258. bytes:=bytes or (1 shl 20);
  1259. end;
  1260. #$ff:
  1261. internalerror(2005091101);
  1262. else
  1263. internalerror(2005091102);
  1264. end;
  1265. { we're finished, write code }
  1266. objdata.writebytes(bytes,sizeof(bytes));
  1267. end;
  1268. end.
  1269. {$ifdef dummy}
  1270. (*
  1271. static void gencode (long segment, long offset, int bits,
  1272. insn *ins, char *codes, long insn_end)
  1273. {
  1274. int has_S_code; /* S - setflag */
  1275. int has_B_code; /* B - setflag */
  1276. int has_T_code; /* T - setflag */
  1277. int has_W_code; /* ! => W flag */
  1278. int has_F_code; /* ^ => S flag */
  1279. int keep;
  1280. unsigned char c;
  1281. unsigned char bytes[4];
  1282. long data, size;
  1283. static int cc_code[] = /* bit pattern of cc */
  1284. { /* order as enum in */
  1285. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1286. 0x0A, 0x0C, 0x08, 0x0D,
  1287. 0x09, 0x0B, 0x04, 0x01,
  1288. 0x05, 0x07, 0x06,
  1289. };
  1290. (*
  1291. #ifdef DEBUG
  1292. static char *CC[] =
  1293. { /* condition code names */
  1294. "AL", "CC", "CS", "EQ",
  1295. "GE", "GT", "HI", "LE",
  1296. "LS", "LT", "MI", "NE",
  1297. "PL", "VC", "VS", "",
  1298. "S"
  1299. };
  1300. *)
  1301. has_S_code = (ins->condition & C_SSETFLAG);
  1302. has_B_code = (ins->condition & C_BSETFLAG);
  1303. has_T_code = (ins->condition & C_TSETFLAG);
  1304. has_W_code = (ins->condition & C_EXSETFLAG);
  1305. has_F_code = (ins->condition & C_FSETFLAG);
  1306. ins->condition = (ins->condition & 0x0F);
  1307. (*
  1308. if (rt_debug)
  1309. {
  1310. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1311. CC[ins->condition & 0x0F]);
  1312. if (has_S_code)
  1313. printf ("S");
  1314. if (has_B_code)
  1315. printf ("B");
  1316. if (has_T_code)
  1317. printf ("T");
  1318. if (has_W_code)
  1319. printf ("!");
  1320. if (has_F_code)
  1321. printf ("^");
  1322. printf ("\n");
  1323. c = *codes;
  1324. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1325. bytes[0] = 0xB;
  1326. bytes[1] = 0xE;
  1327. bytes[2] = 0xE;
  1328. bytes[3] = 0xF;
  1329. }
  1330. *)
  1331. // First condition code in upper nibble
  1332. if (ins->condition < C_NONE)
  1333. {
  1334. c = cc_code[ins->condition] << 4;
  1335. }
  1336. else
  1337. {
  1338. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1339. }
  1340. switch (keep = *codes)
  1341. {
  1342. case 1:
  1343. // B, BL
  1344. ++codes;
  1345. c |= *codes++;
  1346. bytes[0] = c;
  1347. if (ins->oprs[0].segment != segment)
  1348. {
  1349. // fais une relocation
  1350. c = 1;
  1351. data = 0; // Let the linker locate ??
  1352. }
  1353. else
  1354. {
  1355. c = 0;
  1356. data = ins->oprs[0].offset - (offset + 8);
  1357. if (data % 4)
  1358. {
  1359. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1360. }
  1361. }
  1362. if (data >= 0x1000)
  1363. {
  1364. errfunc (ERR_NONFATAL, "too long offset");
  1365. }
  1366. data = data >> 2;
  1367. bytes[1] = (data >> 16) & 0xFF;
  1368. bytes[2] = (data >> 8) & 0xFF;
  1369. bytes[3] = (data ) & 0xFF;
  1370. if (c == 1)
  1371. {
  1372. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1373. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1374. }
  1375. else
  1376. {
  1377. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1378. }
  1379. return;
  1380. case 2:
  1381. // SWI
  1382. ++codes;
  1383. c |= *codes++;
  1384. bytes[0] = c;
  1385. data = ins->oprs[0].offset;
  1386. bytes[1] = (data >> 16) & 0xFF;
  1387. bytes[2] = (data >> 8) & 0xFF;
  1388. bytes[3] = (data) & 0xFF;
  1389. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1390. return;
  1391. case 3:
  1392. // BX
  1393. ++codes;
  1394. c |= *codes++;
  1395. bytes[0] = c;
  1396. bytes[1] = *codes++;
  1397. bytes[2] = *codes++;
  1398. bytes[3] = *codes++;
  1399. c = regval (&ins->oprs[0],1);
  1400. if (c == 15) // PC
  1401. {
  1402. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1403. }
  1404. else if (c > 15)
  1405. {
  1406. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1407. }
  1408. bytes[3] |= (c & 0x0F);
  1409. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1410. return;
  1411. case 4: // AND Rd,Rn,Rm
  1412. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1413. case 6: // AND Rd,Rn,Rm,<shift>imm
  1414. case 7: // AND Rd,Rn,<shift>imm
  1415. ++codes;
  1416. #ifdef DEBUG
  1417. if (rt_debug)
  1418. {
  1419. printf (" decode - '0x%02X'\n", keep);
  1420. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1421. }
  1422. #endif
  1423. bytes[0] = c | *codes;
  1424. ++codes;
  1425. bytes[1] = *codes;
  1426. if (has_S_code)
  1427. bytes[1] |= 0x10;
  1428. c = regval (&ins->oprs[1],1);
  1429. // Rn in low nibble
  1430. bytes[1] |= c;
  1431. // Rd in high nibble
  1432. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1433. if (keep != 7)
  1434. {
  1435. // Rm in low nibble
  1436. bytes[3] = regval (&ins->oprs[2],1);
  1437. }
  1438. // Shifts if any
  1439. if (keep == 5 || keep == 6)
  1440. {
  1441. // Shift in bytes 2 and 3
  1442. if (keep == 5)
  1443. {
  1444. // Rs
  1445. c = regval (&ins->oprs[3],1);
  1446. bytes[2] |= c;
  1447. c = 0x10; // Set bit 4 in byte[3]
  1448. }
  1449. if (keep == 6)
  1450. {
  1451. c = (ins->oprs[3].offset) & 0x1F;
  1452. // #imm
  1453. bytes[2] |= c >> 1;
  1454. if (c & 0x01)
  1455. {
  1456. bytes[3] |= 0x80;
  1457. }
  1458. c = 0; // Clr bit 4 in byte[3]
  1459. }
  1460. // <shift>
  1461. c |= shiftval (&ins->oprs[3]) << 5;
  1462. bytes[3] |= c;
  1463. }
  1464. // reg,reg,imm
  1465. if (keep == 7)
  1466. {
  1467. int shimm;
  1468. shimm = imm_shift (ins->oprs[2].offset);
  1469. if (shimm == -1)
  1470. {
  1471. errfunc (ERR_NONFATAL, "cannot create that constant");
  1472. }
  1473. bytes[3] = shimm & 0xFF;
  1474. bytes[2] |= (shimm & 0xF00) >> 8;
  1475. }
  1476. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1477. return;
  1478. case 8: // MOV Rd,Rm
  1479. case 9: // MOV Rd,Rm,<shift>Rs
  1480. case 0xA: // MOV Rd,Rm,<shift>imm
  1481. case 0xB: // MOV Rd,<shift>imm
  1482. ++codes;
  1483. #ifdef DEBUG
  1484. if (rt_debug)
  1485. {
  1486. printf (" decode - '0x%02X'\n", keep);
  1487. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1488. }
  1489. #endif
  1490. bytes[0] = c | *codes;
  1491. ++codes;
  1492. bytes[1] = *codes;
  1493. if (has_S_code)
  1494. bytes[1] |= 0x10;
  1495. // Rd in high nibble
  1496. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1497. if (keep != 0x0B)
  1498. {
  1499. // Rm in low nibble
  1500. bytes[3] = regval (&ins->oprs[1],1);
  1501. }
  1502. // Shifts if any
  1503. if (keep == 0x09 || keep == 0x0A)
  1504. {
  1505. // Shift in bytes 2 and 3
  1506. if (keep == 0x09)
  1507. {
  1508. // Rs
  1509. c = regval (&ins->oprs[2],1);
  1510. bytes[2] |= c;
  1511. c = 0x10; // Set bit 4 in byte[3]
  1512. }
  1513. if (keep == 0x0A)
  1514. {
  1515. c = (ins->oprs[2].offset) & 0x1F;
  1516. // #imm
  1517. bytes[2] |= c >> 1;
  1518. if (c & 0x01)
  1519. {
  1520. bytes[3] |= 0x80;
  1521. }
  1522. c = 0; // Clr bit 4 in byte[3]
  1523. }
  1524. // <shift>
  1525. c |= shiftval (&ins->oprs[2]) << 5;
  1526. bytes[3] |= c;
  1527. }
  1528. // reg,imm
  1529. if (keep == 0x0B)
  1530. {
  1531. int shimm;
  1532. shimm = imm_shift (ins->oprs[1].offset);
  1533. if (shimm == -1)
  1534. {
  1535. errfunc (ERR_NONFATAL, "cannot create that constant");
  1536. }
  1537. bytes[3] = shimm & 0xFF;
  1538. bytes[2] |= (shimm & 0xF00) >> 8;
  1539. }
  1540. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1541. return;
  1542. case 0xC: // CMP Rn,Rm
  1543. case 0xD: // CMP Rn,Rm,<shift>Rs
  1544. case 0xE: // CMP Rn,Rm,<shift>imm
  1545. case 0xF: // CMP Rn,<shift>imm
  1546. ++codes;
  1547. bytes[0] = c | *codes++;
  1548. bytes[1] = *codes;
  1549. // Implicit S code
  1550. bytes[1] |= 0x10;
  1551. c = regval (&ins->oprs[0],1);
  1552. // Rn in low nibble
  1553. bytes[1] |= c;
  1554. // No destination
  1555. bytes[2] = 0;
  1556. if (keep != 0x0B)
  1557. {
  1558. // Rm in low nibble
  1559. bytes[3] = regval (&ins->oprs[1],1);
  1560. }
  1561. // Shifts if any
  1562. if (keep == 0x0D || keep == 0x0E)
  1563. {
  1564. // Shift in bytes 2 and 3
  1565. if (keep == 0x0D)
  1566. {
  1567. // Rs
  1568. c = regval (&ins->oprs[2],1);
  1569. bytes[2] |= c;
  1570. c = 0x10; // Set bit 4 in byte[3]
  1571. }
  1572. if (keep == 0x0E)
  1573. {
  1574. c = (ins->oprs[2].offset) & 0x1F;
  1575. // #imm
  1576. bytes[2] |= c >> 1;
  1577. if (c & 0x01)
  1578. {
  1579. bytes[3] |= 0x80;
  1580. }
  1581. c = 0; // Clr bit 4 in byte[3]
  1582. }
  1583. // <shift>
  1584. c |= shiftval (&ins->oprs[2]) << 5;
  1585. bytes[3] |= c;
  1586. }
  1587. // reg,imm
  1588. if (keep == 0x0F)
  1589. {
  1590. int shimm;
  1591. shimm = imm_shift (ins->oprs[1].offset);
  1592. if (shimm == -1)
  1593. {
  1594. errfunc (ERR_NONFATAL, "cannot create that constant");
  1595. }
  1596. bytes[3] = shimm & 0xFF;
  1597. bytes[2] |= (shimm & 0xF00) >> 8;
  1598. }
  1599. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1600. return;
  1601. case 0x10: // MRS Rd,<psr>
  1602. ++codes;
  1603. bytes[0] = c | *codes++;
  1604. bytes[1] = *codes++;
  1605. // Rd
  1606. c = regval (&ins->oprs[0],1);
  1607. bytes[2] = c << 4;
  1608. bytes[3] = 0;
  1609. c = ins->oprs[1].basereg;
  1610. if (c == R_CPSR || c == R_SPSR)
  1611. {
  1612. if (c == R_SPSR)
  1613. {
  1614. bytes[1] |= 0x40;
  1615. }
  1616. }
  1617. else
  1618. {
  1619. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1620. }
  1621. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1622. return;
  1623. case 0x11: // MSR <psr>,Rm
  1624. case 0x12: // MSR <psrf>,Rm
  1625. case 0x13: // MSR <psrf>,#expression
  1626. ++codes;
  1627. bytes[0] = c | *codes++;
  1628. bytes[1] = *codes++;
  1629. bytes[2] = *codes;
  1630. if (keep == 0x11 || keep == 0x12)
  1631. {
  1632. // Rm
  1633. c = regval (&ins->oprs[1],1);
  1634. bytes[3] = c;
  1635. }
  1636. else
  1637. {
  1638. int shimm;
  1639. shimm = imm_shift (ins->oprs[1].offset);
  1640. if (shimm == -1)
  1641. {
  1642. errfunc (ERR_NONFATAL, "cannot create that constant");
  1643. }
  1644. bytes[3] = shimm & 0xFF;
  1645. bytes[2] |= (shimm & 0xF00) >> 8;
  1646. }
  1647. c = ins->oprs[0].basereg;
  1648. if ( keep == 0x11)
  1649. {
  1650. if ( c == R_CPSR || c == R_SPSR)
  1651. {
  1652. if ( c== R_SPSR)
  1653. {
  1654. bytes[1] |= 0x40;
  1655. }
  1656. }
  1657. else
  1658. {
  1659. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1660. }
  1661. }
  1662. else
  1663. {
  1664. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1665. {
  1666. if ( c== R_SPSR_FLG)
  1667. {
  1668. bytes[1] |= 0x40;
  1669. }
  1670. }
  1671. else
  1672. {
  1673. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1674. }
  1675. }
  1676. break;
  1677. case 0x14: // MUL Rd,Rm,Rs
  1678. case 0x15: // MULA Rd,Rm,Rs,Rn
  1679. ++codes;
  1680. bytes[0] = c | *codes++;
  1681. bytes[1] = *codes++;
  1682. bytes[3] = *codes;
  1683. // Rd
  1684. bytes[1] |= regval (&ins->oprs[0],1);
  1685. if (has_S_code)
  1686. bytes[1] |= 0x10;
  1687. // Rm
  1688. bytes[3] |= regval (&ins->oprs[1],1);
  1689. // Rs
  1690. bytes[2] = regval (&ins->oprs[2],1);
  1691. if (keep == 0x15)
  1692. {
  1693. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1694. }
  1695. break;
  1696. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1697. ++codes;
  1698. bytes[0] = c | *codes++;
  1699. bytes[1] = *codes++;
  1700. bytes[3] = *codes;
  1701. // RdHi
  1702. bytes[1] |= regval (&ins->oprs[1],1);
  1703. if (has_S_code)
  1704. bytes[1] |= 0x10;
  1705. // RdLo
  1706. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1707. // Rm
  1708. bytes[3] |= regval (&ins->oprs[2],1);
  1709. // Rs
  1710. bytes[2] |= regval (&ins->oprs[3],1);
  1711. break;
  1712. case 0x17: // LDR Rd, expression
  1713. ++codes;
  1714. bytes[0] = c | *codes++;
  1715. bytes[1] = *codes++;
  1716. // Rd
  1717. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1718. if (has_B_code)
  1719. bytes[1] |= 0x40;
  1720. if (has_T_code)
  1721. {
  1722. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1723. }
  1724. if (has_W_code)
  1725. {
  1726. errfunc (ERR_NONFATAL, "'!' not allowed");
  1727. }
  1728. // Rn - implicit R15
  1729. bytes[1] |= 0xF;
  1730. if (ins->oprs[1].segment != segment)
  1731. {
  1732. errfunc (ERR_NONFATAL, "label not in same segment");
  1733. }
  1734. data = ins->oprs[1].offset - (offset + 8);
  1735. if (data < 0)
  1736. {
  1737. data = -data;
  1738. }
  1739. else
  1740. {
  1741. bytes[1] |= 0x80;
  1742. }
  1743. if (data >= 0x1000)
  1744. {
  1745. errfunc (ERR_NONFATAL, "too long offset");
  1746. }
  1747. bytes[2] |= ((data & 0xF00) >> 8);
  1748. bytes[3] = data & 0xFF;
  1749. break;
  1750. case 0x18: // LDR Rd, [Rn]
  1751. ++codes;
  1752. bytes[0] = c | *codes++;
  1753. bytes[1] = *codes++;
  1754. // Rd
  1755. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1756. if (has_B_code)
  1757. bytes[1] |= 0x40;
  1758. if (has_T_code)
  1759. {
  1760. bytes[1] |= 0x20; // write-back
  1761. }
  1762. else
  1763. {
  1764. bytes[0] |= 0x01; // implicit pre-index mode
  1765. }
  1766. if (has_W_code)
  1767. {
  1768. bytes[1] |= 0x20; // write-back
  1769. }
  1770. // Rn
  1771. c = regval (&ins->oprs[1],1);
  1772. bytes[1] |= c;
  1773. if (c == 0x15) // R15
  1774. data = -8;
  1775. else
  1776. data = 0;
  1777. if (data < 0)
  1778. {
  1779. data = -data;
  1780. }
  1781. else
  1782. {
  1783. bytes[1] |= 0x80;
  1784. }
  1785. bytes[2] |= ((data & 0xF00) >> 8);
  1786. bytes[3] = data & 0xFF;
  1787. break;
  1788. case 0x19: // LDR Rd, [Rn,#expression]
  1789. case 0x20: // LDR Rd, [Rn,Rm]
  1790. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1791. ++codes;
  1792. bytes[0] = c | *codes++;
  1793. bytes[1] = *codes++;
  1794. // Rd
  1795. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1796. if (has_B_code)
  1797. bytes[1] |= 0x40;
  1798. // Rn
  1799. c = regval (&ins->oprs[1],1);
  1800. bytes[1] |= c;
  1801. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1802. {
  1803. bytes[0] |= 0x01; // pre-index mode
  1804. if (has_W_code)
  1805. {
  1806. bytes[1] |= 0x20;
  1807. }
  1808. if (has_T_code)
  1809. {
  1810. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1811. }
  1812. }
  1813. else
  1814. {
  1815. if (has_T_code) // Forced write-back in post-index mode
  1816. {
  1817. bytes[1] |= 0x20;
  1818. }
  1819. if (has_W_code)
  1820. {
  1821. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1822. }
  1823. }
  1824. if (keep == 0x19)
  1825. {
  1826. data = ins->oprs[2].offset;
  1827. if (data < 0)
  1828. {
  1829. data = -data;
  1830. }
  1831. else
  1832. {
  1833. bytes[1] |= 0x80;
  1834. }
  1835. if (data >= 0x1000)
  1836. {
  1837. errfunc (ERR_NONFATAL, "too long offset");
  1838. }
  1839. bytes[2] |= ((data & 0xF00) >> 8);
  1840. bytes[3] = data & 0xFF;
  1841. }
  1842. else
  1843. {
  1844. if (ins->oprs[2].minus == 0)
  1845. {
  1846. bytes[1] |= 0x80;
  1847. }
  1848. c = regval (&ins->oprs[2],1);
  1849. bytes[3] = c;
  1850. if (keep == 0x21)
  1851. {
  1852. c = ins->oprs[3].offset;
  1853. if (c > 0x1F)
  1854. {
  1855. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1856. c = c & 0x1F;
  1857. }
  1858. bytes[2] |= c >> 1;
  1859. if (c & 0x01)
  1860. {
  1861. bytes[3] |= 0x80;
  1862. }
  1863. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1864. }
  1865. }
  1866. break;
  1867. case 0x22: // LDRH Rd, expression
  1868. ++codes;
  1869. bytes[0] = c | 0x01; // Implicit pre-index
  1870. bytes[1] = *codes++;
  1871. // Rd
  1872. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1873. // Rn - implicit R15
  1874. bytes[1] |= 0xF;
  1875. if (ins->oprs[1].segment != segment)
  1876. {
  1877. errfunc (ERR_NONFATAL, "label not in same segment");
  1878. }
  1879. data = ins->oprs[1].offset - (offset + 8);
  1880. if (data < 0)
  1881. {
  1882. data = -data;
  1883. }
  1884. else
  1885. {
  1886. bytes[1] |= 0x80;
  1887. }
  1888. if (data >= 0x100)
  1889. {
  1890. errfunc (ERR_NONFATAL, "too long offset");
  1891. }
  1892. bytes[3] = *codes++;
  1893. bytes[2] |= ((data & 0xF0) >> 4);
  1894. bytes[3] |= data & 0xF;
  1895. break;
  1896. case 0x23: // LDRH Rd, Rn
  1897. ++codes;
  1898. bytes[0] = c | 0x01; // Implicit pre-index
  1899. bytes[1] = *codes++;
  1900. // Rd
  1901. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1902. // Rn
  1903. c = regval (&ins->oprs[1],1);
  1904. bytes[1] |= c;
  1905. if (c == 0x15) // R15
  1906. data = -8;
  1907. else
  1908. data = 0;
  1909. if (data < 0)
  1910. {
  1911. data = -data;
  1912. }
  1913. else
  1914. {
  1915. bytes[1] |= 0x80;
  1916. }
  1917. if (data >= 0x100)
  1918. {
  1919. errfunc (ERR_NONFATAL, "too long offset");
  1920. }
  1921. bytes[3] = *codes++;
  1922. bytes[2] |= ((data & 0xF0) >> 4);
  1923. bytes[3] |= data & 0xF;
  1924. break;
  1925. case 0x24: // LDRH Rd, Rn, expression
  1926. case 0x25: // LDRH Rd, Rn, Rm
  1927. ++codes;
  1928. bytes[0] = c;
  1929. bytes[1] = *codes++;
  1930. // Rd
  1931. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1932. // Rn
  1933. c = regval (&ins->oprs[1],1);
  1934. bytes[1] |= c;
  1935. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1936. {
  1937. bytes[0] |= 0x01; // pre-index mode
  1938. if (has_W_code)
  1939. {
  1940. bytes[1] |= 0x20;
  1941. }
  1942. }
  1943. else
  1944. {
  1945. if (has_W_code)
  1946. {
  1947. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1948. }
  1949. }
  1950. bytes[3] = *codes++;
  1951. if (keep == 0x24)
  1952. {
  1953. data = ins->oprs[2].offset;
  1954. if (data < 0)
  1955. {
  1956. data = -data;
  1957. }
  1958. else
  1959. {
  1960. bytes[1] |= 0x80;
  1961. }
  1962. if (data >= 0x100)
  1963. {
  1964. errfunc (ERR_NONFATAL, "too long offset");
  1965. }
  1966. bytes[2] |= ((data & 0xF0) >> 4);
  1967. bytes[3] |= data & 0xF;
  1968. }
  1969. else
  1970. {
  1971. if (ins->oprs[2].minus == 0)
  1972. {
  1973. bytes[1] |= 0x80;
  1974. }
  1975. c = regval (&ins->oprs[2],1);
  1976. bytes[3] |= c;
  1977. }
  1978. break;
  1979. case 0x26: // LDM/STM Rn, {reg-list}
  1980. ++codes;
  1981. bytes[0] = c;
  1982. bytes[0] |= ( *codes >> 4) & 0xF;
  1983. bytes[1] = ( *codes << 4) & 0xF0;
  1984. ++codes;
  1985. if (has_W_code)
  1986. {
  1987. bytes[1] |= 0x20;
  1988. }
  1989. if (has_F_code)
  1990. {
  1991. bytes[1] |= 0x40;
  1992. }
  1993. // Rn
  1994. bytes[1] |= regval (&ins->oprs[0],1);
  1995. data = ins->oprs[1].basereg;
  1996. bytes[2] = ((data >> 8) & 0xFF);
  1997. bytes[3] = (data & 0xFF);
  1998. break;
  1999. case 0x27: // SWP Rd, Rm, [Rn]
  2000. ++codes;
  2001. bytes[0] = c;
  2002. bytes[0] |= *codes++;
  2003. bytes[1] = regval (&ins->oprs[2],1);
  2004. if (has_B_code)
  2005. {
  2006. bytes[1] |= 0x40;
  2007. }
  2008. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2009. bytes[3] = *codes++;
  2010. bytes[3] |= regval (&ins->oprs[1],1);
  2011. break;
  2012. default:
  2013. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2014. bytes[0] = c;
  2015. // And a fix nibble
  2016. ++codes;
  2017. bytes[0] |= *codes++;
  2018. if ( *codes == 0x01) // An I bit
  2019. {
  2020. }
  2021. if ( *codes == 0x02) // An I bit
  2022. {
  2023. }
  2024. ++codes;
  2025. }
  2026. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2027. }
  2028. *)
  2029. {$endif dummy
  2030. }