ra386int.pas 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Carl Eric Codere
  4. Does the parsing process for the intel styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit Ra386int;
  19. {**********************************************************************}
  20. { WARNING }
  21. {**********************************************************************}
  22. { Any modification in the order or removal of terms in the tables }
  23. { in i386.pas and intasmi3.pas will BREAK the code in this unit, }
  24. { unless the appropriate changes are made to this unit. Addition }
  25. { of terms though, will not change the code herein. }
  26. {**********************************************************************}
  27. {--------------------------------------------------------------------}
  28. { LEFT TO DO: }
  29. {--------------------------------------------------------------------}
  30. { o Add support for floating point opcodes. }
  31. { o Handle module overrides also... such as crt.white or }
  32. { crt.delay and local typed constants. }
  33. { o Handle label references }
  34. { o Add support for TP styled segment overrides, when the opcode }
  35. { table will be completed. }
  36. { o Add imul,shld and shrd support with references and CL }
  37. { i386.pas requires to be updated to do this. }
  38. { o Bugfix of ao_imm8s for IMUL. (Currently the 3 operand imul will }
  39. { be considered as invalid because I use ao_imm8 and the table }
  40. { uses ao_imm8s). }
  41. {--------------------------------------------------------------------}
  42. Interface
  43. uses
  44. tree,i386;
  45. function assemble: ptree;
  46. const
  47. { this variable is TRUE if the lookup tables have already been setup }
  48. { for fast access. On the first call to assemble the tables are setup }
  49. { and stay set up. }
  50. _asmsorted: boolean = FALSE;
  51. firstreg = R_EAX;
  52. lastreg = R_ST7;
  53. type
  54. tiasmops = array[firstop..lastop] of string[7];
  55. piasmops = ^tiasmops;
  56. var
  57. { sorted tables of opcodes }
  58. iasmops: piasmops;
  59. { uppercased tables of registers }
  60. iasmregs: array[firstreg..lastreg] of string[6];
  61. Implementation
  62. Uses
  63. systems,files,aasm,globals,AsmUtils,strings,hcodegen,scanner,
  64. cobjects,verbose,types;
  65. type
  66. tinteltoken = (
  67. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  68. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  69. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  70. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  71. {------------------ Assembler directives --------------------}
  72. AS_DB,AS_DW,AS_DD,AS_END,
  73. {------------------ Assembler Operators --------------------}
  74. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_NEAR,AS_FAR,
  75. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  76. AS_AND,AS_OR,AS_XOR);
  77. tasmkeyword = string[6];
  78. const
  79. { These tokens should be modified accordingly to the modifications }
  80. { in the different enumerations. }
  81. firstdirective = AS_DB;
  82. lastdirective = AS_END;
  83. firstoperator = AS_BYTE;
  84. lastoperator = AS_XOR;
  85. firstsreg = R_CS;
  86. lastsreg = R_SS;
  87. { this is a hack to accept all opcodes }
  88. { in the opcode table. }
  89. { check is done until A_POPFD }
  90. { otherwise no check. }
  91. lastop_in_table = A_POPFD;
  92. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  93. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  94. _count_asmprefixes = 5;
  95. _count_asmspecialops = 25;
  96. _count_asmoverrides = 3;
  97. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  98. ('DB','DW','DD','END');
  99. { problems with shl,shr,not,and,or and xor, they are }
  100. { context sensitive. }
  101. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  102. 'BYTE','WORD','DWORD','QWORD','TBYTE','NEAR','FAR','HIGH',
  103. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  104. 'OR','XOR');
  105. {------------------ Missing opcodes from std list ----------------}
  106. _asmprefixes: array[0.._count_asmprefixes] of tasmkeyword = (
  107. 'REPNE','REPE','REP','REPZ','REPNZ','LOCK');
  108. _asmoverrides: array[0.._count_asmoverrides] of tasmkeyword =
  109. ('SEGCS','SEGDS','SEGES','SEGSS');
  110. _overridetokens: array[0.._count_asmoverrides] of tregister =
  111. (R_CS,R_DS,R_ES,R_SS);
  112. _prefixtokens: array[0.._count_asmprefixes] of tasmop = (
  113. A_REPNE,A_REPE,A_REP,A_REPE,A_REPNE,A_LOCK);
  114. _specialops: array[0.._count_asmspecialops] of tasmkeyword = (
  115. 'CMPSB','CMPSW','CMPSD','INSB','INSW','INSD','OUTSB','OUTSW','OUTSD',
  116. 'SCASB','SCASW','SCASD','STOSB','STOSW','STOSD','MOVSB','MOVSW','MOVSD',
  117. 'LODSB','LODSW','LODSD','LOCK','SEGCS','SEGDS','SEGES','SEGSS');
  118. _specialopstokens: array[0.._count_asmspecialops] of tasmop = (
  119. A_CMPS,A_CMPS,A_CMPS,A_INS,A_INS,A_INS,A_OUTS,A_OUTS,A_OUTS,
  120. A_SCAS,A_SCAS,A_SCAS,A_STOS,A_STOS,A_STOS,A_MOVS,A_MOVS,A_MOVS,
  121. A_LODS,A_LODS,A_LODS,A_LOCK,A_NONE,A_NONE,A_NONE,A_NONE);
  122. {------------------------------------------------------------------}
  123. { register type definition table for easier searching }
  124. _regtypes:array[firstreg..lastreg] of longint =
  125. (ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,ao_reg32,
  126. ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,ao_reg16,
  127. ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,ao_reg8,
  128. ao_none,ao_sreg2,ao_sreg2,ao_sreg2,ao_sreg3,ao_sreg3,ao_sreg2,
  129. ao_floatacc,ao_floatacc,ao_floatreg,ao_floatreg,ao_floatreg,ao_floatreg,
  130. ao_floatreg,ao_floatreg,ao_floatreg);
  131. _regsizes: array[firstreg..lastreg] of topsize =
  132. (S_L,S_L,S_L,S_L,S_L,S_L,S_L,S_L,
  133. S_W,S_W,S_W,S_W,S_W,S_W,S_W,S_W,
  134. S_B,S_B,S_B,S_B,S_B,S_B,S_B,S_B,
  135. { segment register }
  136. S_W,S_W,S_W,S_W,S_W,S_W,S_W,
  137. { can also be S_S or S_T - must be checked at run-time }
  138. S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL,S_FL);
  139. {topsize = (S_NO,S_B,S_W,S_L,S_BW,S_BL,S_WL,
  140. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX,S_D);}
  141. _constsizes: array[S_NO..S_FS] of longint =
  142. (0,ao_imm8,ao_imm16,ao_imm32,0,0,0,ao_imm16,ao_imm32,0,ao_imm32);
  143. const
  144. newline = #10;
  145. firsttoken : boolean = TRUE;
  146. operandnum : byte = 0;
  147. var
  148. { context for SHL,SHR,AND,NOT,OR,XOR operators }
  149. { if set to true GetToken will return these }
  150. { as operators, otherwise will return these as }
  151. { opcodes. }
  152. inexpression: boolean;
  153. p : paasmoutput;
  154. actasmtoken: tinteltoken;
  155. actasmpattern: string;
  156. c: char;
  157. Instr: TInstruction;
  158. labellist: TAsmLabelList;
  159. old_exit : pointer;
  160. Procedure SetupTables;
  161. { creates uppercased symbol tables for speed access }
  162. var
  163. i: tasmop;
  164. j: tregister;
  165. Begin
  166. Message(assem_d_creating_lookup_tables);
  167. { opcodes }
  168. new(iasmops);
  169. for i:=firstop to lastop do
  170. iasmops^[i] := upper(int_op2str[i]);
  171. { opcodes }
  172. for j:=firstreg to lastreg do
  173. iasmregs[j] := upper(int_reg2str[j]);
  174. end;
  175. {---------------------------------------------------------------------}
  176. { Routines for the tokenizing }
  177. {---------------------------------------------------------------------}
  178. function is_asmopcode(const s: string):Boolean;
  179. {*********************************************************************}
  180. { FUNCTION is_asmopcode(s: string):Boolean }
  181. { Description: Determines if the s string is a valid opcode }
  182. { if so returns TRUE otherwise returns FALSE. }
  183. {*********************************************************************}
  184. var
  185. i: tasmop;
  186. j: byte;
  187. Begin
  188. is_asmopcode := FALSE;
  189. for i:=firstop to lastop do
  190. begin
  191. if s = iasmops^[i] then
  192. begin
  193. is_asmopcode:=TRUE;
  194. exit;
  195. end;
  196. end;
  197. { not found yet, search for extended opcodes }
  198. for j:=0 to _count_asmspecialops do
  199. Begin
  200. if s = _specialops[j] then
  201. Begin
  202. is_asmopcode:=TRUE;
  203. exit;
  204. end;
  205. end;
  206. end;
  207. Procedure is_asmdirective(const s: string; var token: tinteltoken);
  208. {*********************************************************************}
  209. { FUNCTION is_asmdirective(s: string; var token: tinteltoken):Boolean }
  210. { Description: Determines if the s string is a valid directive }
  211. { (an operator can occur in operand fields, while a directive cannot) }
  212. { if so returns the directive token, otherwise does not change token.}
  213. {*********************************************************************}
  214. var
  215. i:byte;
  216. Begin
  217. for i:=0 to _count_asmdirectives do
  218. begin
  219. if s=_asmdirectives[i] then
  220. begin
  221. token := tinteltoken(longint(firstdirective)+i);
  222. exit;
  223. end;
  224. end;
  225. end;
  226. Procedure is_asmoperator(const s: string; var token: tinteltoken);
  227. {*********************************************************************}
  228. { FUNCTION is_asmoperator(s: string; var token: tinteltoken): Boolean}
  229. { Description: Determines if the s string is a valid operator }
  230. { (an operator can occur in operand fields, while a directive cannot) }
  231. { if so returns the operator token, otherwise does not change token. }
  232. {*********************************************************************}
  233. var
  234. i:longint;
  235. Begin
  236. for i:=0 to _count_asmoperators do
  237. begin
  238. if s=_asmoperators[i] then
  239. begin
  240. token := tinteltoken(longint(firstoperator)+i);
  241. exit;
  242. end;
  243. end;
  244. end;
  245. Procedure is_register(const s: string; var token: tinteltoken);
  246. {*********************************************************************}
  247. { PROCEDURE is_register(s: string; var token: tinteltoken); }
  248. { Description: Determines if the s string is a valid register, if }
  249. { so return token equal to A_REGISTER, otherwise does not change token}
  250. {*********************************************************************}
  251. Var
  252. i: tregister;
  253. Begin
  254. for i:=firstreg to lastreg do
  255. begin
  256. if s=iasmregs[i] then
  257. begin
  258. token := AS_REGISTER;
  259. exit;
  260. end;
  261. end;
  262. end;
  263. Function GetToken: tinteltoken;
  264. {*********************************************************************}
  265. { FUNCTION GetToken: tinteltoken; }
  266. { Description: This routine returns intel assembler tokens and }
  267. { does some minor syntax error checking. }
  268. {*********************************************************************}
  269. var
  270. j: integer;
  271. token: tinteltoken;
  272. forcelabel: boolean;
  273. errorflag : boolean;
  274. begin
  275. errorflag := FALSE;
  276. forcelabel := FALSE;
  277. actasmpattern :='';
  278. {* INIT TOKEN TO NOTHING *}
  279. token := AS_NONE;
  280. { while space and tab , continue scan... }
  281. while (c in [' ',#9]) do
  282. c := current_scanner^.asmgetchar;
  283. { Possiblities for first token in a statement: }
  284. { Local Label, Label, Directive, Prefix or Opcode.... }
  285. if firsttoken and not (c in [newline,#13,'{',';']) then
  286. begin
  287. current_scanner^.gettokenpos;
  288. firsttoken := FALSE;
  289. if c = '@' then
  290. begin
  291. token := AS_LLABEL; { this is a local label }
  292. { Let us point to the next character }
  293. c := current_scanner^.asmgetchar;
  294. end;
  295. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  296. begin
  297. { if there is an at_sign, then this must absolutely be a label }
  298. if c = '@' then forcelabel:=TRUE;
  299. actasmpattern := actasmpattern + c;
  300. c := current_scanner^.asmgetchar;
  301. end;
  302. uppervar(actasmpattern);
  303. if c = ':' then
  304. begin
  305. case token of
  306. AS_NONE: token := AS_LABEL;
  307. AS_LLABEL: ; { do nothing }
  308. end; { end case }
  309. { let us point to the next character }
  310. c := current_scanner^.asmgetchar;
  311. gettoken := token;
  312. exit;
  313. end;
  314. { Are we trying to create an identifier with }
  315. { an at-sign...? }
  316. if forcelabel then
  317. Message(assem_e_none_label_contain_at);
  318. If is_asmopcode(actasmpattern) then
  319. Begin
  320. gettoken := AS_OPCODE;
  321. { check if we are in an expression }
  322. { then continue with asm directives }
  323. if not inexpression then
  324. exit;
  325. end;
  326. is_asmdirective(actasmpattern, token);
  327. if (token <> AS_NONE) then
  328. Begin
  329. gettoken := token;
  330. exit
  331. end
  332. else
  333. begin
  334. gettoken := AS_NONE;
  335. Message1(assem_e_invalid_operand,actasmpattern);
  336. end;
  337. end
  338. else { else firsttoken }
  339. { Here we must handle all possible cases }
  340. begin
  341. case c of
  342. '@': { possiblities : - local label reference , such as in jmp @local1 }
  343. { - @Result, @Code or @Data special variables. }
  344. begin
  345. actasmpattern := c;
  346. c:= current_scanner^.asmgetchar;
  347. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  348. begin
  349. actasmpattern := actasmpattern + c;
  350. c := current_scanner^.asmgetchar;
  351. end;
  352. uppervar(actasmpattern);
  353. gettoken := AS_ID;
  354. exit;
  355. end;
  356. { identifier, register, opcode, prefix or directive }
  357. 'A'..'Z','a'..'z','_': begin
  358. actasmpattern := c;
  359. c:= current_scanner^.asmgetchar;
  360. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  361. begin
  362. actasmpattern := actasmpattern + c;
  363. c := current_scanner^.asmgetchar;
  364. end;
  365. uppervar(actasmpattern);
  366. If is_asmopcode(actasmpattern) then
  367. Begin
  368. gettoken := AS_OPCODE;
  369. { if we are not in a constant }
  370. { expression than this is an }
  371. { opcode. }
  372. if not inexpression then
  373. exit;
  374. end;
  375. is_register(actasmpattern, token);
  376. is_asmoperator(actasmpattern,token);
  377. is_asmdirective(actasmpattern,token);
  378. { if found }
  379. if (token <> AS_NONE) then
  380. begin
  381. gettoken := token;
  382. exit;
  383. end
  384. { this is surely an identifier }
  385. else
  386. token := AS_ID;
  387. gettoken := token;
  388. exit;
  389. end;
  390. { override operator... not supported }
  391. '&': begin
  392. Message(assem_w_override_op_not_supported);
  393. c:=current_scanner^.asmgetchar;
  394. gettoken := AS_NONE;
  395. end;
  396. { string or character }
  397. '''' :
  398. begin
  399. actasmpattern:='';
  400. while true do
  401. begin
  402. if c = '''' then
  403. begin
  404. c:=current_scanner^.asmgetchar;
  405. if c=newline then
  406. begin
  407. Message(scan_f_string_exceeds_line);
  408. break;
  409. end;
  410. repeat
  411. if c=''''then
  412. begin
  413. c:=current_scanner^.asmgetchar;
  414. if c='''' then
  415. begin
  416. actasmpattern:=actasmpattern+'''';
  417. c:=current_scanner^.asmgetchar;
  418. if c=newline then
  419. begin
  420. Message(scan_f_string_exceeds_line);
  421. break;
  422. end;
  423. end
  424. else break;
  425. end
  426. else
  427. begin
  428. actasmpattern:=actasmpattern+c;
  429. c:=current_scanner^.asmgetchar;
  430. if c=newline then
  431. begin
  432. Message(scan_f_string_exceeds_line);
  433. break
  434. end;
  435. end;
  436. until false; { end repeat }
  437. end
  438. else break; { end if }
  439. end; { end while }
  440. token:=AS_STRING;
  441. gettoken := token;
  442. exit;
  443. end;
  444. { string or character }
  445. '"' :
  446. begin
  447. actasmpattern:='';
  448. while true do
  449. begin
  450. if c = '"' then
  451. begin
  452. c:=current_scanner^.asmgetchar;
  453. if c=newline then
  454. begin
  455. Message(scan_f_string_exceeds_line);
  456. break;
  457. end;
  458. repeat
  459. if c='"'then
  460. begin
  461. c:=current_scanner^.asmgetchar;
  462. if c='"' then
  463. begin
  464. actasmpattern:=actasmpattern+'"';
  465. c:=current_scanner^.asmgetchar;
  466. if c=newline then
  467. begin
  468. Message(scan_f_string_exceeds_line);
  469. break;
  470. end;
  471. end
  472. else break;
  473. end
  474. else
  475. begin
  476. actasmpattern:=actasmpattern+c;
  477. c:=current_scanner^.asmgetchar;
  478. if c=newline then
  479. begin
  480. Message(scan_f_string_exceeds_line);
  481. break
  482. end;
  483. end;
  484. until false; { end repeat }
  485. end
  486. else break; { end if }
  487. end; { end while }
  488. token := AS_STRING;
  489. gettoken := token;
  490. exit;
  491. end;
  492. '$' : begin
  493. c:=current_scanner^.asmgetchar;
  494. while c in ['0'..'9','A'..'F','a'..'f'] do
  495. begin
  496. actasmpattern := actasmpattern + c;
  497. c := current_scanner^.asmgetchar;
  498. end;
  499. gettoken := AS_HEXNUM;
  500. exit;
  501. end;
  502. ',' : begin
  503. gettoken := AS_COMMA;
  504. c:=current_scanner^.asmgetchar;
  505. exit;
  506. end;
  507. '[' : begin
  508. gettoken := AS_LBRACKET;
  509. c:=current_scanner^.asmgetchar;
  510. exit;
  511. end;
  512. ']' : begin
  513. gettoken := AS_RBRACKET;
  514. c:=current_scanner^.asmgetchar;
  515. exit;
  516. end;
  517. '(' : begin
  518. gettoken := AS_LPAREN;
  519. c:=current_scanner^.asmgetchar;
  520. exit;
  521. end;
  522. ')' : begin
  523. gettoken := AS_RPAREN;
  524. c:=current_scanner^.asmgetchar;
  525. exit;
  526. end;
  527. ':' : begin
  528. gettoken := AS_COLON;
  529. c:=current_scanner^.asmgetchar;
  530. exit;
  531. end;
  532. '.' : begin
  533. gettoken := AS_DOT;
  534. c:=current_scanner^.asmgetchar;
  535. exit;
  536. end;
  537. '+' : begin
  538. gettoken := AS_PLUS;
  539. c:=current_scanner^.asmgetchar;
  540. exit;
  541. end;
  542. '-' : begin
  543. gettoken := AS_MINUS;
  544. c:=current_scanner^.asmgetchar;
  545. exit;
  546. end;
  547. '*' : begin
  548. gettoken := AS_STAR;
  549. c:=current_scanner^.asmgetchar;
  550. exit;
  551. end;
  552. '/' : begin
  553. gettoken := AS_SLASH;
  554. c:=current_scanner^.asmgetchar;
  555. exit;
  556. end;
  557. '0'..'9': begin
  558. { this flag indicates if there was an error }
  559. { if so, then we use a default value instead.}
  560. errorflag := false;
  561. actasmpattern := c;
  562. c := current_scanner^.asmgetchar;
  563. { Get the possible characters }
  564. while c in ['0'..'9','A'..'F','a'..'f'] do
  565. begin
  566. actasmpattern := actasmpattern + c;
  567. c:= current_scanner^.asmgetchar;
  568. end;
  569. { Get ending character }
  570. uppervar(actasmpattern);
  571. c:=upcase(c);
  572. { possibly a binary number. }
  573. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  574. Begin
  575. { Delete the last binary specifier }
  576. delete(actasmpattern,length(actasmpattern),1);
  577. for j:=1 to length(actasmpattern) do
  578. if not (actasmpattern[j] in ['0','1']) then
  579. begin
  580. Message1(assem_e_error_in_binary_const,actasmpattern);
  581. errorflag := TRUE;
  582. end;
  583. { if error, then suppose a binary value of zero. }
  584. if errorflag then
  585. actasmpattern := '0';
  586. gettoken := AS_BINNUM;
  587. exit;
  588. end
  589. else
  590. Begin
  591. case c of
  592. 'O': Begin
  593. for j:=1 to length(actasmpattern) do
  594. if not (actasmpattern[j] in ['0'..'7']) then
  595. begin
  596. Message1(assem_e_error_in_octal_const,actasmpattern);
  597. errorflag := TRUE;
  598. end;
  599. { if error, then suppose an octal value of zero. }
  600. if errorflag then
  601. actasmpattern := '0';
  602. gettoken := AS_OCTALNUM;
  603. c := current_scanner^.asmgetchar;
  604. exit;
  605. end;
  606. 'H': Begin
  607. for j:=1 to length(actasmpattern) do
  608. if not (actasmpattern[j] in ['0'..'9','A'..'F']) then
  609. begin
  610. Message1(assem_e_error_in_hex_const,actasmpattern);
  611. errorflag := TRUE;
  612. end;
  613. { if error, then suppose an hex value of zero. }
  614. if errorflag then
  615. actasmpattern := '0';
  616. gettoken := AS_HEXNUM;
  617. c := current_scanner^.asmgetchar;
  618. exit;
  619. end;
  620. else { must be an integer number }
  621. begin
  622. for j:=1 to length(actasmpattern) do
  623. if not (actasmpattern[j] in ['0'..'9']) then
  624. begin
  625. Message1(assem_e_error_in_integer_const,actasmpattern);
  626. errorflag := TRUE;
  627. end;
  628. { if error, then suppose an int value of zero. }
  629. if errorflag then
  630. actasmpattern := '0';
  631. gettoken := AS_INTNUM;
  632. exit;
  633. end;
  634. end; { end case }
  635. end; { end if }
  636. end;
  637. ';','{',#13,newline : begin
  638. c:=current_scanner^.asmgetchar;
  639. firsttoken := TRUE;
  640. gettoken:=AS_SEPARATOR;
  641. end;
  642. else
  643. Begin
  644. Message(scan_f_illegal_char);
  645. end;
  646. end; { end case }
  647. end; { end else if }
  648. end;
  649. {---------------------------------------------------------------------}
  650. { Routines for the output }
  651. {---------------------------------------------------------------------}
  652. { returns an appropriate ao_xxxx flag indicating the type }
  653. { of operand. }
  654. function findtype(Var Opr: TOperand): longint;
  655. Begin
  656. With Opr do
  657. Begin
  658. case operandtype of
  659. OPR_REFERENCE: Begin
  660. if assigned(ref.symbol) then
  661. { check if in local label list }
  662. { if so then it is considered }
  663. { as a displacement. }
  664. Begin
  665. if labellist.search(ref.symbol^) <> nil then
  666. findtype := ao_disp
  667. else
  668. findtype := ao_mem; { probably a mem ref. }
  669. end
  670. else
  671. findtype := ao_mem;
  672. end;
  673. OPR_CONSTANT: Begin
  674. { check if there is not already a default size }
  675. if opr.size <> S_NO then
  676. Begin
  677. findtype := _constsizes[opr.size];
  678. exit;
  679. end;
  680. if val < $ff then
  681. Begin
  682. findtype := ao_imm8;
  683. opr.size := S_B;
  684. end
  685. else if val < $ffff then
  686. Begin
  687. findtype := ao_imm16;
  688. opr.size := S_W;
  689. end
  690. else
  691. Begin
  692. findtype := ao_imm32;
  693. opr.size := S_L;
  694. end
  695. end;
  696. OPR_REGISTER: Begin
  697. findtype := _regtypes[reg];
  698. exit;
  699. end;
  700. OPR_SYMBOL: Begin
  701. findtype := ao_jumpabsolute;
  702. end;
  703. OPR_NONE: Begin
  704. findtype := 0;
  705. end;
  706. else
  707. Begin
  708. Message(assem_f_internal_error_in_findtype);
  709. end;
  710. end;
  711. end;
  712. end;
  713. Procedure ConcatLabeledInstr(var instr: TInstruction);
  714. Begin
  715. if (instr.getinstruction in [A_JO,A_JNO,A_JB,A_JC,A_JNAE,
  716. A_JNB,A_JNC,A_JAE,A_JE,A_JZ,A_JNE,A_JNZ,A_JBE,A_JNA,A_JNBE,
  717. A_JA,A_JS,A_JNS,A_JP,A_JPE,A_JNP,A_JPO,A_JL,A_JNGE,A_JNL,A_JGE,
  718. A_JLE,A_JNG,A_JNLE,A_JG,A_JCXZ,A_JECXZ,A_LOOP,A_LOOPZ,A_LOOPE,
  719. A_LOOPNZ,A_LOOPNE,A_MOV,A_JMP,A_CALL]) then
  720. Begin
  721. if instr.numops > 1 then
  722. Message(assem_e_invalid_labeled_opcode)
  723. else if instr.operands[1].operandtype <> OPR_LABINSTR then
  724. Message(assem_e_invalid_labeled_opcode)
  725. else if (instr.operands[1].operandtype = OPR_LABINSTR) and
  726. (instr.numops = 1) then
  727. if assigned(instr.operands[1].hl) then
  728. ConcatLabel(p,instr.getinstruction, instr.operands[1].hl)
  729. else
  730. Message(assem_f_internal_error_in_findtype);
  731. end
  732. else if instr.getinstruction = A_MOV then
  733. Begin
  734. { MOV to rel8 }
  735. end
  736. else
  737. Message(assem_e_invalid_operand);
  738. end;
  739. Procedure HandleExtend(var instr: TInstruction);
  740. { Handles MOVZX, MOVSX ... }
  741. var
  742. instruc: tasmop;
  743. opsize: topsize;
  744. Begin
  745. instruc:=instr.getinstruction;
  746. { return the old types ..}
  747. { these tokens still point to valid intel strings, }
  748. { but we must convert them to TRUE intel tokens }
  749. if instruc in [A_MOVSB,A_MOVSBL,A_MOVSBW,A_MOVSWL] then
  750. instruc := A_MOVSX;
  751. if instruc in [A_MOVZB,A_MOVZWL] then
  752. instruc := A_MOVZX;
  753. With instr do
  754. Begin
  755. if operands[1].size = S_B then
  756. Begin
  757. if operands[2].size = S_L then
  758. opsize := S_BL
  759. else
  760. if operands[2].size = S_W then
  761. opsize := S_BW
  762. else
  763. begin
  764. Message(assem_e_invalid_size_movzx);
  765. exit;
  766. end;
  767. end
  768. else
  769. if operands[1].size = S_W then
  770. Begin
  771. if operands[2].size = S_L then
  772. opsize := S_WL
  773. else
  774. begin
  775. Message(assem_e_invalid_size_movzx);
  776. exit;
  777. end;
  778. end
  779. else
  780. begin
  781. Message(assem_e_invalid_size_movzx);
  782. exit;
  783. end;
  784. if operands[1].operandtype = OPR_REGISTER then
  785. Begin
  786. if operands[2].operandtype <> OPR_REGISTER then
  787. Message(assem_e_invalid_opcode)
  788. else
  789. p^.concat(new(pai386,op_reg_reg(instruc,opsize,
  790. operands[1].reg,operands[2].reg)));
  791. end
  792. else
  793. if operands[1].operandtype = OPR_REFERENCE then
  794. Begin
  795. if operands[2].operandtype <> OPR_REGISTER then
  796. Message(assem_e_invalid_opcode)
  797. else
  798. p^.concat(new(pai386,op_ref_reg(instruc,opsize,
  799. newreference(operands[1].ref),operands[2].reg)));
  800. end
  801. end; { end with }
  802. end;
  803. Procedure ConcatOpCode(var instr: TInstruction);
  804. {*********************************************************************}
  805. { First Pass: }
  806. { if instr = Lxxx with a 16bit offset, we emit an error. }
  807. { If the instruction is INS,IN,OUT,OUTS,RCL,ROL,RCR,ROR, }
  808. { SAL,SAR,SHL,SHR,SHLD,SHRD,DIV,IDIV,BT,BTC,BTR,BTS,INT, }
  809. { RET,ENTER,SCAS,CMPS,STOS,LODS,FNSTSW,FSTSW. }
  810. { set up the optypes variables manually, as well as setting }
  811. { operand sizes. }
  812. { Second pass: }
  813. { Check if the combination of opcodes and operands are valid, using }
  814. { the opcode table. }
  815. { Third pass: }
  816. { If there was no error on the 2nd pass , then we check the }
  817. { following: }
  818. { - If this is a 0 operand opcode }
  819. { we verify if it is a string opcode, if so we emit a size also}
  820. { otherwise simply emit the opcode by itself. }
  821. { - If this is a 1 operand opcode, and it is a reference, we make }
  822. { sure that the operand size is valid; we emit the opcode. }
  823. { - If this is a two operand opcode }
  824. { o if the opcode is MOVSX or MOVZX then we handle it specially }
  825. { o we check the operand types (most important combinations): }
  826. { if reg,reg we make sure that both registers are of the }
  827. { same size. }
  828. { if reg,ref or ref,reg we check if the symbol name is }
  829. { assigned, if so a size must be specified and compared }
  830. { to the register size, both must be equal. If there is }
  831. { no symbol name, then we check : }
  832. { if refsize = NO_SIZE then OPCODE_SIZE = regsize }
  833. { else if refsize = regsize then OPCODE_SIZE = regsize}
  834. { else error. }
  835. { if no_error emit the opcode. }
  836. { if ref,const or const,ref if ref does not have any size }
  837. { then error, otherwise emit the opcode. }
  838. { - If this is a three operand opcode: }
  839. { imul,shld,and shrd -> check them manually. }
  840. {*********************************************************************}
  841. var
  842. fits : boolean;
  843. i: longint;
  844. opsize: topsize;
  845. optyp1, optyp2, optyp3: longint;
  846. instruc: tasmop;
  847. Begin
  848. fits := FALSE;
  849. for i:=1 to instr.numops do
  850. Begin
  851. case instr.operands[i].operandtype of
  852. OPR_REGISTER: instr.operands[i].size :=
  853. _regsizes[instr.operands[i].reg];
  854. end; { end case }
  855. end; { endif }
  856. { setup specific instructions for first pass }
  857. instruc := instr.getinstruction;
  858. if (instruc in [A_LEA,A_LDS,A_LSS,A_LES,A_LFS,A_LGS]) then
  859. Begin
  860. if instr.operands[1].size <> S_L then
  861. Begin
  862. Message(assem_e_16bit_base_in_32bit_segment);
  863. exit;
  864. end; { endif }
  865. { In this case the size of the reference is not taken into account! }
  866. instr.operands[2].size := S_NO;
  867. end;
  868. With instr do
  869. Begin
  870. for i:=1 to numops do
  871. Begin
  872. With operands[i] do
  873. Begin
  874. { check for 16-bit bases/indexes and emit an error. }
  875. { we cannot only emit a warning since gas does not }
  876. { accept 16-bit indexes and bases. }
  877. if (operandtype = OPR_REFERENCE) and
  878. ((ref.base <> R_NO) or
  879. (ref.index <> R_NO)) then
  880. Begin
  881. { index or base defined. }
  882. if (ref.base <> R_NO) then
  883. Begin
  884. if not (ref.base in
  885. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  886. Message(assem_e_16bit_base_in_32bit_segment);
  887. end;
  888. { index or base defined. }
  889. if (ref.index <> R_NO) then
  890. Begin
  891. if not (ref.index in
  892. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  893. Message(assem_e_16bit_index_in_32bit_segment);
  894. end;
  895. end;
  896. { Check for constants without bases/indexes in memory }
  897. { references. }
  898. if (operandtype = OPR_REFERENCE) and
  899. (ref.base = R_NO) and
  900. (ref.index = R_NO) and
  901. (ref.symbol = nil) and
  902. (ref.offset <> 0) then
  903. Begin
  904. ref.isintvalue := TRUE;
  905. Message(assem_e_const_ref_not_allowed);
  906. end;
  907. opinfo := findtype(operands[i]);
  908. end; { end with }
  909. end; {endfor}
  910. { TAKE CARE OF SPECIAL OPCODES, TAKE CARE OF THEM INDIVUALLY. }
  911. { ALL THE REST ARE TAKEN CARE BY OPCODE TABLE AND THIRD PASS. }
  912. if instruc = A_FST then
  913. Begin
  914. end
  915. else
  916. if instruc = A_FILD then
  917. Begin
  918. end
  919. else
  920. if instruc = A_FLD then
  921. Begin
  922. {A_FLDS,A_FLDL,A_FLDT}
  923. end
  924. else
  925. if instruc = A_FIST then
  926. Begin
  927. {A_FISTQ,A_FISTS,A_FISTL}
  928. end
  929. else
  930. if instruc = A_FWAIT then
  931. FWaitWarning
  932. else
  933. if instruc = A_MOVSX then
  934. Begin
  935. { change the instruction to conform to GAS }
  936. if operands[1].size = S_W then
  937. Begin
  938. addinstr(A_MOVSBW)
  939. end
  940. else
  941. if operands[1].size = S_L then
  942. Begin
  943. if operands[2].size = S_B then
  944. addinstr(A_MOVSBL)
  945. else
  946. addinstr(A_MOVSWL);
  947. end;
  948. instruc := getinstruction; { reload instruction }
  949. end
  950. else
  951. if instruc = A_MOVZX then
  952. Begin
  953. { change the instruction to conform to GAS }
  954. if operands[1].size = S_W then
  955. Begin
  956. addinstr(A_MOVZB)
  957. end
  958. else
  959. if operands[1].size = S_L then
  960. Begin
  961. if operands[2].size = S_B then
  962. addinstr(A_MOVZB)
  963. else
  964. addinstr(A_MOVZWL);
  965. end;
  966. instruc := getinstruction; { reload instruction }
  967. end
  968. else
  969. if (instruc in [A_BT,A_BTC,A_BTR,A_BTS]) then
  970. Begin
  971. if numops = 2 then
  972. Begin
  973. if (operands[2].operandtype = OPR_CONSTANT)
  974. and (operands[2].val <= $ff) then
  975. Begin
  976. operands[2].opinfo := ao_imm8;
  977. { no operand size if using constant. }
  978. operands[2].size := S_NO;
  979. fits := TRUE;
  980. end
  981. end
  982. else
  983. Begin
  984. Message(assem_e_invalid_opcode_and_operand);
  985. exit;
  986. end;
  987. end
  988. else
  989. if instruc = A_ENTER then
  990. Begin
  991. if numops =2 then
  992. Begin
  993. if (operands[1].operandtype = OPR_CONSTANT) and
  994. (operands[1].val <= $ffff) then
  995. Begin
  996. operands[1].opinfo := ao_imm16;
  997. end { endif }
  998. end { endif }
  999. else
  1000. Begin
  1001. Message(assem_e_invalid_opcode_and_operand);
  1002. exit;
  1003. end
  1004. end { endif }
  1005. else
  1006. { Handle special opcodes for the opcode }
  1007. { table. Set them up correctly. }
  1008. if (instruc in [A_IN,A_INS]) then
  1009. Begin
  1010. if numops =2 then
  1011. Begin
  1012. if (operands[2].operandtype = OPR_REGISTER) and (operands[2].reg = R_DX)
  1013. then
  1014. Begin
  1015. operands[2].opinfo := ao_inoutportreg;
  1016. if (operands[1].operandtype = OPR_REGISTER) and
  1017. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1018. (instruc = A_IN) then
  1019. Begin
  1020. operands[1].opinfo := ao_acc;
  1021. end
  1022. end
  1023. else
  1024. if (operands[2].operandtype = OPR_CONSTANT) and (operands[2].val <= $ff)
  1025. and (instruc = A_IN) then
  1026. Begin
  1027. operands[2].opinfo := ao_imm8;
  1028. operands[2].size := S_B;
  1029. if (operands[1].operandtype = OPR_REGISTER) and
  1030. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1031. (instruc = A_IN) then
  1032. Begin
  1033. operands[1].opinfo := ao_acc;
  1034. end
  1035. end;
  1036. end
  1037. else
  1038. if not ((numops=0) and (instruc=A_INS)) then
  1039. Begin
  1040. Message(assem_e_invalid_opcode_and_operand);
  1041. exit;
  1042. end;
  1043. end
  1044. else
  1045. if (instruc in [A_OUT,A_OUTS]) then
  1046. Begin
  1047. if numops =2 then
  1048. Begin
  1049. if (operands[1].operandtype = OPR_REGISTER) and (operands[1].reg = R_DX)
  1050. then
  1051. Begin
  1052. operands[1].opinfo := ao_inoutportreg;
  1053. if (operands[2].operandtype = OPR_REGISTER) and
  1054. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1055. (instruc = A_OUT) then
  1056. Begin
  1057. operands[2].opinfo := ao_acc;
  1058. fits := TRUE;
  1059. end
  1060. end
  1061. else
  1062. if (operands[1].operandtype = OPR_CONSTANT) and (operands[1].val <= $ff)
  1063. and (instruc = A_OUT) then
  1064. Begin
  1065. operands[1].opinfo := ao_imm8;
  1066. operands[1].size := S_B;
  1067. if (operands[2].operandtype = OPR_REGISTER) and
  1068. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1069. (instruc = A_OUT) then
  1070. Begin
  1071. operands[2].opinfo := ao_acc;
  1072. fits := TRUE;
  1073. end
  1074. end;
  1075. end
  1076. else
  1077. if not ((numops=0) and (instruc=A_OUTS)) then
  1078. Begin
  1079. Message(assem_e_invalid_opcode_and_operand);
  1080. exit;
  1081. end;
  1082. end
  1083. else
  1084. if instruc in [A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHL,A_SHR] then
  1085. { if RCL,ROL,... }
  1086. Begin
  1087. if numops =2 then
  1088. Begin
  1089. if (operands[2].operandtype = OPR_REGISTER) and (operands[2].reg = R_CL)
  1090. then
  1091. Begin
  1092. operands[2].opinfo := ao_shiftcount
  1093. end
  1094. else
  1095. if (operands[2].operandtype = OPR_CONSTANT) and
  1096. (operands[2].val <= $ff) then
  1097. Begin
  1098. operands[2].opinfo := ao_imm8;
  1099. operands[2].size := S_B;
  1100. end;
  1101. end
  1102. else { if numops = 2 }
  1103. Begin
  1104. Message(assem_e_invalid_opcode_and_operand);
  1105. exit;
  1106. end;
  1107. end
  1108. { endif ROL,RCL ... }
  1109. else
  1110. if instruc in [A_DIV, A_IDIV] then
  1111. Begin
  1112. if (operands[1].operandtype = OPR_REGISTER) and
  1113. (operands[1].reg in [R_AL,R_AX,R_EAX]) then
  1114. operands[1].opinfo := ao_acc;
  1115. end
  1116. else
  1117. if (instruc = A_FNSTSW) or (instruc = A_FSTSW) then
  1118. Begin
  1119. if numops = 1 then
  1120. Begin
  1121. if (operands[1].operandtype = OPR_REGISTER) and
  1122. (operands[1].reg = R_AX) then
  1123. operands[1].opinfo := ao_acc;
  1124. end
  1125. else
  1126. Begin
  1127. Message(assem_e_invalid_opcode_and_operand);
  1128. exit;
  1129. end;
  1130. end
  1131. else
  1132. if (instruc = A_SHLD) or (instruc = A_SHRD) then
  1133. { these instruction are fully parsed individually on pass three }
  1134. { so we just do a summary checking here. }
  1135. Begin
  1136. if numops = 3 then
  1137. Begin
  1138. if (operands[3].operandtype = OPR_CONSTANT)
  1139. and (operands[3].val <= $ff) then
  1140. Begin
  1141. operands[3].opinfo := ao_imm8;
  1142. operands[3].size := S_B;
  1143. end;
  1144. end
  1145. else
  1146. Begin
  1147. Message(assem_e_invalid_opcode_and_operand);
  1148. exit;
  1149. end;
  1150. end
  1151. else
  1152. if instruc = A_INT then
  1153. Begin
  1154. if numops = 1 then
  1155. Begin
  1156. if (operands[1].operandtype = OPR_CONSTANT) and
  1157. (operands[1].val <= $ff) then
  1158. operands[1].opinfo := ao_imm8;
  1159. end
  1160. end
  1161. else
  1162. if instruc = A_RET then
  1163. Begin
  1164. if numops =1 then
  1165. Begin
  1166. if (operands[1].operandtype = OPR_CONSTANT) and
  1167. (operands[1].val <= $ffff) then
  1168. operands[1].opinfo := ao_imm16;
  1169. end
  1170. end; { endif }
  1171. { all string instructions have default memory }
  1172. { location which are ignored. Take care of }
  1173. { those. }
  1174. { Here could be added the code for segment }
  1175. { overrides. }
  1176. if instruc in [A_SCAS,A_CMPS,A_STOS,A_LODS] then
  1177. Begin
  1178. if numops =1 then
  1179. Begin
  1180. if (operands[1].operandtype = OPR_REFERENCE) and
  1181. (assigned(operands[1].ref.symbol)) then
  1182. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1183. operands[1].operandtype := OPR_NONE;
  1184. numops := 0;
  1185. end;
  1186. end; { endif }
  1187. if instruc in [A_INS,A_MOVS,A_OUTS] then
  1188. Begin
  1189. if numops =2 then
  1190. Begin
  1191. if (operands[1].operandtype = OPR_REFERENCE) and
  1192. (assigned(operands[1].ref.symbol)) then
  1193. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1194. if (operands[2].operandtype = OPR_REFERENCE) and
  1195. (assigned(operands[2].ref.symbol)) then
  1196. Freemem(operands[2].ref.symbol,length(operands[1].ref.symbol^)+1);
  1197. operands[1].operandtype := OPR_NONE;
  1198. operands[2].operandtype := OPR_NONE;
  1199. numops := 0;
  1200. end;
  1201. end;
  1202. { handle parameter for segment overrides }
  1203. if instruc = A_XLAT then
  1204. Begin
  1205. { handle special TP syntax case for XLAT }
  1206. { here we accept XLAT, XLATB and XLAT m8 }
  1207. if (numops = 1) or (numops = 0) then
  1208. Begin
  1209. if (operands[1].operandtype = OPR_REFERENCE) and
  1210. (assigned(operands[1].ref.symbol)) then
  1211. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1212. operands[1].operandtype := OPR_NONE;
  1213. numops := 0;
  1214. { always a byte for XLAT }
  1215. instr.stropsize := S_B;
  1216. end;
  1217. end;
  1218. { swap the destination and source }
  1219. { to put in AT&T style direction }
  1220. { only if there are 2/3 operand }
  1221. { numbers. }
  1222. if (instruc <> A_ENTER) then
  1223. SwapOperands(instr);
  1224. { copy them to local variables }
  1225. { for faster access }
  1226. optyp1:=operands[1].opinfo;
  1227. optyp2:=operands[2].opinfo;
  1228. optyp3:=operands[3].opinfo;
  1229. end; { end with }
  1230. { after reading the operands }
  1231. { search the instruction }
  1232. { setup startvalue from cache }
  1233. if ins_cache[instruc]<>-1 then
  1234. i:=ins_cache[instruc]
  1235. else i:=0;
  1236. { this makes cpu.pp uncompilable, but i think this code should be }
  1237. { inserted in the system unit anyways. }
  1238. if (instruc >= lastop_in_table) then
  1239. { ((cs_compilesystem in aktswitches) or (aktoptprocessor > systems.i386)) then }
  1240. begin
  1241. Message(assem_w_opcode_not_in_table);
  1242. fits:=true;
  1243. end
  1244. else while not(fits) do
  1245. begin
  1246. { set the instruction cache, if the instruction }
  1247. { occurs the first time }
  1248. if (it[i].i=instruc) and (ins_cache[instruc]=-1) then
  1249. ins_cache[instruc]:=i;
  1250. if (it[i].i=instruc) and (instr.numops=it[i].ops) then
  1251. begin
  1252. { first fit }
  1253. case instr.numops of
  1254. 0 : begin
  1255. fits:=true;
  1256. break;
  1257. end;
  1258. 1 :
  1259. Begin
  1260. if (optyp1 and it[i].o1)<>0 then
  1261. Begin
  1262. fits:=true;
  1263. break;
  1264. end;
  1265. { I consider sign-extended 8bit value to }
  1266. { be equal to immediate 8bit therefore }
  1267. { convert... }
  1268. if (optyp1 = ao_imm8) then
  1269. Begin
  1270. { check if this is a simple sign extend. }
  1271. if (it[i].o1<>ao_imm8s) then
  1272. Begin
  1273. fits:=true;
  1274. break;
  1275. end;
  1276. end;
  1277. end;
  1278. 2 : if ((optyp1 and it[i].o1)<>0) and
  1279. ((optyp2 and it[i].o2)<>0) then
  1280. Begin
  1281. fits:=true;
  1282. break;
  1283. end
  1284. { if the operands can be swaped }
  1285. { then swap them }
  1286. else if ((it[i].m and af_d)<>0) and
  1287. ((optyp1 and it[i].o2)<>0) and
  1288. ((optyp2 and it[i].o1)<>0) then
  1289. begin
  1290. { swap the destination and source }
  1291. { to put in AT&T style direction }
  1292. { What does this mean !!!! ???????????????????????? }
  1293. { if (output_format in [of_o,of_att]) then }
  1294. { ???????????? }
  1295. { SwapOperands(instr); }
  1296. fits:=true;
  1297. break;
  1298. end;
  1299. 3 : if ((optyp1 and it[i].o1)<>0) and
  1300. ((optyp2 and it[i].o2)<>0) and
  1301. ((optyp3 and it[i].o3)<>0) then
  1302. Begin
  1303. fits:=true;
  1304. break;
  1305. end;
  1306. end; { end case }
  1307. end; { endif }
  1308. if it[i].i=A_NONE then
  1309. begin
  1310. { NO MATCH! }
  1311. Message(assem_e_invalid_opcode_and_operand);
  1312. exit;
  1313. end;
  1314. inc(i);
  1315. end; { end while }
  1316. { We add the opcode to the opcode linked list }
  1317. if fits then
  1318. Begin
  1319. if instr.getprefix <> A_NONE then
  1320. Begin
  1321. p^.concat(new(pai386,op_none(instr.getprefix,S_NO)));
  1322. end;
  1323. case instr.numops of
  1324. 0:
  1325. if instr.stropsize <> S_NO then
  1326. { is this a string operation opcode or xlat then check }
  1327. { the size of the operation. }
  1328. p^.concat(new(pai386,op_none(instruc,instr.stropsize)))
  1329. else
  1330. p^.concat(new(pai386,op_none(instruc,S_NO)));
  1331. 1: Begin
  1332. case instr.operands[1].operandtype of
  1333. { all one operand opcodes with constant have no defined sizes }
  1334. { at least that is what it seems in the tasm 2.0 manual. }
  1335. OPR_CONSTANT: p^.concat(new(pai386,op_const(instruc,
  1336. S_NO, instr.operands[1].val)));
  1337. OPR_REGISTER: if instruc in [A_INC,A_DEC, A_NEG,A_NOT] then
  1338. Begin
  1339. p^.concat(new(pai386,op_reg(instruc,
  1340. instr.operands[1].size,instr.operands[1].reg)));
  1341. end
  1342. else
  1343. p^.concat(new(pai386,op_reg(instruc,
  1344. S_NO,instr.operands[1].reg)));
  1345. { this is where it gets a bit more complicated... }
  1346. OPR_REFERENCE:
  1347. if instr.operands[1].size <> S_NO then
  1348. Begin
  1349. p^.concat(new(pai386,op_ref(instruc,
  1350. instr.operands[1].size,newreference(instr.operands[1].ref))));
  1351. end
  1352. else
  1353. Begin
  1354. { special jmp and call case with }
  1355. { symbolic references. }
  1356. if instruc in [A_CALL,A_JMP] then
  1357. Begin
  1358. p^.concat(new(pai386,op_ref(instruc,
  1359. S_NO,newreference(instr.operands[1].ref))));
  1360. end
  1361. else
  1362. Message(assem_e_invalid_opcode_and_operand);
  1363. end;
  1364. OPR_SYMBOL: Begin
  1365. p^.concat(new(pai386,op_csymbol(instruc,
  1366. instr.stropsize, newcsymbol(instr.operands[1].symbol^,0))));
  1367. End;
  1368. OPR_NONE: Begin
  1369. Message(assem_f_internal_error_in_concatopcode);
  1370. end;
  1371. else
  1372. Begin
  1373. Message(assem_f_internal_error_in_concatopcode);
  1374. end;
  1375. end;
  1376. end;
  1377. 2:
  1378. Begin
  1379. if instruc in [A_MOVSX,A_MOVZX,A_MOVSB,A_MOVSBL,A_MOVSBW,
  1380. A_MOVSWL,A_MOVZB,A_MOVZWL] then
  1381. { movzx and movsx }
  1382. HandleExtend(instr)
  1383. else
  1384. { other instructions }
  1385. Begin
  1386. With instr do
  1387. Begin
  1388. { source }
  1389. opsize := operands[1].size;
  1390. case operands[1].operandtype of
  1391. { reg,reg }
  1392. { reg,ref }
  1393. OPR_REGISTER:
  1394. Begin
  1395. case operands[2].operandtype of
  1396. OPR_REGISTER:
  1397. { see info in ratti386.pas, about the problem }
  1398. { which can cause gas here. }
  1399. if (opsize = operands[2].size) then
  1400. begin
  1401. p^.concat(new(pai386,op_reg_reg(instruc,
  1402. opsize,operands[1].reg,operands[2].reg)));
  1403. end
  1404. else
  1405. { these do not require any size specification. }
  1406. if (instruc in [A_IN,A_OUT,A_SAL,A_SAR,A_SHL,A_SHR,A_ROL,
  1407. A_ROR,A_RCR,A_RCL]) then
  1408. { outs and ins are already taken care by }
  1409. { the first pass. }
  1410. p^.concat(new(pai386,op_reg_reg(instruc,
  1411. S_NO,operands[1].reg,operands[2].reg)))
  1412. else
  1413. Begin
  1414. Message(assem_e_invalid_opcode_and_operand);
  1415. end;
  1416. OPR_REFERENCE:
  1417. { variable name. }
  1418. { here we must check the instruction type }
  1419. { before deciding if to use and compare }
  1420. { any sizes. }
  1421. if assigned(operands[2].ref.symbol) then
  1422. Begin
  1423. if (opsize = operands[2].size) or (instruc in
  1424. [A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHR,A_SHL]) then
  1425. p^.concat(new(pai386,op_reg_ref(instruc,
  1426. opsize,operands[1].reg,newreference(operands[2].ref))))
  1427. else
  1428. Message(assem_e_invalid_size_in_ref);
  1429. end
  1430. else
  1431. Begin
  1432. { register reference }
  1433. { possiblities:1) local variable which }
  1434. { has been replaced by bp and offset }
  1435. { in this case size should be valid }
  1436. { 2) Indirect register }
  1437. { adressing, 1st operand determines }
  1438. { size. }
  1439. if (opsize = operands[2].size) or (operands[2].size = S_NO) then
  1440. p^.concat(new(pai386,op_reg_ref(instruc,
  1441. opsize,operands[1].reg,newreference(operands[2].ref))))
  1442. else
  1443. Message(assem_e_invalid_size_in_ref);
  1444. end;
  1445. OPR_CONSTANT: { const,reg }
  1446. Begin { OUT const,reg }
  1447. if (instruc = A_OUT) and (opsize = S_B) then
  1448. p^.concat(new(pai386,op_reg_const(instruc,
  1449. opsize,operands[1].reg,operands[2].val)))
  1450. else
  1451. Message(assem_e_invalid_size_in_ref);
  1452. end;
  1453. else { else case }
  1454. Begin
  1455. Message(assem_f_internal_error_in_concatopcode);
  1456. end;
  1457. end; { end inner case }
  1458. end;
  1459. { const,reg }
  1460. { const,const }
  1461. { const,ref }
  1462. OPR_CONSTANT:
  1463. case instr.operands[2].operandtype of
  1464. { constant, constant does not have a specific size. }
  1465. OPR_CONSTANT:
  1466. p^.concat(new(pai386,op_const_const(instruc,
  1467. S_NO,operands[1].val,operands[2].val)));
  1468. OPR_REFERENCE:
  1469. Begin
  1470. if (operands[1].val <= $ff) and
  1471. (operands[2].size in [S_B,S_W,S_L,
  1472. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1473. p^.concat(new(pai386,op_const_ref(instruc,
  1474. operands[2].size,operands[1].val,
  1475. newreference(operands[2].ref))))
  1476. else
  1477. if (operands[1].val <= $ffff) and
  1478. (operands[2].size in [S_W,S_L,
  1479. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1480. p^.concat(new(pai386,op_const_ref(instruc,
  1481. operands[2].size,operands[1].val,
  1482. newreference(operands[2].ref))))
  1483. else
  1484. if (operands[1].val <= $7fffffff) and
  1485. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1486. p^.concat(new(pai386,op_const_ref(instruc,
  1487. operands[2].size,operands[1].val,
  1488. newreference(operands[2].ref))))
  1489. else
  1490. Message(assem_e_invalid_size_in_ref);
  1491. end;
  1492. OPR_REGISTER:
  1493. Begin
  1494. { size of opcode determined by register }
  1495. if (operands[1].val <= $ff) and
  1496. (operands[2].size in [S_B,S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1497. p^.concat(new(pai386,op_const_reg(instruc,
  1498. operands[2].size,operands[1].val,
  1499. operands[2].reg)))
  1500. else
  1501. if (operands[1].val <= $ffff) and
  1502. (operands[2].size in [S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1503. p^.concat(new(pai386,op_const_reg(instruc,
  1504. operands[2].size,operands[1].val,
  1505. operands[2].reg)))
  1506. else
  1507. if (operands[1].val <= $7fffffff) and
  1508. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1509. p^.concat(new(pai386,op_const_reg(instruc,
  1510. operands[2].size,operands[1].val,
  1511. operands[2].reg)))
  1512. else
  1513. Message(assem_e_invalid_opcode_size);
  1514. end;
  1515. else
  1516. Begin
  1517. Message(assem_f_internal_error_in_concatopcode);
  1518. end;
  1519. end; { end case }
  1520. { ref,reg }
  1521. { ref,ref }
  1522. OPR_REFERENCE:
  1523. case instr.operands[2].operandtype of
  1524. OPR_REGISTER:
  1525. if assigned(operands[1].ref.symbol) then
  1526. { global variable }
  1527. Begin
  1528. if instruc in [A_LEA,A_LDS,A_LES,A_LFS,A_LGS,A_LSS]
  1529. then
  1530. p^.concat(new(pai386,op_ref_reg(instruc,
  1531. S_NO,newreference(operands[1].ref),
  1532. operands[2].reg)))
  1533. else
  1534. if (opsize = operands[2].size) then
  1535. p^.concat(new(pai386,op_ref_reg(instruc,
  1536. opsize,newreference(operands[1].ref),
  1537. operands[2].reg)))
  1538. else
  1539. Begin
  1540. Message(assem_e_invalid_opcode_and_operand);
  1541. end;
  1542. end
  1543. else
  1544. Begin
  1545. { register reference }
  1546. { possiblities:1) local variable which }
  1547. { has been replaced by bp and offset }
  1548. { in this case size should be valid }
  1549. { 2) Indirect register }
  1550. { adressing, 2nd operand determines }
  1551. { size. }
  1552. if (opsize = operands[2].size) or (opsize = S_NO) then
  1553. Begin
  1554. p^.concat(new(pai386,op_ref_reg(instruc,
  1555. operands[2].size,newreference(operands[1].ref),
  1556. operands[2].reg)));
  1557. end
  1558. else
  1559. Message(assem_e_invalid_size_in_ref);
  1560. end;
  1561. OPR_REFERENCE: { special opcodes }
  1562. p^.concat(new(pai386,op_ref_ref(instruc,
  1563. opsize,newreference(operands[1].ref),
  1564. newreference(operands[2].ref))));
  1565. else
  1566. Begin
  1567. Message(assem_f_internal_error_in_concatopcode);
  1568. end;
  1569. end; { end inner case }
  1570. end; { end case }
  1571. end; { end with }
  1572. end; {end if movsx... }
  1573. end;
  1574. 3: Begin
  1575. { only imul, shld and shrd }
  1576. { middle must be a register }
  1577. if (instruc in [A_SHLD,A_SHRD]) and (instr.operands[2].operandtype =
  1578. OPR_REGISTER) then
  1579. Begin
  1580. case instr.operands[2].size of
  1581. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1582. Begin
  1583. if instr.operands[1].val <= $ff then
  1584. Begin
  1585. if instr.operands[3].size in [S_W] then
  1586. Begin
  1587. case instr.operands[3].operandtype of
  1588. OPR_REFERENCE: { MISSING !!!! } ;
  1589. OPR_REGISTER: p^.concat(new(pai386,
  1590. op_const_reg_reg(instruc, S_W,
  1591. instr.operands[1].val, instr.operands[2].reg,
  1592. instr.operands[3].reg)));
  1593. else
  1594. Message(assem_e_invalid_opcode_and_operand);
  1595. Message(assem_e_invalid_opcode_and_operand);
  1596. end;
  1597. end
  1598. else
  1599. Message(assem_e_invalid_opcode_and_operand);
  1600. end;
  1601. end
  1602. else
  1603. Message(assem_e_invalid_opcode_and_operand);
  1604. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1605. Begin
  1606. if instr.operands[1].val <= $ff then
  1607. Begin
  1608. if instr.operands[3].size in [S_L] then
  1609. Begin
  1610. case instr.operands[3].operandtype of
  1611. OPR_REFERENCE: { MISSING !!!! } ;
  1612. OPR_REGISTER: p^.concat(new(pai386,
  1613. op_const_reg_reg(instruc, S_L,
  1614. instr.operands[1].val, instr.operands[2].reg,
  1615. instr.operands[3].reg)));
  1616. else
  1617. Message(assem_e_invalid_opcode_and_operand);
  1618. end;
  1619. end
  1620. else
  1621. Message(assem_e_invalid_opcode_and_operand);
  1622. end;
  1623. end
  1624. else
  1625. Message(assem_e_invalid_opcode_and_operand);
  1626. else
  1627. Message(assem_e_invalid_opcode_and_operand);
  1628. end; { end case }
  1629. end
  1630. else
  1631. if (instruc in [A_IMUL]) and (instr.operands[3].operandtype
  1632. = OPR_REGISTER) then
  1633. Begin
  1634. case instr.operands[3].size of
  1635. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1636. Begin
  1637. if instr.operands[1].val <= $ffff then
  1638. Begin
  1639. if instr.operands[2].size in [S_W] then
  1640. Begin
  1641. case instr.operands[2].operandtype of
  1642. OPR_REFERENCE: { MISSING !!!! } ;
  1643. OPR_REGISTER: p^.concat(new(pai386,
  1644. op_const_reg_reg(instruc, S_W,
  1645. instr.operands[1].val, instr.operands[2].reg,
  1646. instr.operands[3].reg)));
  1647. else
  1648. Message(assem_e_invalid_opcode_and_operand);
  1649. end; { end case }
  1650. end
  1651. else
  1652. Message(assem_e_invalid_opcode_and_operand);
  1653. end;
  1654. end
  1655. else
  1656. Message(assem_e_invalid_opcode_and_operand);
  1657. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1658. Begin
  1659. if instr.operands[1].val <= $7fffffff then
  1660. Begin
  1661. if instr.operands[2].size in [S_L] then
  1662. Begin
  1663. case instr.operands[2].operandtype of
  1664. OPR_REFERENCE: { MISSING !!!! } ;
  1665. OPR_REGISTER: p^.concat(new(pai386,
  1666. op_const_reg_reg(instruc, S_L,
  1667. instr.operands[1].val, instr.operands[2].reg,
  1668. instr.operands[3].reg)));
  1669. else
  1670. Message(assem_e_invalid_opcode_and_operand);
  1671. end; { end case }
  1672. end
  1673. else
  1674. Message(assem_e_invalid_opcode_and_operand);
  1675. end;
  1676. end
  1677. else
  1678. Message(assem_e_invalid_opcode_and_operand);
  1679. else
  1680. Message(assem_e_invalid_middle_sized_operand);
  1681. end; { end case }
  1682. end { endif }
  1683. else
  1684. Message(assem_e_invalid_three_operand_opcode);
  1685. end;
  1686. end; { end case }
  1687. end;
  1688. end;
  1689. {---------------------------------------------------------------------}
  1690. { Routines for the parsing }
  1691. {---------------------------------------------------------------------}
  1692. procedure consume(t : tinteltoken);
  1693. begin
  1694. if t<>actasmtoken then
  1695. Message(assem_e_syntax_error);
  1696. actasmtoken:=gettoken;
  1697. { if the token must be ignored, then }
  1698. { get another token to parse. }
  1699. if actasmtoken = AS_NONE then
  1700. actasmtoken := gettoken;
  1701. end;
  1702. function findregister(const s : string): tregister;
  1703. {*********************************************************************}
  1704. { FUNCTION findregister(s: string):tasmop; }
  1705. { Description: Determines if the s string is a valid register, }
  1706. { if so returns correct tregister token, or R_NO if not found. }
  1707. {*********************************************************************}
  1708. var
  1709. i: tregister;
  1710. begin
  1711. findregister := R_NO;
  1712. for i:=firstreg to lastreg do
  1713. if s = iasmregs[i] then
  1714. Begin
  1715. findregister := i;
  1716. exit;
  1717. end;
  1718. end;
  1719. function findoverride(const s: string; var reg:tregister): boolean;
  1720. var
  1721. i: byte;
  1722. begin
  1723. findoverride := FALSE;
  1724. reg := R_NO;
  1725. for i:=0 to _count_asmoverrides do
  1726. Begin
  1727. if s = _asmoverrides[i] then
  1728. begin
  1729. reg := _overridetokens[i];
  1730. findoverride := TRUE;
  1731. exit;
  1732. end;
  1733. end;
  1734. end;
  1735. function findprefix(const s: string; var token: tasmop): boolean;
  1736. var i: byte;
  1737. Begin
  1738. findprefix := FALSE;
  1739. for i:=0 to _count_asmprefixes do
  1740. Begin
  1741. if s = _asmprefixes[i] then
  1742. begin
  1743. token := _prefixtokens[i];
  1744. findprefix := TRUE;
  1745. exit;
  1746. end;
  1747. end;
  1748. end;
  1749. function findsegment(const s:string): tregister;
  1750. {*********************************************************************}
  1751. { FUNCTION findsegment(s: string):tasmop; }
  1752. { Description: Determines if the s string is a valid segment register}
  1753. { if so returns correct tregister token, or R_NO if not found. }
  1754. {*********************************************************************}
  1755. var
  1756. i: tregister;
  1757. Begin
  1758. findsegment := R_DEFAULT_SEG;
  1759. for i:=firstsreg to lastsreg do
  1760. if s = iasmregs[i] then
  1761. Begin
  1762. findsegment := i;
  1763. exit;
  1764. end;
  1765. end;
  1766. function findopcode(const s: string): tasmop;
  1767. {*********************************************************************}
  1768. { FUNCTION findopcode(s: string): tasmop; }
  1769. { Description: Determines if the s string is a valid opcode }
  1770. { if so returns correct tasmop token. }
  1771. {*********************************************************************}
  1772. var
  1773. i: tasmop;
  1774. j: byte;
  1775. Begin
  1776. findopcode := A_NONE;
  1777. for i:=firstop to lastop do
  1778. if s = iasmops^[i] then
  1779. begin
  1780. findopcode:=i;
  1781. exit;
  1782. end;
  1783. { not found yet, search for extended opcodes }
  1784. { now, in this case, we must use the suffix }
  1785. { to determine the size of the instruction }
  1786. for j:=0 to _count_asmspecialops do
  1787. Begin
  1788. if s = _specialops[j] then
  1789. Begin
  1790. findopcode := _specialopstokens[j];
  1791. { set the size }
  1792. case s[length(s)] of
  1793. 'B': instr.stropsize := S_B;
  1794. 'D': instr.stropsize := S_L;
  1795. 'W': instr.stropsize := S_W;
  1796. end;
  1797. exit;
  1798. end;
  1799. end;
  1800. end;
  1801. Function CheckPrefix(prefix: tasmop; opcode:tasmop): Boolean;
  1802. { Checks if the prefix is valid with the following instruction }
  1803. { return false if not, otherwise true }
  1804. Begin
  1805. CheckPrefix := TRUE;
  1806. Case prefix of
  1807. A_REP,A_REPNE,A_REPE: if not (opcode in [A_SCAS,A_INS,A_OUTS,A_MOVS,
  1808. A_CMPS,A_LODS,A_STOS]) then
  1809. Begin
  1810. CheckPrefix := FALSE;
  1811. exit;
  1812. end;
  1813. A_LOCK: if not (opcode in [A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,
  1814. A_ADC,A_SBB,A_AND,A_SUB,A_XOR,A_NOT,A_NEG,A_INC,A_DEC]) then
  1815. Begin
  1816. CheckPrefix := FALSE;
  1817. Exit;
  1818. end;
  1819. A_NONE: exit; { no prefix here }
  1820. else
  1821. CheckPrefix := FALSE;
  1822. end; { end case }
  1823. end;
  1824. Procedure InitAsmRef(var instr: TInstruction);
  1825. {*********************************************************************}
  1826. { Description: This routine first check if the instruction is of }
  1827. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1828. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1829. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1830. { to point to the default segment. }
  1831. {*********************************************************************}
  1832. Begin
  1833. With instr do
  1834. Begin
  1835. case operands[operandnum].operandtype of
  1836. OPR_REFERENCE: exit;
  1837. OPR_NONE: ;
  1838. else
  1839. Message(assem_e_invalid_operand_type);
  1840. end;
  1841. operands[operandnum].operandtype := OPR_REFERENCE;
  1842. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  1843. end;
  1844. end;
  1845. Function CheckOverride(segreg: tregister; var instr: TInstruction): Boolean;
  1846. { Check if the override is valid, and if so then }
  1847. { update the instr variable accordingly. }
  1848. Begin
  1849. CheckOverride := FALSE;
  1850. if instr.getinstruction in [A_MOVS,A_XLAT,A_CMPS] then
  1851. Begin
  1852. CheckOverride := TRUE;
  1853. Message(assem_e_segment_override_not_supported);
  1854. end
  1855. end;
  1856. Function CalculateExpression(expression: string): longint;
  1857. var
  1858. expr: TExprParse;
  1859. Begin
  1860. expr.Init;
  1861. CalculateExpression := expr.Evaluate(expression);
  1862. expr.Done;
  1863. end;
  1864. Function BuildRefExpression: longint;
  1865. {*********************************************************************}
  1866. { FUNCTION BuildExpression: longint }
  1867. { Description: This routine calculates a constant expression to }
  1868. { a given value. The return value is the value calculated from }
  1869. { the expression. }
  1870. { The following tokens (not strings) are recognized: }
  1871. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1872. {*********************************************************************}
  1873. { ENTRY: On entry the token should be any valid expression token. }
  1874. { EXIT: On Exit the token points to any token after the closing }
  1875. { RBRACKET }
  1876. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1877. { invalid tokens. }
  1878. {*********************************************************************}
  1879. var tempstr: string;
  1880. expr: string;
  1881. l : longint;
  1882. errorflag : boolean;
  1883. Begin
  1884. errorflag := FALSE;
  1885. tempstr := '';
  1886. expr := '';
  1887. { tell tokenizer that we are in }
  1888. { an expression. }
  1889. inexpression := TRUE;
  1890. Repeat
  1891. Case actasmtoken of
  1892. AS_LPAREN: Begin
  1893. Consume(AS_LPAREN);
  1894. expr := expr + '(';
  1895. end;
  1896. AS_RPAREN: Begin
  1897. Consume(AS_RPAREN);
  1898. expr := expr + ')';
  1899. end;
  1900. AS_SHL: Begin
  1901. Consume(AS_SHL);
  1902. expr := expr + '<';
  1903. end;
  1904. AS_SHR: Begin
  1905. Consume(AS_SHR);
  1906. expr := expr + '>';
  1907. end;
  1908. AS_SLASH: Begin
  1909. Consume(AS_SLASH);
  1910. expr := expr + '/';
  1911. end;
  1912. AS_MOD: Begin
  1913. Consume(AS_MOD);
  1914. expr := expr + '%';
  1915. end;
  1916. AS_STAR: Begin
  1917. Consume(AS_STAR);
  1918. expr := expr + '*';
  1919. end;
  1920. AS_PLUS: Begin
  1921. Consume(AS_PLUS);
  1922. expr := expr + '+';
  1923. end;
  1924. AS_MINUS: Begin
  1925. Consume(AS_MINUS);
  1926. expr := expr + '-';
  1927. end;
  1928. AS_AND: Begin
  1929. Consume(AS_AND);
  1930. expr := expr + '&';
  1931. end;
  1932. AS_NOT: Begin
  1933. Consume(AS_NOT);
  1934. expr := expr + '~';
  1935. end;
  1936. AS_XOR: Begin
  1937. Consume(AS_XOR);
  1938. expr := expr + '^';
  1939. end;
  1940. AS_OR: Begin
  1941. Consume(AS_OR);
  1942. expr := expr + '|';
  1943. end;
  1944. { End of reference }
  1945. AS_RBRACKET: Begin
  1946. if not ErrorFlag then
  1947. BuildRefExpression := CalculateExpression(expr)
  1948. else
  1949. BuildRefExpression := 0;
  1950. Consume(AS_RBRACKET);
  1951. { no longer in an expression }
  1952. inexpression := FALSE;
  1953. exit;
  1954. end;
  1955. AS_ID:
  1956. Begin
  1957. if NOT SearchIConstant(actasmpattern,l) then
  1958. Begin
  1959. Message1(assem_e_invalid_const_symbol,actasmpattern);
  1960. l := 0;
  1961. end;
  1962. str(l, tempstr);
  1963. expr := expr + tempstr;
  1964. Consume(AS_ID);
  1965. end;
  1966. AS_INTNUM: Begin
  1967. expr := expr + actasmpattern;
  1968. Consume(AS_INTNUM);
  1969. end;
  1970. AS_BINNUM: Begin
  1971. tempstr := BinaryToDec(actasmpattern);
  1972. if tempstr = '' then
  1973. Message(assem_f_error_converting_bin);
  1974. expr:=expr+tempstr;
  1975. Consume(AS_BINNUM);
  1976. end;
  1977. AS_HEXNUM: Begin
  1978. tempstr := HexToDec(actasmpattern);
  1979. if tempstr = '' then
  1980. Message(assem_f_error_converting_hex);
  1981. expr:=expr+tempstr;
  1982. Consume(AS_HEXNUM);
  1983. end;
  1984. AS_OCTALNUM: Begin
  1985. tempstr := OctalToDec(actasmpattern);
  1986. if tempstr = '' then
  1987. Message(assem_f_error_converting_octal);
  1988. expr:=expr+tempstr;
  1989. Consume(AS_OCTALNUM);
  1990. end;
  1991. else
  1992. Begin
  1993. { write error only once. }
  1994. if not errorflag then
  1995. Message(assem_e_invalid_constant_expression);
  1996. BuildRefExpression := 0;
  1997. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then exit;
  1998. { consume tokens until we find COMMA or SEPARATOR }
  1999. Consume(actasmtoken);
  2000. errorflag := TRUE;
  2001. end;
  2002. end;
  2003. Until false;
  2004. end;
  2005. Procedure BuildRecordOffset(var instr: TInstruction; varname: string);
  2006. {*********************************************************************}
  2007. { PROCEDURE BuildRecordOffset(var Instr: TInstruction) }
  2008. { Description: This routine takes care of field specifiers of records }
  2009. { and/or variables in asm operands. It updates the offset accordingly}
  2010. {*********************************************************************}
  2011. { ENTRY: On entry the token should be DOT. }
  2012. { name: should be the name of the variable to be expanded. '' if }
  2013. { no variabled specified. }
  2014. { EXIT: On Exit the token points to SEPARATOR or COMMA. }
  2015. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2016. { invalid tokens. }
  2017. {*********************************************************************}
  2018. var
  2019. firstpass: boolean;
  2020. offset: longint;
  2021. basetypename : string;
  2022. Begin
  2023. basetypename := '';
  2024. firstpass := TRUE;
  2025. { // .ID[REG].ID ... // }
  2026. { // .ID.ID... // }
  2027. Consume(AS_DOT);
  2028. Repeat
  2029. case actasmtoken of
  2030. AS_ID: Begin
  2031. { we must reset the operand size - since only the last field }
  2032. { will give us the size of the operand. }
  2033. { instr.opsize := S_NO;}
  2034. InitAsmRef(instr);
  2035. { // var_name.typefield.typefield // }
  2036. if (varname <> '') then
  2037. Begin
  2038. if not GetVarOffset(instr,varname,actasmpattern,offset,operandnum) then
  2039. Begin
  2040. Message1(assem_e_unknown_id,actasmpattern);
  2041. end
  2042. else
  2043. Inc(instr.operands[operandnum].ref.offset,Offset);
  2044. end
  2045. else
  2046. { [ref].var_name.typefield.typefield ... }
  2047. { [ref].var_name[reg] }
  2048. if not assigned(instr.operands[operandnum].ref.symbol) and
  2049. firstpass then
  2050. Begin
  2051. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2052. Begin
  2053. { type field ? }
  2054. basetypename := actasmpattern;
  2055. end
  2056. else
  2057. varname := actasmpattern;
  2058. end
  2059. else
  2060. if firstpass then
  2061. { [ref].typefield.typefield ... }
  2062. { where the first typefield must specifiy the base }
  2063. { object or record type. }
  2064. Begin
  2065. basetypename := actasmpattern;
  2066. end
  2067. else
  2068. { [ref].typefield.typefield ... }
  2069. { basetpyename is already set up... now look for fields. }
  2070. Begin
  2071. if not GetTypeOffset(instr,basetypename,actasmpattern,Offset,operandnum) then
  2072. Begin
  2073. Message1(assem_e_unknown_id,actasmpattern);
  2074. end
  2075. else
  2076. Inc(instr.operands[operandnum].ref.offset,Offset);
  2077. end;
  2078. Consume(AS_ID);
  2079. { Take care of index register on this variable }
  2080. if actasmtoken = AS_LBRACKET then
  2081. Begin
  2082. Consume(AS_LBRACKET);
  2083. Case actasmtoken of
  2084. AS_REGISTER: Begin
  2085. if instr.operands[operandnum].ref.index <> R_NO then
  2086. Message(assem_e_defining_index_more_than_once);
  2087. instr.operands[operandnum].ref.index :=
  2088. findregister(actasmpattern);
  2089. Consume(AS_REGISTER);
  2090. end;
  2091. else
  2092. Begin
  2093. { add offsets , assuming these are constant expressions... }
  2094. Inc(instr.operands[operandnum].ref.offset,BuildRefExpression);
  2095. end;
  2096. end;
  2097. Consume(AS_RBRACKET);
  2098. end;
  2099. { Here we should either have AS_DOT, AS_SEPARATOR or AS_COMMA }
  2100. if actasmtoken = AS_DOT then
  2101. Consume(AS_DOT);
  2102. firstpass := FALSE;
  2103. Offset := 0;
  2104. end;
  2105. AS_SEPARATOR: exit;
  2106. AS_COMMA: exit;
  2107. else
  2108. Begin
  2109. Message(assem_e_invalid_field_specifier);
  2110. Consume(actasmtoken);
  2111. firstpass := FALSE;
  2112. end;
  2113. end; { end case }
  2114. Until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  2115. end;
  2116. Function BuildExpression: longint;
  2117. {*********************************************************************}
  2118. { FUNCTION BuildExpression: longint }
  2119. { Description: This routine calculates a constant expression to }
  2120. { a given value. The return value is the value calculated from }
  2121. { the expression. }
  2122. { The following tokens (not strings) are recognized: }
  2123. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  2124. {*********************************************************************}
  2125. { ENTRY: On entry the token should be any valid expression token. }
  2126. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  2127. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2128. { invalid tokens. }
  2129. {*********************************************************************}
  2130. var expr: string;
  2131. tempstr: string;
  2132. l : longint;
  2133. errorflag: boolean;
  2134. Begin
  2135. errorflag := FALSE;
  2136. expr := '';
  2137. tempstr := '';
  2138. { tell tokenizer that we are in an expression. }
  2139. inexpression := TRUE;
  2140. Repeat
  2141. Case actasmtoken of
  2142. AS_LPAREN: Begin
  2143. Consume(AS_LPAREN);
  2144. expr := expr + '(';
  2145. end;
  2146. AS_RPAREN: Begin
  2147. Consume(AS_RPAREN);
  2148. expr := expr + ')';
  2149. end;
  2150. AS_SHL: Begin
  2151. Consume(AS_SHL);
  2152. expr := expr + '<';
  2153. end;
  2154. AS_SHR: Begin
  2155. Consume(AS_SHR);
  2156. expr := expr + '>';
  2157. end;
  2158. AS_SLASH: Begin
  2159. Consume(AS_SLASH);
  2160. expr := expr + '/';
  2161. end;
  2162. AS_MOD: Begin
  2163. Consume(AS_MOD);
  2164. expr := expr + '%';
  2165. end;
  2166. AS_STAR: Begin
  2167. Consume(AS_STAR);
  2168. expr := expr + '*';
  2169. end;
  2170. AS_PLUS: Begin
  2171. Consume(AS_PLUS);
  2172. expr := expr + '+';
  2173. end;
  2174. AS_MINUS: Begin
  2175. Consume(AS_MINUS);
  2176. expr := expr + '-';
  2177. end;
  2178. AS_AND: Begin
  2179. Consume(AS_AND);
  2180. expr := expr + '&';
  2181. end;
  2182. AS_NOT: Begin
  2183. Consume(AS_NOT);
  2184. expr := expr + '~';
  2185. end;
  2186. AS_XOR: Begin
  2187. Consume(AS_XOR);
  2188. expr := expr + '^';
  2189. end;
  2190. AS_OR: Begin
  2191. Consume(AS_OR);
  2192. expr := expr + '|';
  2193. end;
  2194. AS_ID: Begin
  2195. if NOT SearchIConstant(actasmpattern,l) then
  2196. Begin
  2197. Message1(assem_e_invalid_const_symbol,actasmpattern);
  2198. l := 0;
  2199. end;
  2200. str(l, tempstr);
  2201. expr := expr + tempstr;
  2202. Consume(AS_ID);
  2203. end;
  2204. AS_INTNUM: Begin
  2205. expr := expr + actasmpattern;
  2206. Consume(AS_INTNUM);
  2207. end;
  2208. AS_BINNUM: Begin
  2209. tempstr := BinaryToDec(actasmpattern);
  2210. if tempstr = '' then
  2211. Message(assem_f_error_converting_bin);
  2212. expr:=expr+tempstr;
  2213. Consume(AS_BINNUM);
  2214. end;
  2215. AS_HEXNUM: Begin
  2216. tempstr := HexToDec(actasmpattern);
  2217. if tempstr = '' then
  2218. Message(assem_f_error_converting_hex);
  2219. expr:=expr+tempstr;
  2220. Consume(AS_HEXNUM);
  2221. end;
  2222. AS_OCTALNUM: Begin
  2223. tempstr := OctalToDec(actasmpattern);
  2224. if tempstr = '' then
  2225. Message(assem_f_error_converting_octal);
  2226. expr:=expr+tempstr;
  2227. Consume(AS_OCTALNUM);
  2228. end;
  2229. { go to next term }
  2230. AS_COMMA: Begin
  2231. if not ErrorFlag then
  2232. BuildExpression := CalculateExpression(expr)
  2233. else
  2234. BuildExpression := 0;
  2235. inexpression := FALSE;
  2236. Exit;
  2237. end;
  2238. { go to next symbol }
  2239. AS_SEPARATOR: Begin
  2240. if not ErrorFlag then
  2241. BuildExpression := CalculateExpression(expr)
  2242. else
  2243. BuildExpression := 0;
  2244. inexpression := FALSE;
  2245. Exit;
  2246. end;
  2247. else
  2248. Begin
  2249. { only write error once. }
  2250. if not errorflag then
  2251. Message(assem_e_invalid_constant_expression);
  2252. { consume tokens until we find COMMA or SEPARATOR }
  2253. Consume(actasmtoken);
  2254. errorflag := TRUE;
  2255. End;
  2256. end;
  2257. Until false;
  2258. end;
  2259. Procedure BuildScaling(Var instr: TInstruction);
  2260. {*********************************************************************}
  2261. { Takes care of parsing expression starting from the scaling value }
  2262. { up to and including possible field specifiers. }
  2263. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  2264. { or AS_COMMA. On entry should point to AS_STAR token. }
  2265. {*********************************************************************}
  2266. var str:string;
  2267. l: longint;
  2268. code: integer;
  2269. Begin
  2270. Consume(AS_STAR);
  2271. if (instr.operands[operandnum].ref.scalefactor <> 0)
  2272. and (instr.operands[operandnum].ref.scalefactor <> 1) then
  2273. Begin
  2274. Message(assem_f_internal_error_in_buildscale);
  2275. end;
  2276. case actasmtoken of
  2277. AS_INTNUM: str := actasmpattern;
  2278. AS_HEXNUM: str := HexToDec(actasmpattern);
  2279. AS_BINNUM: str := BinaryToDec(actasmpattern);
  2280. AS_OCTALNUM: str := OctalToDec(actasmpattern);
  2281. else
  2282. Message(assem_e_syntax_error);
  2283. end;
  2284. val(str, l, code);
  2285. if code <> 0 then
  2286. Message(assem_e_invalid_scaling_factor);
  2287. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  2288. begin
  2289. instr.operands[operandnum].ref.scalefactor := l;
  2290. end
  2291. else
  2292. Begin
  2293. Message(assem_e_invalid_scaling_value);
  2294. instr.operands[operandnum].ref.scalefactor := 0;
  2295. end;
  2296. if instr.operands[operandnum].ref.index = R_NO then
  2297. Begin
  2298. Message(assem_e_scaling_value_only_allowed_with_index);
  2299. instr.operands[operandnum].ref.scalefactor := 0;
  2300. end;
  2301. { Consume the scaling number }
  2302. Consume(actasmtoken);
  2303. case actasmtoken of
  2304. { // [...*SCALING-expr] ... // }
  2305. AS_MINUS: Begin
  2306. if instr.operands[operandnum].ref.offset <> 0 then
  2307. Message(assem_f_internal_error_in_buildscale);
  2308. instr.operands[operandnum].ref.offset :=
  2309. BuildRefExpression;
  2310. end;
  2311. { // [...*SCALING+expr] ... // }
  2312. AS_PLUS: Begin
  2313. if instr.operands[operandnum].ref.offset <> 0 then
  2314. Message(assem_f_internal_error_in_buildscale);
  2315. instr.operands[operandnum].ref.offset :=
  2316. BuildRefExpression;
  2317. end;
  2318. { // [...*SCALING] ... // }
  2319. AS_RBRACKET: Consume(AS_RBRACKET);
  2320. else
  2321. Message(assem_e_invalid_scaling_value);
  2322. end;
  2323. { // .Field.Field ... or separator/comma // }
  2324. Case actasmtoken of
  2325. AS_DOT: BuildRecordOffset(instr,'');
  2326. AS_COMMA, AS_SEPARATOR: ;
  2327. else
  2328. Message(assem_e_syntax_error);
  2329. end;
  2330. end;
  2331. Procedure BuildReference(var instr: TInstruction);
  2332. {*********************************************************************}
  2333. { EXIT CONDITION: On exit the routine should point to either the }
  2334. { AS_COMMA or AS_SEPARATOR token. }
  2335. { On entry: contains the register after the opening bracket if any. }
  2336. {*********************************************************************}
  2337. var
  2338. reg:string;
  2339. segreg: boolean;
  2340. negative: boolean;
  2341. expr: string;
  2342. Begin
  2343. expr := '';
  2344. if instr.operands[operandnum].operandtype <> OPR_REFERENCE then
  2345. Begin
  2346. Message(assem_e_syn_no_ref_with_brackets);
  2347. InitAsmRef(instr);
  2348. consume(AS_REGISTER);
  2349. end
  2350. else
  2351. Begin
  2352. { save the reg }
  2353. reg := actasmpattern;
  2354. { is the syntax of the form: [REG:REG...] }
  2355. consume(AS_REGISTER);
  2356. if actasmtoken = AS_COLON then
  2357. begin
  2358. segreg := TRUE;
  2359. Message(assem_e_expression_form_not_supported);
  2360. if instr.operands[operandnum].ref.segment <> R_NO then
  2361. Message(assem_e_defining_seg_more_than_once);
  2362. instr.operands[operandnum].ref.segment := findsegment(reg);
  2363. { Here we should process the syntax of the form }
  2364. { [reg:reg...] }
  2365. {!!!!!!!!!!!!!!!!!!!!!!!! }
  2366. end
  2367. { This is probably of the following syntax: }
  2368. { SREG:[REG...] where SReg: is optional. }
  2369. { Therefore we immediately say that reg }
  2370. { is the base. }
  2371. else
  2372. Begin
  2373. if instr.operands[operandnum].ref.base <> R_NO then
  2374. Message(assem_e_defining_base_more_than_once);
  2375. instr.operands[operandnum].ref.base := findregister(reg);
  2376. end;
  2377. { we process this type of syntax immediately... }
  2378. case actasmtoken of
  2379. { // REG:[REG].Field.Field ... // }
  2380. { // REG:[REG].Field[REG].Field... // }
  2381. AS_RBRACKET: Begin
  2382. Consume(AS_RBRACKET);
  2383. { check for record fields }
  2384. if actasmtoken = AS_DOT then
  2385. BuildRecordOffset(instr,'');
  2386. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2387. exit
  2388. else
  2389. Message(assem_e_syn_reference);
  2390. end;
  2391. { // REG:[REG +/- ...].Field.Field ... // }
  2392. AS_PLUS,AS_MINUS: Begin
  2393. if actasmtoken = AS_MINUS then
  2394. Begin
  2395. expr := '-';
  2396. negative := TRUE
  2397. end
  2398. else
  2399. Begin
  2400. negative := FALSE;
  2401. expr := '+';
  2402. end;
  2403. Consume(actasmtoken);
  2404. { // REG:[REG+REG+/-...].Field.Field // }
  2405. if actasmtoken = AS_REGISTER then
  2406. Begin
  2407. if negative then
  2408. Message(assem_e_negative_index_register);
  2409. if instr.operands[operandnum].ref.index <> R_NO then
  2410. Message(assem_e_defining_index_more_than_once);
  2411. instr.operands[operandnum].ref.index := findregister(actasmpattern);
  2412. Consume(AS_REGISTER);
  2413. case actasmtoken of
  2414. AS_RBRACKET: { // REG:[REG+REG].Field.Field... // }
  2415. Begin
  2416. Consume(AS_RBRACKET);
  2417. Case actasmtoken of
  2418. AS_DOT: BuildRecordOffset(instr,'');
  2419. AS_COMMA,AS_SEPARATOR: exit;
  2420. else
  2421. Message(assem_e_syntax_error);
  2422. end
  2423. end;
  2424. AS_PLUS,AS_MINUS: { // REG:[REG+REG+/-expr].Field.Field... // }
  2425. Begin
  2426. if instr.operands[operandnum].ref.offset <> 0 then
  2427. Message(assem_f_internal_error_in_buildreference);
  2428. instr.operands[operandnum].ref.offset :=
  2429. BuildRefExpression;
  2430. case actasmtoken of
  2431. AS_DOT: BuildRecordOffset(instr,'');
  2432. AS_COMMA,AS_SEPARATOR: ;
  2433. else
  2434. Message(assem_e_syntax_error);
  2435. end; { end case }
  2436. end;
  2437. AS_STAR: Begin { // REG:[REG+REG*SCALING...].Field.Field... // }
  2438. BuildScaling(instr);
  2439. end;
  2440. else
  2441. Begin
  2442. Message(assem_e_syntax_error);
  2443. end;
  2444. end; { end case }
  2445. end
  2446. else if actasmtoken = AS_STAR then
  2447. { // REG:[REG*SCALING ... ] // }
  2448. Begin
  2449. BuildScaling(instr);
  2450. end
  2451. else
  2452. { // REG:[REG+expr].Field.Field // }
  2453. Begin
  2454. if instr.operands[operandnum].ref.offset <> 0 then
  2455. Message(assem_f_internal_error_in_buildreference);
  2456. instr.operands[operandnum].ref.offset := BuildRefExpression;
  2457. case actasmtoken of
  2458. AS_DOT: BuildRecordOffset(instr,'');
  2459. AS_COMMA,AS_SEPARATOR: ;
  2460. else
  2461. Message(assem_e_syntax_error);
  2462. end; { end case }
  2463. end; { end if }
  2464. end; { end this case }
  2465. { // REG:[REG*scaling] ... // }
  2466. AS_STAR: Begin
  2467. BuildScaling(instr);
  2468. end;
  2469. end;
  2470. end; { end outer if }
  2471. end;
  2472. Procedure BuildBracketExpression(var Instr: TInstruction; var_prefix: boolean);
  2473. {*********************************************************************}
  2474. { PROCEDURE BuildBracketExpression }
  2475. { Description: This routine builds up an expression after a LBRACKET }
  2476. { token is encountered. }
  2477. { On entry actasmtoken should be equal to AS_LBRACKET. }
  2478. { var_prefix : Should be set to true if variable identifier has }
  2479. { been defined, such as in ID[ }
  2480. {*********************************************************************}
  2481. { EXIT CONDITION: On exit the routine should point to either the }
  2482. { AS_COMMA or AS_SEPARATOR token. }
  2483. {*********************************************************************}
  2484. var
  2485. l:longint;
  2486. Begin
  2487. Consume(AS_LBRACKET);
  2488. initAsmRef(instr);
  2489. Case actasmtoken of
  2490. { // Constant reference expression OR variable reference expression // }
  2491. AS_ID: Begin
  2492. if actasmpattern[1] = '@' then
  2493. Message(assem_e_local_symbol_not_allowed_as_ref);
  2494. if SearchIConstant(actasmpattern,l) then
  2495. Begin
  2496. { if there was a variable prefix then }
  2497. { add to offset }
  2498. If var_prefix then
  2499. Begin
  2500. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2501. end
  2502. else
  2503. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2504. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2505. Message(assem_e_invalid_operand_in_bracket_expression);
  2506. end
  2507. else if NOT var_prefix then
  2508. Begin
  2509. InitAsmRef(instr);
  2510. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2511. Message1(assem_e_unknown_id,actasmpattern);
  2512. Consume(AS_ID);
  2513. { is there a constant expression following }
  2514. { the variable name? }
  2515. if actasmtoken <> AS_RBRACKET then
  2516. Begin
  2517. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2518. end
  2519. else
  2520. Consume(AS_RBRACKET);
  2521. end
  2522. else
  2523. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2524. end;
  2525. { Here we handle the special case in tp where }
  2526. { the + operator is allowed with reg and var }
  2527. { references, such as in mov al, byte ptr [+bx] }
  2528. AS_PLUS: Begin
  2529. Consume(AS_PLUS);
  2530. Case actasmtoken of
  2531. AS_REGISTER: Begin
  2532. BuildReference(instr);
  2533. end;
  2534. AS_ID: Begin
  2535. if actasmpattern[1] = '@' then
  2536. Message(assem_e_local_symbol_not_allowed_as_ref);
  2537. if SearchIConstant(actasmpattern,l) then
  2538. Begin
  2539. { if there was a variable prefix then }
  2540. { add to offset }
  2541. If var_prefix then
  2542. Begin
  2543. Inc(instr.operands[operandnum].ref.offset,
  2544. BuildRefExpression);
  2545. end
  2546. else
  2547. instr.operands[operandnum].ref.offset :=
  2548. BuildRefExpression;
  2549. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2550. Message(assem_e_invalid_operand_in_bracket_expression);
  2551. end
  2552. else if NOT var_prefix then
  2553. Begin
  2554. InitAsmRef(instr);
  2555. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2556. Message1(assem_e_unknown_id,actasmpattern);
  2557. Consume(AS_ID);
  2558. { is there a constant expression following }
  2559. { the variable name? }
  2560. if actasmtoken <> AS_RBRACKET then
  2561. Begin
  2562. Inc(instr.operands[operandnum].ref.offset,
  2563. BuildRefExpression);
  2564. end
  2565. else
  2566. Consume(AS_RBRACKET);
  2567. end
  2568. else
  2569. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2570. end;
  2571. { // Constant reference expression // }
  2572. AS_INTNUM,AS_BINNUM,AS_OCTALNUM,
  2573. AS_HEXNUM: Begin
  2574. { if there was a variable prefix then }
  2575. { add to offset instead. }
  2576. If var_prefix then
  2577. Begin
  2578. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2579. end
  2580. else
  2581. Begin
  2582. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2583. end;
  2584. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2585. Message(assem_e_invalid_operand_in_bracket_expression);
  2586. end;
  2587. else
  2588. Message(assem_e_syntax_error);
  2589. end;
  2590. end;
  2591. { // Constant reference expression // }
  2592. AS_MINUS,AS_NOT,AS_LPAREN:
  2593. Begin
  2594. { if there was a variable prefix then }
  2595. { add to offset instead. }
  2596. If var_prefix then
  2597. Begin
  2598. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2599. end
  2600. else
  2601. Begin
  2602. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2603. end;
  2604. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2605. Message(assem_e_invalid_operand_in_bracket_expression);
  2606. end;
  2607. { // Constant reference expression // }
  2608. AS_INTNUM,AS_OCTALNUM,AS_BINNUM,AS_HEXNUM: Begin
  2609. { if there was a variable prefix then }
  2610. { add to offset instead. }
  2611. If var_prefix then
  2612. Begin
  2613. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2614. end
  2615. else
  2616. Begin
  2617. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2618. end;
  2619. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2620. Message(assem_e_invalid_operand_in_bracket_expression);
  2621. end;
  2622. { // Variable reference expression // }
  2623. AS_REGISTER: BuildReference(instr);
  2624. else
  2625. Begin
  2626. Message(assem_e_invalid_reference_syntax);
  2627. while (actasmtoken <> AS_SEPARATOR) do
  2628. Consume(actasmtoken);
  2629. end;
  2630. end; { end case }
  2631. end;
  2632. Procedure BuildOperand(var instr: TInstruction);
  2633. {*********************************************************************}
  2634. { EXIT CONDITION: On exit the routine should point to either the }
  2635. { AS_COMMA or AS_SEPARATOR token. }
  2636. {*********************************************************************}
  2637. var
  2638. tempstr: string;
  2639. expr: string;
  2640. lab: Pasmlabel;
  2641. l : longint;
  2642. hl: plabel;
  2643. Begin
  2644. tempstr := '';
  2645. expr := '';
  2646. case actasmtoken of
  2647. { // Constant expression // }
  2648. AS_PLUS,AS_MINUS,AS_NOT,AS_LPAREN:
  2649. Begin
  2650. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2651. Message(assem_e_invalid_operand_type);
  2652. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2653. instr.operands[operandnum].val :=BuildExpression;
  2654. end;
  2655. { // Constant expression // }
  2656. AS_STRING: Begin
  2657. if not (instr.operands[operandnum].operandtype in [OPR_NONE]) then
  2658. Message(assem_e_invalid_operand_type);
  2659. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2660. if not PadZero(actasmpattern,4) then
  2661. Message1(assem_e_invalid_string_as_opcode_operand,actasmpattern);
  2662. instr.operands[operandnum].val :=
  2663. ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  2664. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1])
  2665. shl 24;
  2666. Consume(AS_STRING);
  2667. Case actasmtoken of
  2668. AS_COMMA, AS_SEPARATOR: ;
  2669. else
  2670. Message(assem_e_invalid_string_expression);
  2671. end; { end case }
  2672. end;
  2673. { // Constant expression // }
  2674. AS_INTNUM,AS_BINNUM,
  2675. AS_OCTALNUM,
  2676. AS_HEXNUM: Begin
  2677. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2678. Message(assem_e_invalid_operand_type);
  2679. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2680. instr.operands[operandnum].val :=BuildExpression;
  2681. end;
  2682. { // A constant expression, or a Variable ref. // }
  2683. AS_ID: Begin
  2684. if actasmpattern[1] = '@' then
  2685. { // Label or Special symbol reference // }
  2686. Begin
  2687. if actasmpattern = '@RESULT' then
  2688. Begin
  2689. InitAsmRef(instr);
  2690. SetUpResult(instr,operandnum);
  2691. end
  2692. else
  2693. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2694. Message(assem_w_CODE_and_DATA_not_supported)
  2695. else
  2696. Begin
  2697. delete(actasmpattern,1,1);
  2698. if actasmpattern = '' then
  2699. Message(assem_e_null_label_ref_not_allowed);
  2700. lab := labellist.search(actasmpattern);
  2701. { check if the label is already defined }
  2702. { if so, we then check if the plabel is }
  2703. { non-nil, if so we add it to instruction }
  2704. if assigned(lab) then
  2705. Begin
  2706. if assigned(lab^.lab) then
  2707. Begin
  2708. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2709. instr.operands[operandnum].hl := lab^.lab;
  2710. instr.labeled := TRUE;
  2711. end;
  2712. end
  2713. else
  2714. { the label does not exist, create it }
  2715. { emit the opcode, but set that the }
  2716. { label has not been emitted }
  2717. Begin
  2718. getlabel(hl);
  2719. labellist.insert(actasmpattern,hl,FALSE);
  2720. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2721. instr.operands[operandnum].hl := hl;
  2722. instr.labeled := TRUE;
  2723. end;
  2724. end;
  2725. Consume(AS_ID);
  2726. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2727. Begin
  2728. Message(assem_e_syntax_error);
  2729. end;
  2730. end
  2731. { probably a variable or normal expression }
  2732. { or a procedure (such as in CALL ID) }
  2733. else
  2734. Begin
  2735. { is it a constant ? }
  2736. if SearchIConstant(actasmpattern,l) then
  2737. Begin
  2738. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2739. Message(assem_e_invalid_operand_type);
  2740. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2741. instr.operands[operandnum].val :=BuildExpression;
  2742. end
  2743. else { is it a label variable ? }
  2744. Begin
  2745. { // ID[ , ID.Field.Field or simple ID // }
  2746. { check if this is a label, if so then }
  2747. { emit it as a label. }
  2748. if SearchLabel(actasmpattern,hl) then
  2749. Begin
  2750. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2751. instr.operands[operandnum].hl := hl;
  2752. instr.labeled := TRUE;
  2753. Consume(AS_ID);
  2754. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2755. Message(assem_e_syntax_error);
  2756. end
  2757. else
  2758. { is it a normal variable ? }
  2759. Begin
  2760. initAsmRef(instr);
  2761. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2762. Begin
  2763. { not a variable.. }
  2764. { check special variables.. }
  2765. if actasmpattern = 'SELF' then
  2766. { special self variable }
  2767. Begin
  2768. if assigned(procinfo._class) then
  2769. Begin
  2770. instr.operands[operandnum].ref.offset := procinfo.ESI_offset;
  2771. instr.operands[operandnum].ref.base := procinfo.framepointer;
  2772. end
  2773. else
  2774. Message(assem_e_cannot_use_SELF_outside_a_method);
  2775. end
  2776. else
  2777. Message1(assem_e_unknown_id,actasmpattern);
  2778. end;
  2779. expr := actasmpattern;
  2780. Consume(AS_ID);
  2781. case actasmtoken of
  2782. AS_LBRACKET: { indexing }
  2783. BuildBracketExpression(instr,TRUE);
  2784. AS_DOT: BuildRecordOffset(instr,expr);
  2785. AS_SEPARATOR,AS_COMMA: ;
  2786. else
  2787. Message(assem_e_syntax_error);
  2788. end;
  2789. end;
  2790. end;
  2791. end;
  2792. end;
  2793. { // Register, a variable reference or a constant reference // }
  2794. AS_REGISTER: Begin
  2795. { save the type of register used. }
  2796. tempstr := actasmpattern;
  2797. Consume(AS_REGISTER);
  2798. if actasmtoken = AS_COLON then
  2799. Begin
  2800. Consume(AS_COLON);
  2801. if actasmtoken <> AS_LBRACKET then
  2802. Message(assem_e_syn_start_with_bracket)
  2803. else
  2804. Begin
  2805. initAsmRef(instr);
  2806. instr.operands[operandnum].ref.segment := findsegment(tempstr);
  2807. BuildBracketExpression(instr,false);
  2808. end;
  2809. end
  2810. { // Simple register // }
  2811. else if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2812. Begin
  2813. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  2814. Message(assem_e_invalid_operand_type);
  2815. instr.operands[operandnum].operandtype := OPR_REGISTER;
  2816. instr.operands[operandnum].reg := findregister(tempstr);
  2817. end
  2818. else
  2819. Message1(assem_e_syn_register,tempstr);
  2820. end;
  2821. { // a variable reference, register ref. or a constant reference // }
  2822. AS_LBRACKET: Begin
  2823. BuildBracketExpression(instr,false);
  2824. end;
  2825. { // Unsupported // }
  2826. AS_SEG,AS_OFFSET: Begin
  2827. Message(assem_e_SEG_and_OFFSET_not_supported);
  2828. Consume(actasmtoken);
  2829. { error recovery }
  2830. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2831. Consume(actasmtoken);
  2832. end;
  2833. AS_SEPARATOR, AS_COMMA: ;
  2834. else
  2835. Message(assem_e_syn_opcode_operand);
  2836. end; { end case }
  2837. end;
  2838. Procedure BuildConstant(maxvalue: longint);
  2839. {*********************************************************************}
  2840. { PROCEDURE BuildConstant }
  2841. { Description: This routine takes care of parsing a DB,DD,or DW }
  2842. { line and adding those to the assembler node. Expressions, range- }
  2843. { checking are fullly taken care of. }
  2844. { maxvalue: $ff -> indicates that this is a DB node. }
  2845. { $ffff -> indicates that this is a DW node. }
  2846. { $ffffffff -> indicates that this is a DD node. }
  2847. {*********************************************************************}
  2848. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2849. {*********************************************************************}
  2850. var
  2851. strlength: byte;
  2852. expr: string;
  2853. value : longint;
  2854. Begin
  2855. strlength := 0; { assume it is a DB }
  2856. Repeat
  2857. Case actasmtoken of
  2858. AS_STRING: Begin
  2859. if maxvalue = $ffff then
  2860. strlength := 2
  2861. else if maxvalue = $ffffffff then
  2862. strlength := 4;
  2863. if strlength <> 0 then
  2864. { DD and DW cases }
  2865. Begin
  2866. if Not PadZero(actasmpattern,strlength) then
  2867. Message(scan_f_string_exceeds_line);
  2868. end;
  2869. expr := actasmpattern;
  2870. Consume(AS_STRING);
  2871. Case actasmtoken of
  2872. AS_COMMA: Consume(AS_COMMA);
  2873. AS_SEPARATOR: ;
  2874. else
  2875. Message(assem_e_invalid_string_expression);
  2876. end; { end case }
  2877. ConcatString(p,expr);
  2878. end;
  2879. AS_INTNUM,AS_BINNUM,
  2880. AS_OCTALNUM,AS_HEXNUM:
  2881. Begin
  2882. value:=BuildExpression;
  2883. ConcatConstant(p,value,maxvalue);
  2884. end;
  2885. AS_ID:
  2886. Begin
  2887. value:=BuildExpression;
  2888. if value > maxvalue then
  2889. Begin
  2890. Message(assem_e_expression_out_of_bounds);
  2891. { assuming a value of maxvalue }
  2892. value := maxvalue;
  2893. end;
  2894. ConcatConstant(p,value,maxvalue);
  2895. end;
  2896. { These terms can start an assembler expression }
  2897. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: Begin
  2898. value := BuildExpression;
  2899. ConcatConstant(p,value,maxvalue);
  2900. end;
  2901. AS_COMMA: BEGIN
  2902. Consume(AS_COMMA);
  2903. END;
  2904. AS_SEPARATOR: ;
  2905. else
  2906. Begin
  2907. Message(assem_f_internal_error_in_buildconstant);
  2908. end;
  2909. end; { end case }
  2910. Until actasmtoken = AS_SEPARATOR;
  2911. end;
  2912. Procedure BuildOpCode;
  2913. {*********************************************************************}
  2914. { PROCEDURE BuildOpcode; }
  2915. { Description: Parses the intel opcode and operands, and writes it }
  2916. { in the TInstruction object. }
  2917. {*********************************************************************}
  2918. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2919. { On ENTRY: Token should point to AS_OPCODE }
  2920. {*********************************************************************}
  2921. var asmtok: tasmop;
  2922. op: tasmop;
  2923. expr: string;
  2924. segreg: tregister;
  2925. Begin
  2926. expr := '';
  2927. asmtok := A_NONE; { assmume no prefix }
  2928. segreg := R_NO; { assume no segment override }
  2929. { // prefix seg opcode // }
  2930. { // prefix opcode // }
  2931. if findprefix(actasmpattern,asmtok) then
  2932. Begin
  2933. { standard opcode prefix }
  2934. if asmtok <> A_NONE then
  2935. instr.addprefix(asmtok);
  2936. Consume(AS_OPCODE);
  2937. if findoverride(actasmpattern,segreg) then
  2938. Begin
  2939. Consume(AS_OPCODE);
  2940. Message(assem_w_repeat_prefix_and_seg_override);
  2941. end;
  2942. end
  2943. else
  2944. { // seg prefix opcode // }
  2945. { // seg opcode // }
  2946. if findoverride(actasmpattern,segreg) then
  2947. Begin
  2948. Consume(AS_OPCODE);
  2949. if findprefix(actasmpattern,asmtok) then
  2950. Begin
  2951. { standard opcode prefix }
  2952. Message(assem_w_repeat_prefix_and_seg_override);
  2953. if asmtok <> A_NONE then
  2954. instr.addprefix(asmtok);
  2955. Consume(AS_OPCODE);
  2956. end;
  2957. end;
  2958. { // opcode // }
  2959. if (actasmtoken <> AS_OPCODE) then
  2960. Begin
  2961. Message(assem_e_invalid_or_missing_opcode);
  2962. { error recovery }
  2963. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2964. Consume(actasmtoken);
  2965. exit;
  2966. end
  2967. else
  2968. Begin
  2969. op := findopcode(actasmpattern);
  2970. instr.addinstr(op);
  2971. { // Valid combination of prefix and instruction ? // }
  2972. if (asmtok <> A_NONE) and (NOT CheckPrefix(asmtok,op)) then
  2973. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  2974. { // Valid combination of segment override // }
  2975. if (segreg <> R_NO) and (NOT CheckOverride(segreg,instr)) then
  2976. Message1(assem_e_invalid_override_and_opcode,actasmpattern);
  2977. Consume(AS_OPCODE);
  2978. { // Zero operand opcode ? // }
  2979. if actasmtoken = AS_SEPARATOR then
  2980. exit
  2981. else
  2982. operandnum := 1;
  2983. end;
  2984. While actasmtoken <> AS_SEPARATOR do
  2985. Begin
  2986. case actasmtoken of
  2987. { // Operand delimiter // }
  2988. AS_COMMA: Begin
  2989. if operandnum > MaxOperands then
  2990. Message(assem_e_too_many_operands)
  2991. else
  2992. Inc(operandnum);
  2993. Consume(AS_COMMA);
  2994. end;
  2995. { // Typecast, Constant Expression, Type Specifier // }
  2996. AS_DWORD,AS_BYTE,AS_WORD,AS_TBYTE,AS_QWORD: Begin
  2997. { tell that the instruction was overriden }
  2998. { so we will NEVER override the opsize }
  2999. instr.operands[operandnum].overriden := TRUE;
  3000. Case actasmtoken of
  3001. AS_DWORD: instr.operands[operandnum].size := S_L;
  3002. AS_WORD: instr.operands[operandnum].size := S_W;
  3003. AS_BYTE: instr.operands[operandnum].size := S_B;
  3004. AS_QWORD: instr.operands[operandnum].size := S_IQ;
  3005. AS_TBYTE: instr.operands[operandnum].size := S_FX;
  3006. end;
  3007. Consume(actasmtoken);
  3008. Case actasmtoken of
  3009. { // Reference // }
  3010. AS_PTR: Begin
  3011. initAsmRef(instr);
  3012. Consume(AS_PTR);
  3013. BuildOperand(instr);
  3014. end;
  3015. { // Possibly a typecast or a constant // }
  3016. { // expression. // }
  3017. AS_LPAREN: Begin
  3018. if actasmtoken = AS_ID then
  3019. Begin
  3020. { Case vartype of }
  3021. { LOCAL: Replace by offset and }
  3022. { BP in treference. }
  3023. { GLOBAL: Replace by mangledname}
  3024. { in symbol of treference }
  3025. { Check if next token = RPAREN }
  3026. { otherwise syntax error. }
  3027. initAsmRef(instr);
  3028. if not CreateVarInstr(instr,actasmpattern,
  3029. operandnum) then
  3030. Begin
  3031. Message1(assem_e_unknown_id,actasmpattern);
  3032. end;
  3033. end
  3034. else
  3035. begin
  3036. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3037. instr.operands[operandnum].val := BuildExpression;
  3038. end;
  3039. end;
  3040. else
  3041. BuildOperand(instr);
  3042. end; { end case }
  3043. end;
  3044. { // Type specifier // }
  3045. AS_NEAR,AS_FAR: Begin
  3046. if actasmtoken = AS_NEAR then
  3047. Message(assem_w_near_ignored)
  3048. else
  3049. Message(assem_w_far_ignored);
  3050. Consume(actasmtoken);
  3051. if actasmtoken = AS_PTR then
  3052. begin
  3053. initAsmRef(instr);
  3054. Consume(AS_PTR);
  3055. end;
  3056. BuildOperand(instr);
  3057. end;
  3058. { // End of asm operands for this opcode // }
  3059. AS_SEPARATOR: ;
  3060. { // Constant expression // }
  3061. AS_LPAREN: Begin
  3062. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3063. instr.operands[operandnum].val := BuildExpression;
  3064. end;
  3065. else
  3066. BuildOperand(instr);
  3067. end; { end case }
  3068. end; { end while }
  3069. end;
  3070. Function Assemble: Ptree;
  3071. {*********************************************************************}
  3072. { PROCEDURE Assemble; }
  3073. { Description: Parses the intel assembler syntax, parsing is done }
  3074. { according to the rules in the Turbo Pascal manual. }
  3075. {*********************************************************************}
  3076. Var
  3077. hl: plabel;
  3078. labelptr: pasmlabel;
  3079. Begin
  3080. Message(assem_d_start_intel);
  3081. inexpression := FALSE;
  3082. firsttoken := TRUE;
  3083. operandnum := 0;
  3084. if assigned(procinfo.retdef) and
  3085. (is_fpu(procinfo.retdef) or
  3086. ret_in_acc(procinfo.retdef)) then
  3087. procinfo.funcret_is_valid:=true;
  3088. { sets up all opcode and register tables in uppercase }
  3089. if not _asmsorted then
  3090. Begin
  3091. SetupTables;
  3092. _asmsorted := TRUE;
  3093. end;
  3094. p:=new(paasmoutput,init);
  3095. { setup label linked list }
  3096. labellist.init;
  3097. c:=current_scanner^.asmgetchar;
  3098. actasmtoken:=gettoken;
  3099. while actasmtoken<>AS_END do
  3100. Begin
  3101. case actasmtoken of
  3102. AS_LLABEL: Begin
  3103. labelptr := labellist.search(actasmpattern);
  3104. if not assigned(labelptr) then
  3105. Begin
  3106. getlabel(hl);
  3107. labellist.insert(actasmpattern,hl,TRUE);
  3108. ConcatLabel(p,A_LABEL,hl);
  3109. end
  3110. else
  3111. { the label has already been inserted into the }
  3112. { label list, either as an intruction label (in }
  3113. { this case it has not been emitted), or as a }
  3114. { duplicate local symbol (in this case it has }
  3115. { already been emitted). }
  3116. Begin
  3117. if labelptr^.emitted then
  3118. Message1(assem_e_dup_local_sym,'@'+labelptr^.name^)
  3119. else
  3120. Begin
  3121. if assigned(labelptr^.lab) then
  3122. ConcatLabel(p,A_LABEL,labelptr^.lab);
  3123. labelptr^.emitted := TRUE;
  3124. end;
  3125. end;
  3126. Consume(AS_LLABEL);
  3127. end;
  3128. AS_LABEL: Begin
  3129. if SearchLabel(actasmpattern,hl) then
  3130. ConcatLabel(p,A_LABEL, hl)
  3131. else
  3132. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3133. Consume(AS_LABEL);
  3134. end;
  3135. AS_DW: Begin
  3136. Consume(AS_DW);
  3137. BuildConstant($ffff);
  3138. end;
  3139. AS_DB: Begin
  3140. Consume(AS_DB);
  3141. BuildConstant($ff);
  3142. end;
  3143. AS_DD: Begin
  3144. Consume(AS_DD);
  3145. BuildConstant($ffffffff);
  3146. end;
  3147. AS_OPCODE: Begin
  3148. instr.init;
  3149. BuildOpcode;
  3150. instr.numops := operandnum;
  3151. if instr.labeled then
  3152. ConcatLabeledInstr(instr)
  3153. else
  3154. ConcatOpCode(instr);
  3155. end;
  3156. AS_SEPARATOR:Begin
  3157. Consume(AS_SEPARATOR);
  3158. { let us go back to the first operand }
  3159. operandnum := 0;
  3160. end;
  3161. AS_END: ; { end assembly block }
  3162. else
  3163. Begin
  3164. Message(assem_e_assemble_node_syntax_error);
  3165. { error recovery }
  3166. Consume(actasmtoken);
  3167. end;
  3168. end; { end case }
  3169. end; { end while }
  3170. { check if there were undefined symbols. }
  3171. { if so, then list each of those undefined }
  3172. { labels. }
  3173. if assigned(labellist.First) then
  3174. Begin
  3175. labelptr := labellist.First;
  3176. if labellist.First <> nil then
  3177. Begin
  3178. { first label }
  3179. if not labelptr^.emitted then
  3180. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3181. { other labels ... }
  3182. While (labelptr^.Next <> nil) do
  3183. Begin
  3184. labelptr := labelptr^.Next;
  3185. if not labelptr^.emitted then
  3186. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3187. end;
  3188. end;
  3189. end;
  3190. assemble := genasmnode(p);
  3191. labellist.done;
  3192. Message(assem_d_finish_intel);
  3193. end;
  3194. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  3195. begin
  3196. if assigned(iasmops) then
  3197. dispose(iasmops);
  3198. exitproc:=old_exit;
  3199. end;
  3200. begin
  3201. old_exit:=exitproc;
  3202. exitproc:=@ra386int_exit;
  3203. end.
  3204. {
  3205. $Log$
  3206. Revision 1.7 1998-09-02 01:23:40 carl
  3207. * bugfix of operand overrides, VERY stupid bugfix BTW...
  3208. Revision 1.6 1998/08/27 00:42:17 carl
  3209. * bugfix of leal problem
  3210. * bugfix of using overrides with record offsets
  3211. * bugfix if using records to load values
  3212. Revision 1.5 1998/08/21 08:45:53 pierre
  3213. * better line info for asm statements
  3214. Revision 1.4 1998/07/14 14:47:00 peter
  3215. * released NEWINPUT
  3216. Revision 1.3 1998/07/07 11:20:09 peter
  3217. + NEWINPUT for a better inputfile and scanner object
  3218. Revision 1.2 1998/06/24 14:06:38 peter
  3219. * fixed the name changes
  3220. Revision 1.1 1998/06/23 14:00:18 peter
  3221. * renamed RA* units
  3222. Revision 1.11 1998/06/16 08:56:28 peter
  3223. + targetcpu
  3224. * cleaner pmodules for newppu
  3225. Revision 1.10 1998/06/12 10:32:33 pierre
  3226. * column problem hopefully solved
  3227. + C vars declaration changed
  3228. Revision 1.9 1998/05/31 14:13:32 peter
  3229. * fixed call bugs with assembler readers
  3230. + OPR_SYMBOL to hold a symbol in the asm parser
  3231. * fixed staticsymtable vars which were acessed through %ebp instead of
  3232. name
  3233. Revision 1.8 1998/05/30 14:31:07 peter
  3234. + $ASMMODE
  3235. Revision 1.7 1998/05/28 16:32:05 carl
  3236. * bugfix with operands main branch version (patched manually)
  3237. Revision 1.6 1998/05/23 01:21:26 peter
  3238. + aktasmmode, aktoptprocessor, aktoutputformat
  3239. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3240. + $LIBNAME to set the library name where the unit will be put in
  3241. * splitted cgi386 a bit (codeseg to large for bp7)
  3242. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3243. Revision 1.5 1998/05/20 09:42:36 pierre
  3244. + UseTokenInfo now default
  3245. * unit in interface uses and implementation uses gives error now
  3246. * only one error for unknown symbol (uses lastsymknown boolean)
  3247. the problem came from the label code !
  3248. + first inlined procedures and function work
  3249. (warning there might be allowed cases were the result is still wrong !!)
  3250. * UseBrower updated gives a global list of all position of all used symbols
  3251. with switch -gb
  3252. Revision 1.4 1998/04/29 10:34:03 pierre
  3253. + added some code for ansistring (not complete nor working yet)
  3254. * corrected operator overloading
  3255. * corrected nasm output
  3256. + started inline procedures
  3257. + added starstarn : use ** for exponentiation (^ gave problems)
  3258. + started UseTokenInfo cond to get accurate positions
  3259. Revision 1.3 1998/04/08 16:58:06 pierre
  3260. * several bugfixes
  3261. ADD ADC and AND are also sign extended
  3262. nasm output OK (program still crashes at end
  3263. and creates wrong assembler files !!)
  3264. procsym types sym in tdef removed !!
  3265. }