aasmcpu.pas 63 KB

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