Skip to content

Commit

Permalink
HalfOpenRange/ClosedRange classes
Browse files Browse the repository at this point in the history
New classes provide useful abstractions, such as le_bitrange/nw_bitrange.
  • Loading branch information
grg committed Mar 1, 2024
1 parent 1fba085 commit 62670aa
Show file tree
Hide file tree
Showing 6 changed files with 833 additions and 0 deletions.
1 change: 1 addition & 0 deletions ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

set (IR_SRCS
base.cpp
bitrange.cpp
dbprint.cpp
dbprint-expression.cpp
dbprint-stmt.cpp
Expand Down
32 changes: 32 additions & 0 deletions ir/bitrange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 Intel Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "ir/bitrange.h"

#include "ir/json_generator.h"
#include "ir/json_loader.h"

void registerRangeJsonHelpers() {
RangeJsonHelpers::rangeToJSON = [](JSONGenerator &json, int lo, int hi) -> void {
json.toJSON(std::make_pair(lo, hi));
};

RangeJsonHelpers::rangeFromJSON = [](JSONLoader &json) -> std::pair<int, int> {
std::pair<int, int> endpoints;
json >> endpoints;
return endpoints;
};
}
32 changes: 32 additions & 0 deletions ir/bitrange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 Intel Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef IR_BITRANGE_H_
#define IR_BITRANGE_H_

#include "lib/bitrange.h"

/// Register helper functions for HalfOpenRange/ClosedRange JSON serialization/deserialization.
///
/// These functions can't be defined in lib/bitrange.cpp because lib is compiled before the IR
/// generator runs, preventing the use of JSONGenerator/JSONLoader. Instead, lib/bitrange.cpp
/// defines a pair of placeholder functions that call BUG if called.
///
/// This function does not need to be invoked if HalfOpenRange/ClosedRange objects are never
/// serialized.
void registerRangeJsonHelpers();

#endif /* IR_BITRANGE_H_ */
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
set (LIBP4CTOOLKIT_SRCS
alloc_trace.cpp
backtrace_exception.cpp
bitrange.cpp
bitvec.cpp
compile_context.cpp
crash.cpp
Expand Down
53 changes: 53 additions & 0 deletions lib/bitrange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2024 Intel Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "lib/bitrange.h"

#include <iostream>
#include <utility>

RangeToJSONHandler RangeJsonHelpers::rangeToJSON = ::rangeToJSON;
RangeFromJSONHandler RangeJsonHelpers::rangeFromJSON = ::rangeFromJSON;

// See ir/bitrange.h:registerRangeJsonHelpers()
void rangeToJSON(JSONGenerator &, int, int) { BUG("Unimplemented rangeToJSON functionality"); }

// See ir/bitrange.h:registerRangeJsonHelpers()
std::pair<int, int> rangeFromJSON(JSONLoader &) {
BUG("Unimplemented rangeFromJSON functionality");
return std::make_pair(-1, -1);
}

std::ostream &toStream(std::ostream &out, RangeUnit unit, Endian order, int lo, int hi,
bool closed) {
if (unit == RangeUnit::Bit)
out << "bit";
else if (unit == RangeUnit::Byte)
out << "byte";
else
BUG("unknown range unit");

out << (!closed && order == Endian::Little ? "(" : "[");

if (order == Endian::Little) std::swap(lo, hi);

out << std::dec << lo;
if (lo != hi) out << ".." << hi;

out << (!closed && order == Endian::Network ? ")" : "]");

return out;
}
Loading

0 comments on commit 62670aa

Please sign in to comment.