rax86int.pas 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867
  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. begin
  756. if (dest.opr.typ<>OPR_REFERENCE) or (src.opr.typ<>OPR_REFERENCE) then
  757. internalerror(2018022601);
  758. AddRegister(src.opr.ref.base,1);
  759. AddRegister(src.opr.ref.index,src.opr.ref.scalefactor);
  760. if src.opr.ref.segment<>NR_NO then
  761. SetSegmentOverride(dest,src.opr.ref.segment);
  762. Inc(dest.opr.ref.offset,src.opr.ref.offset);
  763. Inc(dest.opr.constoffset,src.opr.constoffset);
  764. dest.haslabelref:=dest.haslabelref or src.haslabelref;
  765. dest.hasproc:=dest.hasproc or src.hasproc;
  766. dest.hasvar:=dest.hasvar or src.hasvar;
  767. if assigned(src.opr.ref.symbol) then
  768. begin
  769. if assigned(dest.opr.ref.symbol) then
  770. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  771. dest.opr.ref.symbol:=src.opr.ref.symbol;
  772. end;
  773. if assigned(src.opr.ref.relsymbol) then
  774. begin
  775. if assigned(dest.opr.ref.relsymbol) then
  776. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  777. dest.opr.ref.relsymbol:=src.opr.ref.relsymbol;
  778. end;
  779. if dest.opr.ref.refaddr=addr_no then
  780. dest.opr.ref.refaddr:=src.opr.ref.refaddr;
  781. end;
  782. procedure tx86intreader.SetSegmentOverride(oper:tx86operand;seg:tregister);
  783. begin
  784. if oper.opr.typ<>OPR_REFERENCE then
  785. internalerror(2018022801);
  786. if oper.opr.ref.segment<>NR_NO then
  787. begin
  788. if m_tp7 in current_settings.modeswitches then
  789. Message(asmr_w_multiple_segment_overrides)
  790. else
  791. Message(asmr_e_multiple_segment_overrides);
  792. end;
  793. oper.opr.ref.segment:=seg;
  794. end;
  795. { This routine builds up a record offset after a AS_DOT
  796. token is encountered.
  797. On entry actasmtoken should be equal to AS_DOT }
  798. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  799. var
  800. s: string;
  801. Begin
  802. offset:=0;
  803. size:=0;
  804. mangledname:='';
  805. hastypecast:=false;
  806. s:=expr;
  807. while (actasmtoken=AS_DOT) do
  808. begin
  809. Consume(AS_DOT);
  810. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_REGISTER] then
  811. begin
  812. s:=s+'.'+actasmpattern;
  813. consume(actasmtoken);
  814. end
  815. else
  816. begin
  817. Consume(AS_ID);
  818. RecoverConsume(true);
  819. break;
  820. end;
  821. end;
  822. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  823. Message(asmr_e_building_record_offset);
  824. end;
  825. 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);
  826. var
  827. tempstr,expr,hs,mangledname : string;
  828. parenlevel : longint;
  829. l,k : tcgint;
  830. hasparen,
  831. errorflag,
  832. needvmtofs : boolean;
  833. prevtok : tasmtoken;
  834. hl : tasmlabel;
  835. hssymtyp : Tasmsymtype;
  836. def : tdef;
  837. sym : tsym;
  838. srsymtable : TSymtable;
  839. hastypecast : boolean;
  840. Begin
  841. { reset }
  842. value:=0;
  843. asmsym:='';
  844. asmsymtyp:=AT_DATA;
  845. size:=0;
  846. isseg:=false;
  847. is_farproc_entry:=FALSE;
  848. hasofs:=FALSE;
  849. errorflag:=FALSE;
  850. tempstr:='';
  851. expr:='';
  852. if startingminus then
  853. expr:='-';
  854. inexpression:=TRUE;
  855. parenlevel:=0;
  856. sym:=nil;
  857. needvmtofs:=FALSE;
  858. Repeat
  859. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  860. if isref and (actasmtoken=AS_LBRACKET) then
  861. break;
  862. Case actasmtoken of
  863. AS_LPAREN:
  864. Begin
  865. Consume(AS_LPAREN);
  866. expr:=expr + '(';
  867. inc(parenlevel);
  868. end;
  869. AS_RPAREN:
  870. Begin
  871. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  872. if parenlevel=0 then
  873. break;
  874. Consume(AS_RPAREN);
  875. expr:=expr + ')';
  876. dec(parenlevel);
  877. end;
  878. AS_SHL:
  879. Begin
  880. Consume(AS_SHL);
  881. expr:=expr + '<';
  882. end;
  883. AS_SHR:
  884. Begin
  885. Consume(AS_SHR);
  886. expr:=expr + '>';
  887. end;
  888. AS_SLASH:
  889. Begin
  890. Consume(AS_SLASH);
  891. expr:=expr + '/';
  892. end;
  893. AS_MOD:
  894. Begin
  895. Consume(AS_MOD);
  896. expr:=expr + '%';
  897. end;
  898. AS_STAR:
  899. Begin
  900. Consume(AS_STAR);
  901. if isref and (actasmtoken=AS_REGISTER) then
  902. break;
  903. expr:=expr + '*';
  904. end;
  905. AS_PLUS:
  906. Begin
  907. Consume(AS_PLUS);
  908. if isref and ((actasmtoken=AS_REGISTER) or (actasmtoken=AS_LBRACKET)) then
  909. break;
  910. expr:=expr + '+';
  911. end;
  912. AS_MINUS:
  913. Begin
  914. Consume(AS_MINUS);
  915. expr:=expr + '-';
  916. end;
  917. AS_AND:
  918. Begin
  919. Consume(AS_AND);
  920. expr:=expr + '&';
  921. end;
  922. AS_NOT:
  923. Begin
  924. Consume(AS_NOT);
  925. expr:=expr + '~';
  926. end;
  927. AS_XOR:
  928. Begin
  929. Consume(AS_XOR);
  930. expr:=expr + '^';
  931. end;
  932. AS_OR:
  933. Begin
  934. Consume(AS_OR);
  935. expr:=expr + '|';
  936. end;
  937. AS_INTNUM:
  938. Begin
  939. expr:=expr + actasmpattern;
  940. Consume(AS_INTNUM);
  941. end;
  942. {$ifdef i8086}
  943. AS_SEG:
  944. begin
  945. isseg:=true;
  946. Consume(actasmtoken);
  947. if actasmtoken<>AS_ID then
  948. Message(asmr_e_seg_without_identifier);
  949. end;
  950. {$endif i8086}
  951. AS_VMTOFFSET,
  952. AS_OFFSET:
  953. begin
  954. if (actasmtoken = AS_OFFSET) then
  955. begin
  956. needofs:=true;
  957. hasofs:=true;
  958. end
  959. else
  960. needvmtofs:=true;
  961. Consume(actasmtoken);
  962. if actasmtoken<>AS_ID then
  963. Message(asmr_e_offset_without_identifier);
  964. end;
  965. AS_SIZEOF,
  966. AS_TYPE:
  967. begin
  968. l:=0;
  969. hasparen:=false;
  970. Consume(actasmtoken);
  971. if actasmtoken=AS_LPAREN then
  972. begin
  973. hasparen:=true;
  974. Consume(AS_LPAREN);
  975. end;
  976. if actasmtoken<>AS_ID then
  977. Message(asmr_e_type_without_identifier)
  978. else
  979. begin
  980. tempstr:=actasmpattern;
  981. Consume(AS_ID);
  982. if actasmtoken=AS_DOT then
  983. begin
  984. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  985. if mangledname<>'' then
  986. { procsym }
  987. Message(asmr_e_wrong_sym_type);
  988. if hastypecast then
  989. end
  990. else
  991. begin
  992. asmsearchsym(tempstr,sym,srsymtable);
  993. if assigned(sym) then
  994. begin
  995. case sym.typ of
  996. staticvarsym,
  997. localvarsym,
  998. paravarsym :
  999. l:=tabstractvarsym(sym).getsize;
  1000. typesym :
  1001. l:=ttypesym(sym).typedef.size;
  1002. else
  1003. Message(asmr_e_wrong_sym_type);
  1004. end;
  1005. end
  1006. else
  1007. Message1(sym_e_unknown_id,tempstr);
  1008. end;
  1009. end;
  1010. str(l, tempstr);
  1011. expr:=expr + tempstr;
  1012. if hasparen then
  1013. Consume(AS_RPAREN);
  1014. end;
  1015. AS_PTR :
  1016. begin
  1017. { Support ugly delphi constructs like <constant> PTR [ref] }
  1018. break;
  1019. end;
  1020. AS_STRING:
  1021. begin
  1022. l:=0;
  1023. case Length(actasmpattern) of
  1024. 1 :
  1025. l:=ord(actasmpattern[1]);
  1026. 2 :
  1027. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1028. 3 :
  1029. l:=ord(actasmpattern[3]) +
  1030. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1031. 4 :
  1032. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1033. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1034. else
  1035. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1036. end;
  1037. str(l, tempstr);
  1038. expr:=expr + tempstr;
  1039. Consume(AS_STRING);
  1040. end;
  1041. AS_ID:
  1042. begin
  1043. hs:='';
  1044. hssymtyp:=AT_DATA;
  1045. def:=nil;
  1046. tempstr:=actasmpattern;
  1047. prevtok:=prevasmtoken;
  1048. { stop parsing a constant expression if we find an opcode after a
  1049. non-operator like "db $66 mov eax,ebx" }
  1050. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  1051. is_asmopcode(actasmpattern) then
  1052. break;
  1053. consume(AS_ID);
  1054. if (tempstr='@CODE') or (tempstr='@DATA') then
  1055. begin
  1056. if asmsym='' then
  1057. begin
  1058. asmsym:=tempstr;
  1059. asmsymtyp:=AT_SECTION;
  1060. end
  1061. else
  1062. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1063. end
  1064. else if SearchIConstant(tempstr,l) then
  1065. begin
  1066. str(l, tempstr);
  1067. expr:=expr + tempstr;
  1068. end
  1069. else
  1070. begin
  1071. if is_locallabel(tempstr) then
  1072. begin
  1073. CreateLocalLabel(tempstr,hl,false);
  1074. hs:=hl.name;
  1075. hssymtyp:=AT_FUNCTION;
  1076. end
  1077. else
  1078. if SearchLabel(tempstr,hl,false) then
  1079. begin
  1080. hs:=hl.name;
  1081. hssymtyp:=AT_FUNCTION;
  1082. end
  1083. else
  1084. begin
  1085. asmsearchsym(tempstr,sym,srsymtable);
  1086. if assigned(sym) then
  1087. begin
  1088. case sym.typ of
  1089. staticvarsym :
  1090. begin
  1091. hs:=tstaticvarsym(sym).mangledname;
  1092. def:=tstaticvarsym(sym).vardef;
  1093. end;
  1094. localvarsym,
  1095. paravarsym :
  1096. begin
  1097. Message(asmr_e_no_local_or_para_allowed);
  1098. end;
  1099. procsym :
  1100. begin
  1101. if Tprocsym(sym).ProcdefList.Count>1 then
  1102. Message(asmr_w_calling_overload_func);
  1103. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1104. {$ifdef i8086}
  1105. is_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1106. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  1107. {$endif i8086}
  1108. hssymtyp:=AT_FUNCTION;
  1109. end;
  1110. typesym :
  1111. begin
  1112. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1113. Message(asmr_e_wrong_sym_type);
  1114. size:=ttypesym(sym).typedef.size;
  1115. end;
  1116. fieldvarsym :
  1117. begin
  1118. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1119. end;
  1120. else
  1121. Message(asmr_e_wrong_sym_type);
  1122. end;
  1123. end
  1124. else
  1125. Message1(sym_e_unknown_id,tempstr);
  1126. end;
  1127. { symbol found? }
  1128. if hs<>'' then
  1129. begin
  1130. if asmsym='' then
  1131. begin
  1132. asmsym:=hs;
  1133. asmsymtyp:=hssymtyp;
  1134. end
  1135. else
  1136. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1137. if (expr='') or (expr[length(expr)]='+') then
  1138. begin
  1139. { don't remove the + if there could be a record field }
  1140. if actasmtoken<>AS_DOT then
  1141. delete(expr,length(expr),1);
  1142. end
  1143. else
  1144. if needofs then
  1145. begin
  1146. if (prevtok<>AS_OFFSET) then
  1147. Message(asmr_e_need_offset);
  1148. end
  1149. else
  1150. Message(asmr_e_only_add_relocatable_symbol);
  1151. end;
  1152. if (actasmtoken=AS_DOT) or
  1153. (assigned(sym) and
  1154. (sym.typ = fieldvarsym) and
  1155. not(sp_static in sym.symoptions)) then
  1156. begin
  1157. BuildRecordOffsetSize(tempstr,l,size,hs,needvmtofs,hastypecast);
  1158. if hs <> '' then
  1159. hssymtyp:=AT_FUNCTION
  1160. else
  1161. begin
  1162. str(l, tempstr);
  1163. expr:=expr + tempstr;
  1164. end
  1165. end
  1166. else if (actasmtoken<>AS_DOT) and
  1167. assigned(sym) and
  1168. (sym.typ=typesym) and
  1169. (ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1170. begin
  1171. { just a record type (without being followed by dot)
  1172. evaluates to 0. Ugly, but TP7 compatible. }
  1173. expr:=expr+'0';
  1174. end
  1175. else
  1176. begin
  1177. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1178. delete(expr,length(expr),1);
  1179. end;
  1180. if (actasmtoken=AS_LBRACKET) and
  1181. assigned(def) and
  1182. (def.typ=arraydef) then
  1183. begin
  1184. consume(AS_LBRACKET);
  1185. l:=BuildConstExpression;
  1186. if l<tarraydef(def).lowrange then
  1187. begin
  1188. Message(asmr_e_constant_out_of_bounds);
  1189. l:=0;
  1190. end
  1191. else
  1192. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1193. str(l, tempstr);
  1194. expr:=expr + '+' + tempstr;
  1195. consume(AS_RBRACKET);
  1196. end;
  1197. end;
  1198. { check if there are wrong operator used like / or mod etc. }
  1199. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1200. Message(asmr_e_only_add_relocatable_symbol);
  1201. end;
  1202. AS_ALIGN,
  1203. AS_DB,
  1204. AS_DW,
  1205. AS_DD,
  1206. AS_DQ,
  1207. AS_END,
  1208. AS_RBRACKET,
  1209. AS_SEPARATOR,
  1210. AS_COMMA,
  1211. AS_COLON:
  1212. break;
  1213. else
  1214. begin
  1215. { write error only once. }
  1216. if not errorflag then
  1217. Message(asmr_e_invalid_constant_expression);
  1218. { consume tokens until we find COMMA or SEPARATOR }
  1219. Consume(actasmtoken);
  1220. errorflag:=TRUE;
  1221. end;
  1222. end;
  1223. Until false;
  1224. { calculate expression }
  1225. if not ErrorFlag then
  1226. value:=CalculateExpression(expr)
  1227. else
  1228. value:=0;
  1229. { no longer in an expression }
  1230. inexpression:=FALSE;
  1231. end;
  1232. Function tx86intreader.BuildConstExpression:aint;
  1233. var
  1234. l,size : tcgint;
  1235. hs : string;
  1236. hssymtyp : TAsmsymtype;
  1237. isseg,is_farproc_entry,hasofs : boolean;
  1238. begin
  1239. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp,size,isseg,is_farproc_entry,hasofs);
  1240. if hs<>'' then
  1241. Message(asmr_e_relocatable_symbol_not_allowed);
  1242. BuildConstExpression:=l;
  1243. end;
  1244. Function tx86intreader.BuildRefConstExpression(out size:tcgint;startingminus:boolean):aint;
  1245. var
  1246. l : tcgint;
  1247. hs : string;
  1248. hssymtyp : TAsmsymtype;
  1249. isseg,is_farproc_entry,hasofs : boolean;
  1250. begin
  1251. BuildConstSymbolExpression(false,true,startingminus,l,hs,hssymtyp,size,isseg,is_farproc_entry,hasofs);
  1252. if hs<>'' then
  1253. Message(asmr_e_relocatable_symbol_not_allowed);
  1254. BuildRefConstExpression:=l;
  1255. end;
  1256. procedure tx86intreader.BuildReference(oper : tx86operand);
  1257. var
  1258. scale : byte;
  1259. k,l,size : tcgint;
  1260. tempstr,hs : string;
  1261. tempsymtyp : tasmsymtype;
  1262. code : integer;
  1263. hreg : tregister;
  1264. GotStar,GotOffset,HadVar,
  1265. GotPlus,Negative,BracketlessReference : boolean;
  1266. hl : tasmlabel;
  1267. isseg: boolean;
  1268. is_farproc_entry,hasofs,
  1269. hastypecast: boolean;
  1270. tmpoper: tx86operand;
  1271. Begin
  1272. if actasmtoken=AS_LBRACKET then
  1273. begin
  1274. Consume(AS_LBRACKET);
  1275. BracketlessReference:=false;
  1276. end
  1277. else
  1278. BracketlessReference:=true;
  1279. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1280. oper.InitRef;
  1281. GotStar:=false;
  1282. GotPlus:=true;
  1283. GotOffset:=false;
  1284. Negative:=false;
  1285. Scale:=0;
  1286. repeat
  1287. if GotOffset and (actasmtoken<>AS_ID) then
  1288. Message(asmr_e_invalid_reference_syntax);
  1289. Case actasmtoken of
  1290. AS_ID, { Constant reference expression OR variable reference expression }
  1291. AS_VMTOFFSET:
  1292. Begin
  1293. if not GotPlus then
  1294. Message(asmr_e_invalid_reference_syntax);
  1295. GotStar:=false;
  1296. GotPlus:=false;
  1297. if (actasmtoken = AS_VMTOFFSET) or
  1298. (SearchIConstant(actasmpattern,l) or
  1299. SearchRecordType(actasmpattern)) then
  1300. begin
  1301. l:=BuildRefConstExpression(size,negative);
  1302. if size<>0 then
  1303. oper.SetSize(size,false);
  1304. negative:=false; { "l" was negated if necessary }
  1305. GotPlus:=(prevasmtoken=AS_PLUS);
  1306. GotStar:=(prevasmtoken=AS_STAR);
  1307. case oper.opr.typ of
  1308. OPR_LOCAL :
  1309. begin
  1310. if GotStar then
  1311. Message(asmr_e_invalid_reference_syntax);
  1312. Inc(oper.opr.localsymofs,l);
  1313. end;
  1314. OPR_REFERENCE :
  1315. begin
  1316. if GotStar then
  1317. oper.opr.ref.scalefactor:=l
  1318. else
  1319. Inc(oper.opr.ref.offset,l);
  1320. end;
  1321. end;
  1322. end
  1323. else
  1324. Begin
  1325. if negative and not oper.hasvar then
  1326. Message(asmr_e_only_add_relocatable_symbol)
  1327. else if oper.hasvar and not GotOffset and
  1328. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1329. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1330. HadVar:=oper.hasvar and GotOffset;
  1331. tempstr:=actasmpattern;
  1332. Consume(AS_ID);
  1333. { typecasting? }
  1334. if (actasmtoken=AS_LPAREN) and
  1335. SearchType(tempstr,l) then
  1336. begin
  1337. oper.hastype:=true;
  1338. oper.typesize:=l;
  1339. Consume(AS_LPAREN);
  1340. BuildOperand(oper,true);
  1341. Consume(AS_RPAREN);
  1342. end
  1343. else
  1344. if is_locallabel(tempstr) then
  1345. begin
  1346. CreateLocalLabel(tempstr,hl,false);
  1347. oper.InitRef;
  1348. oper.haslabelref:=true;
  1349. if not negative then
  1350. begin
  1351. oper.opr.ref.symbol:=hl;
  1352. oper.hasvar:=true;
  1353. end
  1354. else
  1355. oper.opr.ref.relsymbol:=hl;
  1356. {$ifdef i8086}
  1357. if oper.opr.ref.segment=NR_NO then
  1358. oper.opr.ref.segment:=NR_CS;
  1359. {$endif i8086}
  1360. end
  1361. else
  1362. if oper.SetupVar(tempstr,GotOffset) then
  1363. begin
  1364. { convert OPR_LOCAL register para into a reference base }
  1365. if (oper.opr.typ=OPR_LOCAL) and
  1366. AsmRegisterPara(oper.opr.localsym) then
  1367. oper.InitRefConvertLocal
  1368. else
  1369. begin
  1370. {$ifdef x86_64}
  1371. if actasmtoken=AS_WRT then
  1372. begin
  1373. if (oper.opr.typ=OPR_REFERENCE) then
  1374. begin
  1375. Consume(AS_WRT);
  1376. Consume(AS___GOTPCREL);
  1377. if (oper.opr.ref.base<>NR_NO) or
  1378. (oper.opr.ref.index<>NR_NO) or
  1379. (oper.opr.ref.offset<>0) then
  1380. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1381. if tf_no_pic_supported in target_info.flags then
  1382. Message(asmr_e_no_gotpcrel_support);
  1383. oper.opr.ref.refaddr:=addr_pic;
  1384. oper.opr.ref.base:=NR_RIP;
  1385. end
  1386. else
  1387. message(asmr_e_invalid_reference_syntax);
  1388. end;
  1389. {$endif x86_64}
  1390. end;
  1391. end
  1392. else
  1393. Message1(sym_e_unknown_id,tempstr);
  1394. { record.field ? }
  1395. if actasmtoken=AS_DOT then
  1396. begin
  1397. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1398. if (hs<>'') then
  1399. Message(asmr_e_invalid_symbol_ref);
  1400. case oper.opr.typ of
  1401. OPR_LOCAL :
  1402. inc(oper.opr.localsymofs,l);
  1403. OPR_REFERENCE :
  1404. inc(oper.opr.ref.offset,l);
  1405. end;
  1406. if hastypecast then
  1407. oper.hastype:=true;
  1408. oper.SetSize(k,false);
  1409. end;
  1410. if GotOffset then
  1411. begin
  1412. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1413. begin
  1414. if (oper.opr.typ=OPR_REFERENCE) then
  1415. oper.opr.ref.base:=NR_NO;
  1416. oper.hasvar:=hadvar;
  1417. end
  1418. else
  1419. begin
  1420. if oper.hasvar and hadvar then
  1421. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1422. { should we allow ?? }
  1423. end;
  1424. end;
  1425. end;
  1426. GotOffset:=false;
  1427. end;
  1428. AS_PLUS :
  1429. Begin
  1430. Consume(AS_PLUS);
  1431. Negative:=false;
  1432. GotPlus:=true;
  1433. GotStar:=false;
  1434. Scale:=0;
  1435. end;
  1436. AS_DOT :
  1437. Begin
  1438. { Handle like a + }
  1439. Consume(AS_DOT);
  1440. Negative:=false;
  1441. GotPlus:=true;
  1442. GotStar:=false;
  1443. Scale:=0;
  1444. end;
  1445. AS_MINUS :
  1446. begin
  1447. Consume(AS_MINUS);
  1448. Negative:=true;
  1449. GotPlus:=true;
  1450. GotStar:=false;
  1451. Scale:=0;
  1452. end;
  1453. AS_STAR : { Scaling, with eax*4 order }
  1454. begin
  1455. Consume(AS_STAR);
  1456. hs:='';
  1457. l:=0;
  1458. case actasmtoken of
  1459. AS_ID,
  1460. AS_LPAREN :
  1461. l:=BuildConstExpression;
  1462. AS_INTNUM:
  1463. Begin
  1464. hs:=actasmpattern;
  1465. Consume(AS_INTNUM);
  1466. end;
  1467. AS_REGISTER :
  1468. begin
  1469. case oper.opr.typ of
  1470. OPR_REFERENCE :
  1471. begin
  1472. if oper.opr.ref.scalefactor=0 then
  1473. begin
  1474. if scale<>0 then
  1475. begin
  1476. oper.opr.ref.scalefactor:=scale;
  1477. scale:=0;
  1478. end
  1479. else
  1480. Message(asmr_e_wrong_scale_factor);
  1481. end
  1482. else
  1483. Message(asmr_e_invalid_reference_syntax);
  1484. end;
  1485. OPR_LOCAL :
  1486. begin
  1487. if oper.opr.localscale=0 then
  1488. begin
  1489. if scale<>0 then
  1490. begin
  1491. oper.opr.localscale:=scale;
  1492. scale:=0;
  1493. end
  1494. else
  1495. Message(asmr_e_wrong_scale_factor);
  1496. end
  1497. else
  1498. Message(asmr_e_invalid_reference_syntax);
  1499. end;
  1500. end;
  1501. end;
  1502. else
  1503. Message(asmr_e_invalid_reference_syntax);
  1504. end;
  1505. if actasmtoken<>AS_REGISTER then
  1506. begin
  1507. if hs<>'' then
  1508. val(hs,l,code);
  1509. case oper.opr.typ of
  1510. OPR_REFERENCE :
  1511. oper.opr.ref.scalefactor:=l;
  1512. OPR_LOCAL :
  1513. oper.opr.localscale:=l;
  1514. end;
  1515. if l>9 then
  1516. Message(asmr_e_wrong_scale_factor);
  1517. end;
  1518. GotPlus:=false;
  1519. GotStar:=false;
  1520. end;
  1521. AS_REGISTER :
  1522. begin
  1523. hreg:=actasmregister;
  1524. Consume(AS_REGISTER);
  1525. if actasmtoken=AS_COLON then
  1526. begin
  1527. Consume(AS_COLON);
  1528. oper.InitRefConvertLocal;
  1529. SetSegmentOverride(oper,hreg);
  1530. end
  1531. else
  1532. begin
  1533. if not((GotPlus and (not Negative)) or
  1534. GotStar) then
  1535. Message(asmr_e_invalid_reference_syntax);
  1536. { this register will be the index:
  1537. 1. just read a *
  1538. 2. next token is a *
  1539. 3. base register is already used }
  1540. case oper.opr.typ of
  1541. OPR_LOCAL :
  1542. begin
  1543. if (oper.opr.localindexreg<>NR_NO) then
  1544. Message(asmr_e_multiple_index);
  1545. {$ifdef x86_64}
  1546. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1547. if (hreg=NR_RIP) then
  1548. Message(asmr_e_no_local_or_para_allowed);
  1549. {$endif x86_64}
  1550. oper.opr.localindexreg:=hreg;
  1551. if scale<>0 then
  1552. begin
  1553. oper.opr.localscale:=scale;
  1554. scale:=0;
  1555. end;
  1556. end;
  1557. OPR_REFERENCE :
  1558. begin
  1559. if (GotStar) or
  1560. (actasmtoken=AS_STAR) or
  1561. (oper.opr.ref.base<>NR_NO) then
  1562. begin
  1563. if (oper.opr.ref.index<>NR_NO) then
  1564. Message(asmr_e_multiple_index);
  1565. oper.opr.ref.index:=hreg;
  1566. if scale<>0 then
  1567. begin
  1568. oper.opr.ref.scalefactor:=scale;
  1569. scale:=0;
  1570. end;
  1571. end
  1572. else
  1573. begin
  1574. oper.opr.ref.base:=hreg;
  1575. {$ifdef x86_64}
  1576. { non-GOT based RIP-relative accesses are also position-independent }
  1577. if (oper.opr.ref.base=NR_RIP) and
  1578. (oper.opr.ref.refaddr<>addr_pic) then
  1579. oper.opr.ref.refaddr:=addr_pic_no_got;
  1580. {$endif x86_64}
  1581. end;
  1582. end;
  1583. end;
  1584. GotPlus:=false;
  1585. GotStar:=false;
  1586. end;
  1587. end;
  1588. AS_OFFSET :
  1589. begin
  1590. Consume(AS_OFFSET);
  1591. GotOffset:=true;
  1592. end;
  1593. AS_TYPE,
  1594. AS_NOT,
  1595. AS_STRING,
  1596. AS_INTNUM,
  1597. AS_LPAREN : { Constant reference expression }
  1598. begin
  1599. if not GotPlus and not GotStar then
  1600. Message(asmr_e_invalid_reference_syntax);
  1601. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp,size,isseg,is_farproc_entry,hasofs);
  1602. { already handled by BuildConstSymbolExpression(); must be
  1603. handled there to avoid [reg-1+1] being interpreted as
  1604. [reg-(1+1)] }
  1605. negative:=false;
  1606. if tempstr<>'' then
  1607. begin
  1608. if GotStar then
  1609. Message(asmr_e_only_add_relocatable_symbol);
  1610. if not assigned(oper.opr.ref.symbol) then
  1611. begin
  1612. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1613. {$ifdef i8086}
  1614. if isseg then
  1615. begin
  1616. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1617. oper.opr.ref.refaddr:=addr_seg;
  1618. end
  1619. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1620. oper.opr.ref.segment:=NR_CS;
  1621. {$endif i8086}
  1622. end
  1623. else
  1624. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1625. end;
  1626. case oper.opr.typ of
  1627. OPR_REFERENCE :
  1628. begin
  1629. if GotStar then
  1630. oper.opr.ref.scalefactor:=l
  1631. else if (prevasmtoken = AS_STAR) then
  1632. begin
  1633. if scale<>0 then
  1634. scale:=l*scale
  1635. else
  1636. scale:=l;
  1637. end
  1638. else
  1639. begin
  1640. Inc(oper.opr.ref.offset,l);
  1641. Inc(oper.opr.constoffset,l);
  1642. end;
  1643. end;
  1644. OPR_LOCAL :
  1645. begin
  1646. if GotStar then
  1647. oper.opr.localscale:=l
  1648. else if (prevasmtoken = AS_STAR) then
  1649. begin
  1650. if scale<>0 then
  1651. scale:=l*scale
  1652. else
  1653. scale:=l;
  1654. end
  1655. else
  1656. Inc(oper.opr.localsymofs,l);
  1657. end;
  1658. end;
  1659. GotPlus:=(prevasmtoken=AS_PLUS) or
  1660. (prevasmtoken=AS_MINUS);
  1661. if GotPlus then
  1662. negative := prevasmtoken = AS_MINUS;
  1663. GotStar:=(prevasmtoken=AS_STAR);
  1664. end;
  1665. AS_LBRACKET :
  1666. begin
  1667. if (GotPlus and Negative) or GotStar then
  1668. Message(asmr_e_invalid_reference_syntax);
  1669. tmpoper:=Tx86Operand.create;
  1670. BuildReference(tmpoper);
  1671. AddReferences(oper,tmpoper);
  1672. tmpoper.Free;
  1673. GotPlus:=false;
  1674. GotStar:=false;
  1675. end;
  1676. AS_RBRACKET :
  1677. begin
  1678. if GotPlus or GotStar or BracketlessReference then
  1679. Message(asmr_e_invalid_reference_syntax);
  1680. Consume(AS_RBRACKET);
  1681. if actasmtoken=AS_LBRACKET then
  1682. begin
  1683. tmpoper:=Tx86Operand.create;
  1684. BuildReference(tmpoper);
  1685. AddReferences(oper,tmpoper);
  1686. tmpoper.Free;
  1687. end;
  1688. break;
  1689. end;
  1690. AS_SEPARATOR,
  1691. AS_END,
  1692. AS_COMMA:
  1693. begin
  1694. if not BracketlessReference then
  1695. begin
  1696. Message(asmr_e_invalid_reference_syntax);
  1697. RecoverConsume(true);
  1698. end;
  1699. break;
  1700. end;
  1701. else
  1702. Begin
  1703. Message(asmr_e_invalid_reference_syntax);
  1704. RecoverConsume(true);
  1705. break;
  1706. end;
  1707. end;
  1708. until false;
  1709. end;
  1710. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1711. var
  1712. l,size : tcgint;
  1713. tempstr : string;
  1714. tempsymtyp : tasmsymtype;
  1715. isseg,is_farproc_entry,hasofs : boolean;
  1716. begin
  1717. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1718. Message(asmr_e_invalid_operand_type);
  1719. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp,size,isseg,is_farproc_entry,hasofs);
  1720. {$ifdef i8086}
  1721. if tempstr='@DATA' then
  1722. begin
  1723. if not isseg then
  1724. Message(asmr_e_CODE_or_DATA_without_SEG);
  1725. oper.SetupData;
  1726. end
  1727. else if tempstr='@CODE' then
  1728. begin
  1729. if not isseg then
  1730. Message(asmr_e_CODE_or_DATA_without_SEG);
  1731. oper.SetupCode;
  1732. end
  1733. else
  1734. {$endif i8086}
  1735. if tempstr<>'' then
  1736. begin
  1737. oper.opr.typ:=OPR_SYMBOL;
  1738. oper.opr.symofs:=l;
  1739. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1740. oper.opr.symseg:=isseg;
  1741. oper.opr.sym_farproc_entry:=is_farproc_entry;
  1742. end
  1743. else
  1744. if oper.opr.typ=OPR_NONE then
  1745. begin
  1746. oper.opr.typ:=OPR_CONSTANT;
  1747. oper.opr.val:=l;
  1748. end
  1749. else
  1750. inc(oper.opr.val,l);
  1751. end;
  1752. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1753. procedure AddLabelOperand(hl:tasmlabel);
  1754. begin
  1755. if (oper.opr.typ=OPR_NONE) and
  1756. is_calljmp(actopcode) then
  1757. begin
  1758. oper.opr.typ:=OPR_SYMBOL;
  1759. oper.opr.symbol:=hl;
  1760. end
  1761. else
  1762. begin
  1763. oper.InitRef;
  1764. oper.opr.ref.symbol:=hl;
  1765. oper.haslabelref:=true;
  1766. {$ifdef i8086}
  1767. if oper.opr.ref.segment=NR_NO then
  1768. oper.opr.ref.segment:=NR_CS;
  1769. {$endif i8086}
  1770. end;
  1771. end;
  1772. var
  1773. expr,
  1774. hs : string;
  1775. tempreg : tregister;
  1776. l : tcgint;
  1777. hl : tasmlabel;
  1778. toffset,
  1779. tsize : tcgint;
  1780. hastypecast: boolean;
  1781. begin
  1782. expr:='';
  1783. repeat
  1784. if actasmtoken=AS_DOT then
  1785. begin
  1786. if expr<>'' then
  1787. begin
  1788. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  1789. if (oper.opr.typ<>OPR_NONE) and
  1790. (hs<>'') then
  1791. Message(asmr_e_wrong_sym_type);
  1792. oper.SetSize(tsize,true);
  1793. if hastypecast then
  1794. oper.hastype:=true;
  1795. { we have used the size of a field. Reset the typesize of the record }
  1796. oper.typesize:=0;
  1797. case oper.opr.typ of
  1798. OPR_LOCAL :
  1799. begin
  1800. { don't allow direct access to fields of parameters, because that
  1801. will generate buggy code. Allow it only for explicit typecasting
  1802. and when the parameter is in a register (delphi compatible) }
  1803. if (not oper.hastype) then
  1804. checklocalsubscript(oper.opr.localsym);
  1805. oper.opr.localforceref:=true;
  1806. inc(oper.opr.localsymofs,toffset);
  1807. oper.opr.localvarsize := tsize;
  1808. end;
  1809. OPR_CONSTANT :
  1810. inc(oper.opr.val,toffset);
  1811. OPR_REFERENCE :
  1812. begin
  1813. inc(oper.opr.ref.offset,toffset);
  1814. oper.opr.varsize := tsize;
  1815. end;
  1816. OPR_NONE :
  1817. begin
  1818. if (hs <> '') then
  1819. begin
  1820. oper.opr.typ:=OPR_SYMBOL;
  1821. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  1822. end
  1823. else
  1824. begin
  1825. oper.opr.typ:=OPR_CONSTANT;
  1826. oper.opr.val:=toffset;
  1827. end;
  1828. end;
  1829. OPR_REGISTER :
  1830. Message(asmr_e_invalid_reference_syntax);
  1831. OPR_SYMBOL:
  1832. Message(asmr_e_invalid_symbol_ref);
  1833. else
  1834. internalerror(200309222);
  1835. end;
  1836. expr:='';
  1837. end
  1838. else
  1839. begin
  1840. { See it as a separator }
  1841. Consume(AS_DOT);
  1842. end;
  1843. end;
  1844. case actasmtoken of
  1845. {$ifndef i8086}
  1846. AS_SEG :
  1847. Begin
  1848. Message(asmr_e_seg_not_supported);
  1849. Consume(actasmtoken);
  1850. end;
  1851. {$else not i8086}
  1852. AS_SEG,
  1853. {$endif not i8086}
  1854. AS_OFFSET,
  1855. AS_SIZEOF,
  1856. AS_VMTOFFSET,
  1857. AS_TYPE,
  1858. AS_NOT,
  1859. AS_STRING,
  1860. AS_PLUS,
  1861. AS_MINUS,
  1862. AS_LPAREN,
  1863. AS_INTNUM :
  1864. begin
  1865. case oper.opr.typ of
  1866. OPR_REFERENCE :
  1867. {$ifndef x86_64}
  1868. { this is for the i386 scenario where you have
  1869. <load got into ebx>
  1870. mov eax, [ebx].offset globalvar
  1871. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  1872. relative accesses)
  1873. }
  1874. if (actasmtoken=AS_OFFSET) and
  1875. (cs_create_pic in current_settings.moduleswitches) then
  1876. begin
  1877. Consume(AS_OFFSET);
  1878. oper.opr.ref.refaddr:=addr_pic;
  1879. BuildOperand(oper,false);
  1880. end
  1881. else
  1882. {$endif x86_64}
  1883. begin
  1884. l := BuildRefConstExpression(tsize);
  1885. if tsize<>0 then
  1886. oper.SetSize(tsize,false);
  1887. inc(oper.opr.ref.offset,l);
  1888. inc(oper.opr.constoffset,l);
  1889. end;
  1890. OPR_LOCAL :
  1891. begin
  1892. l := BuildConstExpression;
  1893. inc(oper.opr.localsymofs,l);
  1894. inc(oper.opr.localconstoffset,l);
  1895. end;
  1896. OPR_NONE,
  1897. OPR_CONSTANT :
  1898. BuildConstantOperand(oper);
  1899. else
  1900. Message(asmr_e_invalid_operand_type);
  1901. end;
  1902. end;
  1903. AS_PTR :
  1904. begin
  1905. if not oper.hastype then
  1906. begin
  1907. if (oper.opr.typ=OPR_CONSTANT) then
  1908. begin
  1909. oper.typesize:=oper.opr.val;
  1910. { reset constant value of operand }
  1911. oper.opr.typ:=OPR_NONE;
  1912. oper.opr.val:=0;
  1913. end
  1914. else
  1915. Message(asmr_e_syn_operand);
  1916. end;
  1917. Consume(AS_PTR);
  1918. { in delphi mode, allow e.g. call dword ptr eax,
  1919. see also webtbs/tw18225.pp }
  1920. if not(m_delphi in current_settings.modeswitches) then
  1921. oper.InitRef;
  1922. { if the operand subscripts a record, the typesize will be
  1923. rest -> save it here and restore it afterwards }
  1924. l:=oper.typesize;
  1925. BuildOperand(oper,false);
  1926. oper.setsize(l,true);
  1927. end;
  1928. AS_ID : { A constant expression, or a Variable ref. }
  1929. Begin
  1930. { Label or Special symbol reference? }
  1931. if actasmpattern[1] = '@' then
  1932. Begin
  1933. if actasmpattern = '@RESULT' then
  1934. Begin
  1935. oper.SetupResult;
  1936. Consume(AS_ID);
  1937. expr:='result';
  1938. end
  1939. else
  1940. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1941. begin
  1942. {$ifdef i8086}
  1943. Message(asmr_e_CODE_or_DATA_without_SEG);
  1944. {$else i8086}
  1945. Message(asmr_w_CODE_and_DATA_not_supported);
  1946. {$endif i8086}
  1947. Consume(AS_ID);
  1948. end
  1949. else
  1950. { Local Label }
  1951. begin
  1952. CreateLocalLabel(actasmpattern,hl,false);
  1953. Consume(AS_ID);
  1954. AddLabelOperand(hl);
  1955. end;
  1956. end
  1957. else
  1958. { support result for delphi modes }
  1959. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1960. begin
  1961. oper.SetUpResult;
  1962. Consume(AS_ID);
  1963. expr:='result';
  1964. end
  1965. { probably a variable or normal expression }
  1966. { or a procedure (such as in CALL ID) }
  1967. else
  1968. Begin
  1969. { is it a constant ? }
  1970. if SearchIConstant(actasmpattern,l) then
  1971. Begin
  1972. case oper.opr.typ of
  1973. OPR_REFERENCE :
  1974. begin
  1975. l := BuildRefConstExpression(tsize);
  1976. if tsize<>0 then
  1977. oper.SetSize(tsize,false);
  1978. inc(oper.opr.ref.offset,l);
  1979. inc(oper.opr.constoffset,l);
  1980. end;
  1981. OPR_LOCAL :
  1982. begin
  1983. l := BuildRefConstExpression(tsize);
  1984. if tsize<>0 then
  1985. oper.SetSize(tsize,false);
  1986. inc(oper.opr.localsymofs,l);
  1987. inc(oper.opr.localconstoffset,l);
  1988. end;
  1989. OPR_NONE,
  1990. OPR_CONSTANT :
  1991. BuildConstantOperand(oper);
  1992. else
  1993. Message(asmr_e_invalid_operand_type);
  1994. end;
  1995. end
  1996. else
  1997. { Check for pascal label }
  1998. if SearchLabel(actasmpattern,hl,false) then
  1999. begin
  2000. Consume(AS_ID);
  2001. AddLabelOperand(hl);
  2002. end
  2003. else
  2004. { is it a normal variable ? }
  2005. Begin
  2006. expr:=actasmpattern;
  2007. Consume(AS_ID);
  2008. { typecasting? }
  2009. if SearchType(expr,l) then
  2010. begin
  2011. oper.hastype:=true;
  2012. oper.typesize:=l;
  2013. case actasmtoken of
  2014. AS_LPAREN :
  2015. begin
  2016. { Support Type([Reference]) }
  2017. Consume(AS_LPAREN);
  2018. BuildOperand(oper,true);
  2019. { Delphi also supports Type(Register) and
  2020. interprets it the same as Type([Register]). }
  2021. if (oper.opr.typ = OPR_REGISTER) then
  2022. { This also sets base to the register. }
  2023. oper.InitRef;
  2024. Consume(AS_RPAREN);
  2025. end;
  2026. AS_LBRACKET :
  2027. begin
  2028. { Support Var.Type[Index] }
  2029. { Convert @label.Byte[1] to reference }
  2030. if oper.opr.typ=OPR_SYMBOL then
  2031. oper.initref;
  2032. end;
  2033. end;
  2034. end
  2035. else
  2036. begin
  2037. if not oper.SetupVar(expr,false) then
  2038. Begin
  2039. { not a variable, check special variables.. }
  2040. if expr = 'SELF' then
  2041. begin
  2042. oper.SetupSelf;
  2043. expr:='self';
  2044. end
  2045. else
  2046. begin
  2047. Message1(sym_e_unknown_id,expr);
  2048. expr:='';
  2049. end;
  2050. end;
  2051. { indexed access to variable? }
  2052. if actasmtoken=AS_LBRACKET then
  2053. begin
  2054. { ... then the operand size is not known anymore }
  2055. oper.size:=OS_NO;
  2056. BuildReference(oper);
  2057. end;
  2058. end;
  2059. end;
  2060. end;
  2061. end;
  2062. AS_REGISTER : { Register, a variable reference or a constant reference }
  2063. begin
  2064. { save the type of register used. }
  2065. tempreg:=actasmregister;
  2066. Consume(AS_REGISTER);
  2067. if actasmtoken = AS_COLON then
  2068. Begin
  2069. Consume(AS_COLON);
  2070. oper.InitRef;
  2071. SetSegmentOverride(oper,tempreg);
  2072. BuildReference(oper);
  2073. end
  2074. else
  2075. { Simple register }
  2076. begin
  2077. if (oper.opr.typ <> OPR_NONE) then
  2078. Message(asmr_e_syn_operand);
  2079. oper.opr.typ:=OPR_REGISTER;
  2080. oper.opr.reg:=tempreg;
  2081. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  2082. end;
  2083. end;
  2084. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  2085. Begin
  2086. BuildReference(oper);
  2087. end;
  2088. AS_DWORD,
  2089. AS_BYTE,
  2090. AS_WORD,
  2091. AS_TBYTE,
  2092. AS_DQWORD,
  2093. AS_QWORD,
  2094. AS_OWORD,
  2095. AS_XMMWORD,
  2096. AS_YWORD,
  2097. AS_YMMWORD:
  2098. begin
  2099. { Type specifier }
  2100. oper.hastype:=true;
  2101. oper.typesize:=0;
  2102. case actasmtoken of
  2103. AS_DWORD : oper.typesize:=4;
  2104. AS_WORD : oper.typesize:=2;
  2105. AS_BYTE : oper.typesize:=1;
  2106. AS_QWORD : oper.typesize:=8;
  2107. AS_DQWORD : oper.typesize:=16;
  2108. AS_TBYTE : oper.typesize:=10;
  2109. AS_OWORD,
  2110. AS_XMMWORD: oper.typesize:=16;
  2111. AS_YWORD,
  2112. AS_YMMWORD: oper.typesize:=32;
  2113. else
  2114. internalerror(2010061101);
  2115. end;
  2116. Consume(actasmtoken);
  2117. if (actasmtoken=AS_LPAREN) then
  2118. begin
  2119. { Support "xxx ptr [Reference]" }
  2120. { in case the expression subscripts a record, the typesize
  2121. is reset, so save the explicit size we set above }
  2122. l:=oper.typesize;
  2123. Consume(AS_LPAREN);
  2124. BuildOperand(oper,true);
  2125. Consume(AS_RPAREN);
  2126. oper.setsize(l,true);
  2127. end;
  2128. end;
  2129. AS_SEPARATOR,
  2130. AS_END,
  2131. AS_COMMA,
  2132. AS_COLON:
  2133. begin
  2134. break;
  2135. end;
  2136. AS_RPAREN:
  2137. begin
  2138. if not istypecast then
  2139. begin
  2140. Message(asmr_e_syn_operand);
  2141. Consume(AS_RPAREN);
  2142. end
  2143. else
  2144. break;
  2145. end;
  2146. else
  2147. begin
  2148. Message(asmr_e_syn_operand);
  2149. RecoverConsume(true);
  2150. break;
  2151. end;
  2152. end;
  2153. until false;
  2154. { End of operand, update size if a typecast is forced }
  2155. if (oper.typesize<>0) and
  2156. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  2157. oper.SetSize(oper.typesize,true);
  2158. {$ifdef i8086}
  2159. { references to a procedure/function entry, without an explicit segment
  2160. override, are added an CS: override by default (this is Turbo Pascal 7
  2161. compatible) }
  2162. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2163. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2164. oper.opr.ref.segment:=NR_CS;
  2165. {$endif i8086}
  2166. end;
  2167. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2168. var
  2169. PrefixOp,OverrideOp: tasmop;
  2170. operandnum : longint;
  2171. t: TRegister;
  2172. is_far_const:boolean;
  2173. i:byte;
  2174. tmp: toperand;
  2175. di_param, si_param: ShortInt;
  2176. prefix_or_override_pending_concat: boolean = false;
  2177. {$ifdef i8086}
  2178. hsymbol: TAsmSymbol;
  2179. hoffset: ASizeInt;
  2180. href_farproc_entry: Boolean;
  2181. {$endif i8086}
  2182. begin
  2183. PrefixOp:=A_None;
  2184. OverrideOp:=A_None;
  2185. is_far_const:=false;
  2186. { prefix seg opcode / prefix opcode }
  2187. repeat
  2188. if is_prefix(actopcode) then
  2189. with instr do
  2190. begin
  2191. if prefix_or_override_pending_concat then
  2192. ConcatInstruction(curlist);
  2193. PrefixOp:=ActOpcode;
  2194. opcode:=ActOpcode;
  2195. condition:=ActCondition;
  2196. opsize:=ActOpsize;
  2197. prefix_or_override_pending_concat:=true;
  2198. consume(AS_OPCODE);
  2199. end
  2200. else
  2201. if is_override(actopcode) then
  2202. with instr do
  2203. begin
  2204. if prefix_or_override_pending_concat then
  2205. ConcatInstruction(curlist);
  2206. OverrideOp:=ActOpcode;
  2207. opcode:=ActOpcode;
  2208. condition:=ActCondition;
  2209. opsize:=ActOpsize;
  2210. prefix_or_override_pending_concat:=true;
  2211. consume(AS_OPCODE);
  2212. end
  2213. else
  2214. break;
  2215. { allow for newline after prefix or override }
  2216. while actasmtoken=AS_SEPARATOR do
  2217. consume(AS_SEPARATOR);
  2218. until (actasmtoken<>AS_OPCODE);
  2219. { opcode }
  2220. if (actasmtoken <> AS_OPCODE) then
  2221. begin
  2222. { allow a prefix or override to be used standalone, like an opcode
  2223. with zero operands; this is TP7 compatible and allows compiling
  2224. ugly code like 'seges; db $67,$66; lodsw' }
  2225. if prefix_or_override_pending_concat then
  2226. exit
  2227. else
  2228. begin
  2229. Message(asmr_e_invalid_or_missing_opcode);
  2230. RecoverConsume(false);
  2231. exit;
  2232. end;
  2233. end;
  2234. if prefix_or_override_pending_concat then
  2235. begin
  2236. instr.ConcatInstruction(curlist);
  2237. prefix_or_override_pending_concat:=false;
  2238. end;
  2239. { Fill the instr object with the current state }
  2240. with instr do
  2241. begin
  2242. Opcode:=ActOpcode;
  2243. condition:=ActCondition;
  2244. opsize:=ActOpsize;
  2245. { Valid combination of prefix/override and instruction ? }
  2246. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2247. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2248. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2249. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2250. end;
  2251. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2252. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2253. these instructions doesn't change anything in the internal assember,
  2254. so change the opcode }
  2255. if (instr.opcode=A_POPF) then
  2256. instr.opcode:=A_POPFW
  2257. else if (instr.opcode=A_PUSHF) then
  2258. instr.opcode:=A_PUSHFW
  2259. {$ifndef x86_64}
  2260. else if (instr.opcode=A_PUSHA) then
  2261. instr.opcode:=A_PUSHAW
  2262. else if (instr.opcode=A_POPA) then
  2263. instr.opcode:=A_POPAW
  2264. {$endif x86_64}
  2265. {$ifdef i8086}
  2266. { ret is converted to retn or retf, depending on the call model of the
  2267. current procedure (BP7 compatible) }
  2268. else if (instr.opcode=A_RET) then
  2269. begin
  2270. if is_proc_far(current_procinfo.procdef) and
  2271. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2272. instr.opcode:=A_RETF
  2273. else
  2274. instr.opcode:=A_RETN;
  2275. end
  2276. {$endif i8086}
  2277. ;
  2278. { We are reading operands, so opcode will be an AS_ID }
  2279. { process operands backwards to get them in AT&T order }
  2280. operandnum:=max_operands;
  2281. is_far_const:=false;
  2282. Consume(AS_OPCODE);
  2283. { Zero operand opcode ? }
  2284. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2285. exit;
  2286. { Read Operands }
  2287. repeat
  2288. case actasmtoken of
  2289. { End of asm operands for this opcode }
  2290. AS_END,
  2291. AS_SEPARATOR :
  2292. break;
  2293. { Operand delimiter }
  2294. AS_COMMA :
  2295. begin
  2296. { should have something before the comma }
  2297. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2298. Message(asmr_e_syntax_error);
  2299. if operandnum <= 1 then
  2300. Message(asmr_e_too_many_operands)
  2301. else
  2302. Dec(operandnum);
  2303. Consume(AS_COMMA);
  2304. end;
  2305. {Far constant, i.e. jmp $0000:$11111111.}
  2306. AS_COLON:
  2307. begin
  2308. is_far_const:=true;
  2309. if operandnum<max_operands then
  2310. message(asmr_e_too_many_operands)
  2311. else
  2312. dec(operandnum);
  2313. consume(AS_COLON);
  2314. end;
  2315. { Type specifier }
  2316. AS_NEAR,
  2317. AS_FAR :
  2318. begin
  2319. if actasmtoken = AS_NEAR then
  2320. begin
  2321. {$ifndef i8086}
  2322. Message(asmr_w_near_ignored);
  2323. {$endif not i8086}
  2324. instr.opsize:=S_NEAR;
  2325. end
  2326. else
  2327. begin
  2328. {$ifndef i8086}
  2329. Message(asmr_w_far_ignored);
  2330. {$endif not i8086}
  2331. instr.opsize:=S_FAR;
  2332. end;
  2333. Consume(actasmtoken);
  2334. if actasmtoken=AS_PTR then
  2335. begin
  2336. Consume(AS_PTR);
  2337. instr.Operands[operandnum].InitRef;
  2338. end;
  2339. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2340. end;
  2341. else
  2342. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2343. end; { end case }
  2344. until false;
  2345. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2346. for i:=operandnum to max_operands do
  2347. begin
  2348. tmp:=instr.operands[i+1-operandnum];
  2349. instr.operands[i+1-operandnum]:=instr.operands[i];
  2350. instr.operands[i]:=tmp;
  2351. end;
  2352. operandnum:=(max_operands+1)-operandnum;
  2353. instr.ops:=operandnum;
  2354. { Check operands }
  2355. for i:=1 to operandnum do
  2356. begin
  2357. if is_far_const and
  2358. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2359. message(asmr_e_expr_illegal)
  2360. else
  2361. if instr.operands[i].opr.typ=OPR_NONE then
  2362. Message(asmr_e_syntax_error);
  2363. end;
  2364. { Check for invalid ES: overrides }
  2365. if is_x86_parameterized_string_op(instr.opcode) then
  2366. begin
  2367. si_param:=get_x86_string_op_si_param(instr.opcode);
  2368. if si_param<>-1 then
  2369. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2370. di_param:=get_x86_string_op_di_param(instr.opcode);
  2371. if di_param<>-1 then
  2372. begin
  2373. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2374. if di_param<=operandnum then
  2375. with instr.operands[di_param] do
  2376. if (opr.typ=OPR_REFERENCE) and
  2377. (opr.ref.segment<>NR_NO) and
  2378. (opr.ref.segment<>NR_ES) then
  2379. Message(asmr_e_cannot_override_es_segment);
  2380. end;
  2381. { if two memory parameters, check whether their address sizes are equal }
  2382. if (si_param<>-1) and (di_param<>-1) and
  2383. (si_param<=operandnum) and (di_param<=operandnum) and
  2384. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2385. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2386. begin
  2387. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2388. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2389. Message(asmr_e_address_sizes_do_not_match);
  2390. end;
  2391. end;
  2392. { e.g. for "push dword 1", "push word 6" }
  2393. if (instr.ops=1) and
  2394. (instr.operands[1].typesize<>0) then
  2395. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2396. {$ifdef i8086}
  2397. for i:=1 to operandnum do
  2398. with instr.operands[i].opr do
  2399. begin
  2400. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2401. but Turbo Pascal 7 compatible. }
  2402. if (instr.opcode in [A_CALL,A_JMP]) and
  2403. (instr.operands[i].haslabelref or instr.operands[i].hasproc)
  2404. and (typ=OPR_REFERENCE) and
  2405. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2406. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2407. begin
  2408. hsymbol:=ref.symbol;
  2409. hoffset:=ref.offset;
  2410. href_farproc_entry:=ref_farproc_entry;
  2411. typ:=OPR_SYMBOL;
  2412. symbol:=hsymbol;
  2413. symofs:=hoffset;
  2414. symseg:=False;
  2415. sym_farproc_entry:=href_farproc_entry;
  2416. end;
  2417. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2418. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2419. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2420. instr.opsize:=S_FAR;
  2421. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2422. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2423. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2424. instr.opsize:=S_FAR;
  2425. end;
  2426. {$endif i8086}
  2427. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2428. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2429. begin
  2430. for i:=1 to operandnum do
  2431. begin
  2432. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2433. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2434. (instr.operands[i].opr.ref.index = NR_NO) then
  2435. begin
  2436. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2437. instr.operands[i].opr.ref.base := NR_NO;
  2438. end
  2439. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2440. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2441. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2442. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2443. begin
  2444. // exchange base- and index-register
  2445. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2446. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2447. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2448. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2449. else
  2450. begin
  2451. t := instr.operands[i].opr.ref.base;
  2452. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2453. instr.operands[i].opr.ref.index := t;
  2454. end;
  2455. end;
  2456. end;
  2457. end;
  2458. end;
  2459. Procedure tx86intreader.BuildConstant(constsize: byte);
  2460. var
  2461. asmsymtyp : tasmsymtype;
  2462. asmsym,
  2463. expr: string;
  2464. value,size : tcgint;
  2465. isseg,is_farproc_entry,hasofs : boolean;
  2466. Begin
  2467. Repeat
  2468. Case actasmtoken of
  2469. AS_STRING:
  2470. Begin
  2471. { DD and DW cases }
  2472. if constsize <> 1 then
  2473. Begin
  2474. if Not PadZero(actasmpattern,constsize) then
  2475. Message(scan_f_string_exceeds_line);
  2476. end;
  2477. expr:=actasmpattern;
  2478. Consume(AS_STRING);
  2479. Case actasmtoken of
  2480. AS_COMMA:
  2481. Consume(AS_COMMA);
  2482. AS_END,
  2483. AS_SEPARATOR: ;
  2484. else
  2485. Message(asmr_e_invalid_string_expression);
  2486. end;
  2487. ConcatString(curlist,expr);
  2488. end;
  2489. AS_PLUS,
  2490. AS_MINUS,
  2491. AS_LPAREN,
  2492. AS_NOT,
  2493. AS_INTNUM,
  2494. AS_OFFSET,
  2495. {$ifdef i8086}
  2496. AS_SEG,
  2497. {$endif i8086}
  2498. AS_ID :
  2499. Begin
  2500. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp,size,isseg,is_farproc_entry,hasofs);
  2501. if asmsym<>'' then
  2502. begin
  2503. if (not isseg) and
  2504. {$ifdef i8086}
  2505. ((constsize<>2) and (constsize<>4))
  2506. {$else i8086}
  2507. (constsize<>sizeof(pint))
  2508. {$endif i8086}
  2509. then
  2510. begin
  2511. Message1(asmr_w_const32bit_for_address,asmsym);
  2512. constsize:=sizeof(pint);
  2513. end;
  2514. {$ifdef i8086}
  2515. if asmsym='@DATA' then
  2516. begin
  2517. if not isseg then
  2518. Message(asmr_e_CODE_or_DATA_without_SEG);
  2519. if constsize<2 then
  2520. Message1(asmr_e_const16bit_for_segment,asmsym);
  2521. if current_settings.x86memorymodel=mm_huge then
  2522. curlist.concat(Tai_const.Create_fardataseg)
  2523. else
  2524. curlist.concat(Tai_const.Create_dgroup);
  2525. if constsize>2 then
  2526. ConcatConstant(curlist,0,constsize-2);
  2527. end
  2528. else if asmsym='@CODE' then
  2529. begin
  2530. if not isseg then
  2531. Message(asmr_e_CODE_or_DATA_without_SEG);
  2532. if constsize<2 then
  2533. Message1(asmr_e_const16bit_for_segment,asmsym);
  2534. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2535. if constsize>2 then
  2536. ConcatConstant(curlist,0,constsize-2);
  2537. end
  2538. else if isseg then
  2539. begin
  2540. if constsize<2 then
  2541. Message1(asmr_e_const16bit_for_segment,asmsym);
  2542. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2543. if constsize>2 then
  2544. ConcatConstant(curlist,0,constsize-2);
  2545. end
  2546. else
  2547. {$endif i8086}
  2548. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,hasofs);
  2549. end
  2550. else
  2551. ConcatConstant(curlist,value,constsize);
  2552. end;
  2553. AS_COMMA:
  2554. begin
  2555. Consume(AS_COMMA);
  2556. end;
  2557. AS_ALIGN,
  2558. AS_DB,
  2559. AS_DW,
  2560. AS_DD,
  2561. AS_DQ,
  2562. AS_OPCODE,
  2563. AS_END,
  2564. AS_SEPARATOR:
  2565. break;
  2566. else
  2567. begin
  2568. Message(asmr_e_syn_constant);
  2569. RecoverConsume(false);
  2570. end
  2571. end;
  2572. Until false;
  2573. end;
  2574. function tx86intreader.Assemble: tlinkedlist;
  2575. Var
  2576. hl : tasmlabel;
  2577. instr : Tx86Instruction;
  2578. tmpsym: tsym;
  2579. tmpsrsymtable: TSymtable;
  2580. Begin
  2581. Message1(asmr_d_start_reading,'intel');
  2582. inexpression:=FALSE;
  2583. firsttoken:=TRUE;
  2584. { sets up all opcode and register tables in uppercase
  2585. done in the construtor now
  2586. if not _asmsorted then
  2587. Begin
  2588. SetupTables;
  2589. _asmsorted:=TRUE;
  2590. end;
  2591. }
  2592. curlist:=TAsmList.Create;
  2593. { we might need to know which parameters are passed in registers }
  2594. if not parse_generic then
  2595. current_procinfo.generate_parameter_info;
  2596. { start tokenizer }
  2597. gettoken;
  2598. { main loop }
  2599. repeat
  2600. case actasmtoken of
  2601. AS_LLABEL:
  2602. Begin
  2603. if CreateLocalLabel(actasmpattern,hl,true) then
  2604. ConcatLabel(curlist,hl);
  2605. Consume(AS_LLABEL);
  2606. end;
  2607. AS_LABEL:
  2608. Begin
  2609. if SearchLabel(upper(actasmpattern),hl,true) then
  2610. begin
  2611. if hl.is_public then
  2612. ConcatPublic(curlist,actasmpattern_origcase);
  2613. ConcatLabel(curlist,hl);
  2614. end
  2615. else
  2616. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2617. Consume(AS_LABEL);
  2618. end;
  2619. AS_DW :
  2620. Begin
  2621. inexpression:=true;
  2622. Consume(AS_DW);
  2623. BuildConstant(2);
  2624. inexpression:=false;
  2625. end;
  2626. AS_DB :
  2627. Begin
  2628. inexpression:=true;
  2629. Consume(AS_DB);
  2630. BuildConstant(1);
  2631. inexpression:=false;
  2632. end;
  2633. AS_DD :
  2634. Begin
  2635. inexpression:=true;
  2636. Consume(AS_DD);
  2637. BuildConstant(4);
  2638. inexpression:=false;
  2639. end;
  2640. {$ifdef cpu64bitaddr}
  2641. AS_DQ:
  2642. Begin
  2643. inexpression:=true;
  2644. Consume(AS_DQ);
  2645. BuildConstant(8);
  2646. inexpression:=false;
  2647. end;
  2648. {$endif cpu64bitaddr}
  2649. AS_PUBLIC:
  2650. Begin
  2651. Consume(AS_PUBLIC);
  2652. repeat
  2653. if actasmtoken=AS_ID then
  2654. begin
  2655. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  2656. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  2657. else if SearchLabel(upper(actasmpattern),hl,false) then
  2658. begin
  2659. if not hl.is_public then
  2660. begin
  2661. hl.is_public:=true;
  2662. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  2663. if tlabelsym(tmpsym).defined then
  2664. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  2665. end;
  2666. end
  2667. else
  2668. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2669. end;
  2670. Consume(AS_ID);
  2671. if actasmtoken=AS_COMMA then
  2672. begin
  2673. Consume(AS_COMMA);
  2674. if actasmtoken<>AS_ID then
  2675. Consume(AS_ID);
  2676. end;
  2677. until actasmtoken=AS_SEPARATOR;
  2678. end;
  2679. AS_ALIGN:
  2680. Begin
  2681. Consume(AS_ALIGN);
  2682. ConcatAlign(curlist,BuildConstExpression);
  2683. if actasmtoken<>AS_SEPARATOR then
  2684. Consume(AS_SEPARATOR);
  2685. end;
  2686. AS_OPCODE :
  2687. Begin
  2688. instr:=Tx86Instruction.Create(Tx86Operand);
  2689. BuildOpcode(instr);
  2690. with instr do
  2691. begin
  2692. CheckNonCommutativeOpcodes;
  2693. AddReferenceSizes;
  2694. SetInstructionOpsize;
  2695. CheckOperandSizes;
  2696. ConcatInstruction(curlist);
  2697. end;
  2698. instr.Free;
  2699. end;
  2700. AS_SEPARATOR :
  2701. Begin
  2702. Consume(AS_SEPARATOR);
  2703. end;
  2704. AS_TARGET_DIRECTIVE:
  2705. HandleTargetDirective;
  2706. AS_END :
  2707. break; { end assembly block }
  2708. else
  2709. Begin
  2710. Message(asmr_e_syntax_error);
  2711. RecoverConsume(false);
  2712. end;
  2713. end; { end case }
  2714. until false;
  2715. { check that all referenced local labels are defined }
  2716. checklocallabels;
  2717. { Return the list in an asmnode }
  2718. assemble:=curlist;
  2719. Message1(asmr_d_finish_reading,'intel');
  2720. end;
  2721. end.