aboutsummaryrefslogtreecommitdiffstats
path: root/fpga_interchange/xdc.cc
blob: 53a80b7da4b243aaa4fda2faab6426748fe7cc03 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2019  gatecat <gatecat@ds0.me>
 *  Copyright (C) 2021  Symbiflow Authors
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "xdc.h"

#include <string>

#include "context.h"
#include "log.h"

// Include tcl.h late because it messed with #define's and lets them leave the
// scope of the header.
#include <tcl.h>

NEXTPNR_NAMESPACE_BEGIN

static int obj_set_from_any(Tcl_Interp *interp, Tcl_Obj *objPtr) { return TCL_ERROR; }

static void set_tcl_obj_string(Tcl_Obj *objPtr, const std::string &s)
{
    NPNR_ASSERT(objPtr->bytes == nullptr);
    // Need to have space for the end null byte.
    objPtr->bytes = Tcl_Alloc(s.size() + 1);

    // Length is length of string, not including the end null byte.
    objPtr->length = s.size();

    std::copy(s.begin(), s.end(), objPtr->bytes);
    objPtr->bytes[objPtr->length] = '\0';
}

static void port_update_string(Tcl_Obj *objPtr)
{
    const Context *ctx = static_cast<const Context *>(objPtr->internalRep.twoPtrValue.ptr1);
    PortInfo *port_info = static_cast<PortInfo *>(objPtr->internalRep.twoPtrValue.ptr2);

    std::string port_name = port_info->name.str(ctx);
    set_tcl_obj_string(objPtr, port_name);
}

static void cell_update_string(Tcl_Obj *objPtr)
{
    const Context *ctx = static_cast<const Context *>(objPtr->internalRep.twoPtrValue.ptr1);
    CellInfo *cell_info = static_cast<CellInfo *>(objPtr->internalRep.twoPtrValue.ptr2);

    std::string cell_name = cell_info->name.str(ctx);
    set_tcl_obj_string(objPtr, cell_name);
}

static void obj_dup(Tcl_Obj *srcPtr, Tcl_Obj *dupPtr)
{
    dupPtr->internalRep.twoPtrValue = srcPtr->internalRep.twoPtrValue;
}

static void obj_free(Tcl_Obj *objPtr) {}

static void Tcl_SetStringResult(Tcl_Interp *interp, const std::string &s)
{
    char *copy = Tcl_Alloc(s.size() + 1);
    std::copy(s.begin(), s.end(), copy);
    copy[s.size()] = '\0';
    Tcl_SetResult(interp, copy, TCL_DYNAMIC);
}

static Tcl_ObjType port_object = {
        "port", obj_free, obj_dup, port_update_string, obj_set_from_any,
};

static Tcl_ObjType cell_object = {
        "cell", obj_free, obj_dup, cell_update_string, obj_set_from_any,
};

static int get_ports(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
    const Context *ctx = static_cast<const Context *>(data);
    if (objc == 1) {
        // Return list of all ports.
        Tcl_SetStringResult(interp, "Unimplemented");
        return TCL_ERROR;
    } else if (objc == 2) {
        const char *arg0 = Tcl_GetString(objv[1]);
        IdString port_name = ctx->id(arg0);

        auto iter = ctx->ports.find(port_name);
        if (iter == ctx->ports.end()) {
            Tcl_SetStringResult(interp, "Could not find port " + port_name.str(ctx));
            return TCL_ERROR;
        }

        Tcl_Obj *result = Tcl_NewObj();
        result->typePtr = &port_object;
        result->internalRep.twoPtrValue.ptr1 = (void *)(ctx);
        result->internalRep.twoPtrValue.ptr2 = (void *)(&iter->second);

        result->bytes = nullptr;
        port_update_string(result);

        Tcl_SetObjResult(interp, result);
        return TCL_OK;
    } else if (objc > 2) {
        log_warning("get_ports options not implemented!\n");
        return TCL_OK;
    } else {
        return TCL_ERROR;
    }
}

static int get_cells(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
    const Context *ctx = static_cast<const Context *>(data);
    if (objc == 1) {
        // Return list of all ports.
        Tcl_SetStringResult(interp, "Unimplemented");
        return TCL_ERROR;
    } else if (objc == 2) {
        const char *arg0 = Tcl_GetString(objv[1]);
        IdString cell_name = ctx->id(arg0);

        auto iter = ctx->cells.find(cell_name);
        if (iter == ctx->cells.end()) {
            Tcl_SetStringResult(interp, "Could not find cell " + cell_name.str(ctx));
            return TCL_ERROR;
        }

        Tcl_Obj *result = Tcl_NewObj();
        result->typePtr = &cell_object;
        result->internalRep.twoPtrValue.ptr1 = (void *)(ctx);
        result->internalRep.twoPtrValue.ptr2 = (void *)(iter->second.get());

        result->bytes = nullptr;
        cell_update_string(result);

        Tcl_SetObjResult(interp, result);
        return TCL_OK;
    } else if (objc > 2) {
        log_warning("get_cells options not implemented!\n");
        return TCL_OK;
    } else {
        return TCL_ERROR;
    }
}

