ra386int.pas 138 KB

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