aasmcpu.pas 85 KB

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