rautils.pas 65 KB

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