File: CodeGen.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,000 kB
  • ctags: 3,667
  • sloc: java: 23,109; xml: 414; makefile: 22
file content (133 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: CodeGen.java,v 1.1.1.1 2004/05/09 16:57:49 vlad_r Exp $
 */
package com.vladium.jcd.compiler;

import com.vladium.jcd.cls.ClassDef;
import com.vladium.jcd.cls.constant.CONSTANT_Integer_info;
import com.vladium.jcd.opcodes.IOpcodes;
import com.vladium.util.ByteArrayOStream;

// ----------------------------------------------------------------------------
/**
 * @author Vlad Roubtsov, (C) 2003
 */
public
abstract class CodeGen implements IOpcodes
{
    // public: ................................................................
    
    
    public static void load_local_object_var (final ByteArrayOStream out, final int index)
    {
        if (index <= 3)
        {
            out.write (_aload_0 + index); // aload_n
        }
        else if (index <= 0xFF)
        {
            out.write2 (_aload,
                        index);  // indexbyte
        }
        else
        {
            out.write4 (_wide,
                        _aload,
                        index >>> 8,    // indexbyte1
                        index);         // indexbyte2
        }
    }
    
    public static void store_local_object_var (final ByteArrayOStream out, final int index)
    {
        if (index <= 3)
        {
            out.write (_astore_0 + index); // astore_n
        }
        else if (index <= 0xFF)
        {
            out.write2 (_astore,
                        index);  // indexbyte
        }
        else
        {
            out.write4 (_wide,
                        _astore,
                        index >>> 8,    // indexbyte1
                        index);         // indexbyte2
        }
        
        // [stack -1]
    }
    
    public static void push_int_value (final ByteArrayOStream out, final ClassDef cls, final int value)
    {
        if ((-1 <= value) && (value <= 5))
        {
            out.write (_iconst_0 + value);
        }
        else if ((-128 <= value) && (value <= 127))
        {
            out.write2 (_bipush,
                        value); // byte1
        }
        else if ((-32768 <= value) && (value <= 32767))
        {
            out.write3 (_sipush,
                        value >>> 8,    // byte1
                        value);         // byte2
        }
        else // we have to create an Integer constant in the constant pool:
        {
            // TODO: check if it's already there
            final int index = cls.getConstants ().add (new CONSTANT_Integer_info (value));
            
            if (index <= 0xFF)
            {
                out.write2 (_ldc,
                            index);  // index
            }
            else // must use ldc_w
            {
                out.write3 (_ldc_w,
                            index >>> 8,  // indexbyte1
                            index);       // indexbyte2
            }
        }
        
        // [stack +1]
    }
    
    public static void push_constant_index (final ByteArrayOStream out, final int index)
    {
        if (index <= 0xFF)
        {
            out.write2 (_ldc,
                       index);  // indexbyte
        }
        else
        {
            out.write3 (_ldc_w,
                        index >>> 8,     // indexbyte1
                        index);          // indexbyte2
        }
        
        // [stack +1]
    }
    
    // protected: .............................................................

    // package: ...............................................................
    
    // private: ...............................................................
    
    
    private CodeGen () {} // prevent subclassing

} // end of class
// ----------------------------------------------------------------------------