ra386int.pas 136 KB

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