asmutils.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. {
  2. $Id$
  3. Copyright (c) 1998 Carl Eric Codere
  4. This unit implements some support routines for assembler parsing
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit AsmUtils;
  18. {*************************************************************************}
  19. { This unit implements some objects as well as utilities which will be }
  20. { used by all inline assembler parsers (non-processor specific). }
  21. { }
  22. { Main routines/objects herein: }
  23. { o Object TExprParse is a simple expression parser to resolve assembler }
  24. { expressions. (Based generally on some code by Thai Tran from SWAG). }
  25. { o Object TInstruction is a simple object used for instructions }
  26. { o Record TOperand is a simple record used to store information on }
  27. { each operand. }
  28. { o String conversion routines from octal,binary and hex to decimal. }
  29. { o A linked list object/record for local labels }
  30. { o Routines for retrieving symbols (local and global) }
  31. { o Object for a linked list of strings (with duplicate strings not }
  32. { allowed). }
  33. { o Non-processor dependant routines for adding instructions to the }
  34. { instruction list. }
  35. {*************************************************************************}
  36. {--------------------------------------------------------------------}
  37. { LEFT TO DO: }
  38. { o Fix the remaining bugs in the expression parser, such as with }
  39. { 4+-3 }
  40. { o Add support for local typed constants search. }
  41. { o Add support for private/protected fields in method assembler }
  42. { routines. }
  43. {--------------------------------------------------------------------}
  44. Interface
  45. Uses
  46. globtype,systems,
  47. symtable,aasm,hcodegen,verbose,globals,files,strings,
  48. cobjects,
  49. {$ifdef i386}
  50. i386;
  51. {$endif}
  52. {$ifdef m68k}
  53. m68k;
  54. {$endif}
  55. Const
  56. RPNMax = 10; { I think you only need 4, but just to be safe }
  57. OpMax = 25;
  58. maxoperands = 3; { Maximum operands for assembler instructions }
  59. Type
  60. {---------------------------------------------------------------------}
  61. { Label Management types }
  62. {---------------------------------------------------------------------}
  63. PAsmLabel = ^TAsmLabel;
  64. { Each local label has this structure associated with it }
  65. TAsmLabel = record
  66. name: PString; { pointer to a pascal string name of label }
  67. lab: PLabel; { pointer to a label as defined in FPC }
  68. emitted: boolean; { as the label itself been emitted ? }
  69. next: PAsmLabel; { next node }
  70. end;
  71. TAsmLabelList = Object
  72. public
  73. First: PAsmLabel;
  74. Constructor Init;
  75. Destructor Done;
  76. Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  77. Function Search(const s: string): PAsmLabel;
  78. private
  79. Last: PAsmLabel;
  80. Function NewPasStr(s:string): PString;
  81. end;
  82. {---------------------------------------------------------------------}
  83. { Instruction management types }
  84. {---------------------------------------------------------------------}
  85. toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  86. OPR_REGLIST,OPR_SYMBOL);
  87. { When the TReference field isintvalue = TRUE }
  88. { then offset points to an ABSOLUTE address }
  89. { otherwise isintvalue should always be false }
  90. { Special cases: }
  91. { For the M68k Target, size is UNUSED, the }
  92. { opcode determines the size of the }
  93. { instruction. }
  94. { DIVS/DIVU/MULS/MULU of the form dn,dn:dn }
  95. { is stored as three operands!! }
  96. { Each instruction operand can be of this type }
  97. TOperand = record
  98. size: topsize;
  99. opinfo: longint; { ao_xxxx flags }
  100. overriden : boolean; { indicates if the opcode has been overriden }
  101. { by a pseudo-opcode such as DWORD PTR }
  102. case operandtype:toperandtype of
  103. { the size of the opr_none field should be at least equal to each }
  104. { other field as to facilitate initialization. }
  105. OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  106. OPR_REFERENCE: (ref:treference);
  107. OPR_CONSTANT: (val: longint);
  108. OPR_REGISTER: (reg:tregister);
  109. OPR_LABINSTR: (hl: plabel);
  110. { Register list such as in the movem instruction }
  111. OPR_REGLIST: (list: set of tregister);
  112. OPR_SYMBOL : (symbol:pstring);
  113. end;
  114. TInstruction = object
  115. public
  116. operands: array[1..maxoperands] of TOperand;
  117. { if numops = zero, a size may still be valid in operands[1] }
  118. { it still should be checked. }
  119. numops: byte;
  120. { set to TRUE if the instruction is labeled. }
  121. labeled: boolean;
  122. { This is used for instructions such A_CMPSB... etc, to determine }
  123. { the size of the instruction. }
  124. stropsize: topsize;
  125. procedure init;
  126. procedure done;
  127. { sets up the prefix field with the instruction pointed to in s }
  128. procedure addprefix(tok: tasmop);
  129. { sets up the instruction with the instruction pointed to in s }
  130. procedure addinstr(tok: tasmop);
  131. { get the current instruction of this object }
  132. function getinstruction: tasmop;
  133. { get the current prefix of this instruction }
  134. function getprefix: tasmop;
  135. private
  136. prefix: tasmop;
  137. instruction: tasmop;
  138. end;
  139. {---------------------------------------------------------------------}
  140. { Expression parser types }
  141. {---------------------------------------------------------------------}
  142. { expression parser error codes }
  143. texpr_error =
  144. (zero_divide, { divide by zero. }
  145. stack_overflow, { stack overflow. }
  146. stack_underflow, { stack underflow. }
  147. invalid_number, { invalid conversion }
  148. invalid_op); { invalid operator }
  149. TExprOperator = record
  150. ch: char; { operator }
  151. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  152. end;
  153. String15 = String[15];
  154. {**********************************************************************}
  155. { The following operators are supported: }
  156. { '+' : addition }
  157. { '-' : subtraction }
  158. { '*' : multiplication }
  159. { '/' : modulo division }
  160. { '^' : exclusive or }
  161. { '<' : shift left }
  162. { '>' : shift right }
  163. { '&' : bitwise and }
  164. { '|' : bitwise or }
  165. { '~' : bitwise complement }
  166. { '%' : modulo division }
  167. { nnn: longint numbers }
  168. { ( and ) parenthesis }
  169. {**********************************************************************}
  170. TExprParse = Object
  171. public
  172. Constructor Init;
  173. Destructor Done;
  174. Function Evaluate(Expr: String): longint;
  175. Procedure Error(anerror: texpr_error); virtual;
  176. Function Priority(_Operator: Char): Integer; virtual;
  177. private
  178. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  179. RPNTop : Integer;
  180. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  181. OpTop : Integer;
  182. Procedure RPNPush(Num: Longint);
  183. Function RPNPop: Longint;
  184. Procedure RPNCalc(token: String15; prefix: boolean);
  185. Procedure OpPush(_Operator: char; prefix: boolean);
  186. { In reality returns TExprOperaotr }
  187. Procedure OpPop(var _Operator:TExprOperator);
  188. end;
  189. { Evaluate an expression string to a longint }
  190. Function CalculateExpression(const expression: string): longint;
  191. {---------------------------------------------------------------------}
  192. { String routines }
  193. {---------------------------------------------------------------------}
  194. {*********************************************************************}
  195. { PROCEDURE PadZero; }
  196. { Description: Makes sure that the string specified is of the given }
  197. { length, by padding it with binary zeros, or truncating if necessary}
  198. { Remark: The return value is determined BEFORE any eventual padding.}
  199. { Return Value: TRUE = if length of string s was <= then n }
  200. { FALSE = if length of string s was > then n }
  201. {*********************************************************************}
  202. Function PadZero(Var s: String; n: byte): Boolean;
  203. { Converts an Hex digit string to a Decimal string }
  204. { Returns '' if there was an error. }
  205. Function HexToDec(const S:String): String;
  206. { Converts a binary digit string to a Decimal string }
  207. { Returns '' if there was an error. }
  208. Function BinaryToDec(const S:String): String;
  209. { Converts an octal digit string to a Decimal string }
  210. { Returns '' if there was an error. }
  211. Function OctalToDec(const S:String): String;
  212. { Converts a string containing C styled escape sequences to }
  213. { a pascal style string. }
  214. Function EscapeToPascal(const s:string): string;
  215. Procedure ConcatPasString(p : paasmoutput;s:string);
  216. { Writes the string s directly to the assembler output }
  217. Procedure ConcatDirect(p : paasmoutput;s:string);
  218. {---------------------------------------------------------------------}
  219. { Symbol helper routines }
  220. {---------------------------------------------------------------------}
  221. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  222. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  223. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  224. Function SearchIConstant(const s:string; var l:longint): boolean;
  225. Function SearchLabel(const s: string; var hl: plabel): boolean;
  226. Function CreateVarInstr(var Instr: TInstruction; const hs:string;
  227. operandnum:byte):boolean;
  228. {*********************************************************************}
  229. { FUNCTION NewPasStr(s:string): PString }
  230. { Description: This routine allocates a string on the heap and }
  231. { returns a pointer to the allocated string. }
  232. { }
  233. { Remarks: The string allocated should not be modified, since it's }
  234. { length will be less then 255. }
  235. { Remarks: It is assumed that HeapError will be called if an }
  236. { allocation fails. }
  237. {*********************************************************************}
  238. Function newpasstr(s: string): Pointer;
  239. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  240. {$ifdef i386}
  241. Procedure FWaitWarning;
  242. {$endif}
  243. {---------------------------------------------------------------------}
  244. { Instruction generation routines }
  245. {---------------------------------------------------------------------}
  246. { swaps in the case of a 2/3 operand opcode the destination and the }
  247. { source as to put it in AT&T style instruction format. }
  248. Procedure SwapOperands(Var instr: TInstruction);
  249. Procedure ConcatLabel(p : paasmoutput;op : tasmop;var l : plabel);
  250. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  251. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  252. Procedure ConcatString(p : paasmoutput;s:string);
  253. Procedure ConcatPublic(p:paasmoutput;const s : string);
  254. Procedure ConcatLocal(p:paasmoutput;const s : string);
  255. Procedure ConcatGlobalBss(const s : string;size : longint);
  256. Procedure ConcatLocalBss(const s : string;size : longint);
  257. { add to list of external labels }
  258. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  259. { add to internal list of labels }
  260. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  261. Implementation
  262. {*************************************************************************}
  263. { Expression Parser }
  264. {*************************************************************************}
  265. Constructor TExprParse.Init;
  266. Begin
  267. end;
  268. Procedure TExprParse.Error(anerror:texpr_error);
  269. var
  270. t : tmsgconst;
  271. Begin
  272. case anerror of
  273. zero_divide: t:=assem_f_ev_zero_divide;
  274. stack_overflow: t:=assem_f_ev_stack_overflow;
  275. stack_underflow: t:=assem_f_ev_stack_underflow;
  276. invalid_number: t:=assem_f_ev_invalid_number;
  277. invalid_op: t:=assem_f_ev_invalid_op;
  278. else
  279. t:=assem_f_ev_unknown;
  280. end;
  281. Message(t);
  282. end;
  283. Procedure TExprParse.RPNPush(Num : longint); { Add an operand to the top of the RPN stack }
  284. begin
  285. if RPNTop < RPNMax then
  286. begin
  287. Inc(RPNTop);
  288. RPNStack[RPNTop] := Num;
  289. end
  290. else
  291. Error(stack_overflow); { Put some error handler here }
  292. end;
  293. Function TExprParse.RPNPop : longint; { Get the operand at the top of the RPN stack }
  294. begin
  295. if RPNTop > 0 then
  296. begin
  297. RPNPop := RPNStack[RPNTop];
  298. Dec(RPNTop);
  299. end
  300. else { Put some error handler here }
  301. Error(stack_underflow);
  302. end;
  303. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  304. Var
  305. Temp : longint;
  306. LocalError : Integer;
  307. begin
  308. { Write(Token, ' '); This just outputs the RPN expression }
  309. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  310. Case Token[1] of { Handle operators }
  311. '+' : Begin
  312. if prefix then
  313. else
  314. RPNPush(RPNPop + RPNPop);
  315. end;
  316. '-' : Begin
  317. if prefix then
  318. RPNPush(-(RPNPop))
  319. else
  320. RPNPush(RPNPop - RPNPop);
  321. end;
  322. '*' : RPNPush(RPNPop * RPNPop);
  323. '&' : RPNPush(RPNPop AND RPNPop);
  324. '|' : RPNPush(RPNPop OR RPNPop);
  325. '~' : RPNPush(NOT RPNPop);
  326. '<' : RPNPush(RPNPop SHL RPNPop);
  327. '>' : RPNPush(RPNPop SHR RPNPop);
  328. '%' : begin
  329. Temp := RPNPop;
  330. if Temp <> 0 then
  331. RPNPush(RPNPop mod Temp)
  332. else Error(zero_divide); { Handle divide by zero error }
  333. end;
  334. '^' : RPNPush(RPNPop XOR RPNPop);
  335. '/' :
  336. begin
  337. Temp := RPNPop;
  338. if Temp <> 0 then
  339. RPNPush(RPNPop div Temp)
  340. else Error(zero_divide);{ Handle divide by 0 error }
  341. end;
  342. end
  343. else
  344. begin { Convert String to number and add to stack }
  345. if token='-2147483648' then
  346. begin
  347. temp:=$80000000;
  348. localerror:=0;
  349. end
  350. else
  351. Val(Token, Temp, LocalError);
  352. if LocalError = 0 then
  353. RPNPush(Temp)
  354. else Error(invalid_number);{ Handle error }
  355. end;
  356. end;
  357. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean); { Add an operator onto top of the stack }
  358. begin
  359. if OpTop < OpMax then
  360. begin
  361. Inc(OpTop);
  362. OpStack[OpTop].ch := _Operator;
  363. OpStack[OpTop].is_prefix := prefix;
  364. end
  365. else Error(stack_overflow); { Put some error handler here }
  366. end;
  367. Procedure TExprParse.OpPop(var _Operator:TExprOperator); { Get operator at the top of the stack }
  368. begin
  369. if OpTop > 0 then
  370. begin
  371. _Operator := OpStack[OpTop];
  372. Dec(OpTop);
  373. end
  374. else Error(stack_underflow); { Put some error handler here }
  375. end;
  376. Function TExprParse.Priority(_Operator : Char) : Integer; { Return priority of operator }
  377. { The greater the priority, the higher the precedence }
  378. begin
  379. Case _Operator OF
  380. '(' : Priority := 0;
  381. '+', '-' : Priority := 1;
  382. '*', '/','%','<','>' : Priority := 2;
  383. '|','&','^','~': Priority := 0;
  384. else Error(invalid_op);{ More error handling }
  385. end;
  386. end;
  387. Function TExprParse.Evaluate(Expr : String):longint;
  388. Var
  389. I : Integer;
  390. Token : String15;
  391. opr: TExprOperator;
  392. begin
  393. OpTop := 0; { Reset stacks }
  394. RPNTop := 0;
  395. Token := '';
  396. For I := 1 to Length(Expr) DO
  397. begin
  398. if Expr[I] in ['0'..'9'] then
  399. begin { Build multi-digit numbers }
  400. Token := Token + Expr[I];
  401. if I = Length(Expr) then { Send last one to calculator }
  402. RPNCalc(Token,false);
  403. end
  404. else
  405. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  406. begin
  407. if Token <> '' then
  408. begin { Send last built number to calc. }
  409. RPNCalc(Token,false);
  410. Token := '';
  411. end;
  412. Case Expr[I] OF
  413. '(' : OpPush('(',false);
  414. ')' : begin
  415. While OpStack[OpTop].ch <> '(' DO
  416. Begin
  417. OpPop(opr);
  418. RPNCalc(opr.ch,opr.is_prefix);
  419. end;
  420. OpPop(opr); { Pop off and ignore the '(' }
  421. end;
  422. '+','-','~' : Begin
  423. { workaround for -2147483648 }
  424. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  425. begin
  426. token:='-';
  427. expr[i]:='+';
  428. end;
  429. { if start of expression then surely a prefix }
  430. { or if previous char was also an operator }
  431. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  432. OpPush(Expr[I],true)
  433. else
  434. Begin
  435. { Evaluate all higher priority operators }
  436. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  437. Begin
  438. OpPop(opr);
  439. RPNCalc(opr.ch,opr.is_prefix);
  440. end;
  441. OpPush(Expr[I],false);
  442. End;
  443. end;
  444. '*', '/',
  445. '^','|','&',
  446. '%','<','>' : begin
  447. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  448. Begin
  449. OpPop(opr);
  450. RPNCalc(opr.ch,opr.is_prefix);
  451. end;
  452. OpPush(Expr[I],false);
  453. end;
  454. end; { Case }
  455. end
  456. else
  457. Error(invalid_op); { Handle bad input error }
  458. end;
  459. { Pop off the remaining operators }
  460. While OpTop > 0 do
  461. Begin
  462. OpPop(opr);
  463. RPNCalc(opr.ch,opr.is_prefix);
  464. end;
  465. { The result is stored on the top of the stack }
  466. Evaluate := RPNPop;
  467. end;
  468. Destructor TExprParse.Done;
  469. Begin
  470. end;
  471. Function CalculateExpression(const expression: string): longint;
  472. var
  473. expr: TExprParse;
  474. Begin
  475. expr.Init;
  476. CalculateExpression := expr.Evaluate(expression);
  477. expr.Done;
  478. end;
  479. {*************************************************************************}
  480. { String conversions/utils }
  481. {*************************************************************************}
  482. Function newpasstr(s: string): Pointer;
  483. Var
  484. StrPtr: PString;
  485. Begin
  486. GetMem(StrPtr, length(s)+1);
  487. Move(s,StrPtr^,length(s)+1);
  488. newpasstr:= Strptr;
  489. end;
  490. Function EscapeToPascal(const s:string): string;
  491. { converts a C styled string - which contains escape }
  492. { characters to a pascal style string. }
  493. var
  494. i,j: word;
  495. str: string;
  496. temp: string;
  497. value: byte;
  498. code: integer;
  499. Begin
  500. str:='';
  501. i:=1;
  502. j:=1;
  503. repeat
  504. if s[i] = '\' then
  505. Begin
  506. Inc(i);
  507. if i > 255 then
  508. Begin
  509. EscapeToPascal:=str;
  510. exit;
  511. end;
  512. case s[i] of
  513. '\': insert('\',str,j);
  514. 'b': insert(#08,str,j);
  515. 'f': insert(#12,str,j);
  516. 'n': insert(#10,str,j);
  517. 'r': insert(#13,str,j);
  518. 't': insert(#09,str,j);
  519. '"': insert('"',str,j);
  520. { octal number }
  521. '0'..'7': Begin
  522. temp:=s[i];
  523. temp:=temp+s[i+1];
  524. temp:=temp+s[i+2];
  525. inc(i,2);
  526. val(octaltodec(temp),value,code);
  527. if (code <> 0) then
  528. Message(assem_w_invalid_numeric);
  529. insert(chr(value),str,j);
  530. end;
  531. { hexadecimal number }
  532. 'x': Begin
  533. temp:=s[i+1];
  534. temp:=temp+s[i+2];
  535. inc(i,2);
  536. val(hextodec(temp),value,code);
  537. if (code <> 0) then
  538. Message(assem_w_invalid_numeric);
  539. insert(chr(value),str,j);
  540. end;
  541. else
  542. Begin
  543. Message1(assem_e_escape_seq_ignored,s[i]);
  544. insert(s[i],str,j);
  545. end;
  546. end; {end case }
  547. Inc(i);
  548. end
  549. else
  550. Begin
  551. Insert(s[i],str,j);
  552. Inc(i);
  553. if i > 255 then
  554. Begin
  555. EscapeToPascal:=str;
  556. exit;
  557. end;
  558. end;
  559. Inc(j);
  560. until (i > length(s)) or (j > 255);
  561. EscapeToPascal:=str;
  562. end;
  563. Function OctalToDec(const S:String): String;
  564. { Converts an octal string to a Decimal string }
  565. { Returns '' if there was an error. }
  566. var vs: longint;
  567. c: byte;
  568. st: string;
  569. Begin
  570. vs := 0;
  571. for c:=1 to length(s) do
  572. begin
  573. case s[c] of
  574. '0': vs:=vs shl 3;
  575. '1': vs:=vs shl 3+1;
  576. '2': vs:=vs shl 3+2;
  577. '3': vs:=vs shl 3+3;
  578. '4': vs:=vs shl 3+4;
  579. '5': vs:=vs shl 3+5;
  580. '6': vs:=vs shl 3+6;
  581. '7': vs:=vs shl 3+7;
  582. else
  583. begin
  584. Message(assem_f_error_converting_octal);
  585. OctalToDec := '';
  586. exit;
  587. end;
  588. end;
  589. end;
  590. str(vs,st);
  591. OctalToDec := st;
  592. end;
  593. Function BinaryToDec(const S:String): String;
  594. { Converts a binary string to a Decimal string }
  595. { Returns '' if there was an error. }
  596. var vs: longint;
  597. c: byte;
  598. st: string;
  599. Begin
  600. vs := 0;
  601. for c:=1 to length(s) do
  602. begin
  603. if s[c] = '0' then
  604. vs:=vs shl 1
  605. else
  606. if s[c]='1' then
  607. vs:=vs shl 1+1
  608. else
  609. begin
  610. Message(assem_f_error_converting_bin);
  611. BinaryToDec := '';
  612. exit;
  613. end;
  614. end;
  615. str(vs,st);
  616. BinaryToDec := st;
  617. end;
  618. Function HexToDec(const S:String): String;
  619. var vs: longint;
  620. c: byte;
  621. st: string;
  622. Begin
  623. vs := 0;
  624. for c:=1 to length(s) do
  625. begin
  626. case upcase(s[c]) of
  627. '0': vs:=vs shl 4;
  628. '1': vs:=vs shl 4+1;
  629. '2': vs:=vs shl 4+2;
  630. '3': vs:=vs shl 4+3;
  631. '4': vs:=vs shl 4+4;
  632. '5': vs:=vs shl 4+5;
  633. '6': vs:=vs shl 4+6;
  634. '7': vs:=vs shl 4+7;
  635. '8': vs:=vs shl 4+8;
  636. '9': vs:=vs shl 4+9;
  637. 'A': vs:=vs shl 4+10;
  638. 'B': vs:=vs shl 4+11;
  639. 'C': vs:=vs shl 4+12;
  640. 'D': vs:=vs shl 4+13;
  641. 'E': vs:=vs shl 4+14;
  642. 'F': vs:=vs shl 4+15;
  643. else
  644. begin
  645. Message(assem_f_error_converting_hex);
  646. HexToDec := '';
  647. exit;
  648. end;
  649. end;
  650. end;
  651. str(vs,st);
  652. HexToDec := st;
  653. end;
  654. Function PadZero(Var s: String; n: byte): Boolean;
  655. Begin
  656. PadZero := TRUE;
  657. { Do some error checking first }
  658. if Length(s) = n then
  659. exit
  660. else
  661. if Length(s) > n then
  662. Begin
  663. PadZero := FALSE;
  664. delete(s,n+1,length(s));
  665. exit;
  666. end
  667. else
  668. PadZero := TRUE;
  669. { Fill it up with the specified character }
  670. fillchar(s[length(s)+1],n-1,#0);
  671. {$ifndef TP}
  672. {$ifopt H+}
  673. setlength(s,n);
  674. {$else}
  675. s[0] := chr(n);
  676. {$endif}
  677. {$else}
  678. s[0] := chr(n);
  679. {$endif}
  680. end;
  681. {*************************************************************************}
  682. { Instruction utilities }
  683. {*************************************************************************}
  684. Procedure TInstruction.init;
  685. var
  686. k: integer;
  687. Begin
  688. numops := 0;
  689. labeled := FALSE;
  690. stropsize := S_NO;
  691. prefix := A_NONE;
  692. instruction := A_NONE;
  693. for k:=1 to maxoperands do
  694. begin
  695. operands[k].size := S_NO;
  696. operands[k].overriden := FALSE;
  697. operands[k].operandtype := OPR_NONE;
  698. { init to zeros }
  699. fillchar(operands[k].l, sizeof(operands[k].l),#0);
  700. end;
  701. end;
  702. Procedure TInstruction.addprefix(tok: tasmop);
  703. Begin
  704. if tok = A_NONE then
  705. Message(assem_e_syn_prefix_not_found);
  706. if Prefix = A_NONE then
  707. Prefix := tok
  708. else
  709. Message(assem_e_syn_try_add_more_prefix);
  710. end;
  711. Procedure TInstruction.addinstr(tok: tasmop);
  712. Begin
  713. if tok = A_NONE then
  714. Message(assem_e_syn_opcode_not_found);
  715. Instruction := tok;
  716. end;
  717. function TInstruction.getinstruction: tasmop;
  718. Begin
  719. getinstruction := Instruction;
  720. end;
  721. { get the current prefix of this instruction }
  722. function TInstruction.getprefix: tasmop;
  723. Begin
  724. getprefix := prefix;
  725. end;
  726. Procedure TInstruction.done;
  727. var
  728. k: integer;
  729. Begin
  730. for k:=1 to numops do
  731. begin
  732. if (operands[k].operandtype=OPR_REFERENCE) and
  733. assigned(operands[k].ref.symbol) then
  734. stringdispose(operands[k].ref.symbol);
  735. if (operands[k].operandtype=OPR_SYMBOL) and
  736. assigned(operands[k].symbol) then
  737. stringdispose(operands[k].symbol);
  738. end;
  739. end;
  740. {*************************************************************************}
  741. { Local label utilities }
  742. {*************************************************************************}
  743. Constructor TAsmLabelList.Init;
  744. Begin
  745. First := nil;
  746. Last := nil;
  747. end;
  748. Procedure TAsmLabelList.Insert(s:string; lab: PLabel; emitted: boolean);
  749. {*********************************************************************}
  750. { Description: Insert a node at the end of the list with lab and }
  751. { and the name in s. The name is allocated on the heap. }
  752. { Duplicates are not allowed. }
  753. { Indicate in emitted if this label itself has been emitted, or is it}
  754. { a simple labeled instruction? }
  755. {*********************************************************************}
  756. Begin
  757. if search(s) = nil then
  758. Begin
  759. if First = nil then
  760. Begin
  761. New(First);
  762. Last := First;
  763. end
  764. else
  765. Begin
  766. New(Last^.Next);
  767. Last := Last^.Next;
  768. end;
  769. Last^.name := NewPasStr(s);
  770. Last^.Lab := lab;
  771. Last^.Next := nil;
  772. Last^.emitted := emitted;
  773. end;
  774. end;
  775. Function TAsmLabelList.Search(const s: string): PAsmLabel;
  776. {*********************************************************************}
  777. { Description: This routine searches for a label named s in the }
  778. { linked list, returns a pointer to the label if found, otherwise }
  779. { returns nil. }
  780. {*********************************************************************}
  781. Var
  782. asmlab: PAsmLabel;
  783. Begin
  784. asmlab := First;
  785. if First = nil then
  786. Begin
  787. Search := nil;
  788. exit;
  789. end;
  790. While (asmlab^.name^ <> s) and (asmlab^.Next <> nil) do
  791. asmlab := asmlab^.Next;
  792. if asmlab^.name^ = s then
  793. search := asmlab
  794. else
  795. search := nil;
  796. end;
  797. Destructor TAsmLabelList.Done;
  798. {*********************************************************************}
  799. { Description: This routine takes care of deallocating all nodes }
  800. { in the linked list, as well as deallocating the string pointers }
  801. { of these nodes. }
  802. { }
  803. { Remark: The PLabel field is NOT freed, the compiler takes care of }
  804. { this. }
  805. {*********************************************************************}
  806. Var
  807. temp: PAsmLabel;
  808. temp1: PAsmLabel;
  809. Begin
  810. temp := First;
  811. while temp <> nil do
  812. Begin
  813. Freemem(Temp^.name, length(Temp^.name^)+1);
  814. Temp1 := Temp^.Next;
  815. Dispose(Temp);
  816. Temp := Temp1;
  817. { The plabel could be deleted here, but let us not do }
  818. { it, FPC will do it instead. }
  819. end;
  820. end;
  821. Function TAsmLabelList.newpasstr(s: string): PString;
  822. {*********************************************************************}
  823. { FUNCTION NewPasStr(s:string): PString }
  824. { Description: This routine allocates a string on the heap and }
  825. { returns a pointer to the allocated string. }
  826. { }
  827. { Remarks: The string allocated should not be modified, since it's }
  828. { length will be less then 255. }
  829. { Remarks: It is assumed that HeapError will be called if an }
  830. { allocation fails. }
  831. {*********************************************************************}
  832. Var
  833. StrPtr: PString;
  834. Begin
  835. GetMem(StrPtr, length(s)+1);
  836. Move(s,StrPtr^,length(s)+1);
  837. newpasstr:= Strptr;
  838. end;
  839. {*************************************************************************}
  840. { Symbol table helper routines }
  841. {*************************************************************************}
  842. Procedure SwapOperands(Var instr: TInstruction);
  843. Var
  844. tempopr: TOperand;
  845. Begin
  846. if instr.numops = 2 then
  847. Begin
  848. tempopr := instr.operands[1];
  849. instr.operands[1] := instr.operands[2];
  850. instr.operands[2] := tempopr;
  851. end
  852. else
  853. if instr.numops = 3 then
  854. Begin
  855. tempopr := instr.operands[1];
  856. instr.operands[1] := instr.operands[3];
  857. instr.operands[3] := tempopr;
  858. end;
  859. end;
  860. Function SearchIConstant(const s:string; var l:longint): boolean;
  861. {**********************************************************************}
  862. { Description: Searches for a CONSTANT of name s in either the local }
  863. { symbol list, then in the global symbol list, and returns the value }
  864. { of that constant in l. Returns TRUE if successfull, if not found, }
  865. { or if the constant is not of correct type, then returns FALSE }
  866. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  867. { respectively. }
  868. {**********************************************************************}
  869. var
  870. sym: psym;
  871. Begin
  872. SearchIConstant := FALSE;
  873. { check for TRUE or FALSE reserved words first }
  874. if s = 'TRUE' then
  875. Begin
  876. SearchIConstant := TRUE;
  877. l := 1;
  878. end
  879. else
  880. if s = 'FALSE' then
  881. Begin
  882. SearchIConstant := TRUE;
  883. l := 0;
  884. end
  885. else
  886. if assigned(aktprocsym) then
  887. Begin
  888. if assigned(aktprocsym^.definition) then
  889. Begin
  890. { Check the local constants }
  891. if assigned(aktprocsym^.definition^.localst) then
  892. sym := aktprocsym^.definition^.localst^.search(s)
  893. else
  894. sym := nil;
  895. if assigned(sym) then
  896. Begin
  897. if (sym^.typ = constsym) and
  898. (pconstsym(sym)^.consttype in [constord,constint,constchar,constbool]) then
  899. Begin
  900. l:=pconstsym(sym)^.value;
  901. SearchIConstant := TRUE;
  902. exit;
  903. end;
  904. end;
  905. end;
  906. end;
  907. { Check the global constants }
  908. getsym(s,false);
  909. if srsym <> nil then
  910. Begin
  911. case srsym^.typ of
  912. constsym :
  913. begin
  914. if (pconstsym(srsym)^.consttype in [constord,constint,constchar,constbool]) then
  915. Begin
  916. l:=pconstsym(srsym)^.value;
  917. SearchIConstant := TRUE;
  918. exit;
  919. end;
  920. end;
  921. end;
  922. end;
  923. end;
  924. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  925. {**********************************************************************}
  926. { Description: This routine changes the correct fields and correct }
  927. { offset in the reference, so that it points to the __RESULT or }
  928. { @Result variable (depending on the inline asm). }
  929. { Resturns a reference with all correct offset correctly set up. }
  930. { The Operand should already point to a treference on entry. }
  931. {**********************************************************************}
  932. Begin
  933. { replace by correct offset. }
  934. if assigned(procinfo.retdef) and
  935. (procinfo.retdef<>pdef(voiddef)) then
  936. begin
  937. instr.operands[operandnum].ref.offset := procinfo.retoffset;
  938. instr.operands[operandnum].ref.base := procinfo.framepointer;
  939. { always assume that the result is valid. }
  940. procinfo.funcret_is_valid:=true;
  941. end
  942. else
  943. Message(assem_e_invalid_symbol_ref);
  944. end;
  945. {$ifdef i386}
  946. Procedure FWaitWarning;
  947. begin
  948. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  949. Message(assem_w_fwait_emu_prob);
  950. end;
  951. {$endif i386}
  952. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  953. begin
  954. { the current size is NOT overriden if it already }
  955. { exists, such as in the case of a byte ptr, in }
  956. { front of the identifier. }
  957. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  958. Begin
  959. case size of
  960. 1: instr.operands[operandnum].size := S_B;
  961. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  962. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  963. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  964. extended_size: instr.operands[operandnum].size := S_FX;
  965. else
  966. { this is in the case where the instruction is LEA }
  967. { or something like that, in that case size is not }
  968. { important. }
  969. instr.operands[operandnum].size := S_NO;
  970. end; { end case }
  971. end;
  972. end;
  973. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  974. { search and returns the offset and size of records/objects of the base }
  975. { with field name setup in field. }
  976. { returns FALSE if not found. }
  977. { used when base is a variable or a typed constant name. }
  978. var
  979. sym:psym;
  980. p: psym;
  981. Begin
  982. GetVarOffsetSize := FALSE;
  983. Offset := 0;
  984. { local list }
  985. if assigned(aktprocsym) then
  986. begin
  987. if assigned(aktprocsym^.definition^.localst) then
  988. sym:=aktprocsym^.definition^.localst^.search(base)
  989. else
  990. sym:=nil;
  991. if assigned(sym) then
  992. begin
  993. { field of local record variable. }
  994. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  995. begin
  996. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  997. if assigned(pvarsym(p)) then
  998. Begin
  999. Offset := pvarsym(p)^.address;
  1000. Size:=PVarsym(p)^.getsize;
  1001. GetVarOffsetSize := TRUE;
  1002. Exit;
  1003. end;
  1004. end
  1005. else
  1006. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1007. begin
  1008. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1009. begin
  1010. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1011. if assigned(pvarsym(p)) then
  1012. Begin
  1013. Offset := pvarsym(p)^.address;
  1014. Size:=PVarsym(p)^.getsize;
  1015. GetVarOffsetSize := TRUE;
  1016. Exit;
  1017. end;
  1018. end;
  1019. end;
  1020. end
  1021. else
  1022. begin
  1023. { field of local record parameter to routine. }
  1024. if assigned(aktprocsym^.definition^.parast) then
  1025. sym:=aktprocsym^.definition^.parast^.search(base)
  1026. else
  1027. sym:=nil;
  1028. if assigned(sym) then
  1029. begin
  1030. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef)
  1031. then
  1032. begin
  1033. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1034. if assigned(p) then
  1035. Begin
  1036. Offset := pvarsym(p)^.address;
  1037. Size:=PVarsym(p)^.getsize;
  1038. GetVarOffsetSize := TRUE;
  1039. Exit;
  1040. end;
  1041. end { endif }
  1042. else
  1043. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1044. begin
  1045. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1046. begin
  1047. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1048. if assigned(pvarsym(p)) then
  1049. Begin
  1050. Offset := pvarsym(p)^.address;
  1051. Size:=PVarsym(p)^.getsize;
  1052. GetVarOffsetSize := TRUE;
  1053. Exit;
  1054. end;
  1055. end;
  1056. end;
  1057. end;
  1058. end;
  1059. end; { endif assigned(aktprocsym) }
  1060. { not found.. .now look for global variables. }
  1061. getsym(base,false);
  1062. sym:=srsym;
  1063. if assigned(sym) then
  1064. Begin
  1065. { field of global record variable. }
  1066. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  1067. begin
  1068. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1069. if assigned(p) then
  1070. Begin
  1071. Offset := pvarsym(p)^.address;
  1072. Size:=PVarsym(p)^.getsize;
  1073. GetVarOffsetSize := TRUE;
  1074. Exit;
  1075. end;
  1076. end
  1077. else
  1078. { field of global record type constant. }
  1079. if (sym^.typ=typedconstsym) and (ptypedconstsym(sym)^.definition^.deftype=recorddef)
  1080. then
  1081. begin
  1082. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1083. if assigned(p) then
  1084. Begin
  1085. Offset := pvarsym(p)^.address;
  1086. Size:=PVarsym(p)^.getsize;
  1087. GetVarOffsetSize := TRUE;
  1088. Exit;
  1089. end;
  1090. end
  1091. else
  1092. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1093. begin
  1094. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1095. begin
  1096. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1097. if assigned(pvarsym(p)) then
  1098. Begin
  1099. Offset := pvarsym(p)^.address;
  1100. Size:=PVarsym(p)^.getsize;
  1101. GetVarOffsetSize := TRUE;
  1102. Exit;
  1103. end;
  1104. end;
  1105. end;
  1106. end; { end looking for global variables .. }
  1107. end;
  1108. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  1109. { search and returns the offset of records/objects of the base }
  1110. { with field name setup in field. }
  1111. { returns 0 if not found. }
  1112. { used when base is a variable or a typed constant name. }
  1113. var
  1114. sym:psym;
  1115. p: psym;
  1116. Begin
  1117. Offset := 0;
  1118. GetTypeOffsetSize := FALSE;
  1119. { local list }
  1120. if assigned(aktprocsym) then
  1121. begin
  1122. if assigned(aktprocsym^.definition^.localst) then
  1123. sym:=aktprocsym^.definition^.localst^.search(base)
  1124. else
  1125. sym:=nil;
  1126. if assigned(sym) then
  1127. begin
  1128. { field of local record type. }
  1129. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1130. begin
  1131. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1132. if assigned(p) then
  1133. Begin
  1134. Offset := pvarsym(p)^.address;
  1135. Size:=PVarsym(p)^.getsize;
  1136. GetTypeOffsetSize := TRUE;
  1137. Exit;
  1138. end;
  1139. end;
  1140. end
  1141. else
  1142. begin
  1143. { field of local record type to routine. }
  1144. if assigned(aktprocsym^.definition^.parast) then
  1145. sym:=aktprocsym^.definition^.parast^.search(base)
  1146. else
  1147. sym:=nil;
  1148. if assigned(sym) then
  1149. begin
  1150. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef)
  1151. then
  1152. begin
  1153. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1154. if assigned(p) then
  1155. Begin
  1156. Offset := pvarsym(p)^.address;
  1157. GetTypeOffsetSize := TRUE;
  1158. Exit;
  1159. end;
  1160. end; { endif }
  1161. end; {endif }
  1162. end; { endif }
  1163. end;
  1164. { not found.. .now look for global types. }
  1165. getsym(base,false);
  1166. sym:=srsym;
  1167. if assigned(sym) then
  1168. Begin
  1169. { field of global record types. }
  1170. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1171. begin
  1172. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1173. if assigned(p) then
  1174. Begin
  1175. Offset := pvarsym(p)^.address;
  1176. Size:=PVarsym(p)^.getsize;
  1177. GetTypeOffsetSize := TRUE;
  1178. Exit;
  1179. end
  1180. end
  1181. else
  1182. { public field names of objects }
  1183. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=objectdef)then
  1184. begin
  1185. if assigned(pobjectdef(ptypesym(sym)^.definition)^.publicsyms) then
  1186. Begin
  1187. p:=pobjectdef(ptypesym(sym)^.definition)^.publicsyms^.search(field);
  1188. if assigned(p) then
  1189. Begin
  1190. Offset := pvarsym(p)^.address;
  1191. Size:=PVarsym(p)^.getsize;
  1192. GetTypeOffsetSize := TRUE;
  1193. Exit;
  1194. end
  1195. end;
  1196. end;
  1197. end; { end looking for global variables .. }
  1198. end;
  1199. Function CreateVarInstr(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  1200. { search and sets up the correct fields in the Instr record }
  1201. { for the NON-constant identifier passed to the routine. }
  1202. { if not found returns FALSE. }
  1203. var
  1204. sym : psym;
  1205. l : longint;
  1206. Begin
  1207. CreateVarInstr := FALSE;
  1208. { are we in a routine ? }
  1209. if assigned(aktprocsym) then
  1210. begin
  1211. { search the local list for the name of this variable. }
  1212. if assigned(aktprocsym^.definition^.localst) then
  1213. sym:=aktprocsym^.definition^.localst^.search(hs)
  1214. else
  1215. sym:=nil;
  1216. if assigned(sym) then
  1217. begin
  1218. case sym^.typ of
  1219. varsym : begin
  1220. { we always assume in asm statements that }
  1221. { that the variable is valid. }
  1222. pvarsym(sym)^.is_valid:=1;
  1223. if pvarsym(sym)^.owner^.symtabletype=staticsymtable then
  1224. begin
  1225. if assigned(instr.operands[operandnum].ref.symbol) then
  1226. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1227. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1228. end
  1229. else
  1230. begin
  1231. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1232. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1233. end;
  1234. { the current size is NOT overriden if it already }
  1235. { exists, such as in the case of a byte ptr, in }
  1236. { front of the identifier. }
  1237. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1238. Begin
  1239. case pvarsym(sym)^.getsize of
  1240. 1: instr.operands[operandnum].size := S_B;
  1241. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1242. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1243. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1244. extended_size: instr.operands[operandnum].size := S_FX;
  1245. else
  1246. { this is in the case where the instruction is LEA }
  1247. { or something like that, in that case size is not }
  1248. { important. }
  1249. instr.operands[operandnum].size := S_NO;
  1250. end; { end case }
  1251. end;
  1252. { ok, finished for this var }
  1253. CreateVarInstr := TRUE;
  1254. Exit;
  1255. end;
  1256. typedconstsym : begin
  1257. { we always assume in asm statements that }
  1258. { that the variable is valid. }
  1259. if assigned(instr.operands[operandnum].ref.symbol) then
  1260. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1261. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1262. { the current size is NOT overriden if it already }
  1263. { exists, such as in the case of a byte ptr, in }
  1264. { front of the identifier. }
  1265. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1266. Begin
  1267. case ptypedconstsym(sym)^.getsize of
  1268. 1: instr.operands[operandnum].size := S_B;
  1269. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1270. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1271. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1272. extended_size: instr.operands[operandnum].size := S_FX;
  1273. else
  1274. instr.operands[operandnum].size := S_NO;
  1275. end; { end case }
  1276. end;
  1277. { ok, finished for this var }
  1278. CreateVarInstr := TRUE;
  1279. Exit;
  1280. end;
  1281. constsym : begin
  1282. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1283. begin
  1284. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1285. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1286. CreateVarInstr := TRUE;
  1287. Exit;
  1288. end;
  1289. end;
  1290. typesym : begin
  1291. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1292. begin
  1293. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1294. instr.operands[operandnum].val:=0;
  1295. CreateVarInstr := TRUE;
  1296. Exit;
  1297. end;
  1298. end;
  1299. procsym : begin
  1300. { free the memory before changing the symbol name. }
  1301. if assigned(instr.operands[operandnum].ref.symbol) then
  1302. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1303. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1304. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1305. CreateVarInstr := TRUE;
  1306. Exit;
  1307. end
  1308. else
  1309. begin
  1310. Message(assem_e_unsupported_symbol_type);
  1311. exit;
  1312. end;
  1313. end;
  1314. end;
  1315. { now check for parameters passed to routine }
  1316. if assigned(aktprocsym^.definition^.parast) then
  1317. sym:=aktprocsym^.definition^.parast^.search(hs)
  1318. else
  1319. sym:=nil;
  1320. if assigned(sym) then
  1321. begin
  1322. case sym^.typ of
  1323. varsym : begin
  1324. l:=pvarsym(sym)^.address;
  1325. { set offset }
  1326. inc(l,aktprocsym^.definition^.parast^.call_offset);
  1327. pvarsym(sym)^.is_valid:=1;
  1328. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1329. instr.operands[operandnum].ref.offset := l;
  1330. { the current size is NOT overriden if it already }
  1331. { exists, such as in the case of a byte ptr, in }
  1332. { front of the identifier. }
  1333. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1334. Begin
  1335. case pvarsym(sym)^.getsize of
  1336. 1: instr.operands[operandnum].size := S_B;
  1337. 2: instr.operands[operandnum].size := S_W;
  1338. 4: instr.operands[operandnum].size := S_L;
  1339. 8: instr.operands[operandnum].size := S_IQ;
  1340. extended_size: instr.operands[operandnum].size := S_FX;
  1341. else
  1342. { this is in the case where the instruction is LEA }
  1343. { or something like that, in that case size is not }
  1344. { important. }
  1345. instr.operands[operandnum].size := S_NO;
  1346. end; { end case }
  1347. end; { endif }
  1348. CreateVarInstr := TRUE;
  1349. Exit;
  1350. end;
  1351. else
  1352. begin
  1353. Message(assem_e_unsupported_symbol_type);
  1354. exit;
  1355. end;
  1356. end; { case }
  1357. end; { endif }
  1358. end;
  1359. { not found.. .now look for global variables. }
  1360. getsym(hs,false);
  1361. sym:=srsym;
  1362. if assigned(sym) then
  1363. Begin
  1364. case sym^.typ of
  1365. varsym,
  1366. typedconstsym : Begin
  1367. { free the memory before changing the symbol name. }
  1368. if assigned(instr.operands[operandnum].ref.symbol) then
  1369. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1370. instr.operands[operandnum].ref.symbol:=newpasstr(sym^.mangledname);
  1371. { the current size is NOT overriden if it already }
  1372. { exists, such as in the case of a byte ptr, in }
  1373. { front of the identifier. }
  1374. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1375. Begin
  1376. case pvarsym(sym)^.getsize of
  1377. 1: instr.operands[operandnum].size := S_B;
  1378. 2: instr.operands[operandnum].size := S_W;
  1379. 4: instr.operands[operandnum].size := S_L;
  1380. 8: instr.operands[operandnum].size := S_IQ;
  1381. else
  1382. { this is in the case where the instruction is LEA }
  1383. { or something like that, in that case size is not }
  1384. { important. }
  1385. instr.operands[operandnum].size := S_NO;
  1386. end;
  1387. end
  1388. else
  1389. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = typedconstsym) then
  1390. Begin
  1391. { only these are valid sizes, otherwise prefixes are }
  1392. { required. }
  1393. case ptypedconstsym(sym)^.definition^.size of
  1394. 1: instr.operands[operandnum].size := S_B;
  1395. 2: instr.operands[operandnum].size := S_W;
  1396. 4: instr.operands[operandnum].size := S_L;
  1397. 8: instr.operands[operandnum].size := S_IQ;
  1398. else
  1399. { this is in the case where the instruction is LEA }
  1400. { or something like that, in that case size is not }
  1401. { important. }
  1402. instr.operands[operandnum].size := S_NO;
  1403. end;
  1404. end; { endif }
  1405. CreateVarInstr := TRUE;
  1406. Exit;
  1407. end;
  1408. constsym : begin
  1409. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1410. begin
  1411. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1412. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1413. CreateVarInstr := TRUE;
  1414. Exit;
  1415. end;
  1416. end;
  1417. typesym : begin
  1418. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1419. begin
  1420. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1421. instr.operands[operandnum].val:=0;
  1422. CreateVarInstr := TRUE;
  1423. Exit;
  1424. end;
  1425. end;
  1426. procsym : begin
  1427. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1428. Message(assem_w_calling_overload_func);
  1429. { free the memory before changing the symbol name. }
  1430. if assigned(instr.operands[operandnum].ref.symbol) then
  1431. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1432. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1433. instr.operands[operandnum].size:=S_L;
  1434. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1435. CreateVarInstr := TRUE;
  1436. Exit;
  1437. end;
  1438. else
  1439. begin
  1440. Message(assem_e_unsupported_symbol_type);
  1441. exit;
  1442. end;
  1443. end; {case}
  1444. end; { end looking for global variables .. }
  1445. end;
  1446. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1447. {**********************************************************************}
  1448. { Description: Searches for a pascal label definition, first in the }
  1449. { local symbol list and then in the global symbol list. If found then }
  1450. { return pointer to label and return true, otherwise returns false. }
  1451. {**********************************************************************}
  1452. var
  1453. sym: psym;
  1454. Begin
  1455. SearchLabel := FALSE;
  1456. if assigned(aktprocsym) then
  1457. Begin
  1458. { Check the local constants }
  1459. if assigned(aktprocsym^.definition) then
  1460. Begin
  1461. if assigned(aktprocsym^.definition^.localst) then
  1462. sym := aktprocsym^.definition^.localst^.search(s)
  1463. else
  1464. sym := nil;
  1465. if assigned(sym) then
  1466. Begin
  1467. if (sym^.typ = labelsym) then
  1468. Begin
  1469. hl:=plabelsym(sym)^.number;
  1470. SearchLabel := TRUE;
  1471. exit;
  1472. end;
  1473. end;
  1474. end;
  1475. end;
  1476. { Check the global label symbols... }
  1477. getsym(s,false);
  1478. if srsym <> nil then
  1479. Begin
  1480. if (srsym^.typ=labelsym) then
  1481. Begin
  1482. hl:=plabelsym(srsym)^.number;
  1483. SearchLabel:= TRUE;
  1484. exit;
  1485. end;
  1486. end;
  1487. end;
  1488. {*************************************************************************}
  1489. { Instruction Generation Utilities }
  1490. {*************************************************************************}
  1491. Procedure ConcatString(p : paasmoutput;s:string);
  1492. {*********************************************************************}
  1493. { PROCEDURE ConcatString(s:string); }
  1494. { Description: This routine adds the character chain pointed to in }
  1495. { s to the instruction linked list. }
  1496. {*********************************************************************}
  1497. Var
  1498. pc: PChar;
  1499. Begin
  1500. getmem(pc,length(s)+1);
  1501. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1502. end;
  1503. Procedure ConcatPasString(p : paasmoutput;s:string);
  1504. {*********************************************************************}
  1505. { PROCEDURE ConcatPasString(s:string); }
  1506. { Description: This routine adds the character chain pointed to in }
  1507. { s to the instruction linked list, contrary to ConcatString it }
  1508. { uses a pascal style string, so it conserves null characters. }
  1509. {*********************************************************************}
  1510. Begin
  1511. p^.concat(new(pai_string,init(s)));
  1512. end;
  1513. Procedure ConcatDirect(p : paasmoutput;s:string);
  1514. {*********************************************************************}
  1515. { PROCEDURE ConcatDirect(s:string) }
  1516. { Description: This routine output the string directly to the asm }
  1517. { output, it is only sed when writing special labels in AT&T mode, }
  1518. { and should not be used without due consideration, since it may }
  1519. { cause problems. }
  1520. {*********************************************************************}
  1521. Var
  1522. pc: PChar;
  1523. Begin
  1524. getmem(pc,length(s)+1);
  1525. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1526. end;
  1527. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1528. {*********************************************************************}
  1529. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1530. { Description: This routine adds the value constant to the current }
  1531. { instruction linked list. }
  1532. { maxvalue -> indicates the size of the data to initialize: }
  1533. { $ff -> create a byte node. }
  1534. { $ffff -> create a word node. }
  1535. { $ffffffff -> create a dword node. }
  1536. {*********************************************************************}
  1537. Begin
  1538. if value > maxvalue then
  1539. Begin
  1540. Message(assem_e_constant_out_of_bounds);
  1541. { assuming a value of maxvalue }
  1542. value := maxvalue;
  1543. end;
  1544. if maxvalue = $ff then
  1545. p^.concat(new(pai_const,init_8bit(byte(value))))
  1546. else
  1547. if maxvalue = $ffff then
  1548. p^.concat(new(pai_const,init_16bit(word(value))))
  1549. else
  1550. if maxvalue = $ffffffff then
  1551. p^.concat(new(pai_const,init_32bit(longint(value))));
  1552. end;
  1553. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1554. {***********************************************************************}
  1555. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1556. { Description: This routine adds the value constant to the current }
  1557. { instruction linked list. }
  1558. { real_typ -> indicates the type of the real data to initialize: }
  1559. { s32real -> create a single node. }
  1560. { s64real -> create a double node. }
  1561. { s80real -> create an extended node. }
  1562. { s64bit -> create a comp node. }
  1563. { f32bit -> create a fixed node. (not used normally) }
  1564. {***********************************************************************}
  1565. Begin
  1566. case real_typ of
  1567. s32real : p^.concat(new(pai_single,init(value)));
  1568. s64real : p^.concat(new(pai_double,init(value)));
  1569. s80real : p^.concat(new(pai_extended,init(value)));
  1570. s64bit : p^.concat(new(pai_comp,init(value)));
  1571. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1572. end;
  1573. end;
  1574. Procedure ConcatLabel(p: paasmoutput;op : tasmop;var l : plabel);
  1575. {*********************************************************************}
  1576. { PROCEDURE ConcatLabel }
  1577. { Description: This routine either emits a label or a labeled }
  1578. { instruction to the linked list of instructions. }
  1579. {*********************************************************************}
  1580. begin
  1581. if op=A_LABEL then
  1582. p^.concat(new(pai_label,init(l)))
  1583. else
  1584. p^.concat(new(pai_labeled,init(op,l)))
  1585. end;
  1586. procedure ConcatPublic(p:paasmoutput;const s : string);
  1587. {*********************************************************************}
  1588. { PROCEDURE ConcatPublic }
  1589. { Description: This routine emits an global definition to the }
  1590. { linked list of instructions.(used by AT&T styled asm) }
  1591. {*********************************************************************}
  1592. begin
  1593. p^.concat(new(pai_symbol,init_global(s)));
  1594. { concat_internal(s,EXT_NEAR); done in aasm }
  1595. end;
  1596. procedure ConcatLocal(p:paasmoutput;const s : string);
  1597. {*********************************************************************}
  1598. { PROCEDURE ConcatLocal }
  1599. { Description: This routine emits an local definition to the }
  1600. { linked list of instructions. }
  1601. {*********************************************************************}
  1602. begin
  1603. p^.concat(new(pai_symbol,init(s)));
  1604. { concat_internal(s,EXT_NEAR); done in aasm }
  1605. end;
  1606. Procedure ConcatGlobalBss(const s : string;size : longint);
  1607. {*********************************************************************}
  1608. { PROCEDURE ConcatGlobalBss }
  1609. { Description: This routine emits an global datablock to the }
  1610. { linked list of instructions. }
  1611. {*********************************************************************}
  1612. begin
  1613. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1614. { concat_internal(s,EXT_NEAR); done in aasm }
  1615. end;
  1616. Procedure ConcatLocalBss(const s : string;size : longint);
  1617. {*********************************************************************}
  1618. { PROCEDURE ConcatLocalBss }
  1619. { Description: This routine emits a local datablcok to the }
  1620. { linked list of instructions. }
  1621. {*********************************************************************}
  1622. begin
  1623. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1624. { concat_internal(s,EXT_NEAR); done in aasm }
  1625. end;
  1626. { add to list of external labels }
  1627. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1628. {*********************************************************************}
  1629. { PROCEDURE ConcatExternal }
  1630. { Description: This routine emits an external definition to the }
  1631. { linked list of instructions.(used by AT&T styled asm) }
  1632. {*********************************************************************}
  1633. { check if in internal list and remove it there }
  1634. var p : pai_external;
  1635. begin
  1636. p:=search_assembler_symbol(internals,s,typ);
  1637. if p<>nil then internals^.remove(p);
  1638. concat_external(s,typ);
  1639. end;
  1640. { add to internal list of labels }
  1641. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1642. {*********************************************************************}
  1643. { PROCEDURE ConcatInternal }
  1644. { Description: This routine emits an internal definition of a symbol }
  1645. { (used by AT&T styled asm for undefined labels) }
  1646. {*********************************************************************}
  1647. begin
  1648. concat_internal(s,typ);
  1649. end;
  1650. end.
  1651. {
  1652. $Log$
  1653. Revision 1.17 1998-12-23 22:55:55 peter
  1654. + rec.field(%esi) support
  1655. + [esi+rec.field] support
  1656. Revision 1.16 1998/12/11 00:02:44 peter
  1657. + globtype,tokens,version unit splitted from globals
  1658. Revision 1.15 1998/11/17 00:26:11 peter
  1659. * fixed for $H+
  1660. Revision 1.14 1998/11/05 23:48:17 peter
  1661. * recordtype.field support in constant expressions
  1662. * fixed imul for oa_imm8 which was not allowed
  1663. * fixed reading of local typed constants
  1664. * fixed comment reading which is not any longer a separator
  1665. Revision 1.13 1998/10/28 00:08:45 peter
  1666. + leal procsym,eax is now allowed
  1667. + constants are now handled also when starting an expression
  1668. + call *pointer is now allowed
  1669. Revision 1.12 1998/10/14 11:28:13 florian
  1670. * emitpushreferenceaddress gets now the asmlist as parameter
  1671. * m68k version compiles with -duseansistrings
  1672. Revision 1.11 1998/10/13 16:49:59 pierre
  1673. * undid some changes of Peter that made the compiler wrong
  1674. for m68k (I had to reinsert some ifdefs)
  1675. * removed several memory leaks under m68k
  1676. * removed the meory leaks for assembler readers
  1677. * cross compiling shoud work again better
  1678. ( crosscompiling sysamiga works
  1679. but as68k still complain about some code !)
  1680. Revision 1.10 1998/10/13 13:10:10 peter
  1681. * new style for m68k/i386 infos and enums
  1682. Revision 1.9 1998/09/24 17:54:15 carl
  1683. * bugfixes from fix branch
  1684. Revision 1.8.2.1 1998/09/24 17:46:25 carl
  1685. * support for objects in asm statements
  1686. Revision 1.8 1998/08/27 00:43:06 carl
  1687. +} now record offsets searches set the operand sizes
  1688. Revision 1.7 1998/08/18 20:51:32 peter
  1689. * fixed bug 42
  1690. Revision 1.6 1998/08/10 14:49:40 peter
  1691. + localswitches, moduleswitches, globalswitches splitting
  1692. Revision 1.5 1998/07/14 21:46:38 peter
  1693. * updated messages file
  1694. Revision 1.4 1998/06/04 23:51:31 peter
  1695. * m68k compiles
  1696. + .def file creation moved to gendef.pas so it could also be used
  1697. for win32
  1698. Revision 1.3 1998/05/31 14:13:30 peter
  1699. * fixed call bugs with assembler readers
  1700. + OPR_SYMBOL to hold a symbol in the asm parser
  1701. * fixed staticsymtable vars which were acessed through %ebp instead of
  1702. name
  1703. Revision 1.2 1998/04/29 10:33:43 pierre
  1704. + added some code for ansistring (not complete nor working yet)
  1705. * corrected operator overloading
  1706. * corrected nasm output
  1707. + started inline procedures
  1708. + added starstarn : use ** for exponentiation (^ gave problems)
  1709. + started UseTokenInfo cond to get accurate positions
  1710. }