aasmcpu.pas 61 KB

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