-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
344 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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,75 @@ | ||
use std::{error::Error, fs::OpenOptions, path::{Path, PathBuf}}; | ||
use gimli::DW_LANG_C; | ||
use ygen::{prelude::*, Support::ColorProfile, Target::initializeAllTargets}; | ||
|
||
|
||
// hello_world.c: | ||
// 1 #include <stdout.h> | ||
// 2 | ||
// 3 int main() { | ||
// 4 printf("Hello World!"); | ||
// 5 return 0; | ||
// 6 } | ||
|
||
pub fn main() -> Result<(), Box<dyn Error>> { | ||
let mut module = Module(); | ||
module.init_dbg("ygen example".to_owned(), DW_LANG_C, &PathBuf::from("hello_world.c")); | ||
|
||
let mut builder = IRBuilder(); | ||
|
||
let other = module.add("printf", &FnTy(vec![TypeMetadata::ptr], TypeMetadata::Void)); | ||
other.import(); | ||
let other = other.clone(); | ||
|
||
let string = module.addConst("str"); | ||
string.set("Hello World!\n\0".as_bytes().to_vec()); | ||
let string = string.clone(); | ||
|
||
let ty = FnTy(vec![], TypeMetadata::Void); | ||
|
||
let func = module.add( | ||
"main", &ty | ||
); | ||
|
||
func.extrn(); | ||
|
||
let entry = func.addBlock("entry"); | ||
builder.positionAtEnd(entry); | ||
|
||
builder.BuildDebug(4, 0, PathBuf::from("hello_world.c")); | ||
let string = builder.BuildAssign(&string); | ||
builder.BuildCall( &other, vec![string] ); | ||
|
||
builder.BuildDebug(5, 0, PathBuf::from("hello_world.c")); | ||
builder.BuildRet( Type::Void ); | ||
|
||
module.verify()?; | ||
|
||
eprintln!( | ||
"{}", module.dumpColored(ColorProfile::default()) | ||
); | ||
|
||
let triple = Triple::host(); | ||
|
||
module.emitToAsmFile( | ||
triple, | ||
&mut initializeAllTargets(triple)?, | ||
Path::new("out.o") | ||
)?; | ||
|
||
let (mut object, debug) = module | ||
.emitMachineCode( | ||
triple, | ||
&mut initializeAllTargets(triple)?, | ||
true // turns on debugging information | ||
)?; | ||
|
||
object.debug = true; // additionaly we need to turn debugging information in the object file on | ||
|
||
object.emit( | ||
OpenOptions::new().write(true).create(true).open("out.o")?, | ||
debug | ||
)?; | ||
|
||
Ok(()) | ||
} |
This file contains 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 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 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,6 @@ | ||
#include <stdout.h> | ||
|
||
int main() { | ||
printf("Hello World!"); | ||
return 0; | ||
} |
This file contains 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 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 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 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,81 @@ | ||
use std::path::PathBuf; | ||
|
||
use crate::{Support::ColorClass, IR::IRBuilder}; | ||
|
||
use super::Ir; | ||
|
||
/// A node which startes a debugging line programm | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct DebugNode { | ||
/// the code line | ||
pub line: i64, | ||
/// the code coloumn | ||
pub coloumn: i64, | ||
/// the file path | ||
pub file: PathBuf, | ||
} | ||
|
||
impl Ir for DebugNode { | ||
fn dump(&self) -> String { | ||
format!("!dbg {}:{} in {:?}", self.line, self.coloumn, self.file.to_str().unwrap()) | ||
} | ||
|
||
fn dumpColored(&self, profile: crate::Support::ColorProfile) -> String { | ||
format!("{}: {}:{} {} {}", | ||
profile.markup("!dbg", ColorClass::Instr), | ||
profile.markup(&self.line.to_string(), ColorClass::Value), | ||
profile.markup(&self.coloumn.to_string(), ColorClass::Value), | ||
profile.markup("in", ColorClass::Instr), | ||
profile.markup(&self.file.to_str().unwrap(), ColorClass::Value), | ||
) | ||
} | ||
|
||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn verify(&self, _: crate::prelude::FunctionType) -> Result<(), crate::prelude::VerifyError> { | ||
Ok(()) | ||
} | ||
|
||
fn clone_box(&self) -> Box<dyn Ir> { | ||
Box::from( self.clone() ) | ||
} | ||
|
||
fn compile(&self, _: &mut crate::Target::TargetBackendDescr) { | ||
// NOTHING TODO for an normal build | ||
} | ||
|
||
fn compile_dir(&self, compiler: &mut crate::CodeGen::IrCodeGenHelper, _: &crate::prelude::Block) { | ||
compiler.set_location_node(self) | ||
} | ||
|
||
fn maybe_inline(&self, _: &std::collections::HashMap<String, crate::prelude::Type>) -> Option<Box<dyn Ir>> { | ||
None | ||
} | ||
|
||
fn eval(&self) -> Option<Box<dyn Ir>> { | ||
None | ||
} | ||
|
||
fn inputs(&self) -> Vec<crate::prelude::Var> { | ||
vec![] | ||
} | ||
|
||
fn output(&self) -> Option<crate::prelude::Var> { | ||
None | ||
} | ||
} | ||
|
||
impl IRBuilder<'_> { | ||
/// Sets the source location for debugging (all of the ir nodes will respond to the location till an new location is set) | ||
pub fn BuildDebug(&mut self, line: i64, coloumn: i64, file: PathBuf) { | ||
let block = self.blocks.get_mut(self.curr).expect("the IRBuilder needs to have an current block\nConsider creating one"); | ||
|
||
block.push_ir( Box::new( DebugNode { | ||
line: line, | ||
coloumn: coloumn, | ||
file: file | ||
} )); | ||
} | ||
} |
This file contains 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.