blob: 3cbc378ab021619d9939395f53d928c719ae248a [file] [log] [blame]
Brian Andersond6c0d852015-11-26 19:05:101// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
Pierre Krieger5b224ec2016-02-14 16:07:4411#![allow(non_upper_case_globals)]
12
13use llvm::{Struct, Array, Attribute};
Eduard Burtescu035a6452016-03-22 17:23:3614use abi::{FnType, ArgType};
15use context::CrateContext;
Brian Andersond6c0d852015-11-26 19:05:1016
Pierre Krieger5b224ec2016-02-14 16:07:4417// Data layout: e-p:32:32-i64:64-v128:32:128-n32-S128
18
19// See the https://github.com/kripken/emscripten-fastcomp-clang repository.
20// The class `EmscriptenABIInfo` in `/lib/CodeGen/TargetInfo.cpp` contains the ABI definitions.
21
Eduard Burtescude5f8242016-02-25 17:35:4022fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) {
Eduard Burtescuac603182016-02-25 08:49:5823 match ret.ty.kind() {
Pierre Krieger5b224ec2016-02-14 16:07:4424 Struct => {
Eduard Burtescuac603182016-02-25 08:49:5825 let field_types = ret.ty.field_types();
Pierre Krieger5b224ec2016-02-14 16:07:4426 if field_types.len() == 1 {
Eduard Burtescuac603182016-02-25 08:49:5827 ret.cast = Some(field_types[0]);
Pierre Krieger5b224ec2016-02-14 16:07:4428 } else {
Eduard Burtescude5f8242016-02-25 17:35:4029 ret.make_indirect(ccx);
Eduard Burtescuac603182016-02-25 08:49:5830 }
Pierre Krieger5b224ec2016-02-14 16:07:4431 }
Eduard Burtescud492d092016-02-25 10:11:0232 Array => {
Eduard Burtescude5f8242016-02-25 17:35:4033 ret.make_indirect(ccx);
Eduard Burtescud492d092016-02-25 10:11:0234 }
35 _ => {}
Pierre Krieger5b224ec2016-02-14 16:07:4436 }
37}
38
Eduard Burtescude5f8242016-02-25 17:35:4039fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) {
Eduard Burtescuac603182016-02-25 08:49:5840 if arg.ty.is_aggregate() {
Eduard Burtescude5f8242016-02-25 17:35:4041 arg.make_indirect(ccx);
42 arg.attrs.set(Attribute::ByVal);
Pierre Krieger5b224ec2016-02-14 16:07:4443 }
44}
45
Eduard Burtescuc6d214b2016-02-23 19:55:1946pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) {
Eduard Burtescu80d939f2016-03-06 10:36:3947 if !fty.ret.is_ignore() {
Eduard Burtescude5f8242016-02-25 17:35:4048 classify_ret_ty(ccx, &mut fty.ret);
Pierre Krieger5b224ec2016-02-14 16:07:4449 }
50
Eduard Burtescuc6d214b2016-02-23 19:55:1951 for arg in &mut fty.args {
Eduard Burtescu80d939f2016-03-06 10:36:3952 if arg.is_ignore() { continue; }
Eduard Burtescude5f8242016-02-25 17:35:4053 classify_arg_ty(ccx, arg);
Eduard Burtescuc6d214b2016-02-23 19:55:1954 }
Brian Andersond6c0d852015-11-26 19:05:1055}