ra386int.pas 137 KB

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