static int set_property(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
    // set_property <property> <value> <object>
    if (objc != 4) {
        Tcl_SetStringResult(interp, "Only simple 'set_property <property> <value> <object>' is supported");
        return TCL_ERROR;
    }

    const char *property = Tcl_GetString(objv[1]);
    const char *value = Tcl_GetString(objv[2]);
    const Tcl_Obj *object = objv[3];

    if (object->typePtr == &port_object) {
        const Context *ctx = static_cast<const Context *>(object->internalRep.twoPtrValue.ptr1);
        PortInfo *port_info = static_cast<PortInfo *>(object->internalRep.twoPtrValue.ptr2);
        NPNR_ASSERT(port_info->net != nullptr);
        CellInfo *cell = ctx->port_cells.at(port_info->name);

        cell->attrs[ctx->id(property)] = Property(value);
    } else if (object->typePtr == &cell_object) {
        const Context *ctx = static_cast<const Context *>(object->internalRep.twoPtrValue.ptr1);
        CellInfo *cell = static_cast<CellInfo *>(object->internalRep.twoPtrValue.ptr2);

        cell->attrs[ctx->id(property)] = Property(value);
    }

    return TCL_OK;
}

static int not_implemented(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
    // TCL command that is not yet implemented
    log_warning("%s command is not implemented!\n", Tcl_GetString(objv[0]));
    return TCL_OK;
}

TclInterp::TclInterp(Context *ctx)
{
    interp = Tcl_CreateInterp();
    NPNR_ASSERT(Tcl_Init(interp) == TCL_OK);

    Tcl_RegisterObjType(&port_object);
    Tcl_RegisterObjType(&cell_object);

    NPNR_ASSERT(Tcl_Eval(interp, "rename unknown _original_unknown") == TCL_OK);
    NPNR_ASSERT(Tcl_Eval(interp, "proc unknown args {\n"
                                 "  set result [scan [lindex $args 0] \"%d\" value]\n"
                                 "  if { $result == 1 && [llength $args] == 1 } {\n"
                                 "    return \\[$value\\]\n"
                                 "  } else {\n"
                                 "    uplevel 1 [list _original_unknown {*}$args]\n"
                                 "  }\n"
                                 "}") == TCL_OK);
    Tcl_CreateObjCommand(interp, "get_ports", get_ports, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "get_cells", get_cells, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "set_property", set_property, ctx, nullptr);

    // Not implemented TCL commands
    Tcl_CreateObjCommand(interp, "create_clock", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "get_clocks", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "get_iobanks", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "get_nets", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "get_pins", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "set_clock_groups", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "set_false_path", not_implemented, ctx, nullptr);
    Tcl_CreateObjCommand(interp, "set_max_delay", not_implemented, ctx, nullptr);
}

TclInterp::~TclInterp() { Tcl_DeleteInterp(interp); }

