generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 135
Runnable gen-c output #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
vecchiot-aws
wants to merge
9
commits into
model-checking:main-153-2021-07-02
from
vecchiot-aws:name-normalization
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c5675f2
Introduce transformer trait.
avanhatt 7792020
Identity works
vecchiot-aws f3cc755
Finished transformations.
vecchiot-aws 9016356
Added backend option for genc.
vecchiot-aws 44f0d48
Some cleanup.
vecchiot-aws 6f8e5d5
Add post-processing for gen-c.
vecchiot-aws a0e4260
Add copyright message.
vecchiot-aws 728c447
Combined name and genc transformers.
vecchiot-aws 7dc2a55
Added more genc_lib code.
vecchiot-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
compiler/rustc_codegen_llvm/src/gotoc/cbmc/goto_program/gen_c_transformer.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| use super::{ | ||
| CIntType, DatatypeComponent, Expr, Location, Parameter, Stmt, StmtBody, Symbol, SymbolTable, | ||
| SymbolValues, Transformer, Type, | ||
| }; | ||
| use std::collections::{BTreeMap, HashMap, HashSet}; | ||
|
|
||
| fn normalize_identifier(name: &str) -> String { | ||
| let valid_chars = name.replace(|ch: char| !(ch.is_alphanumeric() || ch == '_'), "_"); | ||
| let new_name = match valid_chars.chars().next() { | ||
| Some(first) => { | ||
| if first.is_numeric() { | ||
| let mut name = "_".to_string(); | ||
| name.push_str(&valid_chars); | ||
| name | ||
| } else { | ||
| valid_chars | ||
| } | ||
| } | ||
| None => "_".to_string(), | ||
| }; | ||
|
|
||
| let mut illegal_names: HashMap<_, _> = [("case", "case_"), ("main", "main_")] | ||
| .iter() | ||
| .map(|(key, value)| (key.to_string(), value.to_string())) | ||
| .collect(); | ||
|
|
||
| illegal_names.remove(&new_name).unwrap_or(new_name) | ||
| } | ||
|
|
||
| pub struct GenCTransformer { | ||
| new_symbol_table: SymbolTable, | ||
| } | ||
|
|
||
| impl GenCTransformer { | ||
| pub fn transform(original_symbol_table: &SymbolTable) -> SymbolTable { | ||
| let new_symbol_table = SymbolTable::new(original_symbol_table.machine_model().clone()); | ||
| GenCTransformer { new_symbol_table }.transform_symbol_table(original_symbol_table) | ||
| } | ||
| } | ||
|
|
||
| impl Transformer for GenCTransformer { | ||
| fn symbol_table(&self) -> &SymbolTable { | ||
| &self.new_symbol_table | ||
| } | ||
|
|
||
| fn mut_symbol_table(&mut self) -> &mut SymbolTable { | ||
| &mut self.new_symbol_table | ||
| } | ||
|
|
||
| fn extract_symbol_table(self) -> SymbolTable { | ||
| self.new_symbol_table | ||
| } | ||
|
|
||
| fn transform_parameter(&self, parameter: &Parameter) -> Parameter { | ||
| Type::parameter( | ||
| parameter.identifier().map(|name| normalize_identifier(name)), | ||
| parameter.base_name().map(|name| normalize_identifier(name)), | ||
| self.transform_type(parameter.typ()), | ||
| ) | ||
| } | ||
|
|
||
| fn transform_member_expr(&self, _typ: &Type, lhs: &Expr, field: &str) -> Expr { | ||
| let transformed_lhs = self.transform_expr(lhs); | ||
| transformed_lhs.member(&normalize_identifier(field), self.symbol_table()) | ||
| } | ||
|
|
||
| fn transform_symbol_expr(&self, typ: &Type, identifier: &str) -> Expr { | ||
| let transformed_typ = self.transform_type(typ); | ||
| Expr::symbol_expression(normalize_identifier(identifier), transformed_typ) | ||
| } | ||
|
|
||
| fn transform_union_expr(&self, typ: &Type, value: &Expr, field: &str) -> Expr { | ||
| let transformed_typ = self.transform_type(typ); | ||
| let transformed_value = self.transform_expr(value); | ||
| Expr::union_expr( | ||
| transformed_typ, | ||
| &normalize_identifier(field), | ||
| transformed_value, | ||
| self.symbol_table(), | ||
| ) | ||
| } | ||
|
|
||
| fn transform_incomplete_struct_type(&self, tag: &str) -> Type { | ||
| Type::incomplete_struct(&normalize_identifier(tag)) | ||
| } | ||
|
|
||
| fn transform_incomplete_union_type(&self, tag: &str) -> Type { | ||
| Type::incomplete_union(&normalize_identifier(tag)) | ||
| } | ||
|
|
||
| fn transform_datatype_component(&self, component: &DatatypeComponent) -> DatatypeComponent { | ||
| match component { | ||
| DatatypeComponent::Field { name, typ } => DatatypeComponent::Field { | ||
| name: normalize_identifier(name), | ||
| typ: self.transform_type(typ), | ||
| }, | ||
| DatatypeComponent::Padding { name, bits } => { | ||
| DatatypeComponent::Padding { name: normalize_identifier(name), bits: *bits } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn transform_struct_type(&self, tag: &str, components: &[DatatypeComponent]) -> Type { | ||
| let transformed_components = components | ||
| .iter() | ||
| .map(|component| self.transform_datatype_component(component)) | ||
| .collect(); | ||
| Type::struct_type(&normalize_identifier(tag), transformed_components) | ||
| } | ||
|
|
||
| fn transform_struct_tag_type(&self, tag: &str) -> Type { | ||
| Type::struct_tag_raw(&normalize_identifier(tag)) | ||
| } | ||
|
|
||
| fn transform_union_type(&self, tag: &str, components: &[DatatypeComponent]) -> Type { | ||
| let transformed_components = components | ||
| .iter() | ||
| .map(|component| self.transform_datatype_component(component)) | ||
| .collect(); | ||
| Type::union_type(&normalize_identifier(tag), transformed_components) | ||
| } | ||
|
|
||
| fn transform_union_tag_type(&self, tag: &str) -> Type { | ||
| Type::union_tag_raw(&normalize_identifier(tag)) | ||
| } | ||
|
|
||
| fn transform_goto_stmt(&self, label: &str) -> Stmt { | ||
| Stmt::goto(normalize_identifier(label), Location::none()) | ||
| } | ||
|
|
||
| fn transform_label_stmt(&self, label: &str, body: &Stmt) -> Stmt { | ||
| let transformed_body = self.transform_stmt(body); | ||
| transformed_body.with_label(normalize_identifier(label)) | ||
| } | ||
|
|
||
| fn transform_symbol(&self, symbol: &Symbol) -> Symbol { | ||
| let new_typ = self.transform_type(&symbol.typ); | ||
| let new_value = self.transform_value(&symbol.value); | ||
| let mut new_symbol = symbol.clone(); | ||
| new_symbol.value = new_value; | ||
| new_symbol.typ = new_typ; | ||
| new_symbol.name = normalize_identifier(&new_symbol.name); | ||
| new_symbol.base_name = new_symbol.base_name.map(|name| normalize_identifier(&name)); | ||
| new_symbol.pretty_name = new_symbol.pretty_name.map(|name| normalize_identifier(&name)); | ||
| new_symbol | ||
| } | ||
|
|
||
| fn postprocess(&mut self) { | ||
| let memcpy = self.mut_symbol_table().remove("memcpy"); | ||
| assert!(memcpy.is_some()); | ||
| let memmove = self.mut_symbol_table().remove("memmove"); | ||
| assert!(memmove.is_some()); | ||
|
|
||
| let old_main = self.symbol_table().lookup("main_"); | ||
| if let Some(old_main) = old_main { | ||
| let new_main = Symbol::function( | ||
| "main", | ||
| Type::code(Vec::new(), Type::CInteger(CIntType::Int)), | ||
| Some(Stmt::block( | ||
| vec![ | ||
| Stmt::code_expression( | ||
| Expr::symbol_expression("main_".to_string(), old_main.typ.clone()) | ||
| .call(Vec::new()), | ||
| Location::none(), | ||
| ), | ||
| Stmt::ret( | ||
| Some(Expr::int_constant(0, Type::CInteger(CIntType::Int))), | ||
| Location::none(), | ||
| ), | ||
| ], | ||
| Location::none(), | ||
| )), | ||
| Some("main".to_string()), | ||
| Location::none(), | ||
| ); | ||
| self.mut_symbol_table().insert(new_main); | ||
| } | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
compiler/rustc_codegen_llvm/src/gotoc/cbmc/goto_program/identity_transformer.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| use super::{ | ||
| DatatypeComponent, Expr, Location, Stmt, Symbol, SymbolTable, SymbolValues, Transformer, Type, | ||
| }; | ||
| use std::collections::{BTreeMap, HashSet}; | ||
|
|
||
| pub struct IdentityTransformer { | ||
| new_symbol_table: SymbolTable, | ||
| } | ||
|
|
||
| impl IdentityTransformer { | ||
| pub fn transform(original_symbol_table: &SymbolTable) -> SymbolTable { | ||
| let new_symbol_table = SymbolTable::new(original_symbol_table.machine_model().clone()); | ||
| IdentityTransformer { new_symbol_table }.transform_symbol_table(original_symbol_table) | ||
| } | ||
| } | ||
|
|
||
| impl Transformer for IdentityTransformer { | ||
| fn symbol_table(&self) -> &SymbolTable { | ||
| &self.new_symbol_table | ||
| } | ||
|
|
||
| fn mut_symbol_table(&mut self) -> &mut SymbolTable { | ||
| &mut self.new_symbol_table | ||
| } | ||
|
|
||
| fn extract_symbol_table(self) -> SymbolTable { | ||
| self.new_symbol_table | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a check that injective