aasmcpu.pas 52 KB

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