NEXTPNR_NAMESPACE_END
> end loop; end Disp_Iir_Chain_Elements; procedure Disp_Iir_Chain (Id : String; N : Iir) is begin if N = Null_Iir then return; end if; Put_Stag (Id); Put_Stag_End; Disp_Iir_Chain_Elements (N); Put_Etag (Id); end Disp_Iir_Chain; procedure Disp_Iir_List (Id : String; L : Iir_List; Ref : Boolean) is El : Iir; It : List_Iterator; begin case L is when Null_Iir_List => null; when Iir_List_All => Put_Stag (Id); Put_Attribute ("list-id", "all"); Put_Empty_Stag_End; when others => Put_Stag (Id); Put_Attribute ("list-id", Strip (Iir_List'Image (L))); Put_Stag_End; It := List_Iterate (L); while Is_Valid (It) loop El := Get_Element (It); if Ref then Disp_Iir_Ref ("el", El); else Disp_Iir ("el", El); end if; Next (It); end loop; Put_Etag (Id); end case; end Disp_Iir_List; procedure Disp_Iir_Flist (Id : String; L : Iir_Flist; Ref : Boolean) is El : Iir; begin if L = Null_Iir_Flist then return; end if; Put_Stag (Id); case L is when Iir_Flist_All => Put_Attribute ("flist-id", "all"); Put_Empty_Stag_End; return; when Iir_Flist_Others => Put_Attribute ("flist-id", "others"); Put_Empty_Stag_End; return; when others => Put_Attribute ("flist-id", Strip (Iir_Flist'Image (L))); Put_Stag_End; end case; for I in Flist_First .. Flist_Last (L) loop El := Get_Nth_Element (L, I); if Ref then Disp_Iir_Ref ("el", El); else Disp_Iir ("el", El); end if; end loop; Put_Etag (Id); end Disp_Iir_Flist; procedure Disp_Iir (Id : String; N : Iir) is begin if N = Null_Iir then return; end if; Put_Stag (Id); Put_Attribute ("id", Strip (Iir'Image (N))); Put_Attribute ("kind", Get_Iir_Image (Get_Kind (N))); declare Loc : constant Location_Type := Get_Location (N); File : Name_Id; Line : Natural; Col : Natural; begin if Loc /= No_Location then Files_Map.Location_To_Position (Loc, File, Line, Col); Put_Attribute ("file", Image (File)); Put_Attribute ("line", Strip (Natural'Image (Line))); Put_Attribute ("col", Strip (Natural'Image (Col))); end if; end; declare Fields : constant Fields_Array := Get_Fields (Get_Kind (N)); F : Fields_Enum; begin -- First attributes for I in Fields'Range loop F := Fields (I); case Get_Field_Type (F) is when Type_Iir => null; when Type_Iir_List => null; when Type_Iir_Flist => null; when Type_String8_Id => null; when Type_PSL_NFA => Put_Field (F, "PSL-NFA"); -- Disp_PSL_NFA (Get_PSL_NFA (N, F), Sub_Indent); when Type_PSL_Node => Put_Field (F, "PSL-NODE"); when Type_Source_Ptr => null; when Type_Date_Type => Put_Field (F, Strip (Date_Type'Image (Get_Date_Type (N, F)))); when Type_Number_Base_Type => Put_Field (F, Number_Base_Type'Image (Get_Number_Base_Type (N, F))); when Type_Iir_Constraint => Put_Field (F, Image_Iir_Constraint (Get_Iir_Constraint (N, F))); when Type_Iir_Mode => Put_Field (F, Image_Iir_Mode (Get_Iir_Mode (N, F))); when Type_Iir_Index32 => Put_Field (F, Iir_Index32'Image (Get_Iir_Index32 (N, F))); when Type_Int64 => Put_Field (F, Int64'Image (Get_Int64 (N, F))); when Type_Boolean => Put_Field (F, Image_Boolean (Get_Boolean (N, F))); when Type_Iir_Staticness => Put_Field (F, Image_Iir_Staticness (Get_Iir_Staticness (N, F))); when Type_Date_State_Type => Put_Field (F, Image_Date_State_Type (Get_Date_State_Type (N, F))); when Type_Iir_All_Sensitized => Put_Field (F, Image_Iir_All_Sensitized (Get_Iir_All_Sensitized (N, F))); when Type_Iir_Signal_Kind => Put_Field (F, Image_Iir_Signal_Kind (Get_Iir_Signal_Kind (N, F))); when Type_Tri_State_Type => Put_Field (F, Image_Tri_State_Type (Get_Tri_State_Type (N, F))); when Type_Iir_Pure_State => Put_Field (F, Image_Iir_Pure_State (Get_Iir_Pure_State (N, F))); when Type_Iir_Delay_Mechanism => Put_Field (F, Image_Iir_Delay_Mechanism (Get_Iir_Delay_Mechanism (N, F))); when Type_Iir_Predefined_Functions => Put_Field (F, Image_Iir_Predefined_Functions (Get_Iir_Predefined_Functions (N, F))); when Type_Iir_Direction => Put_Field (F, Image_Iir_Direction (Get_Iir_Direction (N, F))); when Type_Iir_Int32 => Put_Field (F, Strip (Iir_Int32'Image (Get_Iir_Int32 (N, F)))); when Type_Int32 => Put_Field (F, Strip (Int32'Image (Get_Int32 (N, F)))); when Type_Fp64 => Put_Field (F, Fp64'Image (Get_Fp64 (N, F))); when Type_Time_Stamp_Id => Put_Field (F, Image_Time_Stamp_Id (Get_Time_Stamp_Id (N, F))); when Type_File_Checksum_Id => Put_Field (F, Image_File_Checksum_Id (Get_File_Checksum_Id (N, F))); when Type_Token_Type => Put_Field (F, Image_Token_Type (Get_Token_Type (N, F))); when Type_Name_Id => Put_Field (F, XML_Image (Get_Name_Id (N, F))); when Type_Source_File_Entry => null; end case; end loop; Put_Stag_End; for I in Fields'Range loop F := Fields (I); case Get_Field_Type (F) is when Type_Iir => declare V : constant Iir := Get_Iir (N, F); Img : constant String := Get_Field_Image (F); begin case Get_Field_Attribute (F) is when Attr_None => Disp_Iir (Img, V); when Attr_Ref | Attr_Forward_Ref | Attr_Maybe_Forward_Ref => Disp_Iir_Ref (Img, V); when Attr_Maybe_Ref => if Get_Is_Ref (N) then Disp_Iir_Ref (Img, V); else Disp_Iir (Img, V); end if; when Attr_Chain => Disp_Iir_Chain (Img, V); when Attr_Chain_Next => null; when Attr_Of_Ref | Attr_Of_Maybe_Ref => raise Internal_Error; end case; end; when Type_Iir_List => declare L : constant Iir_List := Get_Iir_List (N, F); Img : constant String := Get_Field_Image (F); begin case Get_Field_Attribute (F) is when Attr_None => Disp_Iir_List (Img, L, False); when Attr_Of_Ref => Disp_Iir_List (Img, L, True); when Attr_Of_Maybe_Ref => Disp_Iir_List (Img, L, Get_Is_Ref (N)); when Attr_Ref => Disp_Iir_List_Ref (Img, L); when others => raise Internal_Error; end case; end; when Type_Iir_Flist => declare L : constant Iir_Flist := Get_Iir_Flist (N, F); Img : constant String := Get_Field_Image (F); begin case Get_Field_Attribute (F) is when Attr_None => Disp_Iir_Flist (Img, L, False); when Attr_Of_Ref => Disp_Iir_Flist (Img, L, True); when Attr_Of_Maybe_Ref => Disp_Iir_Flist (Img, L, Get_Is_Ref (N)); when Attr_Ref => Disp_Iir_Flist_Ref (Img, L); when others => raise Internal_Error; end case; end; when Type_String8_Id => -- Special handling for strings declare Len : constant Int32 := Get_String_Length (N); begin Put_Stag (Get_Field_Image (F)); Put_Attribute ("length", Strip (Int32'Image (Len))); Put_Attribute ("content", To_XML (Image_String8 (N))); Put_Empty_Stag_End; end; when others => null; end case; end loop; end; Put_Etag (Id); end Disp_Iir; -- Command --file-to-xml type Command_File_To_Xml is new Command_Lib with null record; function Decode_Command (Cmd : Command_File_To_Xml; Name : String) return Boolean; function Get_Short_Help (Cmd : Command_File_To_Xml) return String; procedure Perform_Action (Cmd : Command_File_To_Xml; Files_Name : Argument_List); function Decode_Command (Cmd : Command_File_To_Xml; Name : String) return Boolean is pragma Unreferenced (Cmd); begin return Name = "--file-to-xml"; end Decode_Command; function Get_Short_Help (Cmd : Command_File_To_Xml) return String is pragma Unreferenced (Cmd); begin return "--file-to-xml FILEs Dump AST in XML"; end Get_Short_Help; procedure Perform_Action (Cmd : Command_File_To_Xml; Files_Name : Argument_List) is pragma Unreferenced (Cmd); use Files_Map; Id : Name_Id; File : Source_File_Entry; type File_Data is record Fe : Source_File_Entry; Design_File : Iir; end record; type File_Data_Array is array (Files_Name'Range) of File_Data; Files : File_Data_Array; begin -- Load work library. Setup_Libraries (True); -- Parse all files. for I in Files'Range loop Id := Get_Identifier (Files_Name (I).all); File := Read_Source_File (Libraries.Local_Directory, Id); if File = No_Source_File_Entry then Error ("cannot open " & Image (Id)); return; end if; Files (I).Fe := File; Files (I).Design_File := Load_File (File); if Files (I).Design_File = Null_Iir then return; end if; -- Put units in library. -- Note: design_units stay while design_file get empty. Libraries.Add_Design_File_Into_Library (Files (I).Design_File); end loop; -- Analyze all files. for I in Files'Range loop Analyze_Design_File_Units (Files (I).Design_File); end loop; Indent := 0; Col := 0; Put_Line ("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>"); Put_Stag ("root"); Put_Attribute ("version", "0.13"); Put_Stag_End; Disp_Iir_Chain_Elements (Libraries.Get_Libraries_Chain); Put_Etag ("root"); exception when Compilation_Error => Error ("xml dump failed due to compilation error"); end Perform_Action; procedure Register_Commands is begin Register_Command (new Command_File_To_Xml); end Register_Commands; end Ghdlxml;