rautils.pas 66 KB

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