gdbint_6.html   [plain text]


<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from /mnt/apple/gdb/source/gdb.apple/source/gdb/gdb/doc/gdbint.texinfo on 23 November 1999 -->

<TITLE>GDB Internals - Language Support</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="gdbint_1.html">first</A>, <A HREF="gdbint_5.html">previous</A>, <A HREF="gdbint_7.html">next</A>, <A HREF="gdbint_16.html">last</A> section, <A HREF="gdbint_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC40" HREF="gdbint_toc.html#TOC40">Language Support</A></H1>

<P>
GDB's language support is mainly driven by the symbol reader, although
it is possible for the user to set the source language manually.

</P>
<P>
GDB chooses the source language by looking at the extension of the file
recorded in the debug info; <CODE>.c</CODE> means C, <CODE>.f</CODE> means Fortran,
etc.  It may also use a special-purpose language identifier if the debug
format supports it, such as DWARF.

</P>


<H2><A NAME="SEC41" HREF="gdbint_toc.html#TOC41">Adding a Source Language to GDB</A></H2>

<P>
To add other languages to GDB's expression parser, follow the following
steps:

</P>
<DL COMPACT>

<DT><EM>Create the expression parser.</EM>
<DD>
This should reside in a file <TT>`<VAR>lang</VAR>-exp.y'</TT>.  Routines for
building parsed expressions into a <SAMP>`union exp_element'</SAMP> list are in
<TT>`parse.c'</TT>.

Since we can't depend upon everyone having Bison, and YACC produces
parsers that define a bunch of global names, the following lines
<EM>must</EM> be included at the top of the YACC parser, to prevent the
various parsers from defining the same global names:


<PRE>
#define yyparse 	<VAR>lang</VAR>_parse
#define yylex 	<VAR>lang</VAR>_lex
#define yyerror 	<VAR>lang</VAR>_error
#define yylval 	<VAR>lang</VAR>_lval
#define yychar 	<VAR>lang</VAR>_char
#define yydebug 	<VAR>lang</VAR>_debug
#define yypact  	<VAR>lang</VAR>_pact 
#define yyr1		<VAR>lang</VAR>_r1   
#define yyr2		<VAR>lang</VAR>_r2   
#define yydef		<VAR>lang</VAR>_def  
#define yychk		<VAR>lang</VAR>_chk  
#define yypgo		<VAR>lang</VAR>_pgo  
#define yyact  	<VAR>lang</VAR>_act  
#define yyexca  	<VAR>lang</VAR>_exca
#define yyerrflag  	<VAR>lang</VAR>_errflag
#define yynerrs  	<VAR>lang</VAR>_nerrs
</PRE>

At the bottom of your parser, define a <CODE>struct language_defn</CODE> and
initialize it with the right values for your language.  Define an
<CODE>initialize_<VAR>lang</VAR></CODE> routine and have it call
<SAMP>`add_language(<VAR>lang</VAR>_language_defn)'</SAMP> to tell the rest of GDB
that your language exists.  You'll need some other supporting variables
and functions, which will be used via pointers from your
<CODE><VAR>lang</VAR>_language_defn</CODE>.  See the declaration of <CODE>struct
language_defn</CODE> in <TT>`language.h'</TT>, and the other <TT>`*-exp.y'</TT> files,
for more information.

<DT><EM>Add any evaluation routines, if necessary</EM>
<DD>
If you need new opcodes (that represent the operations of the language),
add them to the enumerated type in <TT>`expression.h'</TT>.  Add support
code for these operations in <CODE>eval.c:evaluate_subexp()</CODE>.  Add cases
for new opcodes in two functions from <TT>`parse.c'</TT>:
<CODE>prefixify_subexp()</CODE> and <CODE>length_of_subexp()</CODE>.  These compute
the number of <CODE>exp_element</CODE>s that a given operation takes up.

<DT><EM>Update some existing code</EM>
<DD>
Add an enumerated identifier for your language to the enumerated type
<CODE>enum language</CODE> in <TT>`defs.h'</TT>.

Update the routines in <TT>`language.c'</TT> so your language is included.
These routines include type predicates and such, which (in some cases)
are language dependent.  If your language does not appear in the switch
statement, an error is reported.

Also included in <TT>`language.c'</TT> is the code that updates the variable
<CODE>current_language</CODE>, and the routines that translate the
<CODE>language_<VAR>lang</VAR></CODE> enumerated identifier into a printable
string.

Update the function <CODE>_initialize_language</CODE> to include your
language.  This function picks the default language upon startup, so is
dependent upon which languages that GDB is built for.

Update <CODE>allocate_symtab</CODE> in <TT>`symfile.c'</TT> and/or symbol-reading
code so that the language of each symtab (source file) is set properly.
This is used to determine the language to use at each stack frame level.
Currently, the language is set based upon the extension of the source
file.  If the language can be better inferred from the symbol
information, please set the language of the symtab in the symbol-reading
code.

Add helper code to <CODE>expprint.c:print_subexp()</CODE> to handle any new
expression opcodes you have added to <TT>`expression.h'</TT>.  Also, add the
printed representations of your operators to <CODE>op_print_tab</CODE>.

<DT><EM>Add a place of call</EM>
<DD>
Add a call to <CODE><VAR>lang</VAR>_parse()</CODE> and <CODE><VAR>lang</VAR>_error</CODE> in
<CODE>parse.c:parse_exp_1()</CODE>.

<DT><EM>Use macros to trim code</EM>
<DD>
The user has the option of building GDB for some or all of the
languages.  If the user decides to build GDB for the language
<VAR>lang</VAR>, then every file dependent on <TT>`language.h'</TT> will have the
macro <CODE>_LANG_<VAR>lang</VAR></CODE> defined in it.  Use <CODE>#ifdef</CODE>s to
leave out large routines that the user won't need if he or she is not
using your language.

Note that you do not need to do this in your YACC parser, since if GDB
is not build for <VAR>lang</VAR>, then <TT>`<VAR>lang</VAR>-exp.tab.o'</TT> (the
compiled form of your parser) is not linked into GDB at all.

See the file <TT>`configure.in'</TT> for how GDB is configured for different
languages.

<DT><EM>Edit <TT>`Makefile.in'</TT></EM>
<DD>
Add dependencies in <TT>`Makefile.in'</TT>.  Make sure you update the macro
variables such as <CODE>HFILES</CODE> and <CODE>OBJS</CODE>, otherwise your code may
not get linked in, or, worse yet, it may not get <CODE>tar</CODE>red into the
distribution!

</DL>

<P><HR><P>
Go to the <A HREF="gdbint_1.html">first</A>, <A HREF="gdbint_5.html">previous</A>, <A HREF="gdbint_7.html">next</A>, <A HREF="gdbint_16.html">last</A> section, <A HREF="gdbint_toc.html">table of contents</A>.
</BODY>
</HTML>