asmutils.pas 67 KB

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