aasmcpu.pas 85 KB

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