• No results found

The C programming language and other languages based on it specify the rules of the syntax in the form of a grammar. The grammar describes the set of legal tokens or keywords of the language and how these can be legally structured. The actual meaning of the tokens and they way in which they are structured is referred to as semantics. The syntax of the language is processed by the compiler front end, the main parts being the lexical analyser and parser. The semantics of the language are derived by the compiler back end including the optimiser and the code generator. As with any practical programming language, the semantics of C are complex. For example, C expressions specify a number of complicated concepts such as lvalue and rvalue semantics, sequence points, operator precedence, associativity and integral type promotions. Expressions also control how operands are accessed, for example as simple variables, array elements or pointers. Such semantics must be implemented by the compiler back end when generating target assembly language or machine code for the particular target processor. It is normal for the ISA to offer support for the semantics of common programming language features. However, this support is low level. For example, a traditional ISA may provide indirect addressing which supports pointer semantics and displacement addressing which supports array semantics. The Expression Engine discussed in Chapter 4 provides support for indirect addressing and for obtaining the address of a variable, which supports pointer semantics.

The Expression Engine provides semantic support for expressions, however there is an oppor- tunity for the SDLP to provide better support for programming control constructs such as loops and conditional statements. Any support provided for C, should also be applicable for all lan- guages derived from and based on C. SDLP instructions can be used to directly support C control constructs and are illustrated in Table 5.4.

It can be seen in Table 5.4, that some SDLP instructions are marked as Not currently supported. These are considered out of scope for this thesis. The supported SDLP instructions are while, if and if-else. The remaining SDLP instructions should be considered for future work.

C Control Construct SDLP

if if

if else ifelse

while while

function call and return Not currently supported switch, case, default Not currently supported

break, continue Not currently supported

goto, label Not currently supported

sizeof Not currently supported

Table 5.4: C keywords mapping onto SDLP Instructions

because some C constructs can be simply re-written or transformed using the supported keywords. Table 5.5 shows how a for loop can be rewritten as a while loop, obviating the need for an SDLP for instruction.

For loop Equivalent while loop

f o r ( i n t i =0; i < 1 0 ; i ++) { <s t a t e m e n t s > } i n t i = 0 ; while ( i++ < 1 0 ) { <s t a t e m e n t s > }

Table 5.6 illustrates various other C programming constructs and whether or not support is provided by the SDLP.

C Language Construct SDLP Support

Scalar types, e.g. char, int

Expression engine (control bits RP0-RP5, D) provides direct addressing which supports scalar type access

Aggregate types, e.g. struct, union, enum

Expression engine (control bits RP0-RP5, D) provides indirect addressing which supports accessing aggregate types

Function calls and return Not currently supported Arrays

Not currently supported. Would require assembler modification so that expression engine supports displacement addressing.

Pointers

Expression engine (control bits RP0-RP5, D) provides indirect addressing and address of operator which supports pointer semantics

static, volatile, register,

const, extern, typedef Compiler constructs, not supported directly by SDLP Table 5.6: C Language Constructs and SDLP Support

Listings 5.1, 5.2, and 5.3 illustrate the Backus-Naur form for the supported SDLP instructions and how they are described for the Assembler. For all of these, expression id refers to an ex- pression as defined in the previous chapter. A statement is defined as either an instruction or an expression id.

while <l a b e l > <e x p r e s s i o n i d > { , <e x p r e s s i o n i d >} <s t a t e m e n t > {<s t a t e m e n t >}

<l a b e l >:

Listing 5.1: BNF for while instruction

In Listing 5.1, the label defines the point in the program following the while. In C, this would be the next statement after the closing brace of a while block. expression id refers to an expression defined elsewhere in the binary. Multiple expression ids can be specified for complex expressions. Expressions can be chained by specifying the lvalue output of a preceding expression as an rvalue input to the next expression. If the result of the last expression evaluation is zero, then the program control branches to label. Otherwise, control passes to the first statement in the while block.

Listing 5.2 specifies an if statement that works in a similar way to the while.

i f <l a b e l > <e x p r e s s i o n i d > { , <e x p r e s s i o n i d >} <s t a t e m e n t > {<s t a t e m e n t >}

<l a b e l >:

Listing 5.2: BNF for if instruction

Listing 5.3 specifies an if-else statement. The label 0 defines the start of the else block. In C, this would be the statement after the opening brace of the else block. label 1 defines the point in the program after the else block. In C, this would be the statement after the closing brace of the else block. If the result of the last expression evaluation is zero, then the program control branches to label 0, which is the else block. Otherwise, control passes to the first statement in the if block. The if block and the else block must both contain at least one statement each. When the last statement of the if block has been executed, program control jumps to label 1.

i f e l s e <l a b e l 0 > <l a b e l 1 > <e x p r e s s i o n i d > { , <e x p r e s s i o n i d >} <s t a t e m e n t > {<s t a t e m e n t >}

<l a b e l 0 >:

<s t a t e m e n t > {<s t a t e m e n t >} <l a b e l 1 >:

Listing 5.3: BNF for ifelse instruction