aasmcpu.pas 81 KB

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