rax86int.pas 96 KB

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