Use of labels in the interpreter

Source: Internet
Author: User

Android_6200/Dalvik/Vm/mterp/out/InterpC-portdbg.c + 467

C89 is also supported

Looking at code from me, I will never learn programs well, and I feel confused. Who can have a general understanding ?!

Write a program:

1 # include <stdio. h>
2 # include <stdlib. h>
3
4 # define H (_ OP) & OP _ ### _ OP
5 # define handle_opcode (_ OP) OP ###_ OP:
6
7 # define define_goto_table (_ name )\
8 Static const void * _ name [8] = {\
9 h (op_nop ),\
10 h (op_move ),\
11 h (op_move_16 ),\
12 h (op_move_wide ),\
13 H (op_move_object ),\
14 h (op_return_wide ),\
15 h (op_const_4 ),\
16 h (op_const )\
17}
18
19 int main (void)
20 {
21 define_goto_table (handlertable );
22
23 goto * handlertable [7];
24
25
26 handle_opcode (op_nop)
27 printf ("op_nop \ n ");
28 exit (1 );
29 handle_opcode (op_move)
30 printf ("op_move \ n ");
31 exit (1 );
32 handle_opcode (op_move_16)
33 printf ("op_move_16 \ n ");
34 exit (1 );
35 handle_opcode (op_move_wide)
36 printf ("op_move_wide \ n ");
37 exit (1 );
38 handle_opcode (op_move_object)
39 printf ("op_move_object \ n ");
40 exit (1 );
41 handle_opcode (op_return_wide)
42 printf ("op_return_wide \ n ");
43 exit (1 );
44 handle_opcode (op_const_4)
45 printf ("op_const_4 \ n ");
46 exit (1 );
47 handle_opcode (op_const)
48 printf ("op_const \ n ");
49 exit (1 );
50
51
52 return 0;
53}

See http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Labels-as-Values.html#fn-1

Labels as values

& A magical role

You can get the address of a label defined in the current function (or a containing function) with the unary operator&&.

The value has Typevoid *. This value is a constant and can be used wherever a constant of that type is valid. For example:

     void *ptr;     ...     ptr = &&foo;     

To use these values, you need to be able to jump to one. This is done with the computed GOTO statement,goto *Exp;. For example,

     goto *ptr;     

Any expression of typevoid *Is allowed.

One way of using these constants is in initializing a static array that will serve as a jump table: Usage

     static void *array[] = { &&foo, &&bar, &&hack };     

Then you can select a label with indexing, like this:

     goto *array[i];     

Note that this does not check whether the subscript is in bounds -- Array Indexing in C never does that.

Compared with the switch statement:

Such an array of label values serves a purpose much like that ofswitchStatement.switchStatement is cleaner, so use that rather than array unless the problem does not fitswitchStatement very well.

This method is used in the Dalvik virtual machine.

Another use of label values is in an interpreter for Threaded code. The labels within the interpreter function can be stored in the threaded code for super-fast dispatching.

You may not use this mechanism to jump to code in a different function across functions. if you do that, totally unpredictable things will happen. the best way to avoid this is to store the label address only in automatic variables and never pass it as an argument.

An alternate way to write the above example is

     static const int array[] = { &&foo - &&foo, &&bar - &&foo,                                  &&hack - &&foo };     goto *(&&foo + array[i]);     

Usage in a dynamic library:

This is more friendly to code living in shared libraries, as it usually CES the number of dynamic relocations that are needed, and by consequence, allows the data to be read-only.

 

Let's look at the partial labels in the macro definition:

Locally declared labels

Each statement expression is a scope in whichLocal labelsCan be declared. A local label is simply an identifier; you can jump to it with an ordinarygotoStatement, but only from within the statement expression it belongs.

A local label declaration looks like this:

     __label__ label;     

Or

     __label__ label1, label2, ...;     

Local label declarations must come at the beginning of the statement expression, right after({, Before any ordinary declarations.

The label Declaration defines the labelName, But does not define the label itself. You must do this in the usual way,Label:, Within the statements of the statement expression.

The local label feature is useful because Statement expressions are often used in macros. If the macro contains nested loops,gotoCan be useful for breaking out of them. however, an ordinary label whose scope is the whole function cannot be used: If the macro can be expanded several times in one function, the label will be Multiply defined in that function. A local label avoids this problem. for example:

     #define SEARCH(array, target)                     \     ({                                                \       __label__ found;                                \       typeof (target) _SEARCH_target = (target);      \       typeof (*(array)) *_SEARCH_array = (array);     \       int i, j;                                       \       int value;                                      \       for (i = 0; i < max; i++)                       \         for (j = 0; j < max; j++)                     \           if (_SEARCH_array[i][j] == _SEARCH_target)  \             { value = i; goto found; }                \       value = -1;                                     \      found:                                           \       value;                                          \     })

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.