1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2 *
3 * This program and the accompanying materials are made available under
4 * the terms of the Common Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * $Id: CodeGen.java,v 1.1.1.1 2004/05/09 16:57:49 vlad_r Exp $
8 */
9package com.vladium.jcd.compiler;
10
11import com.vladium.jcd.cls.ClassDef;
12import com.vladium.jcd.cls.constant.CONSTANT_Integer_info;
13import com.vladium.jcd.opcodes.IOpcodes;
14import com.vladium.util.ByteArrayOStream;
15
16// ----------------------------------------------------------------------------
17/**
18 * @author Vlad Roubtsov, (C) 2003
19 */
20public
21abstract class CodeGen implements IOpcodes
22{
23    // public: ................................................................
24
25
26    public static void load_local_object_var (final ByteArrayOStream out, final int index)
27    {
28        if (index <= 3)
29        {
30            out.write (_aload_0 + index); // aload_n
31        }
32        else if (index <= 0xFF)
33        {
34            out.write2 (_aload,
35                        index);  // indexbyte
36        }
37        else
38        {
39            out.write4 (_wide,
40                        _aload,
41                        index >>> 8,    // indexbyte1
42                        index);         // indexbyte2
43        }
44    }
45
46    public static void store_local_object_var (final ByteArrayOStream out, final int index)
47    {
48        if (index <= 3)
49        {
50            out.write (_astore_0 + index); // astore_n
51        }
52        else if (index <= 0xFF)
53        {
54            out.write2 (_astore,
55                        index);  // indexbyte
56        }
57        else
58        {
59            out.write4 (_wide,
60                        _astore,
61                        index >>> 8,    // indexbyte1
62                        index);         // indexbyte2
63        }
64
65        // [stack -1]
66    }
67
68    public static void push_int_value (final ByteArrayOStream out, final ClassDef cls, final int value)
69    {
70        if ((-1 <= value) && (value <= 5))
71        {
72            out.write (_iconst_0 + value);
73        }
74        else if ((-128 <= value) && (value <= 127))
75        {
76            out.write2 (_bipush,
77                        value); // byte1
78        }
79        else if ((-32768 <= value) && (value <= 32767))
80        {
81            out.write3 (_sipush,
82                        value >>> 8,    // byte1
83                        value);         // byte2
84        }
85        else // we have to create an Integer constant in the constant pool:
86        {
87            // TODO: check if it's already there
88            final int index = cls.getConstants ().add (new CONSTANT_Integer_info (value));
89
90            if (index <= 0xFF)
91            {
92                out.write2 (_ldc,
93                            index);  // index
94            }
95            else // must use ldc_w
96            {
97                out.write3 (_ldc_w,
98                            index >>> 8,  // indexbyte1
99                            index);       // indexbyte2
100            }
101        }
102
103        // [stack +1]
104    }
105
106    public static void push_constant_index (final ByteArrayOStream out, final int index)
107    {
108        if (index <= 0xFF)
109        {
110            out.write2 (_ldc,
111                       index);  // indexbyte
112        }
113        else
114        {
115            out.write3 (_ldc_w,
116                        index >>> 8,     // indexbyte1
117                        index);          // indexbyte2
118        }
119
120        // [stack +1]
121    }
122
123    // protected: .............................................................
124
125    // package: ...............................................................
126
127    // private: ...............................................................
128
129
130    private CodeGen () {} // prevent subclassing
131
132} // end of class
133// ----------------------------------------------------------------------------
134