rax86int.pas 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing process for the intel styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit Rax86int;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. cpubase,
  23. globtype,
  24. aasmbase,
  25. cgbase,
  26. rasm,
  27. rax86;
  28. type
  29. tasmtoken = (
  30. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  31. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  32. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  33. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  34. {------------------ Assembler directives --------------------}
  35. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_DQ,AS_PUBLIC,AS_END,
  36. {------------------ Assembler Operators --------------------}
  37. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_NEAR,AS_FAR,
  38. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  39. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL,AS_TARGET_DIRECTIVE);
  40. type
  41. tx86intreader = class(tasmreader)
  42. actasmpattern_origcase : string;
  43. actasmtoken : tasmtoken;
  44. prevasmtoken : tasmtoken;
  45. ActOpsize : topsize;
  46. inexpression : boolean;
  47. constructor create;override;
  48. function is_asmopcode(const s: string):boolean;
  49. function is_asmoperator(const s: string):boolean;
  50. function is_asmdirective(const s: string):boolean;
  51. function is_register(const s:string):boolean;
  52. function is_locallabel(const s:string):boolean;
  53. function Assemble: tlinkedlist;override;
  54. procedure GetToken;
  55. function consume(t : tasmtoken):boolean;
  56. procedure RecoverConsume(allowcomma:boolean);
  57. procedure AddReferences(dest,src : tx86operand);
  58. procedure SetSegmentOverride(oper:tx86operand;seg:tregister);
  59. procedure BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  60. procedure BuildConstSymbolExpression(needofs,isref,startingminus:boolean;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out isseg,is_farproc_entry,hasofs:boolean);
  61. function BuildConstExpression:aint;
  62. function BuildRefConstExpression(out size:tcgint;startingminus:boolean=false):aint;
  63. procedure BuildReference(oper : tx86operand);
  64. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  65. procedure BuildConstantOperand(oper: tx86operand);
  66. procedure BuildOpCode(instr : tx86instruction);
  67. procedure BuildConstant(constsize: byte);
  68. function is_targetdirective(const s: string): boolean;virtual;
  69. procedure HandleTargetDirective;virtual;
  70. end;
  71. implementation
  72. uses
  73. { common }
  74. cutils,
  75. { global }
  76. globals,verbose,
  77. systems,
  78. { aasm }
  79. aasmdata,aasmcpu,
  80. {$ifdef i8086}
  81. aasmtai,
  82. {$endif i8086}
  83. { symtable }
  84. symconst,symbase,symtype,symsym,symdef,
  85. {$ifdef i8086}
  86. symcpu,
  87. {$endif i8086}
  88. { parser }
  89. scanner,pbase,
  90. { register allocator }
  91. rautils,itx86int,
  92. { codegen }
  93. procinfo,paramgr
  94. ;
  95. type
  96. tasmkeyword = string[9];
  97. const
  98. { These tokens should be modified accordingly to the modifications }
  99. { in the different enumerations. }
  100. firstdirective = AS_ALIGN;
  101. lastdirective = AS_END;
  102. firstoperator = AS_BYTE;
  103. lastoperator = AS___GOTPCREL;
  104. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  105. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  106. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  107. ('ALIGN','DB','DW','DD','DQ','PUBLIC','END');
  108. { problems with shl,shr,not,and,or and xor, they are }
  109. { context sensitive. }
  110. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  111. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','NEAR','FAR','HIGH',
  112. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  113. 'OR','XOR','WRT','GOTPCREL');
  114. token2str : array[tasmtoken] of string[10] = (
  115. '','Label','LLabel','String','Integer',
  116. ',','[',']','(',
  117. ')',':','.','+','-','*',
  118. ';','identifier','register','opcode','/',
  119. '','','','','','','END',
  120. '','','','','','','','','','','','',
  121. '','','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  122. 'and','or','xor','wrt','..gotpcrel',''
  123. );
  124. constructor tx86intreader.create;
  125. var
  126. i : tasmop;
  127. Begin
  128. inherited create;
  129. iasmops:=TFPHashList.create;
  130. for i:=firstop to lastop do
  131. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  132. end;
  133. {---------------------------------------------------------------------}
  134. { Routines for the tokenizing }
  135. {---------------------------------------------------------------------}
  136. function tx86intreader.is_asmopcode(const s: string):boolean;
  137. var
  138. cond : string[4];
  139. cnd : tasmcond;
  140. j: longint;
  141. Begin
  142. is_asmopcode:=FALSE;
  143. actopcode:=A_None;
  144. actcondition:=C_None;
  145. actopsize:=S_NO;
  146. { Search opcodes }
  147. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  148. if actopcode<>A_NONE then
  149. begin
  150. actasmtoken:=AS_OPCODE;
  151. result:=TRUE;
  152. exit;
  153. end;
  154. { not found yet, check condition opcodes }
  155. j:=0;
  156. while (j<CondAsmOps) do
  157. begin
  158. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  159. begin
  160. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  161. if cond<>'' then
  162. begin
  163. for cnd:=low(TasmCond) to high(TasmCond) do
  164. if Cond=Upper(cond2str[cnd]) then
  165. begin
  166. actopcode:=CondASmOp[j];
  167. actcondition:=cnd;
  168. is_asmopcode:=TRUE;
  169. actasmtoken:=AS_OPCODE;
  170. exit
  171. end;
  172. end;
  173. end;
  174. inc(j);
  175. end;
  176. end;
  177. function tx86intreader.is_asmoperator(const s: string):boolean;
  178. var
  179. i : longint;
  180. Begin
  181. for i:=0 to _count_asmoperators do
  182. if s=_asmoperators[i] then
  183. begin
  184. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  185. is_asmoperator:=true;
  186. exit;
  187. end;
  188. is_asmoperator:=false;
  189. end;
  190. Function tx86intreader.is_asmdirective(const s: string):boolean;
  191. var
  192. i : longint;
  193. Begin
  194. for i:=0 to _count_asmdirectives do
  195. if s=_asmdirectives[i] then
  196. begin
  197. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  198. is_asmdirective:=true;
  199. exit;
  200. end;
  201. is_asmdirective:=false;
  202. end;
  203. function tx86intreader.is_register(const s:string):boolean;
  204. begin
  205. is_register:=false;
  206. actasmregister:=masm_regnum_search(lower(s));
  207. { don't acceps "flags" as register name in an instruction }
  208. if (getsupreg(actasmregister)=RS_DEFAULTFLAGS) and (getregtype(actasmregister)=getregtype(NR_DEFAULTFLAGS)) then
  209. actasmregister:=NR_NO;
  210. if actasmregister<>NR_NO then
  211. begin
  212. is_register:=true;
  213. actasmtoken:=AS_REGISTER;
  214. end;
  215. end;
  216. function tx86intreader.is_locallabel(const s:string):boolean;
  217. begin
  218. is_locallabel:=(length(s)>1) and (s[1]='@');
  219. end;
  220. function tx86intreader.is_targetdirective(const s: string): boolean;
  221. begin
  222. result:=false;
  223. end;
  224. procedure tx86intreader.handletargetdirective;
  225. begin
  226. end;
  227. Procedure tx86intreader.GetToken;
  228. var
  229. len : longint;
  230. forcelabel : boolean;
  231. srsym : tsym;
  232. srsymtable : TSymtable;
  233. begin
  234. c:=scanner.c;
  235. { save old token and reset new token }
  236. prevasmtoken:=actasmtoken;
  237. actasmtoken:=AS_NONE;
  238. { reset }
  239. forcelabel:=FALSE;
  240. actasmpattern:='';
  241. { while space and tab , continue scan... }
  242. while (c in [' ',#9]) do
  243. c:=current_scanner.asmgetchar;
  244. { get token pos }
  245. if not (c in [#10,#13,'{',';','(','/']) then
  246. current_scanner.gettokenpos;
  247. { Local Label, Label, Directive, Prefix or Opcode }
  248. if firsttoken and not (c in [#10,#13,'{',';','(','/']) then
  249. begin
  250. firsttoken:=FALSE;
  251. len:=0;
  252. { directive check }
  253. if c = '.' then
  254. begin
  255. actasmpattern:='.';
  256. c:=current_scanner.asmgetchar;
  257. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  258. begin
  259. actasmpattern:=actasmpattern+c;
  260. c:=current_scanner.asmgetchar;
  261. end;
  262. { directives are case sensitive!! }
  263. if is_asmdirective(actasmpattern) then
  264. exit;
  265. if is_targetdirective(actasmpattern) then
  266. begin
  267. actasmtoken:=AS_TARGET_DIRECTIVE;
  268. exit;
  269. end;
  270. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  271. exit;
  272. end;
  273. while (c in ['A'..'Z','a'..'z','0'..'9','_','@']) or
  274. { TP7 also allows $&? characters in local labels }
  275. (forcelabel and (c in ['$','&','?'])) do
  276. begin
  277. { if there is an at_sign, then this must absolutely be a label }
  278. if c = '@' then
  279. forcelabel:=TRUE;
  280. inc(len);
  281. actasmpattern[len]:=c;
  282. c:=current_scanner.asmgetchar;
  283. end;
  284. actasmpattern[0]:=chr(len);
  285. actasmpattern_origcase:=actasmpattern;
  286. uppervar(actasmpattern);
  287. { allow spaces }
  288. while (c in [' ',#9]) do
  289. c:=current_scanner.asmgetchar;
  290. { label ? }
  291. if c = ':' then
  292. begin
  293. if actasmpattern[1]='@' then
  294. actasmtoken:=AS_LLABEL
  295. else
  296. actasmtoken:=AS_LABEL;
  297. { let us point to the next character }
  298. c:=current_scanner.asmgetchar;
  299. firsttoken:=true;
  300. exit;
  301. end;
  302. { Are we trying to create an identifier with }
  303. { an at-sign...? }
  304. if forcelabel then
  305. Message(asmr_e_none_label_contain_at);
  306. { opcode ? }
  307. If is_asmopcode(actasmpattern) then
  308. Begin
  309. { check if we are in an expression }
  310. { then continue with asm directives }
  311. if not inexpression then
  312. exit;
  313. end;
  314. if is_asmdirective(actasmpattern) then
  315. exit;
  316. message1(asmr_e_unknown_opcode,actasmpattern);
  317. actasmtoken:=AS_NONE;
  318. exit;
  319. end
  320. else { else firsttoken }
  321. begin
  322. case c of
  323. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  324. { - @Result, @Code or @Data special variables. }
  325. begin
  326. actasmpattern:=c;
  327. c:=current_scanner.asmgetchar;
  328. while c in ['A'..'Z','a'..'z','0'..'9','_','@','$','&','?'] do
  329. begin
  330. actasmpattern:=actasmpattern + c;
  331. c:=current_scanner.asmgetchar;
  332. end;
  333. actasmpattern_origcase:=actasmpattern;
  334. uppervar(actasmpattern);
  335. actasmtoken:=AS_ID;
  336. exit;
  337. end;
  338. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  339. begin
  340. actasmpattern:=c;
  341. c:=current_scanner.asmgetchar;
  342. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  343. begin
  344. actasmpattern:=actasmpattern + c;
  345. c:=current_scanner.asmgetchar;
  346. end;
  347. actasmpattern_origcase:=actasmpattern;
  348. uppervar(actasmpattern);
  349. { after prefix (or segment override) we allow also a new opcode }
  350. If (is_prefix(actopcode) or is_override(actopcode)) and is_asmopcode(actasmpattern) then
  351. Begin
  352. { if we are not in a constant }
  353. { expression than this is an }
  354. { opcode. }
  355. if not inexpression then
  356. exit;
  357. end;
  358. { support st(X) for fpu registers }
  359. if (actasmpattern = 'ST') and (c='(') then
  360. Begin
  361. actasmpattern:=actasmpattern+c;
  362. c:=current_scanner.asmgetchar;
  363. { allow spaces }
  364. while (c in [' ',#9]) do
  365. c:=current_scanner.asmgetchar;
  366. if c in ['0'..'7'] then
  367. actasmpattern:=actasmpattern + c
  368. else
  369. Message(asmr_e_invalid_fpu_register);
  370. c:=current_scanner.asmgetchar;
  371. { allow spaces }
  372. while (c in [' ',#9]) do
  373. c:=current_scanner.asmgetchar;
  374. if c <> ')' then
  375. Message(asmr_e_invalid_fpu_register)
  376. else
  377. Begin
  378. actasmpattern:=actasmpattern + c;
  379. c:=current_scanner.asmgetchar;
  380. end;
  381. end;
  382. if is_asmdirective(actasmpattern) then
  383. exit;
  384. if is_asmoperator(actasmpattern) then
  385. exit;
  386. if is_register(actasmpattern) then
  387. exit;
  388. { allow spaces }
  389. while (c in [' ',#9]) do
  390. c:=current_scanner.asmgetchar;
  391. { if next is a '.' and this is a unitsym then we also need to
  392. parse the identifier }
  393. if (c='.') then
  394. begin
  395. asmsearchsym(actasmpattern,srsym,srsymtable);
  396. if assigned(srsym) and
  397. (srsym.typ=unitsym) and
  398. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  399. srsym.owner.iscurrentunit then
  400. begin
  401. { Add . to create System.Identifier }
  402. actasmpattern:=actasmpattern+c;
  403. c:=current_scanner.asmgetchar;
  404. { Delphi allows System.@Halt, just ignore the @ }
  405. if c='@' then
  406. c:=current_scanner.asmgetchar;
  407. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  408. begin
  409. actasmpattern:=actasmpattern + upcase(c);
  410. c:=current_scanner.asmgetchar;
  411. end;
  412. end;
  413. end;
  414. actasmtoken:=AS_ID;
  415. exit;
  416. end;
  417. '''' : { string or character }
  418. begin
  419. actasmpattern:='';
  420. repeat
  421. if c = '''' then
  422. begin
  423. c:=current_scanner.asmgetchar;
  424. if c in [#10,#13] then
  425. begin
  426. Message(scan_f_string_exceeds_line);
  427. break;
  428. end;
  429. repeat
  430. if c='''' then
  431. begin
  432. c:=current_scanner.asmgetchar;
  433. if c='''' then
  434. begin
  435. actasmpattern:=actasmpattern+'''';
  436. c:=current_scanner.asmgetchar;
  437. if c in [#10,#13] then
  438. begin
  439. Message(scan_f_string_exceeds_line);
  440. break;
  441. end;
  442. end
  443. else
  444. break;
  445. end
  446. else
  447. begin
  448. actasmpattern:=actasmpattern+c;
  449. c:=current_scanner.asmgetchar;
  450. if c in [#10,#13] then
  451. begin
  452. Message(scan_f_string_exceeds_line);
  453. break
  454. end;
  455. end;
  456. until false; { end repeat }
  457. end
  458. else
  459. break; { end if }
  460. until false;
  461. actasmtoken:=AS_STRING;
  462. exit;
  463. end;
  464. '"' : { string or character }
  465. begin
  466. actasmpattern:='';
  467. repeat
  468. if c = '"' then
  469. begin
  470. c:=current_scanner.asmgetchar;
  471. if c in [#10,#13] then
  472. begin
  473. Message(scan_f_string_exceeds_line);
  474. break;
  475. end;
  476. repeat
  477. if c='"' then
  478. begin
  479. c:=current_scanner.asmgetchar;
  480. if c='"' then
  481. begin
  482. actasmpattern:=actasmpattern+'"';
  483. c:=current_scanner.asmgetchar;
  484. if c in [#10,#13] then
  485. begin
  486. Message(scan_f_string_exceeds_line);
  487. break;
  488. end;
  489. end
  490. else
  491. break;
  492. end
  493. else
  494. begin
  495. actasmpattern:=actasmpattern+c;
  496. c:=current_scanner.asmgetchar;
  497. if c in [#10,#13] then
  498. begin
  499. Message(scan_f_string_exceeds_line);
  500. break
  501. end;
  502. end;
  503. until false; { end repeat }
  504. end
  505. else
  506. break; { end if }
  507. until false;
  508. actasmtoken:=AS_STRING;
  509. exit;
  510. end;
  511. '$' :
  512. begin
  513. c:=current_scanner.asmgetchar;
  514. while c in ['0'..'9','A'..'F','a'..'f'] do
  515. begin
  516. actasmpattern:=actasmpattern + c;
  517. c:=current_scanner.asmgetchar;
  518. end;
  519. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  520. actasmtoken:=AS_INTNUM;
  521. exit;
  522. end;
  523. '&' : { identifier }
  524. begin
  525. actasmpattern:='';
  526. c:=current_scanner.asmgetchar;
  527. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  528. begin
  529. actasmpattern:=actasmpattern + c;
  530. c:=current_scanner.asmgetchar;
  531. end;
  532. actasmpattern_origcase:=actasmpattern;
  533. uppervar(actasmpattern);
  534. actasmtoken:=AS_ID;
  535. exit;
  536. end;
  537. ',' :
  538. begin
  539. actasmtoken:=AS_COMMA;
  540. c:=current_scanner.asmgetchar;
  541. exit;
  542. end;
  543. '[' :
  544. begin
  545. actasmtoken:=AS_LBRACKET;
  546. c:=current_scanner.asmgetchar;
  547. exit;
  548. end;
  549. ']' :
  550. begin
  551. actasmtoken:=AS_RBRACKET;
  552. c:=current_scanner.asmgetchar;
  553. exit;
  554. end;
  555. '(' :
  556. begin
  557. c:=current_scanner.asmgetchar;
  558. if c='*' then
  559. begin
  560. current_scanner.skipoldtpcomment(true);
  561. GetToken;
  562. end
  563. else
  564. actasmtoken:=AS_LPAREN;
  565. exit;
  566. end;
  567. ')' :
  568. begin
  569. actasmtoken:=AS_RPAREN;
  570. c:=current_scanner.asmgetchar;
  571. exit;
  572. end;
  573. ':' :
  574. begin
  575. actasmtoken:=AS_COLON;
  576. c:=current_scanner.asmgetchar;
  577. exit;
  578. end;
  579. '.' :
  580. begin
  581. c:=current_scanner.asmgetchar;
  582. {$ifdef x86_64}
  583. if c='.' then
  584. begin
  585. actasmpattern:='..';
  586. c:=current_scanner.asmgetchar;
  587. repeat
  588. actasmpattern:=actasmpattern+c;
  589. c:=current_scanner.asmgetchar;
  590. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  591. if upper(actasmpattern)<>'..GOTPCREL' then
  592. begin
  593. actasmtoken:=AS_ID;
  594. consume(AS___GOTPCREL);
  595. end;
  596. actasmtoken:=AS___GOTPCREL
  597. end
  598. else
  599. {$endif x86_64}
  600. actasmtoken:=AS_DOT;
  601. exit;
  602. end;
  603. '+' :
  604. begin
  605. actasmtoken:=AS_PLUS;
  606. c:=current_scanner.asmgetchar;
  607. exit;
  608. end;
  609. '-' :
  610. begin
  611. actasmtoken:=AS_MINUS;
  612. c:=current_scanner.asmgetchar;
  613. exit;
  614. end;
  615. '*' :
  616. begin
  617. actasmtoken:=AS_STAR;
  618. c:=current_scanner.asmgetchar;
  619. exit;
  620. end;
  621. '/' :
  622. begin
  623. c:=current_scanner.asmgetchar;
  624. if c='/' then
  625. begin
  626. current_scanner.skipdelphicomment;
  627. GetToken;
  628. end
  629. else
  630. actasmtoken:=AS_SLASH;
  631. exit;
  632. end;
  633. '0'..'9':
  634. begin
  635. actasmpattern:=c;
  636. c:=current_scanner.asmgetchar;
  637. { Get the possible characters }
  638. while c in ['0'..'9','A'..'F','a'..'f'] do
  639. begin
  640. actasmpattern:=actasmpattern + c;
  641. c:=current_scanner.asmgetchar;
  642. end;
  643. { Get ending character }
  644. actasmpattern_origcase:=actasmpattern;
  645. uppervar(actasmpattern);
  646. c:=upcase(c);
  647. { possibly a binary number. }
  648. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  649. Begin
  650. { Delete the last binary specifier }
  651. delete(actasmpattern,length(actasmpattern),1);
  652. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  653. actasmtoken:=AS_INTNUM;
  654. exit;
  655. end
  656. else
  657. Begin
  658. case c of
  659. 'O' :
  660. Begin
  661. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  662. actasmtoken:=AS_INTNUM;
  663. c:=current_scanner.asmgetchar;
  664. exit;
  665. end;
  666. 'H' :
  667. Begin
  668. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  669. actasmtoken:=AS_INTNUM;
  670. c:=current_scanner.asmgetchar;
  671. exit;
  672. end;
  673. else { must be an integer number }
  674. begin
  675. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  676. actasmtoken:=AS_INTNUM;
  677. exit;
  678. end;
  679. end;
  680. end;
  681. end;
  682. #13,#10:
  683. begin
  684. current_scanner.linebreak;
  685. c:=current_scanner.asmgetchar;
  686. firsttoken:=TRUE;
  687. actasmtoken:=AS_SEPARATOR;
  688. exit;
  689. end;
  690. ';':
  691. begin
  692. c:=current_scanner.asmgetchar;
  693. firsttoken:=TRUE;
  694. actasmtoken:=AS_SEPARATOR;
  695. exit;
  696. end;
  697. '{':
  698. begin
  699. current_scanner.skipcomment(true);
  700. GetToken;
  701. end;
  702. else
  703. current_scanner.illegal_char(c);
  704. end;
  705. end;
  706. end;
  707. function tx86intreader.consume(t : tasmtoken):boolean;
  708. begin
  709. Consume:=true;
  710. if t<>actasmtoken then
  711. begin
  712. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  713. Consume:=false;
  714. end;
  715. repeat
  716. gettoken;
  717. until actasmtoken<>AS_NONE;
  718. end;
  719. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  720. begin
  721. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  722. begin
  723. if allowcomma and (actasmtoken=AS_COMMA) then
  724. break;
  725. Consume(actasmtoken);
  726. end;
  727. end;
  728. {*****************************************************************************
  729. Parsing Helpers
  730. *****************************************************************************}
  731. { Adds two references (dest:=dest+src) }
  732. procedure tx86intreader.AddReferences(dest,src : tx86operand);
  733. procedure AddRegister(reg:tregister;scalefactor:byte);
  734. begin
  735. if reg=NR_NO then
  736. exit;
  737. if (dest.opr.ref.base=NR_NO) and (scalefactor=1) then
  738. begin
  739. dest.opr.ref.base:=reg;
  740. exit;
  741. end;
  742. if dest.opr.ref.index=NR_NO then
  743. begin
  744. dest.opr.ref.index:=reg;
  745. dest.opr.ref.scalefactor:=scalefactor;
  746. exit;
  747. end;
  748. if dest.opr.ref.index=reg then
  749. begin
  750. Inc(dest.opr.ref.scalefactor,scalefactor);
  751. exit;
  752. end;
  753. Message(asmr_e_multiple_index);
  754. end;
  755. var
  756. tmplocal: TOprRec;
  757. segreg: TRegister;
  758. begin
  759. case dest.opr.typ of
  760. OPR_REFERENCE:
  761. begin
  762. case src.opr.typ of
  763. OPR_REFERENCE:
  764. begin
  765. AddRegister(src.opr.ref.base,1);
  766. AddRegister(src.opr.ref.index,src.opr.ref.scalefactor);
  767. if src.opr.ref.segment<>NR_NO then
  768. SetSegmentOverride(dest,src.opr.ref.segment);
  769. Inc(dest.opr.ref.offset,src.opr.ref.offset);
  770. Inc(dest.opr.constoffset,src.opr.constoffset);
  771. dest.haslabelref:=dest.haslabelref or src.haslabelref;
  772. dest.hasproc:=dest.hasproc or src.hasproc;
  773. dest.hasvar:=dest.hasvar or src.hasvar;
  774. if assigned(src.opr.ref.symbol) then
  775. begin
  776. if assigned(dest.opr.ref.symbol) then
  777. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  778. dest.opr.ref.symbol:=src.opr.ref.symbol;
  779. end;
  780. if assigned(src.opr.ref.relsymbol) then
  781. begin
  782. if assigned(dest.opr.ref.relsymbol) then
  783. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  784. dest.opr.ref.relsymbol:=src.opr.ref.relsymbol;
  785. end;
  786. if dest.opr.ref.refaddr=addr_no then
  787. dest.opr.ref.refaddr:=src.opr.ref.refaddr;
  788. end;
  789. OPR_LOCAL:
  790. begin
  791. tmplocal:=src.opr;
  792. if dest.opr.ref.base<>NR_NO then
  793. begin
  794. if tmplocal.localindexreg=NR_NO then
  795. begin
  796. tmplocal.localindexreg:=dest.opr.ref.base;
  797. tmplocal.localscale:=0;
  798. end
  799. else if tmplocal.localindexreg=dest.opr.ref.base then
  800. tmplocal.localscale:=Min(tmplocal.localscale,1)+1
  801. else
  802. Message(asmr_e_multiple_index);
  803. end;
  804. if dest.opr.ref.index<>NR_NO then
  805. begin
  806. if tmplocal.localindexreg=NR_NO then
  807. begin
  808. tmplocal.localindexreg:=dest.opr.ref.index;
  809. tmplocal.localscale:=dest.opr.ref.scalefactor;
  810. end
  811. else if tmplocal.localindexreg=dest.opr.ref.index then
  812. tmplocal.localscale:=Min(tmplocal.localscale,1)+Min(dest.opr.ref.scalefactor,1)
  813. else
  814. Message(asmr_e_multiple_index);
  815. end;
  816. Inc(tmplocal.localconstoffset,dest.opr.constoffset);
  817. Inc(tmplocal.localsymofs,dest.opr.ref.offset);
  818. segreg:=dest.opr.ref.segment;
  819. dest.opr:=tmplocal;
  820. if segreg<>NR_NO then
  821. SetSegmentOverride(dest,segreg);
  822. end;
  823. else
  824. internalerror(2018030701);
  825. end;
  826. end;
  827. OPR_LOCAL:
  828. begin
  829. case src.opr.typ of
  830. OPR_REFERENCE:
  831. begin
  832. if src.opr.ref.base<>NR_NO then
  833. begin
  834. if dest.opr.localindexreg=NR_NO then
  835. begin
  836. dest.opr.localindexreg:=src.opr.ref.base;
  837. dest.opr.localscale:=0;
  838. end
  839. else if dest.opr.localindexreg=src.opr.ref.base then
  840. dest.opr.localscale:=Min(dest.opr.localscale,1)+1
  841. else
  842. Message(asmr_e_multiple_index);
  843. end;
  844. if src.opr.ref.index<>NR_NO then
  845. begin
  846. if dest.opr.localindexreg=NR_NO then
  847. begin
  848. dest.opr.localindexreg:=src.opr.ref.index;
  849. dest.opr.localscale:=src.opr.ref.scalefactor;
  850. end
  851. else if dest.opr.localindexreg=src.opr.ref.index then
  852. dest.opr.localscale:=Min(dest.opr.localscale,1)+Min(src.opr.ref.scalefactor,1)
  853. else
  854. Message(asmr_e_multiple_index);
  855. end;
  856. Inc(dest.opr.localconstoffset,src.opr.constoffset);
  857. Inc(dest.opr.localsymofs,src.opr.ref.offset);
  858. if src.opr.ref.segment<>NR_NO then
  859. SetSegmentOverride(dest,src.opr.ref.segment);
  860. end;
  861. else
  862. internalerror(2018030703);
  863. end;
  864. end;
  865. else
  866. internalerror(2018030702);
  867. end;
  868. end;
  869. procedure tx86intreader.SetSegmentOverride(oper:tx86operand;seg:tregister);
  870. begin
  871. if not is_segment_reg(seg) then
  872. Message(asmr_e_invalid_seg_override);
  873. case oper.opr.typ of
  874. OPR_REFERENCE:
  875. begin
  876. if oper.opr.ref.segment<>NR_NO then
  877. begin
  878. if m_tp7 in current_settings.modeswitches then
  879. Message(asmr_w_multiple_segment_overrides)
  880. else
  881. Message(asmr_e_multiple_segment_overrides);
  882. end;
  883. oper.opr.ref.segment:=seg;
  884. end;
  885. OPR_LOCAL:
  886. begin
  887. if oper.opr.localsegment<>NR_NO then
  888. begin
  889. if m_tp7 in current_settings.modeswitches then
  890. Message(asmr_w_multiple_segment_overrides)
  891. else
  892. Message(asmr_e_multiple_segment_overrides);
  893. end;
  894. oper.opr.localsegment:=seg;
  895. end;
  896. else
  897. internalerror(2018030703);
  898. end;
  899. end;
  900. { This routine builds up a record offset after a AS_DOT
  901. token is encountered.
  902. On entry actasmtoken should be equal to AS_DOT }
  903. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  904. var
  905. s: string;
  906. Begin
  907. offset:=0;
  908. size:=0;
  909. mangledname:='';
  910. hastypecast:=false;
  911. s:=expr;
  912. while (actasmtoken=AS_DOT) do
  913. begin
  914. Consume(AS_DOT);
  915. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_REGISTER] then
  916. begin
  917. s:=s+'.'+actasmpattern;
  918. consume(actasmtoken);
  919. end
  920. else
  921. begin
  922. Consume(AS_ID);
  923. RecoverConsume(true);
  924. break;
  925. end;
  926. end;
  927. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  928. Message(asmr_e_building_record_offset);
  929. end;
  930. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out isseg,is_farproc_entry,hasofs:boolean);
  931. var
  932. tempstr,expr,hs,mangledname : string;
  933. parenlevel : longint;
  934. l,k : tcgint;
  935. hasparen,
  936. errorflag,
  937. needvmtofs : boolean;
  938. prevtok : tasmtoken;
  939. hl : tasmlabel;
  940. hssymtyp : Tasmsymtype;
  941. def : tdef;
  942. sym : tsym;
  943. srsymtable : TSymtable;
  944. hastypecast : boolean;
  945. Begin
  946. { reset }
  947. value:=0;
  948. asmsym:='';
  949. asmsymtyp:=AT_DATA;
  950. size:=0;
  951. isseg:=false;
  952. is_farproc_entry:=FALSE;
  953. hasofs:=FALSE;
  954. errorflag:=FALSE;
  955. tempstr:='';
  956. expr:='';
  957. if startingminus then
  958. expr:='-';
  959. inexpression:=TRUE;
  960. parenlevel:=0;
  961. sym:=nil;
  962. needvmtofs:=FALSE;
  963. Repeat
  964. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  965. if isref and (actasmtoken=AS_LBRACKET) then
  966. break;
  967. Case actasmtoken of
  968. AS_LPAREN:
  969. Begin
  970. Consume(AS_LPAREN);
  971. expr:=expr + '(';
  972. inc(parenlevel);
  973. end;
  974. AS_RPAREN:
  975. Begin
  976. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  977. if parenlevel=0 then
  978. break;
  979. Consume(AS_RPAREN);
  980. expr:=expr + ')';
  981. dec(parenlevel);
  982. end;
  983. AS_SHL:
  984. Begin
  985. Consume(AS_SHL);
  986. expr:=expr + '<';
  987. end;
  988. AS_SHR:
  989. Begin
  990. Consume(AS_SHR);
  991. expr:=expr + '>';
  992. end;
  993. AS_SLASH:
  994. Begin
  995. Consume(AS_SLASH);
  996. expr:=expr + '/';
  997. end;
  998. AS_MOD:
  999. Begin
  1000. Consume(AS_MOD);
  1001. expr:=expr + '%';
  1002. end;
  1003. AS_STAR:
  1004. Begin
  1005. Consume(AS_STAR);
  1006. if isref and (actasmtoken=AS_REGISTER) then
  1007. break;
  1008. expr:=expr + '*';
  1009. end;
  1010. AS_PLUS:
  1011. Begin
  1012. Consume(AS_PLUS);
  1013. if isref and ((actasmtoken=AS_REGISTER) or (actasmtoken=AS_LBRACKET)) then
  1014. break;
  1015. expr:=expr + '+';
  1016. end;
  1017. AS_MINUS:
  1018. Begin
  1019. Consume(AS_MINUS);
  1020. expr:=expr + '-';
  1021. end;
  1022. AS_AND:
  1023. Begin
  1024. Consume(AS_AND);
  1025. expr:=expr + '&';
  1026. end;
  1027. AS_NOT:
  1028. Begin
  1029. Consume(AS_NOT);
  1030. expr:=expr + '~';
  1031. end;
  1032. AS_XOR:
  1033. Begin
  1034. Consume(AS_XOR);
  1035. expr:=expr + '^';
  1036. end;
  1037. AS_OR:
  1038. Begin
  1039. Consume(AS_OR);
  1040. expr:=expr + '|';
  1041. end;
  1042. AS_INTNUM:
  1043. Begin
  1044. expr:=expr + actasmpattern;
  1045. Consume(AS_INTNUM);
  1046. end;
  1047. {$ifdef i8086}
  1048. AS_SEG:
  1049. begin
  1050. isseg:=true;
  1051. Consume(actasmtoken);
  1052. if actasmtoken<>AS_ID then
  1053. Message(asmr_e_seg_without_identifier);
  1054. end;
  1055. {$endif i8086}
  1056. AS_VMTOFFSET,
  1057. AS_OFFSET:
  1058. begin
  1059. if (actasmtoken = AS_OFFSET) then
  1060. begin
  1061. needofs:=true;
  1062. hasofs:=true;
  1063. end
  1064. else
  1065. needvmtofs:=true;
  1066. Consume(actasmtoken);
  1067. if actasmtoken<>AS_ID then
  1068. Message(asmr_e_offset_without_identifier);
  1069. end;
  1070. AS_SIZEOF,
  1071. AS_TYPE:
  1072. begin
  1073. l:=0;
  1074. hasparen:=false;
  1075. Consume(actasmtoken);
  1076. if actasmtoken=AS_LPAREN then
  1077. begin
  1078. hasparen:=true;
  1079. Consume(AS_LPAREN);
  1080. end;
  1081. if actasmtoken<>AS_ID then
  1082. Message(asmr_e_type_without_identifier)
  1083. else
  1084. begin
  1085. tempstr:=actasmpattern;
  1086. Consume(AS_ID);
  1087. if actasmtoken=AS_DOT then
  1088. begin
  1089. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  1090. if mangledname<>'' then
  1091. { procsym }
  1092. Message(asmr_e_wrong_sym_type);
  1093. if hastypecast then
  1094. end
  1095. else
  1096. begin
  1097. asmsearchsym(tempstr,sym,srsymtable);
  1098. if assigned(sym) then
  1099. begin
  1100. case sym.typ of
  1101. staticvarsym,
  1102. localvarsym,
  1103. paravarsym :
  1104. l:=tabstractvarsym(sym).getsize;
  1105. typesym :
  1106. l:=ttypesym(sym).typedef.size;
  1107. else
  1108. Message(asmr_e_wrong_sym_type);
  1109. end;
  1110. end
  1111. else
  1112. Message1(sym_e_unknown_id,tempstr);
  1113. end;
  1114. end;
  1115. str(l, tempstr);
  1116. expr:=expr + tempstr;
  1117. if hasparen then
  1118. Consume(AS_RPAREN);
  1119. end;
  1120. AS_PTR :
  1121. begin
  1122. { Support ugly delphi constructs like <constant> PTR [ref] }
  1123. break;
  1124. end;
  1125. AS_STRING:
  1126. begin
  1127. l:=0;
  1128. case Length(actasmpattern) of
  1129. 1 :
  1130. l:=ord(actasmpattern[1]);
  1131. 2 :
  1132. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1133. 3 :
  1134. l:=ord(actasmpattern[3]) +
  1135. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1136. 4 :
  1137. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1138. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1139. else
  1140. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1141. end;
  1142. str(l, tempstr);
  1143. expr:=expr + tempstr;
  1144. Consume(AS_STRING);
  1145. end;
  1146. AS_ID:
  1147. begin
  1148. hs:='';
  1149. hssymtyp:=AT_DATA;
  1150. def:=nil;
  1151. tempstr:=actasmpattern;
  1152. prevtok:=prevasmtoken;
  1153. { stop parsing a constant expression if we find an opcode after a
  1154. non-operator like "db $66 mov eax,ebx" }
  1155. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  1156. is_asmopcode(actasmpattern) then
  1157. break;
  1158. consume(AS_ID);
  1159. if (tempstr='@CODE') or (tempstr='@DATA') then
  1160. begin
  1161. if asmsym='' then
  1162. begin
  1163. asmsym:=tempstr;
  1164. asmsymtyp:=AT_SECTION;
  1165. end
  1166. else
  1167. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1168. end
  1169. else if SearchIConstant(tempstr,l) then
  1170. begin
  1171. str(l, tempstr);
  1172. expr:=expr + tempstr;
  1173. end
  1174. else
  1175. begin
  1176. if is_locallabel(tempstr) then
  1177. begin
  1178. CreateLocalLabel(tempstr,hl,false);
  1179. hs:=hl.name;
  1180. hssymtyp:=AT_FUNCTION;
  1181. end
  1182. else
  1183. if SearchLabel(tempstr,hl,false) then
  1184. begin
  1185. hs:=hl.name;
  1186. hssymtyp:=AT_FUNCTION;
  1187. end
  1188. else
  1189. begin
  1190. asmsearchsym(tempstr,sym,srsymtable);
  1191. if assigned(sym) then
  1192. begin
  1193. case sym.typ of
  1194. staticvarsym :
  1195. begin
  1196. hs:=tstaticvarsym(sym).mangledname;
  1197. def:=tstaticvarsym(sym).vardef;
  1198. end;
  1199. localvarsym,
  1200. paravarsym :
  1201. begin
  1202. Message(asmr_e_no_local_or_para_allowed);
  1203. end;
  1204. procsym :
  1205. begin
  1206. if Tprocsym(sym).ProcdefList.Count>1 then
  1207. Message(asmr_w_calling_overload_func);
  1208. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1209. {$ifdef i8086}
  1210. is_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1211. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  1212. {$endif i8086}
  1213. hssymtyp:=AT_FUNCTION;
  1214. end;
  1215. typesym :
  1216. begin
  1217. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1218. Message(asmr_e_wrong_sym_type);
  1219. size:=ttypesym(sym).typedef.size;
  1220. end;
  1221. fieldvarsym :
  1222. begin
  1223. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1224. end;
  1225. else
  1226. Message(asmr_e_wrong_sym_type);
  1227. end;
  1228. end
  1229. else
  1230. Message1(sym_e_unknown_id,tempstr);
  1231. end;
  1232. { symbol found? }
  1233. if hs<>'' then
  1234. begin
  1235. if asmsym='' then
  1236. begin
  1237. asmsym:=hs;
  1238. asmsymtyp:=hssymtyp;
  1239. end
  1240. else
  1241. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1242. if (expr='') or (expr[length(expr)]='+') then
  1243. begin
  1244. { don't remove the + if there could be a record field }
  1245. if actasmtoken<>AS_DOT then
  1246. delete(expr,length(expr),1);
  1247. end
  1248. else
  1249. if needofs then
  1250. begin
  1251. if (prevtok<>AS_OFFSET) then
  1252. Message(asmr_e_need_offset);
  1253. end
  1254. else
  1255. Message(asmr_e_only_add_relocatable_symbol);
  1256. end;
  1257. if (actasmtoken=AS_DOT) or
  1258. (assigned(sym) and
  1259. (sym.typ = fieldvarsym) and
  1260. not(sp_static in sym.symoptions)) then
  1261. begin
  1262. BuildRecordOffsetSize(tempstr,l,size,hs,needvmtofs,hastypecast);
  1263. if hs <> '' then
  1264. hssymtyp:=AT_FUNCTION
  1265. else
  1266. begin
  1267. str(l, tempstr);
  1268. expr:=expr + tempstr;
  1269. end
  1270. end
  1271. else if (actasmtoken<>AS_DOT) and
  1272. assigned(sym) and
  1273. (sym.typ=typesym) and
  1274. (ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1275. begin
  1276. { just a record type (without being followed by dot)
  1277. evaluates to 0. Ugly, but TP7 compatible. }
  1278. expr:=expr+'0';
  1279. end
  1280. else
  1281. begin
  1282. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1283. delete(expr,length(expr),1);
  1284. end;
  1285. if (actasmtoken=AS_LBRACKET) and
  1286. assigned(def) and
  1287. (def.typ=arraydef) then
  1288. begin
  1289. consume(AS_LBRACKET);
  1290. l:=BuildConstExpression;
  1291. if l<tarraydef(def).lowrange then
  1292. begin
  1293. Message(asmr_e_constant_out_of_bounds);
  1294. l:=0;
  1295. end
  1296. else
  1297. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1298. str(l, tempstr);
  1299. expr:=expr + '+' + tempstr;
  1300. consume(AS_RBRACKET);
  1301. end;
  1302. end;
  1303. { check if there are wrong operator used like / or mod etc. }
  1304. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1305. Message(asmr_e_only_add_relocatable_symbol);
  1306. end;
  1307. AS_ALIGN,
  1308. AS_DB,
  1309. AS_DW,
  1310. AS_DD,
  1311. AS_DQ,
  1312. AS_END,
  1313. AS_RBRACKET,
  1314. AS_SEPARATOR,
  1315. AS_COMMA,
  1316. AS_COLON:
  1317. break;
  1318. else
  1319. begin
  1320. { write error only once. }
  1321. if not errorflag then
  1322. Message(asmr_e_invalid_constant_expression);
  1323. { consume tokens until we find COMMA or SEPARATOR }
  1324. Consume(actasmtoken);
  1325. errorflag:=TRUE;
  1326. end;
  1327. end;
  1328. Until false;
  1329. { calculate expression }
  1330. if not ErrorFlag then
  1331. value:=CalculateExpression(expr)
  1332. else
  1333. value:=0;
  1334. { no longer in an expression }
  1335. inexpression:=FALSE;
  1336. end;
  1337. Function tx86intreader.BuildConstExpression:aint;
  1338. var
  1339. l,size : tcgint;
  1340. hs : string;
  1341. hssymtyp : TAsmsymtype;
  1342. isseg,is_farproc_entry,hasofs : boolean;
  1343. begin
  1344. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp,size,isseg,is_farproc_entry,hasofs);
  1345. if hs<>'' then
  1346. Message(asmr_e_relocatable_symbol_not_allowed);
  1347. BuildConstExpression:=l;
  1348. end;
  1349. Function tx86intreader.BuildRefConstExpression(out size:tcgint;startingminus:boolean):aint;
  1350. var
  1351. l : tcgint;
  1352. hs : string;
  1353. hssymtyp : TAsmsymtype;
  1354. isseg,is_farproc_entry,hasofs : boolean;
  1355. begin
  1356. BuildConstSymbolExpression(false,true,startingminus,l,hs,hssymtyp,size,isseg,is_farproc_entry,hasofs);
  1357. if hs<>'' then
  1358. Message(asmr_e_relocatable_symbol_not_allowed);
  1359. BuildRefConstExpression:=l;
  1360. end;
  1361. procedure tx86intreader.BuildReference(oper : tx86operand);
  1362. var
  1363. scale : byte;
  1364. k,l,size : tcgint;
  1365. tempstr,hs : string;
  1366. tempsymtyp : tasmsymtype;
  1367. code : integer;
  1368. hreg : tregister;
  1369. GotStar,GotOffset,HadVar,
  1370. GotPlus,Negative,BracketlessReference : boolean;
  1371. hl : tasmlabel;
  1372. isseg: boolean;
  1373. is_farproc_entry,hasofs,
  1374. hastypecast: boolean;
  1375. tmpoper: tx86operand;
  1376. Begin
  1377. if actasmtoken=AS_LBRACKET then
  1378. begin
  1379. Consume(AS_LBRACKET);
  1380. BracketlessReference:=false;
  1381. end
  1382. else
  1383. BracketlessReference:=true;
  1384. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1385. oper.InitRef;
  1386. GotStar:=false;
  1387. GotPlus:=true;
  1388. GotOffset:=false;
  1389. Negative:=false;
  1390. Scale:=0;
  1391. repeat
  1392. if GotOffset and (actasmtoken<>AS_ID) then
  1393. Message(asmr_e_invalid_reference_syntax);
  1394. Case actasmtoken of
  1395. AS_ID, { Constant reference expression OR variable reference expression }
  1396. AS_VMTOFFSET:
  1397. Begin
  1398. if not GotPlus then
  1399. Message(asmr_e_invalid_reference_syntax);
  1400. GotStar:=false;
  1401. GotPlus:=false;
  1402. if (actasmtoken = AS_VMTOFFSET) or
  1403. (SearchIConstant(actasmpattern,l) or
  1404. SearchRecordType(actasmpattern)) then
  1405. begin
  1406. l:=BuildRefConstExpression(size,negative);
  1407. if size<>0 then
  1408. oper.SetSize(size,false);
  1409. negative:=false; { "l" was negated if necessary }
  1410. GotPlus:=(prevasmtoken=AS_PLUS);
  1411. GotStar:=(prevasmtoken=AS_STAR);
  1412. case oper.opr.typ of
  1413. OPR_LOCAL :
  1414. begin
  1415. if GotStar then
  1416. Message(asmr_e_invalid_reference_syntax);
  1417. Inc(oper.opr.localsymofs,l);
  1418. end;
  1419. OPR_REFERENCE :
  1420. begin
  1421. if GotStar then
  1422. oper.opr.ref.scalefactor:=l
  1423. else
  1424. Inc(oper.opr.ref.offset,l);
  1425. end;
  1426. end;
  1427. end
  1428. else
  1429. Begin
  1430. if negative and not oper.hasvar then
  1431. Message(asmr_e_only_add_relocatable_symbol)
  1432. else if oper.hasvar and not GotOffset and
  1433. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1434. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1435. HadVar:=oper.hasvar and GotOffset;
  1436. tempstr:=actasmpattern;
  1437. Consume(AS_ID);
  1438. { typecasting? }
  1439. if (actasmtoken=AS_LPAREN) and
  1440. SearchType(tempstr,l) then
  1441. begin
  1442. oper.hastype:=true;
  1443. oper.typesize:=l;
  1444. Consume(AS_LPAREN);
  1445. BuildOperand(oper,true);
  1446. Consume(AS_RPAREN);
  1447. end
  1448. else
  1449. if is_locallabel(tempstr) then
  1450. begin
  1451. CreateLocalLabel(tempstr,hl,false);
  1452. oper.InitRef;
  1453. oper.haslabelref:=true;
  1454. if not negative then
  1455. begin
  1456. oper.opr.ref.symbol:=hl;
  1457. oper.hasvar:=true;
  1458. end
  1459. else
  1460. oper.opr.ref.relsymbol:=hl;
  1461. {$ifdef i8086}
  1462. if oper.opr.ref.segment=NR_NO then
  1463. oper.opr.ref.segment:=NR_CS;
  1464. {$endif i8086}
  1465. end
  1466. else
  1467. if oper.SetupVar(tempstr,GotOffset) then
  1468. begin
  1469. { convert OPR_LOCAL register para into a reference base }
  1470. if (oper.opr.typ=OPR_LOCAL) and
  1471. AsmRegisterPara(oper.opr.localsym) then
  1472. oper.InitRefConvertLocal
  1473. else
  1474. begin
  1475. {$ifdef x86_64}
  1476. if actasmtoken=AS_WRT then
  1477. begin
  1478. if (oper.opr.typ=OPR_REFERENCE) then
  1479. begin
  1480. Consume(AS_WRT);
  1481. Consume(AS___GOTPCREL);
  1482. if (oper.opr.ref.base<>NR_NO) or
  1483. (oper.opr.ref.index<>NR_NO) or
  1484. (oper.opr.ref.offset<>0) then
  1485. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1486. if tf_no_pic_supported in target_info.flags then
  1487. Message(asmr_e_no_gotpcrel_support);
  1488. oper.opr.ref.refaddr:=addr_pic;
  1489. oper.opr.ref.base:=NR_RIP;
  1490. end
  1491. else
  1492. message(asmr_e_invalid_reference_syntax);
  1493. end;
  1494. {$endif x86_64}
  1495. end;
  1496. end
  1497. else
  1498. Message1(sym_e_unknown_id,tempstr);
  1499. { record.field ? }
  1500. if actasmtoken=AS_DOT then
  1501. begin
  1502. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1503. if (hs<>'') then
  1504. Message(asmr_e_invalid_symbol_ref);
  1505. case oper.opr.typ of
  1506. OPR_LOCAL :
  1507. inc(oper.opr.localsymofs,l);
  1508. OPR_REFERENCE :
  1509. inc(oper.opr.ref.offset,l);
  1510. end;
  1511. if hastypecast then
  1512. oper.hastype:=true;
  1513. oper.SetSize(k,false);
  1514. end;
  1515. if GotOffset then
  1516. begin
  1517. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1518. begin
  1519. if (oper.opr.typ=OPR_REFERENCE) then
  1520. oper.opr.ref.base:=NR_NO;
  1521. oper.hasvar:=hadvar;
  1522. end
  1523. else
  1524. begin
  1525. if oper.hasvar and hadvar then
  1526. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1527. { should we allow ?? }
  1528. end;
  1529. end;
  1530. end;
  1531. GotOffset:=false;
  1532. end;
  1533. AS_PLUS :
  1534. Begin
  1535. Consume(AS_PLUS);
  1536. Negative:=false;
  1537. GotPlus:=true;
  1538. GotStar:=false;
  1539. Scale:=0;
  1540. end;
  1541. AS_DOT :
  1542. Begin
  1543. { Handle like a + }
  1544. Consume(AS_DOT);
  1545. Negative:=false;
  1546. GotPlus:=true;
  1547. GotStar:=false;
  1548. Scale:=0;
  1549. end;
  1550. AS_MINUS :
  1551. begin
  1552. Consume(AS_MINUS);
  1553. Negative:=true;
  1554. GotPlus:=true;
  1555. GotStar:=false;
  1556. Scale:=0;
  1557. end;
  1558. AS_STAR : { Scaling, with eax*4 order }
  1559. begin
  1560. Consume(AS_STAR);
  1561. hs:='';
  1562. l:=0;
  1563. case actasmtoken of
  1564. AS_ID,
  1565. AS_LPAREN :
  1566. l:=BuildConstExpression;
  1567. AS_INTNUM:
  1568. Begin
  1569. hs:=actasmpattern;
  1570. Consume(AS_INTNUM);
  1571. end;
  1572. AS_REGISTER :
  1573. begin
  1574. case oper.opr.typ of
  1575. OPR_REFERENCE :
  1576. begin
  1577. if oper.opr.ref.scalefactor=0 then
  1578. begin
  1579. if scale<>0 then
  1580. begin
  1581. oper.opr.ref.scalefactor:=scale;
  1582. scale:=0;
  1583. end
  1584. else
  1585. Message(asmr_e_wrong_scale_factor);
  1586. end
  1587. else
  1588. Message(asmr_e_invalid_reference_syntax);
  1589. end;
  1590. OPR_LOCAL :
  1591. begin
  1592. if oper.opr.localscale=0 then
  1593. begin
  1594. if scale<>0 then
  1595. begin
  1596. oper.opr.localscale:=scale;
  1597. scale:=0;
  1598. end
  1599. else
  1600. Message(asmr_e_wrong_scale_factor);
  1601. end
  1602. else
  1603. Message(asmr_e_invalid_reference_syntax);
  1604. end;
  1605. end;
  1606. end;
  1607. else
  1608. Message(asmr_e_invalid_reference_syntax);
  1609. end;
  1610. if actasmtoken<>AS_REGISTER then
  1611. begin
  1612. if hs<>'' then
  1613. val(hs,l,code);
  1614. case oper.opr.typ of
  1615. OPR_REFERENCE :
  1616. oper.opr.ref.scalefactor:=l;
  1617. OPR_LOCAL :
  1618. oper.opr.localscale:=l;
  1619. end;
  1620. if l>9 then
  1621. Message(asmr_e_wrong_scale_factor);
  1622. end;
  1623. GotPlus:=false;
  1624. GotStar:=false;
  1625. end;
  1626. AS_REGISTER :
  1627. begin
  1628. hreg:=actasmregister;
  1629. Consume(AS_REGISTER);
  1630. if actasmtoken=AS_COLON then
  1631. begin
  1632. Consume(AS_COLON);
  1633. oper.InitRefConvertLocal;
  1634. SetSegmentOverride(oper,hreg);
  1635. end
  1636. else
  1637. begin
  1638. if not((GotPlus and (not Negative)) or
  1639. GotStar) then
  1640. Message(asmr_e_invalid_reference_syntax);
  1641. { this register will be the index:
  1642. 1. just read a *
  1643. 2. next token is a *
  1644. 3. base register is already used }
  1645. case oper.opr.typ of
  1646. OPR_LOCAL :
  1647. begin
  1648. if (oper.opr.localindexreg<>NR_NO) then
  1649. Message(asmr_e_multiple_index);
  1650. {$ifdef x86_64}
  1651. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1652. if (hreg=NR_RIP) then
  1653. Message(asmr_e_no_local_or_para_allowed);
  1654. {$endif x86_64}
  1655. oper.opr.localindexreg:=hreg;
  1656. if scale<>0 then
  1657. begin
  1658. oper.opr.localscale:=scale;
  1659. scale:=0;
  1660. end;
  1661. end;
  1662. OPR_REFERENCE :
  1663. begin
  1664. if (GotStar) or
  1665. (actasmtoken=AS_STAR) or
  1666. (oper.opr.ref.base<>NR_NO) then
  1667. begin
  1668. if (oper.opr.ref.index<>NR_NO) then
  1669. Message(asmr_e_multiple_index);
  1670. oper.opr.ref.index:=hreg;
  1671. if scale<>0 then
  1672. begin
  1673. oper.opr.ref.scalefactor:=scale;
  1674. scale:=0;
  1675. end;
  1676. end
  1677. else
  1678. begin
  1679. oper.opr.ref.base:=hreg;
  1680. {$ifdef x86_64}
  1681. { non-GOT based RIP-relative accesses are also position-independent }
  1682. if (oper.opr.ref.base=NR_RIP) and
  1683. (oper.opr.ref.refaddr<>addr_pic) then
  1684. oper.opr.ref.refaddr:=addr_pic_no_got;
  1685. {$endif x86_64}
  1686. end;
  1687. end;
  1688. end;
  1689. GotPlus:=false;
  1690. GotStar:=false;
  1691. end;
  1692. end;
  1693. AS_OFFSET :
  1694. begin
  1695. Consume(AS_OFFSET);
  1696. GotOffset:=true;
  1697. end;
  1698. AS_TYPE,
  1699. AS_NOT,
  1700. AS_STRING,
  1701. AS_INTNUM,
  1702. AS_LPAREN : { Constant reference expression }
  1703. begin
  1704. if not GotPlus and not GotStar then
  1705. Message(asmr_e_invalid_reference_syntax);
  1706. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp,size,isseg,is_farproc_entry,hasofs);
  1707. { already handled by BuildConstSymbolExpression(); must be
  1708. handled there to avoid [reg-1+1] being interpreted as
  1709. [reg-(1+1)] }
  1710. negative:=false;
  1711. if tempstr<>'' then
  1712. begin
  1713. if GotStar then
  1714. Message(asmr_e_only_add_relocatable_symbol);
  1715. if not assigned(oper.opr.ref.symbol) then
  1716. begin
  1717. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1718. {$ifdef i8086}
  1719. if isseg then
  1720. begin
  1721. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1722. oper.opr.ref.refaddr:=addr_seg;
  1723. end
  1724. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1725. oper.opr.ref.segment:=NR_CS;
  1726. {$endif i8086}
  1727. end
  1728. else
  1729. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1730. end;
  1731. case oper.opr.typ of
  1732. OPR_REFERENCE :
  1733. begin
  1734. if GotStar then
  1735. oper.opr.ref.scalefactor:=l
  1736. else if (prevasmtoken = AS_STAR) then
  1737. begin
  1738. if scale<>0 then
  1739. scale:=l*scale
  1740. else
  1741. scale:=l;
  1742. end
  1743. else
  1744. begin
  1745. Inc(oper.opr.ref.offset,l);
  1746. Inc(oper.opr.constoffset,l);
  1747. end;
  1748. end;
  1749. OPR_LOCAL :
  1750. begin
  1751. if GotStar then
  1752. oper.opr.localscale:=l
  1753. else if (prevasmtoken = AS_STAR) then
  1754. begin
  1755. if scale<>0 then
  1756. scale:=l*scale
  1757. else
  1758. scale:=l;
  1759. end
  1760. else
  1761. Inc(oper.opr.localsymofs,l);
  1762. end;
  1763. end;
  1764. GotPlus:=(prevasmtoken=AS_PLUS) or
  1765. (prevasmtoken=AS_MINUS);
  1766. if GotPlus then
  1767. negative := prevasmtoken = AS_MINUS;
  1768. GotStar:=(prevasmtoken=AS_STAR);
  1769. end;
  1770. AS_LBRACKET :
  1771. begin
  1772. if (GotPlus and Negative) or GotStar then
  1773. Message(asmr_e_invalid_reference_syntax);
  1774. tmpoper:=Tx86Operand.create;
  1775. BuildReference(tmpoper);
  1776. AddReferences(oper,tmpoper);
  1777. tmpoper.Free;
  1778. GotPlus:=false;
  1779. GotStar:=false;
  1780. end;
  1781. AS_RBRACKET :
  1782. begin
  1783. if GotPlus or GotStar or BracketlessReference then
  1784. Message(asmr_e_invalid_reference_syntax);
  1785. Consume(AS_RBRACKET);
  1786. if actasmtoken=AS_LBRACKET then
  1787. begin
  1788. tmpoper:=Tx86Operand.create;
  1789. BuildReference(tmpoper);
  1790. AddReferences(oper,tmpoper);
  1791. tmpoper.Free;
  1792. end;
  1793. break;
  1794. end;
  1795. AS_SEPARATOR,
  1796. AS_END,
  1797. AS_COMMA:
  1798. begin
  1799. if not BracketlessReference then
  1800. begin
  1801. Message(asmr_e_invalid_reference_syntax);
  1802. RecoverConsume(true);
  1803. end;
  1804. break;
  1805. end;
  1806. else
  1807. Begin
  1808. Message(asmr_e_invalid_reference_syntax);
  1809. RecoverConsume(true);
  1810. break;
  1811. end;
  1812. end;
  1813. until false;
  1814. end;
  1815. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1816. var
  1817. l,size : tcgint;
  1818. tempstr : string;
  1819. tempsymtyp : tasmsymtype;
  1820. isseg,is_farproc_entry,hasofs : boolean;
  1821. begin
  1822. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1823. Message(asmr_e_invalid_operand_type);
  1824. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp,size,isseg,is_farproc_entry,hasofs);
  1825. {$ifdef i8086}
  1826. if tempstr='@DATA' then
  1827. begin
  1828. if not isseg then
  1829. Message(asmr_e_CODE_or_DATA_without_SEG);
  1830. oper.SetupData;
  1831. end
  1832. else if tempstr='@CODE' then
  1833. begin
  1834. if not isseg then
  1835. Message(asmr_e_CODE_or_DATA_without_SEG);
  1836. oper.SetupCode;
  1837. end
  1838. else
  1839. {$endif i8086}
  1840. if tempstr<>'' then
  1841. begin
  1842. oper.opr.typ:=OPR_SYMBOL;
  1843. oper.opr.symofs:=l;
  1844. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1845. oper.opr.symseg:=isseg;
  1846. oper.opr.sym_farproc_entry:=is_farproc_entry;
  1847. end
  1848. else
  1849. if oper.opr.typ=OPR_NONE then
  1850. begin
  1851. oper.opr.typ:=OPR_CONSTANT;
  1852. oper.opr.val:=l;
  1853. end
  1854. else
  1855. inc(oper.opr.val,l);
  1856. end;
  1857. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1858. procedure AddLabelOperand(hl:tasmlabel);
  1859. begin
  1860. if (oper.opr.typ=OPR_NONE) and
  1861. is_calljmp(actopcode) then
  1862. begin
  1863. oper.opr.typ:=OPR_SYMBOL;
  1864. oper.opr.symbol:=hl;
  1865. end
  1866. else
  1867. begin
  1868. oper.InitRef;
  1869. oper.opr.ref.symbol:=hl;
  1870. oper.haslabelref:=true;
  1871. {$ifdef i8086}
  1872. if oper.opr.ref.segment=NR_NO then
  1873. oper.opr.ref.segment:=NR_CS;
  1874. {$endif i8086}
  1875. end;
  1876. end;
  1877. var
  1878. expr,
  1879. hs : string;
  1880. tempreg : tregister;
  1881. l : tcgint;
  1882. hl : tasmlabel;
  1883. toffset,
  1884. tsize : tcgint;
  1885. hastypecast: boolean;
  1886. begin
  1887. expr:='';
  1888. repeat
  1889. if actasmtoken=AS_DOT then
  1890. begin
  1891. if expr<>'' then
  1892. begin
  1893. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  1894. if (oper.opr.typ<>OPR_NONE) and
  1895. (hs<>'') then
  1896. Message(asmr_e_wrong_sym_type);
  1897. oper.SetSize(tsize,true);
  1898. if hastypecast then
  1899. oper.hastype:=true;
  1900. { we have used the size of a field. Reset the typesize of the record }
  1901. oper.typesize:=0;
  1902. case oper.opr.typ of
  1903. OPR_LOCAL :
  1904. begin
  1905. { don't allow direct access to fields of parameters, because that
  1906. will generate buggy code. Allow it only for explicit typecasting
  1907. and when the parameter is in a register (delphi compatible) }
  1908. if (not oper.hastype) then
  1909. checklocalsubscript(oper.opr.localsym);
  1910. oper.opr.localforceref:=true;
  1911. inc(oper.opr.localsymofs,toffset);
  1912. oper.opr.localvarsize := tsize;
  1913. end;
  1914. OPR_CONSTANT :
  1915. inc(oper.opr.val,toffset);
  1916. OPR_REFERENCE :
  1917. begin
  1918. inc(oper.opr.ref.offset,toffset);
  1919. oper.opr.varsize := tsize;
  1920. end;
  1921. OPR_NONE :
  1922. begin
  1923. if (hs <> '') then
  1924. begin
  1925. oper.opr.typ:=OPR_SYMBOL;
  1926. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  1927. end
  1928. else
  1929. begin
  1930. oper.opr.typ:=OPR_CONSTANT;
  1931. oper.opr.val:=toffset;
  1932. end;
  1933. end;
  1934. OPR_REGISTER :
  1935. Message(asmr_e_invalid_reference_syntax);
  1936. OPR_SYMBOL:
  1937. Message(asmr_e_invalid_symbol_ref);
  1938. else
  1939. internalerror(200309222);
  1940. end;
  1941. expr:='';
  1942. end
  1943. else
  1944. begin
  1945. { See it as a separator }
  1946. Consume(AS_DOT);
  1947. end;
  1948. end;
  1949. case actasmtoken of
  1950. {$ifndef i8086}
  1951. AS_SEG :
  1952. Begin
  1953. Message(asmr_e_seg_not_supported);
  1954. Consume(actasmtoken);
  1955. end;
  1956. {$else not i8086}
  1957. AS_SEG,
  1958. {$endif not i8086}
  1959. AS_OFFSET,
  1960. AS_SIZEOF,
  1961. AS_VMTOFFSET,
  1962. AS_TYPE,
  1963. AS_NOT,
  1964. AS_STRING,
  1965. AS_PLUS,
  1966. AS_MINUS,
  1967. AS_LPAREN,
  1968. AS_INTNUM :
  1969. begin
  1970. case oper.opr.typ of
  1971. OPR_REFERENCE :
  1972. {$ifndef x86_64}
  1973. { this is for the i386 scenario where you have
  1974. <load got into ebx>
  1975. mov eax, [ebx].offset globalvar
  1976. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  1977. relative accesses)
  1978. }
  1979. if (actasmtoken=AS_OFFSET) and
  1980. (cs_create_pic in current_settings.moduleswitches) then
  1981. begin
  1982. Consume(AS_OFFSET);
  1983. oper.opr.ref.refaddr:=addr_pic;
  1984. BuildOperand(oper,false);
  1985. end
  1986. else
  1987. {$endif x86_64}
  1988. begin
  1989. l := BuildRefConstExpression(tsize);
  1990. if tsize<>0 then
  1991. oper.SetSize(tsize,false);
  1992. inc(oper.opr.ref.offset,l);
  1993. inc(oper.opr.constoffset,l);
  1994. end;
  1995. OPR_LOCAL :
  1996. begin
  1997. l := BuildConstExpression;
  1998. inc(oper.opr.localsymofs,l);
  1999. inc(oper.opr.localconstoffset,l);
  2000. end;
  2001. OPR_NONE,
  2002. OPR_CONSTANT :
  2003. BuildConstantOperand(oper);
  2004. else
  2005. Message(asmr_e_invalid_operand_type);
  2006. end;
  2007. end;
  2008. AS_PTR :
  2009. begin
  2010. if not oper.hastype then
  2011. begin
  2012. if (oper.opr.typ=OPR_CONSTANT) then
  2013. begin
  2014. oper.typesize:=oper.opr.val;
  2015. { reset constant value of operand }
  2016. oper.opr.typ:=OPR_NONE;
  2017. oper.opr.val:=0;
  2018. end
  2019. else
  2020. Message(asmr_e_syn_operand);
  2021. end;
  2022. Consume(AS_PTR);
  2023. { in delphi mode, allow e.g. call dword ptr eax,
  2024. see also webtbs/tw18225.pp }
  2025. if not(m_delphi in current_settings.modeswitches) then
  2026. oper.InitRef;
  2027. { if the operand subscripts a record, the typesize will be
  2028. rest -> save it here and restore it afterwards }
  2029. l:=oper.typesize;
  2030. BuildOperand(oper,false);
  2031. oper.setsize(l,true);
  2032. end;
  2033. AS_ID : { A constant expression, or a Variable ref. }
  2034. Begin
  2035. { Label or Special symbol reference? }
  2036. if actasmpattern[1] = '@' then
  2037. Begin
  2038. if actasmpattern = '@RESULT' then
  2039. Begin
  2040. oper.SetupResult;
  2041. Consume(AS_ID);
  2042. expr:='result';
  2043. end
  2044. else
  2045. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2046. begin
  2047. {$ifdef i8086}
  2048. Message(asmr_e_CODE_or_DATA_without_SEG);
  2049. {$else i8086}
  2050. Message(asmr_w_CODE_and_DATA_not_supported);
  2051. {$endif i8086}
  2052. Consume(AS_ID);
  2053. end
  2054. else
  2055. { Local Label }
  2056. begin
  2057. CreateLocalLabel(actasmpattern,hl,false);
  2058. Consume(AS_ID);
  2059. AddLabelOperand(hl);
  2060. end;
  2061. end
  2062. else
  2063. { support result for delphi modes }
  2064. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  2065. begin
  2066. oper.SetUpResult;
  2067. Consume(AS_ID);
  2068. expr:='result';
  2069. end
  2070. { probably a variable or normal expression }
  2071. { or a procedure (such as in CALL ID) }
  2072. else
  2073. Begin
  2074. { is it a constant ? }
  2075. if SearchIConstant(actasmpattern,l) then
  2076. Begin
  2077. case oper.opr.typ of
  2078. OPR_REFERENCE :
  2079. begin
  2080. l := BuildRefConstExpression(tsize);
  2081. if tsize<>0 then
  2082. oper.SetSize(tsize,false);
  2083. inc(oper.opr.ref.offset,l);
  2084. inc(oper.opr.constoffset,l);
  2085. end;
  2086. OPR_LOCAL :
  2087. begin
  2088. l := BuildRefConstExpression(tsize);
  2089. if tsize<>0 then
  2090. oper.SetSize(tsize,false);
  2091. inc(oper.opr.localsymofs,l);
  2092. inc(oper.opr.localconstoffset,l);
  2093. end;
  2094. OPR_NONE,
  2095. OPR_CONSTANT :
  2096. BuildConstantOperand(oper);
  2097. else
  2098. Message(asmr_e_invalid_operand_type);
  2099. end;
  2100. end
  2101. else
  2102. { Check for pascal label }
  2103. if SearchLabel(actasmpattern,hl,false) then
  2104. begin
  2105. Consume(AS_ID);
  2106. AddLabelOperand(hl);
  2107. end
  2108. else
  2109. { is it a normal variable ? }
  2110. Begin
  2111. expr:=actasmpattern;
  2112. Consume(AS_ID);
  2113. { typecasting? }
  2114. if SearchType(expr,l) then
  2115. begin
  2116. oper.hastype:=true;
  2117. oper.typesize:=l;
  2118. case actasmtoken of
  2119. AS_LPAREN :
  2120. begin
  2121. { Support Type([Reference]) }
  2122. Consume(AS_LPAREN);
  2123. BuildOperand(oper,true);
  2124. { Delphi also supports Type(Register) and
  2125. interprets it the same as Type([Register]). }
  2126. if (oper.opr.typ = OPR_REGISTER) then
  2127. { This also sets base to the register. }
  2128. oper.InitRef;
  2129. Consume(AS_RPAREN);
  2130. end;
  2131. AS_LBRACKET :
  2132. begin
  2133. { Support Var.Type[Index] }
  2134. { Convert @label.Byte[1] to reference }
  2135. if oper.opr.typ=OPR_SYMBOL then
  2136. oper.initref;
  2137. end;
  2138. end;
  2139. end
  2140. else
  2141. begin
  2142. if not oper.SetupVar(expr,false) then
  2143. Begin
  2144. { not a variable, check special variables.. }
  2145. if expr = 'SELF' then
  2146. begin
  2147. oper.SetupSelf;
  2148. expr:='self';
  2149. end
  2150. else
  2151. begin
  2152. Message1(sym_e_unknown_id,expr);
  2153. expr:='';
  2154. end;
  2155. end;
  2156. { indexed access to variable? }
  2157. if actasmtoken=AS_LBRACKET then
  2158. begin
  2159. { ... then the operand size is not known anymore }
  2160. oper.size:=OS_NO;
  2161. BuildReference(oper);
  2162. end;
  2163. end;
  2164. end;
  2165. end;
  2166. end;
  2167. AS_REGISTER : { Register, a variable reference or a constant reference }
  2168. begin
  2169. { save the type of register used. }
  2170. tempreg:=actasmregister;
  2171. Consume(AS_REGISTER);
  2172. if actasmtoken = AS_COLON then
  2173. Begin
  2174. Consume(AS_COLON);
  2175. oper.InitRef;
  2176. SetSegmentOverride(oper,tempreg);
  2177. BuildReference(oper);
  2178. end
  2179. else
  2180. { Simple register }
  2181. begin
  2182. if (oper.opr.typ <> OPR_NONE) then
  2183. Message(asmr_e_syn_operand);
  2184. oper.opr.typ:=OPR_REGISTER;
  2185. oper.opr.reg:=tempreg;
  2186. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  2187. end;
  2188. end;
  2189. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  2190. Begin
  2191. BuildReference(oper);
  2192. end;
  2193. AS_DWORD,
  2194. AS_BYTE,
  2195. AS_WORD,
  2196. AS_TBYTE,
  2197. AS_DQWORD,
  2198. AS_QWORD,
  2199. AS_OWORD,
  2200. AS_XMMWORD,
  2201. AS_YWORD,
  2202. AS_YMMWORD:
  2203. begin
  2204. { Type specifier }
  2205. oper.hastype:=true;
  2206. oper.typesize:=0;
  2207. case actasmtoken of
  2208. AS_DWORD : oper.typesize:=4;
  2209. AS_WORD : oper.typesize:=2;
  2210. AS_BYTE : oper.typesize:=1;
  2211. AS_QWORD : oper.typesize:=8;
  2212. AS_DQWORD : oper.typesize:=16;
  2213. AS_TBYTE : oper.typesize:=10;
  2214. AS_OWORD,
  2215. AS_XMMWORD: oper.typesize:=16;
  2216. AS_YWORD,
  2217. AS_YMMWORD: oper.typesize:=32;
  2218. else
  2219. internalerror(2010061101);
  2220. end;
  2221. Consume(actasmtoken);
  2222. if (actasmtoken=AS_LPAREN) then
  2223. begin
  2224. { Support "xxx ptr [Reference]" }
  2225. { in case the expression subscripts a record, the typesize
  2226. is reset, so save the explicit size we set above }
  2227. l:=oper.typesize;
  2228. Consume(AS_LPAREN);
  2229. BuildOperand(oper,true);
  2230. Consume(AS_RPAREN);
  2231. oper.setsize(l,true);
  2232. end;
  2233. end;
  2234. AS_SEPARATOR,
  2235. AS_END,
  2236. AS_COMMA,
  2237. AS_COLON:
  2238. begin
  2239. break;
  2240. end;
  2241. AS_RPAREN:
  2242. begin
  2243. if not istypecast then
  2244. begin
  2245. Message(asmr_e_syn_operand);
  2246. Consume(AS_RPAREN);
  2247. end
  2248. else
  2249. break;
  2250. end;
  2251. else
  2252. begin
  2253. Message(asmr_e_syn_operand);
  2254. RecoverConsume(true);
  2255. break;
  2256. end;
  2257. end;
  2258. until false;
  2259. { End of operand, update size if a typecast is forced }
  2260. if (oper.typesize<>0) and
  2261. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  2262. oper.SetSize(oper.typesize,true);
  2263. {$ifdef i8086}
  2264. { references to a procedure/function entry, without an explicit segment
  2265. override, are added an CS: override by default (this is Turbo Pascal 7
  2266. compatible) }
  2267. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2268. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2269. oper.opr.ref.segment:=NR_CS;
  2270. {$endif i8086}
  2271. end;
  2272. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2273. var
  2274. PrefixOp,OverrideOp: tasmop;
  2275. operandnum : longint;
  2276. t: TRegister;
  2277. is_far_const:boolean;
  2278. i:byte;
  2279. tmp: toperand;
  2280. di_param, si_param: ShortInt;
  2281. prefix_or_override_pending_concat: boolean = false;
  2282. {$ifdef i8086}
  2283. hsymbol: TAsmSymbol;
  2284. hoffset: ASizeInt;
  2285. href_farproc_entry: Boolean;
  2286. {$endif i8086}
  2287. begin
  2288. PrefixOp:=A_None;
  2289. OverrideOp:=A_None;
  2290. is_far_const:=false;
  2291. { prefix seg opcode / prefix opcode }
  2292. repeat
  2293. if is_prefix(actopcode) then
  2294. with instr do
  2295. begin
  2296. if prefix_or_override_pending_concat then
  2297. ConcatInstruction(curlist);
  2298. PrefixOp:=ActOpcode;
  2299. opcode:=ActOpcode;
  2300. condition:=ActCondition;
  2301. opsize:=ActOpsize;
  2302. prefix_or_override_pending_concat:=true;
  2303. consume(AS_OPCODE);
  2304. end
  2305. else
  2306. if is_override(actopcode) then
  2307. with instr do
  2308. begin
  2309. if prefix_or_override_pending_concat then
  2310. ConcatInstruction(curlist);
  2311. OverrideOp:=ActOpcode;
  2312. opcode:=ActOpcode;
  2313. condition:=ActCondition;
  2314. opsize:=ActOpsize;
  2315. prefix_or_override_pending_concat:=true;
  2316. consume(AS_OPCODE);
  2317. end
  2318. else
  2319. break;
  2320. { allow for newline after prefix or override }
  2321. while actasmtoken=AS_SEPARATOR do
  2322. consume(AS_SEPARATOR);
  2323. until (actasmtoken<>AS_OPCODE);
  2324. { opcode }
  2325. if (actasmtoken <> AS_OPCODE) then
  2326. begin
  2327. { allow a prefix or override to be used standalone, like an opcode
  2328. with zero operands; this is TP7 compatible and allows compiling
  2329. ugly code like 'seges; db $67,$66; lodsw' }
  2330. if prefix_or_override_pending_concat then
  2331. exit
  2332. else
  2333. begin
  2334. Message(asmr_e_invalid_or_missing_opcode);
  2335. RecoverConsume(false);
  2336. exit;
  2337. end;
  2338. end;
  2339. if prefix_or_override_pending_concat then
  2340. begin
  2341. instr.ConcatInstruction(curlist);
  2342. prefix_or_override_pending_concat:=false;
  2343. end;
  2344. { Fill the instr object with the current state }
  2345. with instr do
  2346. begin
  2347. Opcode:=ActOpcode;
  2348. condition:=ActCondition;
  2349. opsize:=ActOpsize;
  2350. { Valid combination of prefix/override and instruction ? }
  2351. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2352. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2353. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2354. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2355. end;
  2356. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2357. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2358. these instructions doesn't change anything in the internal assember,
  2359. so change the opcode }
  2360. if (instr.opcode=A_POPF) then
  2361. instr.opcode:=A_POPFW
  2362. else if (instr.opcode=A_PUSHF) then
  2363. instr.opcode:=A_PUSHFW
  2364. {$ifndef x86_64}
  2365. else if (instr.opcode=A_PUSHA) then
  2366. instr.opcode:=A_PUSHAW
  2367. else if (instr.opcode=A_POPA) then
  2368. instr.opcode:=A_POPAW
  2369. {$endif x86_64}
  2370. {$ifdef i8086}
  2371. { ret is converted to retn or retf, depending on the call model of the
  2372. current procedure (BP7 compatible) }
  2373. else if (instr.opcode=A_RET) then
  2374. begin
  2375. if is_proc_far(current_procinfo.procdef) and
  2376. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2377. instr.opcode:=A_RETF
  2378. else
  2379. instr.opcode:=A_RETN;
  2380. end
  2381. {$endif i8086}
  2382. ;
  2383. { We are reading operands, so opcode will be an AS_ID }
  2384. { process operands backwards to get them in AT&T order }
  2385. operandnum:=max_operands;
  2386. is_far_const:=false;
  2387. Consume(AS_OPCODE);
  2388. { Zero operand opcode ? }
  2389. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2390. exit;
  2391. { Read Operands }
  2392. repeat
  2393. case actasmtoken of
  2394. { End of asm operands for this opcode }
  2395. AS_END,
  2396. AS_SEPARATOR :
  2397. break;
  2398. { Operand delimiter }
  2399. AS_COMMA :
  2400. begin
  2401. { should have something before the comma }
  2402. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2403. Message(asmr_e_syntax_error);
  2404. if operandnum <= 1 then
  2405. Message(asmr_e_too_many_operands)
  2406. else
  2407. Dec(operandnum);
  2408. Consume(AS_COMMA);
  2409. end;
  2410. {Far constant, i.e. jmp $0000:$11111111.}
  2411. AS_COLON:
  2412. begin
  2413. is_far_const:=true;
  2414. if operandnum<max_operands then
  2415. message(asmr_e_too_many_operands)
  2416. else
  2417. dec(operandnum);
  2418. consume(AS_COLON);
  2419. end;
  2420. { Type specifier }
  2421. AS_NEAR,
  2422. AS_FAR :
  2423. begin
  2424. if actasmtoken = AS_NEAR then
  2425. begin
  2426. {$ifndef i8086}
  2427. Message(asmr_w_near_ignored);
  2428. {$endif not i8086}
  2429. instr.opsize:=S_NEAR;
  2430. end
  2431. else
  2432. begin
  2433. {$ifndef i8086}
  2434. Message(asmr_w_far_ignored);
  2435. {$endif not i8086}
  2436. instr.opsize:=S_FAR;
  2437. end;
  2438. Consume(actasmtoken);
  2439. if actasmtoken=AS_PTR then
  2440. begin
  2441. Consume(AS_PTR);
  2442. instr.Operands[operandnum].InitRef;
  2443. end;
  2444. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2445. end;
  2446. else
  2447. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2448. end; { end case }
  2449. until false;
  2450. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2451. for i:=operandnum to max_operands do
  2452. begin
  2453. tmp:=instr.operands[i+1-operandnum];
  2454. instr.operands[i+1-operandnum]:=instr.operands[i];
  2455. instr.operands[i]:=tmp;
  2456. end;
  2457. operandnum:=(max_operands+1)-operandnum;
  2458. instr.ops:=operandnum;
  2459. { Check operands }
  2460. for i:=1 to operandnum do
  2461. begin
  2462. if is_far_const and
  2463. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2464. message(asmr_e_expr_illegal)
  2465. else
  2466. if instr.operands[i].opr.typ=OPR_NONE then
  2467. Message(asmr_e_syntax_error);
  2468. end;
  2469. { Check for invalid ES: overrides }
  2470. if is_x86_parameterized_string_op(instr.opcode) then
  2471. begin
  2472. si_param:=get_x86_string_op_si_param(instr.opcode);
  2473. if si_param<>-1 then
  2474. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2475. di_param:=get_x86_string_op_di_param(instr.opcode);
  2476. if di_param<>-1 then
  2477. begin
  2478. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2479. if di_param<=operandnum then
  2480. with instr.operands[di_param] do
  2481. if (opr.typ=OPR_REFERENCE) and
  2482. (opr.ref.segment<>NR_NO) and
  2483. (opr.ref.segment<>NR_ES) then
  2484. Message(asmr_e_cannot_override_es_segment);
  2485. end;
  2486. { if two memory parameters, check whether their address sizes are equal }
  2487. if (si_param<>-1) and (di_param<>-1) and
  2488. (si_param<=operandnum) and (di_param<=operandnum) and
  2489. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2490. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2491. begin
  2492. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2493. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2494. Message(asmr_e_address_sizes_do_not_match);
  2495. end;
  2496. end;
  2497. { e.g. for "push dword 1", "push word 6" }
  2498. if (instr.ops=1) and
  2499. (instr.operands[1].typesize<>0) then
  2500. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2501. {$ifdef i8086}
  2502. for i:=1 to operandnum do
  2503. with instr.operands[i].opr do
  2504. begin
  2505. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2506. but Turbo Pascal 7 compatible. }
  2507. if (instr.opcode in [A_CALL,A_JMP]) and
  2508. (instr.operands[i].haslabelref or instr.operands[i].hasproc)
  2509. and (typ=OPR_REFERENCE) and
  2510. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2511. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2512. begin
  2513. hsymbol:=ref.symbol;
  2514. hoffset:=ref.offset;
  2515. href_farproc_entry:=ref_farproc_entry;
  2516. typ:=OPR_SYMBOL;
  2517. symbol:=hsymbol;
  2518. symofs:=hoffset;
  2519. symseg:=False;
  2520. sym_farproc_entry:=href_farproc_entry;
  2521. end;
  2522. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2523. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2524. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2525. instr.opsize:=S_FAR;
  2526. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2527. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2528. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2529. instr.opsize:=S_FAR;
  2530. end;
  2531. {$endif i8086}
  2532. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2533. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2534. begin
  2535. for i:=1 to operandnum do
  2536. begin
  2537. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2538. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2539. (instr.operands[i].opr.ref.index = NR_NO) then
  2540. begin
  2541. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2542. instr.operands[i].opr.ref.base := NR_NO;
  2543. end
  2544. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2545. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2546. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2547. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2548. begin
  2549. // exchange base- and index-register
  2550. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2551. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2552. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2553. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2554. else
  2555. begin
  2556. t := instr.operands[i].opr.ref.base;
  2557. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2558. instr.operands[i].opr.ref.index := t;
  2559. end;
  2560. end;
  2561. end;
  2562. end;
  2563. end;
  2564. Procedure tx86intreader.BuildConstant(constsize: byte);
  2565. var
  2566. asmsymtyp : tasmsymtype;
  2567. asmsym,
  2568. expr: string;
  2569. value,size : tcgint;
  2570. isseg,is_farproc_entry,hasofs : boolean;
  2571. Begin
  2572. Repeat
  2573. Case actasmtoken of
  2574. AS_STRING:
  2575. Begin
  2576. { DD and DW cases }
  2577. if constsize <> 1 then
  2578. Begin
  2579. if Not PadZero(actasmpattern,constsize) then
  2580. Message(scan_f_string_exceeds_line);
  2581. end;
  2582. expr:=actasmpattern;
  2583. Consume(AS_STRING);
  2584. Case actasmtoken of
  2585. AS_COMMA:
  2586. Consume(AS_COMMA);
  2587. AS_END,
  2588. AS_SEPARATOR: ;
  2589. else
  2590. Message(asmr_e_invalid_string_expression);
  2591. end;
  2592. ConcatString(curlist,expr);
  2593. end;
  2594. AS_PLUS,
  2595. AS_MINUS,
  2596. AS_LPAREN,
  2597. AS_NOT,
  2598. AS_INTNUM,
  2599. AS_OFFSET,
  2600. {$ifdef i8086}
  2601. AS_SEG,
  2602. {$endif i8086}
  2603. AS_ID :
  2604. Begin
  2605. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp,size,isseg,is_farproc_entry,hasofs);
  2606. if asmsym<>'' then
  2607. begin
  2608. if (not isseg) and
  2609. {$ifdef i8086}
  2610. ((constsize<>2) and (constsize<>4))
  2611. {$else i8086}
  2612. (constsize<>sizeof(pint))
  2613. {$endif i8086}
  2614. then
  2615. begin
  2616. Message1(asmr_w_const32bit_for_address,asmsym);
  2617. constsize:=sizeof(pint);
  2618. end;
  2619. {$ifdef i8086}
  2620. if asmsym='@DATA' then
  2621. begin
  2622. if not isseg then
  2623. Message(asmr_e_CODE_or_DATA_without_SEG);
  2624. if constsize<2 then
  2625. Message1(asmr_e_const16bit_for_segment,asmsym);
  2626. if current_settings.x86memorymodel=mm_huge then
  2627. curlist.concat(Tai_const.Create_fardataseg)
  2628. else
  2629. curlist.concat(Tai_const.Create_dgroup);
  2630. if constsize>2 then
  2631. ConcatConstant(curlist,0,constsize-2);
  2632. end
  2633. else if asmsym='@CODE' then
  2634. begin
  2635. if not isseg then
  2636. Message(asmr_e_CODE_or_DATA_without_SEG);
  2637. if constsize<2 then
  2638. Message1(asmr_e_const16bit_for_segment,asmsym);
  2639. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2640. if constsize>2 then
  2641. ConcatConstant(curlist,0,constsize-2);
  2642. end
  2643. else if isseg then
  2644. begin
  2645. if constsize<2 then
  2646. Message1(asmr_e_const16bit_for_segment,asmsym);
  2647. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2648. if constsize>2 then
  2649. ConcatConstant(curlist,0,constsize-2);
  2650. end
  2651. else
  2652. {$endif i8086}
  2653. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,hasofs);
  2654. end
  2655. else
  2656. ConcatConstant(curlist,value,constsize);
  2657. end;
  2658. AS_COMMA:
  2659. begin
  2660. Consume(AS_COMMA);
  2661. end;
  2662. AS_ALIGN,
  2663. AS_DB,
  2664. AS_DW,
  2665. AS_DD,
  2666. AS_DQ,
  2667. AS_OPCODE,
  2668. AS_END,
  2669. AS_SEPARATOR:
  2670. break;
  2671. else
  2672. begin
  2673. Message(asmr_e_syn_constant);
  2674. RecoverConsume(false);
  2675. end
  2676. end;
  2677. Until false;
  2678. end;
  2679. function tx86intreader.Assemble: tlinkedlist;
  2680. Var
  2681. hl : tasmlabel;
  2682. instr : Tx86Instruction;
  2683. tmpsym: tsym;
  2684. tmpsrsymtable: TSymtable;
  2685. Begin
  2686. Message1(asmr_d_start_reading,'intel');
  2687. inexpression:=FALSE;
  2688. firsttoken:=TRUE;
  2689. { sets up all opcode and register tables in uppercase
  2690. done in the construtor now
  2691. if not _asmsorted then
  2692. Begin
  2693. SetupTables;
  2694. _asmsorted:=TRUE;
  2695. end;
  2696. }
  2697. curlist:=TAsmList.Create;
  2698. { we might need to know which parameters are passed in registers }
  2699. if not parse_generic then
  2700. current_procinfo.generate_parameter_info;
  2701. { start tokenizer }
  2702. gettoken;
  2703. { main loop }
  2704. repeat
  2705. case actasmtoken of
  2706. AS_LLABEL:
  2707. Begin
  2708. if CreateLocalLabel(actasmpattern,hl,true) then
  2709. ConcatLabel(curlist,hl);
  2710. Consume(AS_LLABEL);
  2711. end;
  2712. AS_LABEL:
  2713. Begin
  2714. if SearchLabel(upper(actasmpattern),hl,true) then
  2715. begin
  2716. if hl.is_public then
  2717. ConcatPublic(curlist,actasmpattern_origcase);
  2718. ConcatLabel(curlist,hl);
  2719. end
  2720. else
  2721. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2722. Consume(AS_LABEL);
  2723. end;
  2724. AS_DW :
  2725. Begin
  2726. inexpression:=true;
  2727. Consume(AS_DW);
  2728. BuildConstant(2);
  2729. inexpression:=false;
  2730. end;
  2731. AS_DB :
  2732. Begin
  2733. inexpression:=true;
  2734. Consume(AS_DB);
  2735. BuildConstant(1);
  2736. inexpression:=false;
  2737. end;
  2738. AS_DD :
  2739. Begin
  2740. inexpression:=true;
  2741. Consume(AS_DD);
  2742. BuildConstant(4);
  2743. inexpression:=false;
  2744. end;
  2745. {$ifdef cpu64bitaddr}
  2746. AS_DQ:
  2747. Begin
  2748. inexpression:=true;
  2749. Consume(AS_DQ);
  2750. BuildConstant(8);
  2751. inexpression:=false;
  2752. end;
  2753. {$endif cpu64bitaddr}
  2754. AS_PUBLIC:
  2755. Begin
  2756. Consume(AS_PUBLIC);
  2757. repeat
  2758. if actasmtoken=AS_ID then
  2759. begin
  2760. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  2761. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  2762. else if SearchLabel(upper(actasmpattern),hl,false) then
  2763. begin
  2764. if not hl.is_public then
  2765. begin
  2766. hl.is_public:=true;
  2767. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  2768. if tlabelsym(tmpsym).defined then
  2769. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  2770. end;
  2771. end
  2772. else
  2773. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2774. end;
  2775. Consume(AS_ID);
  2776. if actasmtoken=AS_COMMA then
  2777. begin
  2778. Consume(AS_COMMA);
  2779. if actasmtoken<>AS_ID then
  2780. Consume(AS_ID);
  2781. end;
  2782. until actasmtoken=AS_SEPARATOR;
  2783. end;
  2784. AS_ALIGN:
  2785. Begin
  2786. Consume(AS_ALIGN);
  2787. ConcatAlign(curlist,BuildConstExpression);
  2788. if actasmtoken<>AS_SEPARATOR then
  2789. Consume(AS_SEPARATOR);
  2790. end;
  2791. AS_OPCODE :
  2792. Begin
  2793. instr:=Tx86Instruction.Create(Tx86Operand);
  2794. BuildOpcode(instr);
  2795. with instr do
  2796. begin
  2797. CheckNonCommutativeOpcodes;
  2798. AddReferenceSizes;
  2799. SetInstructionOpsize;
  2800. CheckOperandSizes;
  2801. ConcatInstruction(curlist);
  2802. end;
  2803. instr.Free;
  2804. end;
  2805. AS_SEPARATOR :
  2806. Begin
  2807. Consume(AS_SEPARATOR);
  2808. end;
  2809. AS_TARGET_DIRECTIVE:
  2810. HandleTargetDirective;
  2811. AS_END :
  2812. break; { end assembly block }
  2813. else
  2814. Begin
  2815. Message(asmr_e_syntax_error);
  2816. RecoverConsume(false);
  2817. end;
  2818. end; { end case }
  2819. until false;
  2820. { check that all referenced local labels are defined }
  2821. checklocallabels;
  2822. { Return the list in an asmnode }
  2823. assemble:=curlist;
  2824. Message1(asmr_d_finish_reading,'intel');
  2825. end;
  2826. end.